Skip to content

Commit dc4e876

Browse files
committed
fixed working with tags.
1 parent e0f9cd2 commit dc4e876

File tree

2 files changed

+32
-25
lines changed

2 files changed

+32
-25
lines changed

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

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,9 @@ public VCSTag createTag(String branchName, String tagName, String tagMessage, St
729729
Repository gitRepo = git.getRepository();
730730
RevWalk rw = new RevWalk(gitRepo)) {
731731

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

734736
RevCommit commitToTag = revisionToTag == null ? null : rw.parseCommit(ObjectId.fromString(revisionToTag));
735737

@@ -763,9 +765,7 @@ public List<VCSTag> getTags() {
763765
Repository gitRepo = git.getRepository();
764766
RevWalk rw = new RevWalk(gitRepo)) {
765767

766-
git.pull().call();
767-
768-
List<Ref> tagRefs = getTagRefs();
768+
List<Ref> tagRefs = getTagRefs(git);
769769
List<VCSTag> res = new ArrayList<>();
770770
RevCommit revCommit;
771771
for (Ref ref : tagRefs) {
@@ -778,6 +778,7 @@ public List<VCSTag> getTags() {
778778
RevTag revTag = (RevTag) revObject;
779779
tag = new VCSTag(revTag.getTagName(), revTag.getFullMessage(), revTag.getTaggerIdent().getName(), relatedCommit);
780780
} else {
781+
// tag is unannotated
781782
tag = new VCSTag(ref.getName().replace("refs/tags/", ""), null, null, relatedCommit);
782783
}
783784
res.add(tag);
@@ -788,19 +789,17 @@ public List<VCSTag> getTags() {
788789
}
789790
}
790791

791-
List<Ref> getTagRefs() throws Exception {
792-
try (IVCSLockedWorkingCopy wc = repo.getVCSLockedWorkingCopy();
793-
Git git = getLocalGit(wc);
794-
Repository gitRepo = git.getRepository()) {
795-
796-
git.pull().call();
797-
798-
List<Ref> refs = git
799-
.tagList()
800-
.call();
801-
802-
return refs;
803-
}
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();
804803
}
805804

806805
@Override
@@ -810,15 +809,16 @@ public void removeTag(String tagName) {
810809
Repository gitRepo = git.getRepository();
811810
RevWalk rw = new RevWalk(gitRepo)) {
812811

813-
checkout(git, gitRepo, MASTER_BRANCH_NAME, null);
814-
812+
git.pull().call();
813+
815814
git
816815
.tagDelete()
817816
.setTags(tagName)
818817
.call();
819-
818+
820819
push(git, new RefSpec(":refs/tags/" + tagName));
821820

821+
822822
} catch (GitAPIException e) {
823823
throw new EVCSException(e);
824824
} catch (Exception e) {
@@ -848,10 +848,8 @@ public List<VCSTag> getTagsOnRevision(String revision) {
848848
RevWalk rw = new RevWalk(gitRepo)) {
849849

850850
List<VCSTag> res = new ArrayList<>();
851-
852-
git.pull().call();
853851

854-
List<Ref> tagRefs = getTagRefs();
852+
List<Ref> tagRefs = getTagRefs(git);
855853
RevCommit revCommit;
856854
for (Ref ref : tagRefs) {
857855
ObjectId relatedCommitObjectId = ref.getPeeledObjectId() == null ? ref.getObjectId() : ref.getPeeledObjectId();

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@
3939
import org.scm4j.vcs.api.VCSTag;
4040
import org.scm4j.vcs.api.abstracttest.VCSAbstractTest;
4141
import org.scm4j.vcs.api.exceptions.EVCSException;
42+
import org.scm4j.vcs.api.workingcopy.IVCSLockedWorkingCopy;
4243
import org.scm4j.vcs.api.workingcopy.IVCSRepositoryWorkspace;
44+
import org.scm4j.vcs.api.workingcopy.IVCSWorkspace;
45+
import org.scm4j.vcs.api.workingcopy.VCSWorkspace;
4346

4447
public class GitVCSTest extends VCSAbstractTest {
4548

@@ -67,7 +70,7 @@ public void tearDown() throws IOException {
6770

6871
@Override
6972
protected String getTestRepoUrl() {
70-
return ("file:///" + localVCSWorkspace.getHomeFolder() + "/").replace("\\", "/");
73+
return localVCSWorkspace.getHomeFolder().toURI().toString();
7174
}
7275

7376
@Override
@@ -254,7 +257,13 @@ public void testGitVCSUtilsCreation() {
254257

255258
@Test
256259
public void testGetTagsUnannotated() throws Exception {
257-
git.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+
}
258267
List<VCSTag> tags = vcs.getTags();
259268
assertTrue(tags.size() == 1);
260269
VCSTag tag = tags.get(0);

0 commit comments

Comments
 (0)