Skip to content

Commit d713342

Browse files
committed
Merge branch 'master' into release/4
# Conflicts: # version
2 parents 9859bd7 + 4d4147f commit d713342

File tree

4 files changed

+75
-64
lines changed

4 files changed

+75
-64
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Features:
1212
- Branches list
1313
- File content getting and setting
1414
- File create and remove
15+
- Working with tags
1516

1617
# Terms
1718
- Workspace Home
@@ -52,6 +53,8 @@ Features:
5253
```
5354
- Use methods of `IVCS` interface. See [scm4j-vcs-api](https://github.com/scm4j/scm4j-vcs-api) for details
5455
- Use `vcs.setProxy()` and `vcs.setCredentials()` if necessary
56+
- Use `VCSTag createUnannotatedTag(String branchName, String tagName, String revisionToTag)` to create git unannontated tag with name `tagName` on `revisionToTag` commit of branch `branchName`. If `branchName` is null then master branch is used. If `revisionToTag` is null then head of branch `branchName` is used.
57+
-
5558

5659
# Implementation details
5760
- [JGit](https://eclipse.org/jgit/) is used as framework to work with Git repositories

build.gradle

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ jacocoTestReport {
2525
}
2626
}
2727

28+
test {
29+
testLogging {
30+
events "failed"
31+
exceptionFormat "full"
32+
}
33+
}
2834

2935
repositories {
3036
mavenCentral()

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

Lines changed: 51 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ protected Git getLocalGit(String folder) throws Exception {
104104
.setCredentialsProvider(credentials)
105105
.setNoCheckout(true)
106106
.setCloneAllBranches(true)
107-
//.setBranch(Constants.R_HEADS + Constants.MASTER)
108107
.call()
109108
.close();
110109
}
@@ -127,6 +126,36 @@ public VCSChangeType gitChangeTypeToVCSChangeType(ChangeType changeType) {
127126
return VCSChangeType.UNKNOWN;
128127
}
129128
}
129+
130+
public VCSTag createUnannotatedTag(String branchName, String tagName, String revisionToTag) {
131+
try (IVCSLockedWorkingCopy wc = repo.getVCSLockedWorkingCopy();
132+
Git git = getLocalGit(wc);
133+
Repository gitRepo = git.getRepository();
134+
RevWalk rw = new RevWalk(gitRepo)) {
135+
136+
git
137+
.pull()
138+
.call();
139+
140+
RevCommit commitToTag = revisionToTag == null ? null : rw.parseCommit(ObjectId.fromString(revisionToTag));
141+
142+
Ref ref = git
143+
.tag()
144+
.setAnnotated(false)
145+
.setName(tagName)
146+
.setObjectId(commitToTag)
147+
.call();
148+
149+
push(git, new RefSpec(ref.getName()));
150+
151+
return new VCSTag(tagName, null, null, revisionToTag == null ? getHeadCommit(branchName)
152+
: getVCSCommit(commitToTag));
153+
} catch (GitAPIException e) {
154+
throw new EVCSException(e);
155+
} catch (Exception e) {
156+
throw new RuntimeException(e);
157+
}
158+
}
130159

131160
@Override
132161
public void createBranch(String srcBranchName, String newBranchName, String commitMessage) {
@@ -700,7 +729,9 @@ public VCSTag createTag(String branchName, String tagName, String tagMessage, St
700729
Repository gitRepo = git.getRepository();
701730
RevWalk rw = new RevWalk(gitRepo)) {
702731

703-
checkout(git, gitRepo, branchName, null);
732+
git
733+
.pull()
734+
.call();
704735

705736
RevCommit commitToTag = revisionToTag == null ? null : rw.parseCommit(ObjectId.fromString(revisionToTag));
706737

@@ -734,9 +765,7 @@ public List<VCSTag> getTags() {
734765
Repository gitRepo = git.getRepository();
735766
RevWalk rw = new RevWalk(gitRepo)) {
736767

737-
git.pull().call();
738-
739-
List<Ref> tagRefs = getTagRefs();
768+
List<Ref> tagRefs = getTagRefs(git);
740769
List<VCSTag> res = new ArrayList<>();
741770
RevCommit revCommit;
742771
for (Ref ref : tagRefs) {
@@ -749,6 +778,7 @@ public List<VCSTag> getTags() {
749778
RevTag revTag = (RevTag) revObject;
750779
tag = new VCSTag(revTag.getTagName(), revTag.getFullMessage(), revTag.getTaggerIdent().getName(), relatedCommit);
751780
} else {
781+
// tag is unannotated
752782
tag = new VCSTag(ref.getName().replace("refs/tags/", ""), null, null, relatedCommit);
753783
}
754784
res.add(tag);
@@ -759,19 +789,17 @@ public List<VCSTag> getTags() {
759789
}
760790
}
761791

762-
List<Ref> getTagRefs() throws Exception {
763-
try (IVCSLockedWorkingCopy wc = repo.getVCSLockedWorkingCopy();
764-
Git git = getLocalGit(wc);
765-
Repository gitRepo = git.getRepository()) {
766-
767-
git.pull().call();
768-
769-
List<Ref> refs = git
770-
.tagList()
771-
.call();
772-
773-
return refs;
774-
}
792+
private List<Ref> getTagRefs(Git git) throws Exception {
793+
// need to remove tags from local repo which are removed in origin
794+
git
795+
.fetch()
796+
.setRefSpecs(new RefSpec("+refs/tags/*:refs/tags/*"))
797+
.setRemoveDeletedRefs(true)
798+
.setCredentialsProvider(credentials)
799+
.call();
800+
return git
801+
.tagList()
802+
.call();
775803
}
776804

777805
@Override
@@ -781,15 +809,16 @@ public void removeTag(String tagName) {
781809
Repository gitRepo = git.getRepository();
782810
RevWalk rw = new RevWalk(gitRepo)) {
783811

784-
checkout(git, gitRepo, MASTER_BRANCH_NAME, null);
785-
812+
git.pull().call();
813+
786814
git
787815
.tagDelete()
788816
.setTags(tagName)
789817
.call();
790-
818+
791819
push(git, new RefSpec(":refs/tags/" + tagName));
792820

821+
793822
} catch (GitAPIException e) {
794823
throw new EVCSException(e);
795824
} catch (Exception e) {
@@ -819,10 +848,8 @@ public List<VCSTag> getTagsOnRevision(String revision) {
819848
RevWalk rw = new RevWalk(gitRepo)) {
820849

821850
List<VCSTag> res = new ArrayList<>();
822-
823-
git.pull().call();
824851

825-
List<Ref> tagRefs = getTagRefs();
852+
List<Ref> tagRefs = getTagRefs(git);
826853
RevCommit revCommit;
827854
for (Ref ref : tagRefs) {
828855
ObjectId relatedCommitObjectId = ref.getPeeledObjectId() == null ? ref.getObjectId() : ref.getPeeledObjectId();

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

Lines changed: 15 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,12 @@
2323
import java.util.List;
2424

2525
import org.apache.commons.io.FileUtils;
26+
import org.apache.commons.lang3.ArrayUtils;
2627
import org.eclipse.jgit.api.Git;
2728
import org.eclipse.jgit.api.errors.GitAPIException;
2829
import org.eclipse.jgit.diff.DiffEntry;
29-
import org.eclipse.jgit.lib.ObjectId;
30-
import org.eclipse.jgit.lib.Ref;
3130
import org.eclipse.jgit.lib.Repository;
32-
import org.eclipse.jgit.revwalk.RevCommit;
33-
import org.eclipse.jgit.revwalk.RevWalk;
3431
import org.eclipse.jgit.transport.CredentialItem;
35-
import org.eclipse.jgit.transport.RefSpec;
3632
import org.junit.After;
3733
import org.junit.Test;
3834
import org.mockito.Mockito;
@@ -45,6 +41,8 @@
4541
import org.scm4j.vcs.api.exceptions.EVCSException;
4642
import org.scm4j.vcs.api.workingcopy.IVCSLockedWorkingCopy;
4743
import org.scm4j.vcs.api.workingcopy.IVCSRepositoryWorkspace;
44+
import org.scm4j.vcs.api.workingcopy.IVCSWorkspace;
45+
import org.scm4j.vcs.api.workingcopy.VCSWorkspace;
4846

4947
public class GitVCSTest extends VCSAbstractTest {
5048

@@ -72,7 +70,7 @@ public void tearDown() throws IOException {
7270

7371
@Override
7472
protected String getTestRepoUrl() {
75-
return ("file:///" + localVCSWorkspace.getHomeFolder() + "/").replace("\\", "/");
73+
return localVCSWorkspace.getHomeFolder().toURI().toString();
7674
}
7775

7876
@Override
@@ -194,7 +192,7 @@ public void testExceptions() throws Exception {
194192
@SuppressWarnings("serial")
195193
GitAPIException eApi = new GitAPIException("test git exception") {};
196194
Exception eCommon = new Exception("test common exception");
197-
for (Method m : IVCS.class.getDeclaredMethods()) {
195+
for (Method m : ArrayUtils.addAll(IVCS.class.getDeclaredMethods(), GitVCS.class.getMethod("createUnannotatedTag", String.class, String.class, String.class))) {
198196
Object[] params = new Object[m.getParameterTypes().length];
199197
Integer i = 0;
200198
for (Class<?> clazz : m.getParameterTypes()) {
@@ -259,7 +257,13 @@ public void testGitVCSUtilsCreation() {
259257

260258
@Test
261259
public void testGetTagsUnannotated() throws Exception {
262-
createUnannotatedTag(null, TAG_NAME_1, null);
260+
// create tag in different working copy
261+
try (IVCSLockedWorkingCopy lwc = localVCSRepo.getVCSLockedWorkingCopyTemp()) {
262+
IVCSWorkspace tempWS = new VCSWorkspace(lwc.getFolder().toString());
263+
IVCSRepositoryWorkspace tempRWS = tempWS.getVCSRepositoryWorkspace(vcs.getRepoUrl());
264+
GitVCS tempVCS = new GitVCS(tempRWS);
265+
tempVCS.createUnannotatedTag(null, TAG_NAME_1, null);
266+
}
263267
List<VCSTag> tags = vcs.getTags();
264268
assertTrue(tags.size() == 1);
265269
VCSTag tag = tags.get(0);
@@ -269,35 +273,6 @@ public void testGetTagsUnannotated() throws Exception {
269273
assertEquals(tag.getRelatedCommit(), vcs.getHeadCommit(null));
270274
}
271275

272-
public VCSTag createUnannotatedTag(String branchName, String tagName, String revisionToTag) throws Exception {
273-
try (IVCSLockedWorkingCopy wc = localVCSRepo.getVCSLockedWorkingCopy();
274-
Git localGit = git.getLocalGit(wc);
275-
Repository gitRepo = localGit.getRepository();
276-
RevWalk rw = new RevWalk(gitRepo)) {
277-
278-
git.checkout(localGit, gitRepo, branchName, null);
279-
280-
RevCommit commitToTag = revisionToTag == null ? null : rw.parseCommit(ObjectId.fromString(revisionToTag));
281-
282-
Ref ref = localGit
283-
.tag()
284-
.setAnnotated(false)
285-
.setName(tagName)
286-
.setObjectId(commitToTag)
287-
.call();
288-
289-
localGit
290-
.push()
291-
.setPushAll()
292-
.setRefSpecs(new RefSpec(ref.getName()))
293-
.setRemote("origin")
294-
.setCredentialsProvider(git.getCredentials())
295-
.call();
296-
return new VCSTag(tagName, null, null, revisionToTag == null ? vcs.getHeadCommit(branchName)
297-
: git.getVCSCommit(commitToTag));
298-
}
299-
}
300-
301276
@Test
302277
public void testCheckoutExceptions() throws Exception {
303278
@SuppressWarnings("serial")
@@ -329,9 +304,9 @@ public void testGetTagsOnRevisionUnannotated() throws Exception {
329304
vcs.createBranch(null, NEW_BRANCH, CREATED_DST_BRANCH_COMMIT_MESSAGE);
330305
VCSCommit c3 = vcs.setFileContent(NEW_BRANCH, FILE1_NAME, LINE_3, FILE1_CONTENT_CHANGED_COMMIT_MESSAGE + " " + LINE_3);
331306

332-
VCSTag tag1 = createUnannotatedTag(null, TAG_NAME_1, c1.getRevision());
333-
VCSTag tag2 = createUnannotatedTag(null, TAG_NAME_2, c1.getRevision());
334-
VCSTag tag3 = createUnannotatedTag(NEW_BRANCH, TAG_NAME_3, c3.getRevision());
307+
VCSTag tag1 = git.createUnannotatedTag(null, TAG_NAME_1, c1.getRevision());
308+
VCSTag tag2 = git.createUnannotatedTag(null, TAG_NAME_2, c1.getRevision());
309+
VCSTag tag3 = git.createUnannotatedTag(NEW_BRANCH, TAG_NAME_3, c3.getRevision());
335310

336311
assertTrue(vcs.getTagsOnRevision(c1.getRevision()).containsAll(Arrays.asList(
337312
tag1, tag2)));

0 commit comments

Comments
 (0)