Skip to content

Commit

Permalink
Provide a more meaningful error-message for builtins.fetchGit if a …
Browse files Browse the repository at this point in the history
…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 in '<commit not on master>' of repo 'https://github.com/owner/myrepo' on ref 'master'!
```

Closes NixOS#2431
  • Loading branch information
Ma27 committed Jul 15, 2020
1 parent 8efa23b commit c0e5222
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/libfetchers/git.cc
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,30 @@ struct GitInput : Input
AutoDelete delTmpDir(tmpDir, true);
PathFilter filter = defaultPathFilter;

RunOptions checkCommitOpts(
"git",
{ "-C", repoDir, "cat-file", "commit", input->rev->gitRev() }
);
checkCommitOpts.searchPath = true;
checkCommitOpts.mergeStderrToStdout = true;

auto result = runProgram(checkCommitOpts);
if (WEXITSTATUS(result.first) == 128
&& result.second.find("bad file") != std::string::npos
) {
std::string msg = fmt("Cannot find git-revision in '" ANSI_YELLOW "%s" ANSI_NORMAL
"' of repo '" ANSI_YELLOW "%s" ANSI_NORMAL "' on ref '" ANSI_YELLOW
"%s" ANSI_NORMAL "!\n\n"
"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->rev->gitRev(),
actualUrl,
*input->ref);

throw Error(msg);
}

if (submodules) {
Path tmpGitDir = createTempDir();
AutoDelete delTmpGitDir(tmpGitDir, true);
Expand Down

0 comments on commit c0e5222

Please sign in to comment.