|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# shellcheck source=common.sh |
| 4 | +source common.sh |
| 5 | + |
| 6 | +requireGit |
| 7 | + |
| 8 | +# Create a test repo with multiple commits for all our tests |
| 9 | +git init "$TEST_ROOT/shallow-parent" |
| 10 | +git -C "$TEST_ROOT/shallow-parent" config user.email "[email protected]" |
| 11 | +git -C "$TEST_ROOT/shallow-parent" config user.name "Foobar" |
| 12 | + |
| 13 | +# Add several commits to have history |
| 14 | +echo "{ outputs = _: {}; }" > "$TEST_ROOT/shallow-parent/flake.nix" |
| 15 | +echo "" > "$TEST_ROOT/shallow-parent/file.txt" |
| 16 | +git -C "$TEST_ROOT/shallow-parent" add file.txt flake.nix |
| 17 | +git -C "$TEST_ROOT/shallow-parent" commit -m "First commit" |
| 18 | + |
| 19 | +echo "second" > "$TEST_ROOT/shallow-parent/file.txt" |
| 20 | +git -C "$TEST_ROOT/shallow-parent" commit -m "Second commit" -a |
| 21 | + |
| 22 | +echo "third" > "$TEST_ROOT/shallow-parent/file.txt" |
| 23 | +git -C "$TEST_ROOT/shallow-parent" commit -m "Third commit" -a |
| 24 | + |
| 25 | +# Add a branch for testing ref fetching |
| 26 | +git -C "$TEST_ROOT/shallow-parent" checkout -b dev |
| 27 | +echo "branch content" > "$TEST_ROOT/shallow-parent/branch-file.txt" |
| 28 | +git -C "$TEST_ROOT/shallow-parent" add branch-file.txt |
| 29 | +git -C "$TEST_ROOT/shallow-parent" commit -m "Branch commit" |
| 30 | + |
| 31 | +# Make a shallow clone (depth=1) |
| 32 | +git clone --depth 1 "file://$TEST_ROOT/shallow-parent" "$TEST_ROOT/shallow-clone" |
| 33 | + |
| 34 | +# Test 1: Fetching a shallow repo shouldn't work by default, because we can't |
| 35 | +# return a revCount. |
| 36 | +(! nix eval --impure --raw --expr "(builtins.fetchGit { url = \"$TEST_ROOT/shallow-clone\"; ref = \"dev\"; }).outPath") |
| 37 | + |
| 38 | +# Test 2: But you can request a shallow clone, which won't return a revCount. |
| 39 | +path=$(nix eval --impure --raw --expr "(builtins.fetchTree { type = \"git\"; url = \"file://$TEST_ROOT/shallow-clone\"; ref = \"dev\"; shallow = true; }).outPath") |
| 40 | +# Verify file from dev branch exists |
| 41 | +[[ -f "$path/branch-file.txt" ]] |
| 42 | +# Verify revCount is missing |
| 43 | +[[ $(nix eval --impure --expr "(builtins.fetchTree { type = \"git\"; url = \"file://$TEST_ROOT/shallow-clone\"; ref = \"dev\"; shallow = true; }).revCount or 123") == 123 ]] |
| 44 | + |
| 45 | +# Test 3: Check unlocked input error message |
| 46 | +expectStderr 1 nix eval --expr 'builtins.fetchTree { type = "git"; url = "file:///foo"; }' | grepQuiet "'fetchTree' will not fetch unlocked input" |
| 47 | + |
| 48 | +# Test 4: Regression test for revCount in worktrees derived from shallow clones |
| 49 | +# Add a worktree to the shallow clone |
| 50 | +git -C "$TEST_ROOT/shallow-clone" worktree add "$TEST_ROOT/shallow-worktree" |
| 51 | + |
| 52 | +# Prior to the fix, this would error out because of the shallow clone's |
| 53 | +# inability to find parent commits. Now it should return an error. |
| 54 | +if nix eval --impure --expr "(builtins.fetchGit { url = \"file://$TEST_ROOT/shallow-worktree\"; }).revCount" 2>/dev/null; then |
| 55 | + echo "fetchGit unexpectedly succeeded on shallow clone" >&2 |
| 56 | + exit 1 |
| 57 | +fi |
| 58 | + |
| 59 | +# Also verify that fetchTree fails similarly |
| 60 | +if nix eval --impure --expr "(builtins.fetchTree { type = \"git\"; url = \"file://$TEST_ROOT/shallow-worktree\"; }).revCount" 2>/dev/null; then |
| 61 | + echo "fetchTree unexpectedly succeeded on shallow clone" >&2 |
| 62 | + exit 1 |
| 63 | +fi |
| 64 | + |
| 65 | +# Verify that we can shallow fetch the worktree |
| 66 | +git -C "$TEST_ROOT/shallow-worktree" rev-list --count HEAD >/dev/null |
| 67 | +nix eval --impure --raw --expr "(builtins.fetchGit { url = \"file://$TEST_ROOT/shallow-worktree\"; shallow = true; }).rev" |
0 commit comments