Allow use on repos without commits

When starting a new repository, it does not have any branches
in `.git/refs/heads` and `.git/refs/HEAD` contains a broken
`ref: refs/heads/master` reference.

This means that cloning a repo like we do for cleaning will fail
with very unhelpful message for freshly created repo:

    fatal: bad revision 'HEAD'

This is quite confusing when you create a new repo by copying
Nix files from another, working one, make some trivial changes,
stage them and then try to test a shell.
This commit is contained in:
Jan Tojnar 2020-11-13 18:36:55 +01:00
parent 19576c2aea
commit d3041dbd47
No known key found for this signature in database
GPG key ID: 7FAB2A15F7A607A4

View file

@ -77,12 +77,14 @@ let
# Try to clean the source tree by using fetchGit, if this source # Try to clean the source tree by using fetchGit, if this source
# tree is a valid git repository. # tree is a valid git repository.
tryFetchGit = src: tryFetchGit = src:
if isGit && !isShallow if isGit && !isShallow && hasBranch
then builtins.fetchGit src then builtins.fetchGit src
else { outPath = src; }; else { outPath = src; };
# NB git worktrees have a file for .git, so we don't check the type of .git # NB git worktrees have a file for .git, so we don't check the type of .git
isGit = builtins.pathExists (src + "/.git"); isGit = builtins.pathExists (src + "/.git");
isShallow = builtins.pathExists (src + "/.git/shallow"); isShallow = builtins.pathExists (src + "/.git/shallow");
# Repos without commits do not have any heads.
hasBranch = builtins.pathExists (src + "/.git/refs/heads") && builtins.readDir (src + "/.git/refs/heads") != { };
in in
(if src ? outPath then src else tryFetchGit src) (if src ? outPath then src else tryFetchGit src)