unpack-trees: fix sparse directory recursion check

Ensure 'is_sparse_directory_entry()' receives a valid 'name_entry *' if one exists in the list of tree(s) being unpacked in 'unpack_callback()'. Currently, 'is_sparse_directory_entry()' is called with the first 'name_entry' in the 'names' list of entries on 'unpack_callback()'. However, this entry may be empty even when other elements of 'names' are not (such as when switching from an orphan branch back to a "normal" branch). As a result, 'is_sparse_directory_entry()' could incorrectly indicate that a sparse directory is *not* actually sparse because the name of the index entry does not match the (empty) 'name_entry' path. Fix the issue by using the existing 'name_entry *p' value in 'unpack_callback()', which points to the first non-empty entry in 'names'. Because 'p' is 'const', also update 'is_sparse_directory_entry()'s 'name_entry *' argument to be 'const'. Finally, add a regression test case. Reported-by: Derrick Stolee <derrickstolee@github.com> Signed-off-by: Victoria Dye <vdye@github.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Victoria Dye committed Sep 1, 2022 at 21:02 UTC 037f8ea6d961a52deaa8df5150049850a53d61ac
2 files changed +11 -2
t/t1092-sparse-checkout-compatibility.sh
+9
@@ -380,6 +380,15 @@ test_expect_success 'checkout with modified sparse directory' '
380 test_all_match git checkout base
381 '
382
383 +test_expect_success 'checkout orphan then non-orphan' '
384 + init_repos &&
385 +
386 + test_all_match git checkout --orphan test-orphan &&
387 + test_all_match git status --porcelain=v2 &&
388 + test_all_match git checkout base &&
389 + test_all_match git status --porcelain=v2
390 +'
391 +
392 test_expect_success 'add outside sparse cone' '
393 init_repos &&
394
unpack-trees.c
+2 -2
@@ -1423,7 +1423,7 @@ static void debug_unpack_callback(int n,
1423 * from the tree walk at the given traverse_info.
1424 */
1425 static int is_sparse_directory_entry(struct cache_entry *ce,
1426 - struct name_entry *name,
1426 + const struct name_entry *name,
1427 struct traverse_info *info)
1428 {
1429 if (!ce || !name || !S_ISSPARSEDIR(ce->ce_mode))
@@ -1562,7 +1562,7 @@ static int unpack_callback(int n, unsigned long mask, unsigned long dirmask, str
1562 }
1563 }
1564
1565 - if (!is_sparse_directory_entry(src[0], names, info) &&
1565 + if (!is_sparse_directory_entry(src[0], p, info) &&
1566 !is_new_sparse_dir &&
1567 traverse_trees_recursive(n, dirmask, mask & ~dirmask,
1568 names, info) < 0) {