Skip to content

Commit 3f94798

Browse files
committed
getBranches(): fixed bug when a branch was created in separate LWC
createTag(): fixed bug when a tag is created on head but local head is not origin head unnecessary getTestRepoUrl() is removed
1 parent e5097c4 commit 3f94798

File tree

3 files changed

+58
-98
lines changed

3 files changed

+58
-98
lines changed

README.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,21 @@ Features:
1212
- File create and remove
1313
- Working with tags: create, remove, browse
1414

15+
Use cases
16+
- VCS server hooks
17+
- Build machines
18+
- checking in\out, tagging
19+
- Software project management systems
20+
- Create own branches from GUI, browse commits, product versions management, etc
21+
- Product release automation
22+
- automatic merging, forking, tagging, version bumping, etc
23+
- Example: [scm4j-releaser](https://github.com/scm4j/scm4j-releaser)
24+
25+
1526
# Terms
1627
- Workspace Home
1728
- Home local folder of all folders used by vcs-related operations. See [scm4j-vcs-api](https://github.com/scm4j/scm4j-vcs-api) for details
18-
- Repository Workspace
19-
- Local folder for LWC folders related to Repository of one type. See [scm4j-vcs-api](https://github.com/scm4j/scm4j-vcs-api) for details
20-
- Locked Working Copy, LWC
29+
- Locked Working Copy,
2130
- Local folder where vcs-related operations are executed. Provides thread- and process-safe repository of working folders. See [scm4j-vcs-api](https://github.com/scm4j/scm4j-vcs-api) for details
2231
- Test Repository
2332
- Git repository which is used to execute functional tests

src/main/java/org/scm4j/vcs/GitVCS.java

Lines changed: 35 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,37 @@
11
package org.scm4j.vcs;
22

3-
import java.io.ByteArrayOutputStream;
4-
import java.io.File;
5-
import java.io.FileWriter;
6-
import java.io.IOException;
7-
import java.io.InputStream;
8-
import java.net.Authenticator;
9-
import java.net.InetSocketAddress;
10-
import java.net.PasswordAuthentication;
11-
import java.net.Proxy;
12-
import java.net.Proxy.Type;
13-
import java.net.ProxySelector;
14-
import java.net.SocketAddress;
15-
import java.net.URI;
16-
import java.nio.charset.StandardCharsets;
17-
import java.util.ArrayList;
18-
import java.util.Collection;
19-
import java.util.Collections;
20-
import java.util.HashSet;
21-
import java.util.List;
22-
import java.util.Set;
23-
243
import org.apache.commons.io.FileUtils;
254
import org.apache.commons.io.IOUtils;
26-
import org.eclipse.jgit.api.CheckoutCommand;
5+
import org.eclipse.jgit.api.*;
276
import org.eclipse.jgit.api.CreateBranchCommand.SetupUpstreamMode;
28-
import org.eclipse.jgit.api.Git;
29-
import org.eclipse.jgit.api.LogCommand;
30-
import org.eclipse.jgit.api.MergeResult;
31-
import org.eclipse.jgit.api.PushCommand;
327
import org.eclipse.jgit.api.ResetCommand.ResetType;
338
import org.eclipse.jgit.api.errors.GitAPIException;
349
import org.eclipse.jgit.api.errors.RefAlreadyExistsException;
3510
import org.eclipse.jgit.diff.DiffEntry;
3611
import org.eclipse.jgit.diff.DiffEntry.ChangeType;
3712
import org.eclipse.jgit.diff.DiffEntry.Side;
3813
import org.eclipse.jgit.diff.DiffFormatter;
39-
import org.eclipse.jgit.lib.Constants;
40-
import org.eclipse.jgit.lib.ObjectId;
41-
import org.eclipse.jgit.lib.ObjectLoader;
42-
import org.eclipse.jgit.lib.ObjectReader;
43-
import org.eclipse.jgit.lib.Ref;
44-
import org.eclipse.jgit.lib.Repository;
45-
import org.eclipse.jgit.revwalk.RevCommit;
46-
import org.eclipse.jgit.revwalk.RevObject;
47-
import org.eclipse.jgit.revwalk.RevSort;
48-
import org.eclipse.jgit.revwalk.RevTag;
49-
import org.eclipse.jgit.revwalk.RevTree;
50-
import org.eclipse.jgit.revwalk.RevWalk;
14+
import org.eclipse.jgit.lib.*;
15+
import org.eclipse.jgit.revwalk.*;
5116
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
5217
import org.eclipse.jgit.transport.CredentialsProvider;
5318
import org.eclipse.jgit.transport.RefSpec;
5419
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
5520
import org.eclipse.jgit.treewalk.CanonicalTreeParser;
5621
import org.eclipse.jgit.treewalk.TreeWalk;
5722
import org.eclipse.jgit.treewalk.filter.PathFilter;
58-
import org.scm4j.vcs.api.IVCS;
59-
import org.scm4j.vcs.api.VCSChangeType;
60-
import org.scm4j.vcs.api.VCSCommit;
61-
import org.scm4j.vcs.api.VCSDiffEntry;
62-
import org.scm4j.vcs.api.VCSMergeResult;
63-
import org.scm4j.vcs.api.VCSTag;
64-
import org.scm4j.vcs.api.WalkDirection;
65-
import org.scm4j.vcs.api.exceptions.EVCSBranchExists;
66-
import org.scm4j.vcs.api.exceptions.EVCSBranchNotFound;
67-
import org.scm4j.vcs.api.exceptions.EVCSException;
68-
import org.scm4j.vcs.api.exceptions.EVCSFileNotFound;
69-
import org.scm4j.vcs.api.exceptions.EVCSTagExists;
23+
import org.scm4j.vcs.api.*;
24+
import org.scm4j.vcs.api.exceptions.*;
7025
import org.scm4j.vcs.api.workingcopy.IVCSLockedWorkingCopy;
7126
import org.scm4j.vcs.api.workingcopy.IVCSRepositoryWorkspace;
7227
import org.scm4j.vcs.api.workingcopy.IVCSWorkspace;
7328

29+
import java.io.*;
30+
import java.net.*;
31+
import java.net.Proxy.Type;
32+
import java.nio.charset.StandardCharsets;
33+
import java.util.*;
34+
7435
public class GitVCS implements IVCS {
7536

7637
public static final String GIT_VCS_TYPE_STRING = "git";
@@ -94,7 +55,7 @@ public void setCredentials(CredentialsProvider credentials) {
9455
private String getRealBranchName(String branchName) {
9556
return branchName == null ? MASTER_BRANCH_NAME : branchName;
9657
}
97-
58+
9859
protected Git getLocalGit(String folder) throws Exception {
9960
Repository gitRepo = new FileRepositoryBuilder()
10061
.setGitDir(new File(folder, ".git"))
@@ -337,12 +298,12 @@ public String getFileContent(String branchName, String fileRelativePath, String
337298
.pull()
338299
.setCredentialsProvider(credentials)
339300
.call(); //TODO: add test when we receive correct file version if we change it from another LWC
340-
301+
341302
ObjectId revisionCommitId = gitRepo.resolve(revision == null ? "refs/heads/" + getRealBranchName(branchName) : revision);
342303
if (revision == null && revisionCommitId == null) {
343-
throw new EVCSBranchNotFound(getRepoUrl(), getRealBranchName(branchName));
304+
throw new EVCSBranchNotFound(getRepoUrl(), getRealBranchName(branchName));
344305
}
345-
306+
346307
RevCommit commit = revWalk.parseCommit(revisionCommitId);
347308
RevTree tree = commit.getTree();
348309
treeWalk.addTree(tree);
@@ -373,7 +334,7 @@ public VCSCommit setFileContent(String branchName, String filePath, String conte
373334
Repository gitRepo = git.getRepository()) {
374335

375336
checkout(git, gitRepo, branchName, null);
376-
337+
377338
File file = new File(wc.getFolder(), filePath);
378339
if (!file.exists()) {
379340
FileUtils.forceMkdir(file.getParentFile());
@@ -408,11 +369,11 @@ public VCSCommit setFileContent(String branchName, String filePath, String conte
408369
void checkout(Git git, Repository gitRepo, String branchName, String revision) throws Exception {
409370
String bn = getRealBranchName(branchName);
410371
CheckoutCommand cmd = git.checkout();
372+
git
373+
.pull()
374+
.setCredentialsProvider(credentials)
375+
.call();
411376
if (revision == null) {
412-
git
413-
.pull()
414-
.setCredentialsProvider(credentials)
415-
.call();
416377
cmd
417378
.setStartPoint("origin/" + bn)
418379
.setCreateBranch(gitRepo.exactRef("refs/heads/" + bn) == null)
@@ -490,11 +451,18 @@ public Set<String> getBranches(String path) {
490451
Git git = getLocalGit(wc);
491452
Repository gitRepo = git.getRepository()) {
492453

454+
git
455+
.fetch()
456+
.setRefSpecs(new RefSpec("+refs/heads/*:refs/heads/*"))
457+
.setRemoveDeletedRefs(true)
458+
.setCredentialsProvider(credentials)
459+
.call();
460+
493461
git
494462
.pull()
495463
.setCredentialsProvider(credentials)
496-
.call(); //TODO: add test when we receive correct branches list if we change it from another LWC
497-
464+
.call();
465+
498466
Collection<Ref> refs = gitRepo.getRefDatabase().getRefs(REFS_REMOTES_ORIGIN).values();
499467
Set<String> res = new HashSet<>();
500468
String bn;
@@ -749,22 +717,24 @@ public VCSTag createTag(String branchName, String tagName, String tagMessage, St
749717

750718
updateLocalTags(git);
751719

720+
checkout(git, gitRepo, branchName, null);
721+
752722
RevCommit commitToTag = revisionToTag == null ? null : rw.parseCommit(ObjectId.fromString(revisionToTag));
753-
723+
754724
Ref ref = git
755725
.tag()
756726
.setAnnotated(true)
757727
.setMessage(tagMessage)
758728
.setName(tagName)
759729
.setObjectId(commitToTag)
760730
.call();
761-
731+
762732
push(git, new RefSpec(ref.getName()));
763-
733+
764734
RevTag revTag = rw.parseTag(ref.getObjectId());
765735
RevCommit revCommit = rw.parseCommit(ref.getObjectId());
766736
VCSCommit relatedCommit = getVCSCommit(revCommit);
767-
return new VCSTag(revTag.getTagName(), revTag.getFullMessage(), revTag.getTaggerIdent().getName(), relatedCommit);
737+
return new VCSTag(revTag.getTagName(), revTag.getFullMessage(), revTag.getTaggerIdent().getName(), relatedCommit);
768738
} catch(RefAlreadyExistsException e) {
769739
throw new EVCSTagExists(e);
770740
} catch (GitAPIException e) {

src/test/java/org/scm4j/vcs/GitVCSTest.java

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,5 @@
11
package org.scm4j.vcs;
22

3-
import static org.junit.Assert.assertEquals;
4-
import static org.junit.Assert.assertNotNull;
5-
import static org.junit.Assert.assertNull;
6-
import static org.junit.Assert.assertTrue;
7-
import static org.junit.Assert.fail;
8-
9-
import java.io.File;
10-
import java.io.IOException;
11-
import java.lang.reflect.InvocationTargetException;
12-
import java.lang.reflect.Method;
13-
import java.net.Authenticator;
14-
import java.net.InetAddress;
15-
import java.net.InetSocketAddress;
16-
import java.net.PasswordAuthentication;
17-
import java.net.Proxy;
18-
import java.net.ProxySelector;
19-
import java.net.SocketAddress;
20-
import java.net.URI;
21-
import java.net.URISyntaxException;
22-
import java.util.Arrays;
23-
import java.util.List;
24-
253
import org.apache.commons.io.FileUtils;
264
import org.apache.commons.lang3.ArrayUtils;
275
import org.eclipse.jgit.api.Git;
@@ -44,6 +22,16 @@
4422
import org.scm4j.vcs.api.workingcopy.IVCSWorkspace;
4523
import org.scm4j.vcs.api.workingcopy.VCSWorkspace;
4624

25+
import java.io.File;
26+
import java.io.IOException;
27+
import java.lang.reflect.InvocationTargetException;
28+
import java.lang.reflect.Method;
29+
import java.net.*;
30+
import java.util.Arrays;
31+
import java.util.List;
32+
33+
import static org.junit.Assert.*;
34+
4735
public class GitVCSTest extends VCSAbstractTest {
4836

4937
private Repository localGitRepo;
@@ -54,7 +42,7 @@ public class GitVCSTest extends VCSAbstractTest {
5442
@Override
5543
public void setUp() throws Exception {
5644
super.setUp();
57-
Git fileGitRepo = GitVCSUtils.createRepository(new File(localVCSWorkspace.getHomeFolder(), repoName));
45+
Git fileGitRepo = GitVCSUtils.createRepository(new File(REPO_DIR, repoName));
5846
localGitRepo = fileGitRepo.getRepository();
5947
proxySelectorBackup = ProxySelector.getDefault();
6048
ProxySelector.setDefault(null);
@@ -68,11 +56,6 @@ public void tearDown() throws IOException {
6856
ProxySelector.setDefault(proxySelectorBackup);
6957
}
7058

71-
@Override
72-
protected String getTestRepoUrl() {
73-
return localVCSWorkspace.getHomeFolder().toURI().toString();
74-
}
75-
7659
@Override
7760
protected IVCS getVCS(IVCSRepositoryWorkspace mockedVCSRepo) {
7861
return Mockito.spy(new GitVCS(mockedVCSRepo));
@@ -222,14 +205,12 @@ private void testExceptionThrowingNoMock(Exception testException, Method m, Obje
222205
fail();
223206
}
224207
}
225-
226208
}
227209

228210
private void testExceptionThrowing(Exception testException, Method m, Object[] params) throws Exception {
229211
Mockito.reset(git);
230212
Mockito.doThrow(testException).when(git).getLocalGit(mockedLWC);
231213
testExceptionThrowingNoMock(testException, m, params);
232-
233214
}
234215

235216
private Boolean wasGetLocalGitInvoked() throws Exception {

0 commit comments

Comments
 (0)