Skip to content

Commit

Permalink
Issue #704: Use built in git-describe (#705)
Browse files Browse the repository at this point in the history
Use built in git-describe
  • Loading branch information
esword authored Mar 29, 2023
1 parent 0ad5312 commit 8e380cc
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 69 deletions.
7 changes: 7 additions & 0 deletions changelog/@unreleased/pr-705.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
type: break
break:
description: Use built in git-describe rather than an expensive recreation of it
to support older versions of git. This version requires git version >=1.8.4 (released
23rd August 2013).
links:
- https://github.com/palantir/gradle-git-version/pull/705
44 changes: 11 additions & 33 deletions src/main/java/com/palantir/gradle/gitversion/Git.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
package com.palantir.gradle.gitversion;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.base.Splitter;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
Expand All @@ -27,23 +25,14 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

private static final Splitter LINE_SPLITTER =
Splitter.on(System.getProperty("line.separator")).omitEmptyStrings();
private static final Splitter WORD_SPLITTER = Splitter.on(" ").omitEmptyStrings();

private final File directory;

Git(File directory) {
Expand Down Expand Up @@ -166,29 +155,18 @@ public Boolean isClean() {

public String describe(String prefix) {
try {
// Get SHAs of all tags, we only need to search for these later on
Set<String> tagRefs = new HashSet<>();
for (String tag : LINE_SPLITTER.splitToList(runGitCmd("show-ref", "--tags", "-d"))) {
List<String> parts = WORD_SPLITTER.splitToList(tag);
Preconditions.checkArgument(parts.size() == 2, "Could not parse output of `git show-ref`: %s", parts);
tagRefs.add(parts.get(0));
}

List<String> revs = LINE_SPLITTER.splitToList(runGitCmd("rev-list", "--first-parent", "HEAD"));
for (int depth = 0; depth < revs.size(); depth++) {
String rev = revs.get(depth);
if (tagRefs.contains(rev)) {
String exactTag = runGitCmd("describe", "--tags", "--exact-match", "--match=" + prefix + "*", rev);
if (!exactTag.isEmpty()) {
return depth == 0
? exactTag
: String.format("%s-%s-g%s", exactTag, depth, GitUtils.abbrevHash(revs.get(0)));
}
}
String result = runGitCmd(
"describe",
"--tags",
"--always",
"--first-parent",
"--abbrev=7",
"--match=" + prefix + "*",
"HEAD");
if (result.isEmpty()) {
return null;
}

// No tags found, so return commit hash of HEAD
return GitUtils.abbrevHash(runGitCmd("rev-parse", "HEAD"));
return result;
} catch (IOException | InterruptedException | RuntimeException e) {
log.debug("Native git describe failed", e);
return null;
Expand Down
28 changes: 0 additions & 28 deletions src/main/java/com/palantir/gradle/gitversion/GitUtils.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,12 @@ private boolean isClean() {
}

private String description() {
String rawDescription = expensiveComputeRawDescription();
String rawDescription = nativeGitInvoker.describe(args.getPrefix());
String processedDescription =
rawDescription == null ? null : rawDescription.replaceFirst("^" + args.getPrefix(), "");
return processedDescription;
}

private String expensiveComputeRawDescription() {

String nativeGitDescribe = nativeGitInvoker.describe(args.getPrefix());

return nativeGitDescribe;
}

@Override
public boolean getIsCleanTag() {
return isClean() && descriptionIsPlainTag();
Expand Down

0 comments on commit 8e380cc

Please sign in to comment.