Skip to content

Commit 9e1afb1

Browse files
pcloudsgitster
authored andcommitted
sparse checkout: inhibit empty worktree
The way sparse checkout works, users may empty their worktree completely, because of non-matching sparse-checkout spec, or empty spec. I believe this is not desired. This patch makes Git refuse to produce such worktree. Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d6b38f6 commit 9e1afb1

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

t/t1009-read-tree-sparse-checkout.sh

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,16 @@ test_expect_success 'read-tree --no-sparse-checkout with empty .git/info/sparse-
5555
test -f sub/added
5656
'
5757

58-
cat >expected.swt <<EOF
59-
S init.t
60-
S sub/added
61-
EOF
6258
test_expect_success 'read-tree with empty .git/info/sparse-checkout' '
6359
git config core.sparsecheckout true &&
6460
echo > .git/info/sparse-checkout &&
65-
git read-tree -m -u HEAD &&
61+
test_must_fail git read-tree -m -u HEAD &&
6662
git ls-files --stage > result &&
6763
test_cmp expected result &&
6864
git ls-files -t > result &&
6965
test_cmp expected.swt result &&
70-
test ! -f init.t &&
71-
test ! -f sub/added
66+
test -f init.t &&
67+
test -f sub/added
7268
'
7369

7470
cat >expected.swt <<EOF

unpack-trees.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,7 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options
498498
}
499499

500500
if (!o->skip_sparse_checkout) {
501+
int empty_worktree = 1;
501502
for (i = 0;i < o->result.cache_nr;i++) {
502503
struct cache_entry *ce = o->result.cache[i];
503504

@@ -512,8 +513,14 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options
512513
*/
513514
if (ce_skip_worktree(ce))
514515
ce->ce_flags &= ~(CE_UPDATE | CE_REMOVE);
516+
else
517+
empty_worktree = 0;
515518

516519
}
520+
if (o->result.cache_nr && empty_worktree) {
521+
ret = unpack_failed(o, "Sparse checkout leaves no entry on working directory");
522+
goto done;
523+
}
517524
}
518525

519526
o->src_index = NULL;

0 commit comments

Comments
 (0)