mirror of
https://github.com/NixOS/nixpkgs.git
synced 2025-06-12 12:45:27 +03:00
Merge pull request #199812 from Artturin/removeusagesoftostringonpath1
lib/sources: remove 2 usages of toString on a path which will be read using fileContents
This commit is contained in:
commit
e3bd5d17b2
2 changed files with 22 additions and 11 deletions
|
@ -166,17 +166,28 @@ let
|
||||||
in type == "directory" || lib.any (ext: lib.hasSuffix ext base) exts;
|
in type == "directory" || lib.any (ext: lib.hasSuffix ext base) exts;
|
||||||
in cleanSourceWith { inherit filter src; };
|
in cleanSourceWith { inherit filter src; };
|
||||||
|
|
||||||
pathIsGitRepo = path: (tryEval (commitIdFromGitRepo path)).success;
|
pathIsGitRepo = path: (commitIdFromGitRepoOrError path)?value;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Get the commit id of a git repo.
|
Get the commit id of a git repo.
|
||||||
|
|
||||||
Example: commitIdFromGitRepo <nixpkgs/.git>
|
Example: commitIdFromGitRepo <nixpkgs/.git>
|
||||||
*/
|
*/
|
||||||
commitIdFromGitRepo =
|
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 readCommitFromFile = file: path:
|
||||||
let fileName = toString path + "/" + file;
|
let fileName = path + "/${file}";
|
||||||
packedRefsName = toString path + "/packed-refs";
|
packedRefsName = path + "/packed-refs";
|
||||||
absolutePath = base: path:
|
absolutePath = base: path:
|
||||||
if lib.hasPrefix "/" path
|
if lib.hasPrefix "/" path
|
||||||
then path
|
then path
|
||||||
|
@ -186,7 +197,7 @@ let
|
||||||
then
|
then
|
||||||
let m = match "^gitdir: (.*)$" (lib.fileContents path);
|
let m = match "^gitdir: (.*)$" (lib.fileContents path);
|
||||||
in if m == null
|
in if m == null
|
||||||
then throw ("File contains no gitdir reference: " + path)
|
then { error = "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"
|
||||||
|
@ -204,7 +215,7 @@ let
|
||||||
let fileContent = lib.fileContents fileName;
|
let fileContent = lib.fileContents fileName;
|
||||||
matchRef = match "^ref: (.*)$" fileContent;
|
matchRef = match "^ref: (.*)$" fileContent;
|
||||||
in if matchRef == null
|
in if matchRef == null
|
||||||
then fileContent
|
then { value = fileContent; }
|
||||||
else readCommitFromFile (lib.head matchRef) path
|
else readCommitFromFile (lib.head matchRef) path
|
||||||
|
|
||||||
else if pathIsRegularFile packedRefsName
|
else if pathIsRegularFile packedRefsName
|
||||||
|
@ -218,10 +229,10 @@ let
|
||||||
# https://github.com/NixOS/nix/issues/2147#issuecomment-659868795
|
# https://github.com/NixOS/nix/issues/2147#issuecomment-659868795
|
||||||
refs = filter isRef (split "\n" fileContent);
|
refs = filter isRef (split "\n" fileContent);
|
||||||
in if refs == []
|
in if refs == []
|
||||||
then throw ("Could not find " + file + " in " + packedRefsName)
|
then { error = "Could not find " + file + " in " + packedRefsName; }
|
||||||
else lib.head (matchRef (lib.head refs))
|
else { value = lib.head (matchRef (lib.head refs)); }
|
||||||
|
|
||||||
else throw ("Not a .git directory: " + path);
|
else { error = "Not a .git directory: " + toString path; };
|
||||||
in readCommitFromFile "HEAD";
|
in readCommitFromFile "HEAD";
|
||||||
|
|
||||||
pathHasContext = builtins.hasContext or (lib.hasPrefix storeDir);
|
pathHasContext = builtins.hasContext or (lib.hasPrefix storeDir);
|
||||||
|
|
|
@ -213,8 +213,8 @@ rec {
|
||||||
# Default value to return if revision can not be determined
|
# Default value to return if revision can not be determined
|
||||||
default:
|
default:
|
||||||
let
|
let
|
||||||
revisionFile = "${toString ./..}/.git-revision";
|
revisionFile = ./.. + "/.git-revision";
|
||||||
gitRepo = "${toString ./..}/.git";
|
gitRepo = ./.. + "/.git";
|
||||||
in if lib.pathIsGitRepo gitRepo
|
in if lib.pathIsGitRepo gitRepo
|
||||||
then lib.commitIdFromGitRepo gitRepo
|
then lib.commitIdFromGitRepo gitRepo
|
||||||
else if lib.pathExists revisionFile then lib.fileContents revisionFile
|
else if lib.pathExists revisionFile then lib.fileContents revisionFile
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue