@@ -63,6 +63,8 @@ import java.time.ZoneOffset
63
63
import java.time.ZonedDateTime
64
64
import java.util.function.Supplier
65
65
import java.util.regex.Matcher
66
+ import java.util.regex.Pattern
67
+ import java.util.stream.Stream
66
68
67
69
/**
68
70
* Encapsulates build configuration for elasticsearch projects.
@@ -1118,7 +1120,26 @@ class BuildPlugin implements Plugin<Project> {
1118
1120
}
1119
1121
final String ref = readFirstLine(head);
1120
1122
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
+ }
1122
1143
} else {
1123
1144
// we are in detached HEAD state
1124
1145
revision = ref;
@@ -1131,17 +1152,22 @@ class BuildPlugin implements Plugin<Project> {
1131
1152
}
1132
1153
1133
1154
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
+ }
1145
1171
}
1146
1172
1147
1173
/* * Configures the test task */
0 commit comments