Skip to content

Commit 1765fb3

Browse files
committed
Merge pull request #84 from ingwarsw/master
Added support for git describe when all tags are on different branches
2 parents 41152b4 + d07257d commit 1765fb3

37 files changed

+582
-57
lines changed

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

Lines changed: 6 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,7 @@ private List<RevCommit> findCommitsUntilSomeTag(Repository repo, RevCommit head,
425426
}
426427
}
427428

428-
throw new RuntimeException("Did not find any commits until some tag");
429+
return Collections.emptyList();
429430
} catch (Exception e) {
430431
throw new RuntimeException("Unable to find commits until some tag", e);
431432
}

src/main/java/pl/project13/maven/git/GitCommitIdMojo.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public class GitCommitIdMojo extends AbstractMojo {
7878
/**
7979
* The maven project.
8080
*
81-
* @parameter expression="${project}"
81+
* @parameter property="project"
8282
* @readonly
8383
*/
8484
@SuppressWarnings("UnusedDeclaration")
@@ -87,7 +87,7 @@ public class GitCommitIdMojo extends AbstractMojo {
8787
/**
8888
* Contains the full list of projects in the reactor.
8989
*
90-
* @parameter expression="${reactorProjects}"
90+
* @parameter property="reactorProjects"
9191
* @readonly
9292
*/
9393
@SuppressWarnings("UnusedDeclaration")
@@ -119,7 +119,7 @@ public class GitCommitIdMojo extends AbstractMojo {
119119
* Specifies whether the execution in pom projects should be skipped.
120120
* Override this value to false if you want to force the plugin to run on 'pom' packaged projects.
121121
*
122-
* @parameter expression="${git.skipPoms}" default-value="true"
122+
* @parameter property="git.skipPoms" default-value="true"
123123
*/
124124
@SuppressWarnings("UnusedDeclaration")
125125
private boolean skipPoms;

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.

0 commit comments

Comments
 (0)