Skip to content

Commit 387740a

Browse files
GlobalBuildInfo plugin should search packed references for commit IDs (#47464) (#47942)
* 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.
1 parent 75f6eb7 commit 387740a

File tree

1 file changed

+38
-12
lines changed

1 file changed

+38
-12
lines changed

buildSrc/src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ import java.time.ZoneOffset
6363
import java.time.ZonedDateTime
6464
import java.util.function.Supplier
6565
import java.util.regex.Matcher
66+
import java.util.regex.Pattern
67+
import java.util.stream.Stream
6668

6769
/**
6870
* Encapsulates build configuration for elasticsearch projects.
@@ -1118,7 +1120,26 @@ class BuildPlugin implements Plugin<Project> {
11181120
}
11191121
final String ref = readFirstLine(head);
11201122
if (ref.startsWith("ref:")) {
1121-
revision = readFirstLine(gitDir.resolve(ref.substring("ref:".length()).trim()));
1123+
String refName = ref.substring("ref:".length()).trim()
1124+
Path refFile = gitDir.resolve(refName)
1125+
if (Files.exists(refFile)) {
1126+
revision = readFirstLine(refFile)
1127+
} else if (Files.exists(dotGit.resolve("packed-refs"))) {
1128+
// Check packed references for commit ID
1129+
Pattern p = Pattern.compile("^([a-f1-9]{40}) " + refName + "\$")
1130+
Stream<String> lines = Files.lines(dotGit.resolve("packed-refs"));
1131+
try {
1132+
revision = lines.map( { s -> p.matcher(s) })
1133+
.filter( { m -> m.matches() })
1134+
.map({ m -> m.group(1) })
1135+
.findFirst()
1136+
.orElseThrow({ -> new IOException("Packed reference not found for refName " + refName) });
1137+
} finally {
1138+
lines.close()
1139+
}
1140+
} else {
1141+
throw new GradleException("Can't find revision for refName " + refName);
1142+
}
11221143
} else {
11231144
// we are in detached HEAD state
11241145
revision = ref;
@@ -1131,17 +1152,22 @@ class BuildPlugin implements Plugin<Project> {
11311152
}
11321153

11331154
private static String readFirstLine(final Path path) throws IOException {
1134-
return Files.lines(path, StandardCharsets.UTF_8)
1135-
.findFirst()
1136-
.orElseThrow(
1137-
new Supplier<IOException>() {
1138-
1139-
@Override
1140-
IOException get() {
1141-
return new IOException("file [" + path + "] is empty");
1142-
}
1143-
1144-
});
1155+
Stream lines = Files.lines(path, StandardCharsets.UTF_8)
1156+
try {
1157+
return Files.lines(path, StandardCharsets.UTF_8)
1158+
.findFirst()
1159+
.orElseThrow(
1160+
new Supplier<IOException>() {
1161+
1162+
@Override
1163+
IOException get() {
1164+
return new IOException("file [" + path + "] is empty");
1165+
}
1166+
1167+
});
1168+
} finally {
1169+
lines.close()
1170+
}
11451171
}
11461172

11471173
/** Configures the test task */

0 commit comments

Comments
 (0)