|
17 | 17 | package com.palantir.gradle.gitversion; |
18 | 18 |
|
19 | 19 | import com.google.common.annotations.VisibleForTesting; |
20 | | -import com.google.common.base.Preconditions; |
21 | | -import com.google.common.base.Splitter; |
22 | 20 | import java.io.BufferedReader; |
23 | 21 | import java.io.File; |
24 | 22 | import java.io.IOException; |
|
27 | 25 | import java.util.ArrayList; |
28 | 26 | import java.util.Arrays; |
29 | 27 | import java.util.HashMap; |
30 | | -import java.util.HashSet; |
31 | 28 | import java.util.List; |
32 | 29 | import java.util.Map; |
33 | | -import java.util.Set; |
34 | 30 | import org.slf4j.Logger; |
35 | 31 | import org.slf4j.LoggerFactory; |
36 | 32 |
|
37 | | -/** |
38 | | - * Mimics git describe by using rev-list to support versions of git < 1.8.4. |
39 | | - */ |
40 | 33 | class Git { |
41 | 34 | private static final Logger log = LoggerFactory.getLogger(Git.class); |
42 | 35 |
|
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 | | - |
47 | 36 | private final File directory; |
48 | 37 |
|
49 | 38 | Git(File directory) { |
@@ -166,29 +155,18 @@ public Boolean isClean() { |
166 | 155 |
|
167 | 156 | public String describe(String prefix) { |
168 | 157 | 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; |
188 | 168 | } |
189 | | - |
190 | | - // No tags found, so return commit hash of HEAD |
191 | | - return GitUtils.abbrevHash(runGitCmd("rev-parse", "HEAD")); |
| 169 | + return result; |
192 | 170 | } catch (IOException | InterruptedException | RuntimeException e) { |
193 | 171 | log.debug("Native git describe failed", e); |
194 | 172 | return null; |
|
0 commit comments