Skip to content

Commit 5d6942a

Browse files
author
Denis
committed
expectedLatency field added
documentation and tests updated
1 parent e864ade commit 5d6942a

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
Pk-vcs-git is lightweight library for execute basic Git VCS operations (merge, branch create etc). It uses [pk-vcs-api](https://github.com/ProjectKaiser/pk-vcs-api) exposing IVCS implementation for Git repositories and [JGit](https://eclipse.org/jgit/) as framework to work with Git repositories.
33
Features:
44
- Branch create and remove
5-
- Branch merge returning result(success or list of conflicted files)
6-
- Commit messages list
5+
- Branch merge returning result (success or list of conflicted files)
6+
- Branch commits messages list
77
- Summarized branch changes list
88
- Branches list
99
- File content getting and setting
@@ -54,6 +54,7 @@ Features:
5454
```
5555
- Use methods of `IVCS` interface. See [pk-vcs-api](https://github.com/ProjectKaiser/pk-vcs-api) for details
5656
- Use `vcs.setProxy()` and `vcs.setCredentials()` if necessary
57+
- Github has some latency for exposing results of previously executed operations. For example if create a new branch and immediately check branches list then Github could return old branches list. Use `GitVCS.setExpectedLatency()` to set delay which will be executed after each operation which may have server latency
5758

5859
# Implementation details
5960
- [JGit](https://eclipse.org/jgit/) is used as framework to work with Git repositories
@@ -70,7 +71,7 @@ Features:
7071
- `PK_VCS_TEST_GITHUB_PASS` environment var or JVM var is used as user password for access to Github
7172
- New Test Repository is created before each test and deletes automatically after each test
7273
- To execute tests just run GitVCSTest class as JUnit test. Tests from VCSAbstractTest class will be executed. See [pk-vcs-test](https://github.com/ProjectKaiser/pk-vcs-test) for details
73-
- NOTE: Github has some latency for exposing results of previously executed operations. For example if create a new branch and immediately check branches list then Github could return old branches list. Need to wait a couple of second to get new list. So if a test failed then try to execute it again.
74+
- NOTE: Github has some latency for exposing results of previously executed operations. For example if create a new branch and immediately check branches list then Github could return old branches list. 2 seconds awaiting is added for testing. Also if a test failed then try to execute it again.
7475

7576
# Limitations
7677
- Commit messages can not be attached to branch create and delete operations because Git does not exposes these operations as separate commits

src/main/java/com/projectkaiser/scm/vcs/GitVCS.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,19 @@ public class GitVCS implements IVCS {
5555
private static final String MASTER_BRANCH_NAME = "master";
5656
public static final String GIT_VCS_TYPE_STRING = "git";
5757
private static final String REFS_REMOTES_ORIGIN = "refs/remotes/origin/";
58+
private Integer expectedLatency = 0;
5859

5960
private CredentialsProvider credentials;
6061
private IVCSRepositoryWorkspace repo;
6162

63+
public Integer getExpectedLatency() {
64+
return expectedLatency;
65+
}
66+
67+
public void setExpectedLatency(Integer expectedLatency) {
68+
this.expectedLatency = expectedLatency;
69+
}
70+
6271
public CredentialsProvider getCredentials() {
6372
return credentials;
6473
}
@@ -100,7 +109,7 @@ public void createBranch(String srcBranchName, String newBranchName, String comm
100109
.setRefSpecs(refSpec)
101110
.setCredentialsProvider(credentials)
102111
.call();
103-
Thread.sleep(2000); // github has some latency on branch operations
112+
Thread.sleep(expectedLatency); // github has some latency on branch operations
104113
// so next request branches operation will return old branches list
105114
} finally {
106115
git.getRepository().close();
@@ -152,7 +161,7 @@ public void deleteBranch(String branchName, String commitMessage) {
152161
.setCredentialsProvider(credentials)
153162
.call();
154163
git.getRepository().close();
155-
Thread.sleep(2000); // github has some latency on branch operations
164+
Thread.sleep(expectedLatency); // github has some latency on branch operations
156165
// so next request branches operation will return old branches list
157166
}
158167
}
@@ -196,7 +205,7 @@ public Git getLocalGit(IVCSLockedWorkingCopy wc) {
196205
}
197206

198207
@Override
199-
public VCSMergeResult merge(String sourceBranchUrl, String destBranchUrl, String commitMessage) {
208+
public VCSMergeResult merge(String srcBranchName, String dstBranchName, String commitMessage) {
200209
try {
201210
try (IVCSLockedWorkingCopy wc = repo.getVCSLockedWorkingCopy()) {
202211
try (Git git = getLocalGit(wc)) {
@@ -208,15 +217,15 @@ public VCSMergeResult merge(String sourceBranchUrl, String destBranchUrl, String
208217

209218
git
210219
.checkout()
211-
.setCreateBranch(git.getRepository().exactRef("refs/heads/" + parseBranch(destBranchUrl)) == null)
212-
.setName(parseBranch(destBranchUrl))
220+
.setCreateBranch(git.getRepository().exactRef("refs/heads/" + parseBranch(dstBranchName)) == null)
221+
.setName(parseBranch(dstBranchName))
213222
.call();
214223

215224
MergeResult mr = git
216225
.merge()
217-
.include(git.getRepository().findRef("origin/" + parseBranch(sourceBranchUrl)))
226+
.include(git.getRepository().findRef("origin/" + parseBranch(srcBranchName)))
218227
.setMessage(commitMessage)
219-
.call(); // actually do the merge
228+
.call();
220229

221230

222231
VCSMergeResult res = new VCSMergeResult();
@@ -388,7 +397,7 @@ public String getFileContent(String branchName, String filePath) {
388397
}
389398

390399
@Override
391-
public List<VCSDiffEntry> getBranchesDiff(String srcBranchName, String destBranchName) {
400+
public List<VCSDiffEntry> getBranchesDiff(String srcBranchName, String dstBranchName) {
392401
List<VCSDiffEntry> res = new ArrayList<>();
393402
try (IVCSLockedWorkingCopy wc = repo.getVCSLockedWorkingCopy()) {
394403
try (Git git = getLocalGit(wc)) {
@@ -397,7 +406,7 @@ public List<VCSDiffEntry> getBranchesDiff(String srcBranchName, String destBranc
397406
RevCommit srcHeadCommit = walk.parseCommit(git.getRepository().resolve("remotes/origin/"
398407
+ parseBranch(srcBranchName)));
399408
RevCommit destHeadCommit = walk.parseCommit(git.getRepository().resolve("remotes/origin/"
400-
+ parseBranch(destBranchName)));
409+
+ parseBranch(dstBranchName)));
401410

402411
List<RevCommit> startPoints = new ArrayList<RevCommit>();
403412
walk.setRevFilter(RevFilter.MERGE_BASE);

src/test/java/com/projectkaiser/scm/vcs/GitVCSTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ protected IVCS getVCS(IVCSRepositoryWorkspace mockedVCSRepo) {
102102
if (PROXY_HOST != null) {
103103
vcs.setProxy(PROXY_HOST, PROXY_PORT, PROXY_USER, PROXY_PASS);
104104
}
105+
((GitVCS) vcs).setExpectedLatency(2000);
105106
return vcs;
106107
}
107108

0 commit comments

Comments
 (0)