summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMaximilian Bosch <maximilian@mbosch.me>2020-07-14 20:59:24 +0200
committerMaximilian Bosch <maximilian@mbosch.me>2020-12-22 10:46:00 +0100
commit629af83b2d86d77305dc994b83f176a377106c3e (patch)
tree7fce778b26bbbac9a2c53c557511fa941b71e73f
parent9fab14adbc3810d5cc1f88672fde1eee4358405c (diff)
Provide a more meaningful error-message for `builtins.fetchGit` if a revision can't be checked out
A common pitfall when using e.g. `builtins.fetchGit` is the `fatal: not a tree object`-error when trying to fetch a revision of a git-repository that isn't on the `master` branch and no `ref` is specified. In order to make clear what's the problem, I added a simple check whether the revision in question exists and if it doesn't a more meaningful error-message is displayed: ``` nix-repl> builtins.fetchGit { url = "https://github.com/owner/myrepo"; rev = "<commit not on master>"; } moderror: --- Error -------------------------------------------------------------------- nix Cannot find Git revision 'bf1cc5c648e6aed7360448a3745bb2fe4fbbf0e9' in ref 'master' of repository 'https://gitlab.com/Ma27/nvim.nix'! Please make sure that the rev exists on the ref you've specified or add allRefs = true; to fetchGit. ``` Closes #2431
-rw-r--r--src/libfetchers/git.cc22
1 files changed, 22 insertions, 0 deletions
diff --git a/src/libfetchers/git.cc b/src/libfetchers/git.cc
index e7712c5fd..1f298c2d6 100644
--- a/src/libfetchers/git.cc
+++ b/src/libfetchers/git.cc
@@ -392,6 +392,28 @@ struct GitInputScheme : InputScheme
AutoDelete delTmpDir(tmpDir, true);
PathFilter filter = defaultPathFilter;
+ RunOptions checkCommitOpts(
+ "git",
+ { "-C", repoDir, "cat-file", "commit", input.getRev()->gitRev() }
+ );
+ checkCommitOpts.searchPath = true;
+ checkCommitOpts.mergeStderrToStdout = true;
+
+ auto result = runProgram(checkCommitOpts);
+ if (WEXITSTATUS(result.first) == 128
+ && result.second.find("bad file") != std::string::npos
+ ) {
+ throw Error(
+ "Cannot find Git revision '%s' in ref '%s' of repository '%s'! "
+ "Please make sure that the " ANSI_BOLD "rev" ANSI_NORMAL " exists on the "
+ ANSI_BOLD "ref" ANSI_NORMAL " you've specified or add " ANSI_BOLD
+ "allRefs = true;" ANSI_NORMAL " to " ANSI_BOLD "fetchGit" ANSI_NORMAL ".",
+ input.getRev()->gitRev(),
+ *input.getRef(),
+ actualUrl
+ );
+ }
+
if (submodules) {
Path tmpGitDir = createTempDir();
AutoDelete delTmpGitDir(tmpGitDir, true);