diff --git a/changelog/@unreleased/pr-705.v2.yml b/changelog/@unreleased/pr-705.v2.yml new file mode 100644 index 00000000..067401d1 --- /dev/null +++ b/changelog/@unreleased/pr-705.v2.yml @@ -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 diff --git a/src/main/java/com/palantir/gradle/gitversion/Git.java b/src/main/java/com/palantir/gradle/gitversion/Git.java index 4531cba3..fcc417eb 100644 --- a/src/main/java/com/palantir/gradle/gitversion/Git.java +++ b/src/main/java/com/palantir/gradle/gitversion/Git.java @@ -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; @@ -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) { @@ -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 tagRefs = new HashSet<>(); - for (String tag : LINE_SPLITTER.splitToList(runGitCmd("show-ref", "--tags", "-d"))) { - List 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 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; diff --git a/src/main/java/com/palantir/gradle/gitversion/GitUtils.java b/src/main/java/com/palantir/gradle/gitversion/GitUtils.java deleted file mode 100644 index a484514b..00000000 --- a/src/main/java/com/palantir/gradle/gitversion/GitUtils.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * (c) Copyright 2019 Palantir Technologies Inc. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.palantir.gradle.gitversion; - -final class GitUtils { - - static final int SHA_ABBR_LENGTH = 7; - - private GitUtils() {} - - static String abbrevHash(String hash) { - return hash.substring(0, SHA_ABBR_LENGTH); - } -} diff --git a/src/main/java/com/palantir/gradle/gitversion/VersionDetailsImpl.java b/src/main/java/com/palantir/gradle/gitversion/VersionDetailsImpl.java index 37aa0a91..7c14fe05 100644 --- a/src/main/java/com/palantir/gradle/gitversion/VersionDetailsImpl.java +++ b/src/main/java/com/palantir/gradle/gitversion/VersionDetailsImpl.java @@ -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();