Skip to content

Commit fa82149

Browse files
committed
getTags() fixed
tests updated
1 parent a67e3fc commit fa82149

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.eclipse.jgit.diff.DiffEntry.ChangeType;
1313
import org.eclipse.jgit.diff.DiffEntry.Side;
1414
import org.eclipse.jgit.diff.DiffFormatter;
15+
import org.eclipse.jgit.errors.IncorrectObjectTypeException;
1516
import org.eclipse.jgit.lib.*;
1617
import org.eclipse.jgit.revwalk.RevCommit;
1718
import org.eclipse.jgit.revwalk.RevSort;
@@ -259,8 +260,8 @@ public String getRepoUrl() {
259260

260261
private File getFileFromRepo(String branchName, String fileRelativePath) {
261262
try (IVCSLockedWorkingCopy wc = repo.getVCSLockedWorkingCopy();
262-
Git git = getLocalGit(wc);
263-
Repository gitRepo = git.getRepository()) {
263+
Git git = getLocalGit(wc);
264+
Repository gitRepo = git.getRepository()) {
264265

265266
checkout(git, gitRepo, branchName);
266267

@@ -678,10 +679,16 @@ public List<VCSTag> getTags() {
678679
RevCommit revCommit;
679680
for (Ref ref : tagRefs) {
680681
ObjectId relatedCommitObjectId = ref.getPeeledObjectId() == null ? ref.getObjectId() : ref.getPeeledObjectId();
681-
revTag = rw.parseTag(ref.getObjectId());
682682
revCommit = rw.parseCommit(relatedCommitObjectId);
683683
VCSCommit relatedCommit = getVCSCommit(revCommit);
684-
VCSTag tag = new VCSTag(revTag.getTagName(), revTag.getFullMessage(), revTag.getTaggerIdent().getName(), relatedCommit);
684+
VCSTag tag;
685+
try {
686+
revTag = rw.parseTag(ref.getObjectId());
687+
tag = new VCSTag(revTag.getTagName(), revTag.getFullMessage(), revTag.getTaggerIdent().getName(), relatedCommit);
688+
} catch (IncorrectObjectTypeException e) {
689+
// tag is unannonated
690+
tag = new VCSTag(ref.getName().replace("refs/tags/", ""), null, null, relatedCommit);
691+
}
685692
res.add(tag);
686693
}
687694
return res;

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

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,17 +196,17 @@ public void testExceptions() throws Exception {
196196
private void testExceptionThrowingNoMock(Exception testException, Method m, Object[] params) throws Exception {
197197
try {
198198
m.invoke(vcs, params);
199-
if (wasGetLocalGitInvoked(vcs)) {
199+
if (!m.getName().equals("checkout") && wasGetLocalGitInvoked(vcs)) {
200200
fail();
201201
}
202202
} catch (InvocationTargetException e) {
203-
if (wasGetLocalGitInvoked(vcs)) {
203+
if (!m.getName().equals("checkout") && wasGetLocalGitInvoked(vcs)) {
204204
// InvocationTargetException <- EVCSException <- GitAPIException
205205
assertTrue(e.getCause().getCause().getClass().isAssignableFrom(testException.getClass()));
206206
assertTrue(e.getCause().getMessage().contains(testException.getMessage()));
207207
}
208208
} catch (Exception e) {
209-
if (wasGetLocalGitInvoked(vcs)) {
209+
if (!m.getName().equals("checkout") && wasGetLocalGitInvoked(vcs)) {
210210
fail();
211211
}
212212
}
@@ -287,6 +287,18 @@ public void testGetLastTagUnannotatedTag() throws Exception {
287287
assertEquals(tag.getRelatedCommit(), vcs.getHeadCommit(null));
288288
}
289289

290+
@Test
291+
public void testGetTagsUnannotated() throws Exception {
292+
createUnannotatedTag(null, TAG_NAME_1);
293+
List<VCSTag> tags = vcs.getTags();
294+
assertTrue(tags.size() == 1);
295+
VCSTag tag = tags.get(0);
296+
assertNull(tag.getAuthor());
297+
assertNull(tag.getTagMessage());
298+
assertEquals(tag.getTagName(), TAG_NAME_1);
299+
assertEquals(tag.getRelatedCommit(), vcs.getHeadCommit(null));
300+
}
301+
290302
public void createUnannotatedTag(String branchName, String tagName) throws Exception {
291303
try (IVCSLockedWorkingCopy wc = localVCSRepo.getVCSLockedWorkingCopy();
292304
Git localGit = git.getLocalGit(wc);
@@ -330,5 +342,18 @@ public void testGetLastTagExceptions() throws Exception {
330342
Mockito.doThrow(eCommon).when(git).getLocalGit(mockedLWC);
331343
testExceptionThrowingNoMock(eCommon, vcs.getClass().getDeclaredMethod("getLastTag"), new Object[0]);
332344
}
345+
346+
@Test
347+
public void testCheckoutExceptions() throws Exception {
348+
Exception eCommon = new Exception("test common exception");
349+
Mockito.doThrow(eCommon).when(git).getLocalGit((String) null);
350+
try {
351+
git.checkout(null, null);
352+
fail();
353+
} catch (RuntimeException e) {
354+
assertTrue(e.getCause().getClass().isAssignableFrom(eCommon.getClass()));
355+
assertTrue(e.getCause().getMessage().contains(eCommon.getMessage()));
356+
}
357+
}
333358
}
334359

0 commit comments

Comments
 (0)