about summary refs log tree commit diff
path: root/lib
diff options
context:
space:
mode:
authorArtturi <Artturin@artturin.com>2022-11-17 15:18:52 +0200
committerGitHub <noreply@github.com>2022-11-17 15:18:52 +0200
commite3bd5d17b2e59cee9df28ab1ed68716ff3e0dfd6 (patch)
treeca4f4d2d5257813fa45744495efb904d6337a765 /lib
parentf15d096a8e89edaf1b0a572e6ed58095e8c593d6 (diff)
parentec8f8f69bd57b9dfd24ed56ef8f3c53c9ee5f453 (diff)
Merge pull request #199812 from Artturin/removeusagesoftostringonpath1
lib/sources: remove 2 usages of toString on a path which will be read using fileContents
Diffstat (limited to 'lib')
-rw-r--r--lib/sources.nix29
-rw-r--r--lib/trivial.nix4
2 files changed, 22 insertions, 11 deletions
diff --git a/lib/sources.nix b/lib/sources.nix
index cec395c9bb180..9db3d23295421 100644
--- a/lib/sources.nix
+++ b/lib/sources.nix
@@ -166,17 +166,28 @@ let
       in type == "directory" || lib.any (ext: lib.hasSuffix ext base) exts;
     in cleanSourceWith { inherit filter src; };
 
-  pathIsGitRepo = path: (tryEval (commitIdFromGitRepo path)).success;
+  pathIsGitRepo = path: (commitIdFromGitRepoOrError path)?value;
 
   /*
     Get the commit id of a git repo.
 
     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 fileName       = toString path + "/" + file;
-            packedRefsName = toString path + "/packed-refs";
+        let fileName       = path + "/${file}";
+            packedRefsName = path + "/packed-refs";
             absolutePath   = base: path:
               if lib.hasPrefix "/" path
               then path
@@ -186,7 +197,7 @@ let
            then
              let m   = match "^gitdir: (.*)$" (lib.fileContents path);
              in if m == null
-                then throw ("File contains no gitdir reference: " + path)
+                then { error = "File contains no gitdir reference: " + path; }
                 else
                   let gitDir      = absolutePath (dirOf path) (lib.head m);
                       commonDir'' = if pathIsRegularFile "${gitDir}/commondir"
@@ -204,7 +215,7 @@ let
              let fileContent = lib.fileContents fileName;
                  matchRef    = match "^ref: (.*)$" fileContent;
              in if  matchRef == null
-                then fileContent
+                then { value = fileContent; }
                 else readCommitFromFile (lib.head matchRef) path
 
            else if pathIsRegularFile packedRefsName
@@ -218,10 +229,10 @@ let
                  # 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))
+                then { error = "Could not find " + file + " in " + packedRefsName; }
+                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";
 
   pathHasContext = builtins.hasContext or (lib.hasPrefix storeDir);
diff --git a/lib/trivial.nix b/lib/trivial.nix
index 0e02660d6b35a..142ed32c9e5a6 100644
--- a/lib/trivial.nix
+++ b/lib/trivial.nix
@@ -213,8 +213,8 @@ rec {
     # Default value to return if revision can not be determined
     default:
     let
-      revisionFile = "${toString ./..}/.git-revision";
-      gitRepo      = "${toString ./..}/.git";
+      revisionFile = ./.. + "/.git-revision";
+      gitRepo      = ./.. + "/.git";
     in if lib.pathIsGitRepo gitRepo
        then lib.commitIdFromGitRepo gitRepo
        else if lib.pathExists revisionFile then lib.fileContents revisionFile