Skip to content

Commit 7d115bc

Browse files
author
Karol Lassak
committed
Added support for git describe when all tags are on different branches
- Before it throws exception on such situation - Now it display commit d if set - Added test repo + test - In process cleaned up gitk.cache
1 parent 399578b commit 7d115bc

36 files changed

+580
-54
lines changed

src/main/java/pl/project13/jgit/DescribeCommand.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -319,14 +319,15 @@ public DescribeResult call() throws GitAPIException {
319319
return new DescribeResult(tagName, dirty, dirtyOption);
320320
}
321321

322-
if (foundZeroTags(tagObjectIdToName)) {
322+
// get commits, up until the nearest tag
323+
List<RevCommit> commits = findCommitsUntilSomeTag(repo, headCommit, tagObjectIdToName);
324+
325+
// if there is no tags or any tag is not on that branch then return generic describe
326+
if (foundZeroTags(tagObjectIdToName) || commits.isEmpty()) {
323327
return new DescribeResult(objectReader, headCommitId, dirty, dirtyOption)
324328
.withCommitIdAbbrev(abbrev);
325329
}
326330

327-
// get commits, up until the nearest tag
328-
List<RevCommit> commits = findCommitsUntilSomeTag(repo, headCommit, tagObjectIdToName);
329-
330331
// check how far away from a tag we are
331332

332333
int distance = distanceBetween(repo, headCommit, commits.get(0));
@@ -425,7 +426,8 @@ private List<RevCommit> findCommitsUntilSomeTag(Repository repo, RevCommit head,
425426
}
426427
}
427428

428-
throw new RuntimeException("Did not find any commits until some tag");
429+
// throw new RuntimeException("Did not find any commits until some tag");
430+
return Collections.emptyList();
429431
} catch (Exception e) {
430432
throw new RuntimeException("Unable to find commits until some tag", e);
431433
}

src/test/java/pl/project13/jgit/DescribeCommandIntegrationTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,31 @@ public void shouldGiveTheCommitIdWhenNothingElseCanBeFound() throws Exception {
9595
RevCommit HEAD = git().log().call().iterator().next();
9696
assertThat(res.toString()).isEqualTo(abbrev(HEAD.getName()));
9797
}
98+
99+
@Test
100+
public void shouldGiveTheCommitIdWhenTagIsOnOtherBranch() throws Exception {
101+
// given
102+
mavenSandbox
103+
.withParentProject(PROJECT_NAME, "jar")
104+
.withNoChildProject()
105+
.withGitRepoInParent(AvailableGitTestRepo.WITH_TAG_ON_DIFFERENT_BRANCH)
106+
.create(FileSystemMavenSandbox.CleanUp.CLEANUP_FIRST);
98107

108+
Repository repo = git().getRepository();
109+
110+
// when
111+
DescribeCommand command = spy(DescribeCommand.on(repo));
112+
doReturn(false).when(command).findDirtyState(any(Repository.class));
113+
114+
command.setVerbose(true);
115+
DescribeResult res = command.call();
116+
117+
// then
118+
assertThat(res).isNotNull();
119+
120+
RevCommit HEAD = git().log().call().iterator().next();
121+
assertThat(res.toString()).isEqualTo(abbrev(HEAD.getName()));
122+
}
99123

100124
@Test
101125
public void shouldGiveTheCommitIdWhenNothingElseCanBeFoundAndUseAbbrevVersionOfIt() throws Exception {

src/test/java/pl/project13/maven/git/AvailableGitTestRepo.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ public enum AvailableGitTestRepo {
5555
* 9597545 - (annotated-tag) initial commit (6 minutes ago) <Konrad Malawski>
5656
* </pre>
5757
*/
58-
WITH_LIGHTWEIGHT_TAG_BEFORE_ANNOTATED_TAG("src/test/resources/_git_lightweight_tag_before_annotated_tag");
58+
WITH_LIGHTWEIGHT_TAG_BEFORE_ANNOTATED_TAG("src/test/resources/_git_lightweight_tag_before_annotated_tag"),
59+
WITH_TAG_ON_DIFFERENT_BRANCH("src/test/resources/_git_with_tag_on_different_branch");
5960

6061
private String dir;
6162

src/test/resources/_git_of_git_commit_id/gitk.cache

Lines changed: 0 additions & 45 deletions
This file was deleted.

src/test/resources/_git_with_commit_that_has_two_tags/gitk.cache

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Change in tag
2+
# Please enter the commit message for your changes. Lines starting
3+
# with '#' will be ignored, and an empty message aborts the commit.
4+
# On branch test
5+
# Changes to be committed:
6+
# (use "git reset HEAD <file>..." to unstage)
7+
#
8+
# modified: readme.txt
9+
#
10+
diff --git a/readme.txt b/readme.txt
11+
index 53a90bf..e69336e 100644
12+
--- a/readme.txt
13+
+++ b/readme.txt
14+
@@ -1 +1 @@
15+
-Empty
16+
+Other content
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ref: refs/heads/master
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[core]
2+
repositoryformatversion = 0
3+
filemode = true
4+
bare = false
5+
logallrefupdates = true
6+
ignorecase = true
7+
precomposeunicode = false
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Unnamed repository; edit this file 'description' to name the repository.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/sh
2+
#
3+
# An example hook script to check the commit log message taken by
4+
# applypatch from an e-mail message.
5+
#
6+
# The hook should exit with non-zero status after issuing an
7+
# appropriate message if it wants to stop the commit. The hook is
8+
# allowed to edit the commit message file.
9+
#
10+
# To enable this hook, rename this file to "applypatch-msg".
11+
12+
. git-sh-setup
13+
test -x "$GIT_DIR/hooks/commit-msg" &&
14+
exec "$GIT_DIR/hooks/commit-msg" ${1+"$@"}
15+
:

0 commit comments

Comments
 (0)