Skip to content

Commit 8e380cc

Browse files
authored
Issue #704: Use built in git-describe (#705)
Use built in git-describe
1 parent 0ad5312 commit 8e380cc

File tree

4 files changed

+19
-69
lines changed

4 files changed

+19
-69
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
type: break
2+
break:
3+
description: Use built in git-describe rather than an expensive recreation of it
4+
to support older versions of git. This version requires git version >=1.8.4 (released
5+
23rd August 2013).
6+
links:
7+
- https://github.com/palantir/gradle-git-version/pull/705

src/main/java/com/palantir/gradle/gitversion/Git.java

Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
package com.palantir.gradle.gitversion;
1818

1919
import com.google.common.annotations.VisibleForTesting;
20-
import com.google.common.base.Preconditions;
21-
import com.google.common.base.Splitter;
2220
import java.io.BufferedReader;
2321
import java.io.File;
2422
import java.io.IOException;
@@ -27,23 +25,14 @@
2725
import java.util.ArrayList;
2826
import java.util.Arrays;
2927
import java.util.HashMap;
30-
import java.util.HashSet;
3128
import java.util.List;
3229
import java.util.Map;
33-
import java.util.Set;
3430
import org.slf4j.Logger;
3531
import org.slf4j.LoggerFactory;
3632

37-
/**
38-
* Mimics git describe by using rev-list to support versions of git < 1.8.4.
39-
*/
4033
class Git {
4134
private static final Logger log = LoggerFactory.getLogger(Git.class);
4235

43-
private static final Splitter LINE_SPLITTER =
44-
Splitter.on(System.getProperty("line.separator")).omitEmptyStrings();
45-
private static final Splitter WORD_SPLITTER = Splitter.on(" ").omitEmptyStrings();
46-
4736
private final File directory;
4837

4938
Git(File directory) {
@@ -166,29 +155,18 @@ public Boolean isClean() {
166155

167156
public String describe(String prefix) {
168157
try {
169-
// Get SHAs of all tags, we only need to search for these later on
170-
Set<String> tagRefs = new HashSet<>();
171-
for (String tag : LINE_SPLITTER.splitToList(runGitCmd("show-ref", "--tags", "-d"))) {
172-
List<String> parts = WORD_SPLITTER.splitToList(tag);
173-
Preconditions.checkArgument(parts.size() == 2, "Could not parse output of `git show-ref`: %s", parts);
174-
tagRefs.add(parts.get(0));
175-
}
176-
177-
List<String> revs = LINE_SPLITTER.splitToList(runGitCmd("rev-list", "--first-parent", "HEAD"));
178-
for (int depth = 0; depth < revs.size(); depth++) {
179-
String rev = revs.get(depth);
180-
if (tagRefs.contains(rev)) {
181-
String exactTag = runGitCmd("describe", "--tags", "--exact-match", "--match=" + prefix + "*", rev);
182-
if (!exactTag.isEmpty()) {
183-
return depth == 0
184-
? exactTag
185-
: String.format("%s-%s-g%s", exactTag, depth, GitUtils.abbrevHash(revs.get(0)));
186-
}
187-
}
158+
String result = runGitCmd(
159+
"describe",
160+
"--tags",
161+
"--always",
162+
"--first-parent",
163+
"--abbrev=7",
164+
"--match=" + prefix + "*",
165+
"HEAD");
166+
if (result.isEmpty()) {
167+
return null;
188168
}
189-
190-
// No tags found, so return commit hash of HEAD
191-
return GitUtils.abbrevHash(runGitCmd("rev-parse", "HEAD"));
169+
return result;
192170
} catch (IOException | InterruptedException | RuntimeException e) {
193171
log.debug("Native git describe failed", e);
194172
return null;

src/main/java/com/palantir/gradle/gitversion/GitUtils.java

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

src/main/java/com/palantir/gradle/gitversion/VersionDetailsImpl.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,12 @@ private boolean isClean() {
5454
}
5555

5656
private String description() {
57-
String rawDescription = expensiveComputeRawDescription();
57+
String rawDescription = nativeGitInvoker.describe(args.getPrefix());
5858
String processedDescription =
5959
rawDescription == null ? null : rawDescription.replaceFirst("^" + args.getPrefix(), "");
6060
return processedDescription;
6161
}
6262

63-
private String expensiveComputeRawDescription() {
64-
65-
String nativeGitDescribe = nativeGitInvoker.describe(args.getPrefix());
66-
67-
return nativeGitDescribe;
68-
}
69-
7063
@Override
7164
public boolean getIsCleanTag() {
7265
return isClean() && descriptionIsPlainTag();

0 commit comments

Comments
 (0)