From fe8767789a959d77647f433bd4bde5dfdcd672bf Mon Sep 17 00:00:00 2001 From: William Brafford Date: Fri, 11 Oct 2019 14:47:34 -0400 Subject: [PATCH] GlobalBuildInfo plugin should search packed references for commit IDs (#47464) (#47936) * GlobalBuildInfo plugin searches packed references In recent versions of Git, references may be packed in a packed-refs file. If this happens, Gradle will need to look in that file to find build information. --- .../gradle/info/GlobalBuildInfoPlugin.java | 31 ++++++++++++++++--- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/buildSrc/src/main/java/org/elasticsearch/gradle/info/GlobalBuildInfoPlugin.java b/buildSrc/src/main/java/org/elasticsearch/gradle/info/GlobalBuildInfoPlugin.java index 46f2db818a4..8577305a7b2 100644 --- a/buildSrc/src/main/java/org/elasticsearch/gradle/info/GlobalBuildInfoPlugin.java +++ b/buildSrc/src/main/java/org/elasticsearch/gradle/info/GlobalBuildInfoPlugin.java @@ -28,7 +28,10 @@ import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Random; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import java.util.stream.Collectors; +import java.util.stream.Stream; public class GlobalBuildInfoPlugin implements Plugin { private static final String GLOBAL_INFO_EXTENSION_NAME = "globalInfo"; @@ -261,7 +264,23 @@ public class GlobalBuildInfoPlugin implements Plugin { } final String ref = readFirstLine(head); if (ref.startsWith("ref:")) { - revision = readFirstLine(gitDir.resolve(ref.substring("ref:".length()).trim())); + String refName = ref.substring("ref:".length()).trim(); + Path refFile = gitDir.resolve(refName); + if (Files.exists(refFile)) { + revision = readFirstLine(refFile); + } else if (Files.exists(dotGit.resolve("packed-refs"))) { + // Check packed references for commit ID + Pattern p = Pattern.compile("^([a-f0-9]{40}) " + refName + "$"); + try (Stream lines = Files.lines(dotGit.resolve("packed-refs"))) { + revision = lines.map(p::matcher) + .filter(Matcher::matches) + .map(m -> m.group(1)) + .findFirst() + .orElseThrow(() -> new IOException("Packed reference not found for refName " + refName)); + } + } else { + throw new GradleException("Can't find revision for refName " + refName); + } } else { // we are in detached HEAD state revision = ref; @@ -274,8 +293,12 @@ public class GlobalBuildInfoPlugin implements Plugin { } private static String readFirstLine(final Path path) throws IOException { - return Files.lines(path, StandardCharsets.UTF_8) - .findFirst() - .orElseThrow(() -> new IOException("file [" + path + "] is empty")); + String firstLine; + try (Stream lines = Files.lines(path, StandardCharsets.UTF_8)) { + firstLine = lines + .findFirst() + .orElseThrow(() -> new IOException("file [" + path + "] is empty")); + } + return firstLine; } }