fetchGit: Support Git trees without any commits
Fixes $ nix build fatal: bad revision 'HEAD' error: program 'git' failed with exit code 128 on a new flake. It is now detected as a dirty tree with revCount = 0.
This commit is contained in:
parent
ce2c755d2a
commit
a15f9b37eb
|
@ -33,13 +33,19 @@ GitInfo exportGit(ref<Store> store, std::string uri,
|
||||||
// or revision is given, then allow the use of an unclean working
|
// or revision is given, then allow the use of an unclean working
|
||||||
// tree.
|
// tree.
|
||||||
if (!ref && !rev && isLocal) {
|
if (!ref && !rev && isLocal) {
|
||||||
bool clean = true;
|
bool clean = false;
|
||||||
|
|
||||||
|
/* Check whether this repo has any commits. There are
|
||||||
|
probably better ways to do this. */
|
||||||
|
bool haveCommits = !readDirectory(uri + "/.git/refs/heads").empty();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
runProgram("git", true, { "-C", uri, "diff-index", "--quiet", "HEAD", "--" });
|
if (haveCommits) {
|
||||||
|
runProgram("git", true, { "-C", uri, "diff-index", "--quiet", "HEAD", "--" });
|
||||||
|
clean = true;
|
||||||
|
}
|
||||||
} catch (ExecError & e) {
|
} catch (ExecError & e) {
|
||||||
if (!WIFEXITED(e.status) || WEXITSTATUS(e.status) != 1) throw;
|
if (!WIFEXITED(e.status) || WEXITSTATUS(e.status) != 1) throw;
|
||||||
clean = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!clean) {
|
if (!clean) {
|
||||||
|
@ -75,10 +81,10 @@ GitInfo exportGit(ref<Store> store, std::string uri,
|
||||||
};
|
};
|
||||||
|
|
||||||
gitInfo.storePath = store->addToStore("source", uri, true, htSHA256, filter);
|
gitInfo.storePath = store->addToStore("source", uri, true, htSHA256, filter);
|
||||||
gitInfo.revCount = std::stoull(runProgram("git", true, { "-C", uri, "rev-list", "--count", "HEAD" }));
|
gitInfo.revCount = haveCommits ? std::stoull(runProgram("git", true, { "-C", uri, "rev-list", "--count", "HEAD" })) : 0;
|
||||||
// FIXME: maybe we should use the timestamp of the last
|
// FIXME: maybe we should use the timestamp of the last
|
||||||
// modified dirty file?
|
// modified dirty file?
|
||||||
gitInfo.lastModified = std::stoull(runProgram("git", true, { "-C", uri, "show", "-s", "--format=%ct", "HEAD" }));
|
gitInfo.lastModified = haveCommits ? std::stoull(runProgram("git", true, { "-C", uri, "show", "-s", "--format=%ct", "HEAD" })) : 0;
|
||||||
|
|
||||||
return gitInfo;
|
return gitInfo;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue