Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to list all tags #817

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ You can get an object containing more detailed information by calling:
```groovy
def details = versionDetails()
details.lastTag
details.lastTags
details.commitDistance
details.gitHash
details.gitHashFull // full 40-character Git commit hash
Expand All @@ -65,6 +66,7 @@ details.isCleanTag
val versionDetails: groovy.lang.Closure<com.palantir.gradle.gitversion.VersionDetails> by extra
val details = versionDetails()
details.lastTag
details.lastTags
details.commitDistance
details.gitHash
details.gitHashFull // full 40-character Git commit hash
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/palantir/gradle/gitversion/Git.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,19 @@ public String describe(String prefix) {
}
}

public String[] getAllTags() {
try {
String result = runGitCmd("tag", "--points-at", "HEAD");
if (result.isEmpty()) {
return new String[0];
}
return result.split("\n");
} catch (IOException | InterruptedException | RuntimeException e) {
log.debug("Native git describe failed", e);
return null;
}
}

private boolean gitCommandExists() {
try {
// verify that "git" command exists (throws exception if it does not)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public interface VersionDetails {

String getLastTag();

String[] getLastTags();

int getCommitDistance();

boolean getIsCleanTag();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ public String getLastTag() {
return match.matches() ? match.group(1) : null;
}

@Override
public String[] getLastTags() {
return nativeGitInvoker.getAllTags();
}

@Override
public String getGitHash() throws IOException {
String gitHashFull = getGitHashFull();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,7 @@ class GitVersionPluginTests extends Specification {
buildResult.output.contains(":printVersion\n2.0.0\n")
}


def 'test multiple tags on same commit - most recent annotated tag' () {
given:
buildFile << '''
Expand Down Expand Up @@ -670,6 +671,33 @@ class GitVersionPluginTests extends Specification {
buildResult.output.contains(":printVersion\n1.0.0\n")
}

def 'test multiple tags on same commit - all tags are listed' () {
given:
buildFile << '''
plugins {
id 'com.palantir.git-version'
}

task printVersionDetails { doLast {
println versionDetails().getLastTags()
}}
'''.stripIndent()
gitIgnoreFile << 'build'
Git git = new Git(projectDir, true)
git.runGitCommand("init", projectDir.toString())
git.runGitCommand("add", ".")
git.runGitCommand("commit", "-m", "'initial commit'")
git.runGitCommand("tag", "1.0.0")
git.runGitCommand("tag", "2.0.0")
git.runGitCommand("tag", "3.0.0")

when:
BuildResult buildResult = with('printVersionDetails').build()

then:
buildResult.output.contains("[1.0.0, 2.0.0, 3.0.0]")
}

def 'test tag set on deep commit' () {
given:
buildFile << '''
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public void short_sha_when_no_annotated_tags_are_present_and_dirty_content() thr

write(new File(temporaryFolder, "foo"));

assertThat(versionDetails().getVersion()).isEqualTo("f0f4555.dirty");
assertThat(versionDetails().getVersion()).isEqualTo("611f9e4.dirty");
}

@Test
Expand Down