Skip to content
New issue

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

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

Already on GitHub? # to your account

Issue #704: Use built in git-describe #705

Merged
merged 8 commits into from
Mar 29, 2023
Merged
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
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
esword marked this conversation as resolved.
Show resolved Hide resolved
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