elastic/elasticsearch · error · GradleException
unable to read the git revision
Error message
unable to read the git revision
What it means
The outer catch in GitInfo.gitInfo wraps every IOException thrown while walking .git: reading HEAD, following a worktree gitdir pointer, resolving commondir, or reading the config for the origin URL. The comment explicitly notes the authors chose not to be lenient yet, so any read failure aborts the build-info lookup.
Source
Thrown at build-conventions/src/main/java/org/elasticsearch/gradle/internal/conventions/info/GitInfo.java:134
}
} else {
File refsDir = gitDir.resolve("refs").toFile();
if (refsDir.exists()) {
String foundRefs = Arrays.stream(refsDir.listFiles()).map(f -> f.getName()).collect(Collectors.joining("\n"));
Logging.getLogger(GitInfo.class).error("Found git refs\n" + foundRefs);
} else {
Logging.getLogger(GitInfo.class).error("No git refs dir found");
}
throw new GradleException("Can't find revision for refName " + refName);
}
} else {
// we are in detached HEAD state
revision = ref;
}
return new GitInfo(revision, findOriginUrl(gitDir.resolve("config")));
} catch (final IOException e) {
// for now, do not be lenient until we have better understanding of real-world scenarios where this happens
throw new GradleException("unable to read the git revision", e);
}
}
private static String findOriginUrl(final Path configFile) throws IOException {
Map<String, String> props = new HashMap<>();
try (Stream<String> stream = Files.lines(configFile, StandardCharsets.UTF_8)) {
Iterator<String> lines = stream.iterator();
boolean foundOrigin = false;
while (lines.hasNext()) {
String line = lines.next().trim();
if (line.startsWith(";") || line.startsWith("#")) {
// ignore comments
continue;
}
if (foundOrigin) {
if (line.startsWith("[")) {View on GitHub (pinned to db6a809a66)
Solutions
- Read the caused-by IOException for the exact path that failed.
- Re-clone or `git worktree prune` to remove dangling worktree references.
- Verify the user running the build has read access to the entire .git tree.
- If building from a source tarball, ensure it preserved the .git directory or accept that git info will be unknown.
Example fix
// before: dangling worktree gitdir pointer causes IOException // prune and recreate git worktree prune && git worktree add ../wt main
Defensive patterns
Strategy: try-catch
Validate before calling
// Verify the minimal .git paths are readable before building
Path dotGit = rootDir.toPath().resolve(".git");
if (!Files.exists(dotGit)) { /* no git info available */ }
else if (!Files.isReadable(dotGit.resolve("HEAD"))) {
throw new GradleException(".git/HEAD is not readable; cannot compute git revision");
} Try / catch
try {
GitInfo info = GitInfo.gitInfo(rootDir);
} catch (GradleException e) {
if (e.getMessage().equals("unable to read the git revision")) {
// Inspect e.getCause() for the failing path; fall back to a placeholder if your build tolerates it
getLogger().warn("Skipping git info: {}", e.getCause().getMessage());
} else { throw e; }
} Prevention
- Ensure the build user has read access across the whole .git tree.
- Prune dangling worktrees with `git worktree prune` before building.
- Avoid source tarballs that drop .git when the build needs git metadata.
When it happens
Trigger: Any IOException inside the try of gitInfo(File) that is not already converted to a GradleException — e.g. readFirstLine on HEAD fails, the commondir file is missing, Files.lines on packed-refs throws, or findOriginUrl cannot read config.
Common situations: A truncated or permission-denied .git file; a worktree whose gitdir pointer dangles (the linked directory was deleted); an I/O error reading the config file; disk corruption in .git; a submodule pointer whose modules dir was removed.
Related errors
- Can't find revision for refName ${refName}
- Cannot load VersionPropertiesBuildService
- Cannot resolve minimum compiler version via VersionPropertie
- Cannot read ${f} to check for duplicate license headers
- Cannot generate license header report for ${path}
AI-assisted analysis of elastic/elasticsearch@db6a809a66 (2026-08-12).
Data as JSON: /api/errors/26d2af66936ab8fe.
Report an issue: GitHub.