lib: fix commitIdFromGitRepo (#117752)

When in the presence of worktrees, it happens that /commondir has a
trailing slash.

In these circumstances, it can lead to `lib.pathType` being passed paths
like `/foo/bar/.git/`, which in turn lead to
`error: attribute '.git' missing`.

With this change, we now make sure send properly-formatted paths to all
other functions.

This, in particular, fixes running NixOS tests on worktrees created by
libgit2 on my machine. (Worktrees created by git itself appear to not
hit the issue.)
This commit is contained in:
Léo Gaspard 2021-03-27 21:16:31 +01:00 committed by GitHub
parent cb231de72b
commit 144a997c8e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -138,12 +138,13 @@ rec {
in if m == null in if m == null
then throw ("File contains no gitdir reference: " + path) then throw ("File contains no gitdir reference: " + path)
else else
let gitDir = absolutePath (dirOf path) (lib.head m); let gitDir = absolutePath (dirOf path) (lib.head m);
commonDir' = if pathIsRegularFile "${gitDir}/commondir" commonDir'' = if pathIsRegularFile "${gitDir}/commondir"
then lib.fileContents "${gitDir}/commondir" then lib.fileContents "${gitDir}/commondir"
else gitDir; else gitDir;
commonDir = absolutePath gitDir commonDir'; commonDir' = lib.removeSuffix "/" commonDir'';
refFile = lib.removePrefix "${commonDir}/" "${gitDir}/${file}"; commonDir = absolutePath gitDir commonDir';
refFile = lib.removePrefix "${commonDir}/" "${gitDir}/${file}";
in readCommitFromFile refFile commonDir in readCommitFromFile refFile commonDir
else if pathIsRegularFile fileName else if pathIsRegularFile fileName