Skip to content

Commit 00315c6

Browse files
authored
Fix sparse-checkout set crashes (#607)
2 parents 6d120bf + c9f342c commit 00315c6

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

sparse-index.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ static int add_path_to_index(const struct object_id *oid,
242242
size_t len = base->len;
243243

244244
if (S_ISDIR(mode)) {
245-
int dtype;
245+
int dtype = DT_DIR;
246246
size_t baselen = base->len;
247247
if (!ctx->pl)
248248
return READ_TREE_RECURSIVE;
@@ -360,7 +360,7 @@ void expand_index(struct index_state *istate, struct pattern_list *pl)
360360
struct cache_entry *ce = istate->cache[i];
361361
struct tree *tree;
362362
struct pathspec ps;
363-
int dtype;
363+
int dtype = DT_UNKNOWN;
364364

365365
if (!S_ISSPARSEDIR(ce->ce_mode)) {
366366
set_index_entry(full, full->cache_nr++, ce);
@@ -371,7 +371,7 @@ void expand_index(struct index_state *istate, struct pattern_list *pl)
371371
if (pl &&
372372
path_matches_pattern_list(ce->name, ce->ce_namelen,
373373
NULL, &dtype,
374-
pl, istate) == NOT_MATCHED) {
374+
pl, full) == NOT_MATCHED) {
375375
set_index_entry(full, full->cache_nr++, ce);
376376
continue;
377377
}
@@ -399,6 +399,7 @@ void expand_index(struct index_state *istate, struct pattern_list *pl)
399399
}
400400

401401
/* Copy back into original index. */
402+
istate->name_hash_initialized = full->name_hash_initialized;
402403
memcpy(&istate->name_hash, &full->name_hash, sizeof(full->name_hash));
403404
memcpy(&istate->dir_hash, &full->dir_hash, sizeof(full->dir_hash));
404405
istate->sparse_index = pl ? INDEX_PARTIALLY_SPARSE : INDEX_EXPANDED;

t/t1092-sparse-checkout-compatibility.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,22 @@ test_expect_success 'root directory cannot be sparse' '
311311
test_cmp expect actual
312312
'
313313

314+
test_expect_success 'sparse-checkout with untracked files and dirs' '
315+
init_repos &&
316+
317+
# Empty directories outside sparse cone are deleted
318+
run_on_sparse mkdir -p deep/empty &&
319+
test_sparse_match git sparse-checkout set folder1 &&
320+
test_must_be_empty sparse-checkout-err &&
321+
run_on_sparse test_path_is_missing deep &&
322+
323+
# Untracked files outside sparse cone are not deleted
324+
run_on_sparse touch folder1/another &&
325+
test_sparse_match git sparse-checkout set folder2 &&
326+
grep "directory ${SQ}folder1/${SQ} contains untracked files" sparse-checkout-err &&
327+
run_on_sparse test_path_exists folder1/another
328+
'
329+
314330
test_expect_success 'status with options' '
315331
init_repos &&
316332
test_sparse_match ls &&

0 commit comments

Comments
 (0)