Skip to content

Commit fb64d5b

Browse files
committed
flakes: for detected git repository now assume shallow clones by default
Shallow clones are faster to access because we don't have to compute the revCount, which in sparse checkouts might not even exists. This is especially useful in combination with lazy trees in mind on large repository such as nixpkgs.
1 parent 19adb40 commit fb64d5b

File tree

2 files changed

+27
-17
lines changed

2 files changed

+27
-17
lines changed

src/libflake/flakeref.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,10 @@ std::pair<FlakeRef, std::string> parsePathFlakeRefWithFragment(
168168
parsedURL.query.insert_or_assign("dir", subdir);
169169
}
170170

171-
if (pathExists(flakeRoot + "/.git/shallow"))
172-
parsedURL.query.insert_or_assign("shallow", "1");
171+
if (!parsedURL.query.count("shallow")) {
172+
// We assume shallow by default, so we don't need to compute the revCount, which is an expensive operation.
173+
parsedURL.query.insert_or_assign("shallow", "1");
174+
}
173175

174176
return fromParsedURL(fetchSettings, std::move(parsedURL), isFlake);
175177
}

tests/functional/fetchGitShallow.sh

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ git -C "$TEST_ROOT/shallow-parent" config user.email "foobar@example.com"
1414
git -C "$TEST_ROOT/shallow-parent" config user.name "Foobar"
1515

1616
# Add several commits to have history
17-
echo "first" > "$TEST_ROOT/shallow-parent/file.txt"
18-
git -C "$TEST_ROOT/shallow-parent" add file.txt
17+
echo "{ outputs = _: {}; }" > "$TEST_ROOT/shallow-parent/flake.nix"
18+
echo "" > "$TEST_ROOT/shallow-parent/file.txt"
19+
git -C "$TEST_ROOT/shallow-parent" add file.txt flake.nix
1920
git -C "$TEST_ROOT/shallow-parent" commit -m "First commit"
2021

2122
echo "second" > "$TEST_ROOT/shallow-parent/file.txt"
@@ -52,16 +53,23 @@ expectStderr 1 nix eval --expr 'builtins.fetchTree { type = "git"; url = "file:/
5253
git -C "$TEST_ROOT/shallow-clone" worktree add "$TEST_ROOT/shallow-worktree"
5354

5455
# Prior to the fix, this would error out because of the shallow clone's
55-
# inability to find parent commits. Now it should return a partial revCount.
56-
revCount=$(nix eval --impure --expr "(builtins.fetchGit { url = \"file://$TEST_ROOT/shallow-worktree\"; }).revCount")
57-
58-
# Verify we got a valid revCount (will be 1 since it's a depth=1 clone)
59-
[[ "${revCount}" -eq 1 ]]
60-
61-
# Also verify that we can get the revCount with fetchTree
62-
revCount=$(nix eval --impure --expr "(builtins.fetchTree { type = \"git\"; url = \"file://$TEST_ROOT/shallow-worktree\"; }).revCount")
63-
[[ "${revCount}" -eq 1 ]]
64-
65-
# Verify that we can get other attributes too
66-
rev=$(nix eval --impure --raw --expr "(builtins.fetchGit { url = \"file://$TEST_ROOT/shallow-worktree\"; }).rev")
67-
[[ "${rev}" != "0000000000000000000000000000000000000000" ]]
56+
# inability to find parent commits. Now it should return an error.
57+
if nix eval --impure --expr "(builtins.fetchGit { url = \"file://$TEST_ROOT/shallow-worktree\"; }).revCount" 2>/dev/null; then
58+
echo "fetchGit unexpectedly succeeded on shallow clone" >&2
59+
exit 1
60+
fi
61+
62+
# Also verify that fetchTree fails similarly
63+
if nix eval --impure --expr "(builtins.fetchTree { type = \"git\"; url = \"file://$TEST_ROOT/shallow-worktree\"; }).revCount" 2>/dev/null; then
64+
echo "fetchTree unexpectedly succeeded on shallow clone" >&2
65+
exit 1
66+
fi
67+
68+
# Verify that we can shallow fetch the worktree
69+
git -C "$TEST_ROOT/shallow-worktree" rev-list --count HEAD >/dev/null
70+
nix eval --impure --raw --expr "(builtins.fetchGit { url = \"file://$TEST_ROOT/shallow-worktree\"; shallow = true; }).rev"
71+
72+
# Normal flake operation work because they use shallow by default
73+
pushd "$TEST_ROOT/shallow-worktree"
74+
nix flake metadata
75+
popd

0 commit comments

Comments
 (0)