Merge pull request #199812 from Artturin/removeusagesoftostringonpath1

lib/sources: remove 2 usages of toString on a path which will be read using fileContents

Artturi e3bd5d17 f15d096a

Changed files
+22 -11
lib
+20 -9
lib/sources.nix
···
in type == "directory" || lib.any (ext: lib.hasSuffix ext base) exts;
in cleanSourceWith { inherit filter src; };
-
pathIsGitRepo = path: (tryEval (commitIdFromGitRepo path)).success;
/*
Get the commit id of a git repo.
Example: commitIdFromGitRepo <nixpkgs/.git>
*/
-
commitIdFromGitRepo =
let readCommitFromFile = file: path:
-
let fileName = toString path + "/" + file;
-
packedRefsName = toString path + "/packed-refs";
absolutePath = base: path:
if lib.hasPrefix "/" path
then path
···
then
let m = match "^gitdir: (.*)$" (lib.fileContents path);
in if m == null
-
then throw ("File contains no gitdir reference: " + path)
else
let gitDir = absolutePath (dirOf path) (lib.head m);
commonDir'' = if pathIsRegularFile "${gitDir}/commondir"
···
let fileContent = lib.fileContents fileName;
matchRef = match "^ref: (.*)$" fileContent;
in if matchRef == null
-
then fileContent
else readCommitFromFile (lib.head matchRef) path
else if pathIsRegularFile packedRefsName
···
# https://github.com/NixOS/nix/issues/2147#issuecomment-659868795
refs = filter isRef (split "\n" fileContent);
in if refs == []
-
then throw ("Could not find " + file + " in " + packedRefsName)
-
else lib.head (matchRef (lib.head refs))
-
else throw ("Not a .git directory: " + path);
in readCommitFromFile "HEAD";
pathHasContext = builtins.hasContext or (lib.hasPrefix storeDir);
···
in type == "directory" || lib.any (ext: lib.hasSuffix ext base) exts;
in cleanSourceWith { inherit filter src; };
+
pathIsGitRepo = path: (commitIdFromGitRepoOrError path)?value;
+
+
/*
+
Get the commit id of a git repo.
+
+
Example: commitIdFromGitRepo <nixpkgs/.git>
+
*/
+
commitIdFromGitRepo = path:
+
let commitIdOrError = commitIdFromGitRepoOrError path;
+
in commitIdOrError.value or (throw commitIdOrError.error);
/*
Get the commit id of a git repo.
+
Returns `{ value = commitHash }` or `{ error = "... message ..." }`.
+
Example: commitIdFromGitRepo <nixpkgs/.git>
*/
+
commitIdFromGitRepoOrError =
let readCommitFromFile = file: path:
+
let fileName = path + "/${file}";
+
packedRefsName = path + "/packed-refs";
absolutePath = base: path:
if lib.hasPrefix "/" path
then path
···
then
let m = match "^gitdir: (.*)$" (lib.fileContents path);
in if m == null
+
then { error = "File contains no gitdir reference: " + path; }
else
let gitDir = absolutePath (dirOf path) (lib.head m);
commonDir'' = if pathIsRegularFile "${gitDir}/commondir"
···
let fileContent = lib.fileContents fileName;
matchRef = match "^ref: (.*)$" fileContent;
in if matchRef == null
+
then { value = fileContent; }
else readCommitFromFile (lib.head matchRef) path
else if pathIsRegularFile packedRefsName
···
# https://github.com/NixOS/nix/issues/2147#issuecomment-659868795
refs = filter isRef (split "\n" fileContent);
in if refs == []
+
then { error = "Could not find " + file + " in " + packedRefsName; }
+
else { value = lib.head (matchRef (lib.head refs)); }
+
else { error = "Not a .git directory: " + toString path; };
in readCommitFromFile "HEAD";
pathHasContext = builtins.hasContext or (lib.hasPrefix storeDir);
+2 -2
lib/trivial.nix
···
# Default value to return if revision can not be determined
default:
let
-
revisionFile = "${toString ./..}/.git-revision";
-
gitRepo = "${toString ./..}/.git";
in if lib.pathIsGitRepo gitRepo
then lib.commitIdFromGitRepo gitRepo
else if lib.pathExists revisionFile then lib.fileContents revisionFile
···
# Default value to return if revision can not be determined
default:
let
+
revisionFile = ./.. + "/.git-revision";
+
gitRepo = ./.. + "/.git";
in if lib.pathIsGitRepo gitRepo
then lib.commitIdFromGitRepo gitRepo
else if lib.pathExists revisionFile then lib.fileContents revisionFile