Skip to content

Commit

Permalink
unpack-trees: fix sparse directory recursion check
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
vdye authored and derrickstolee committed Sep 2, 2022
1 parent 2594b54 commit 2f75d8a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
9 changes: 9 additions & 0 deletions t/t1092-sparse-checkout-compatibility.sh
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,15 @@ test_expect_success 'checkout with modified sparse directory' '
test_all_match git checkout base
'

test_expect_success 'checkout orphan then non-orphan' '
init_repos &&
test_all_match git checkout --orphan test-orphan &&
test_all_match git status --porcelain=v2 &&
test_all_match git checkout base &&
test_all_match git status --porcelain=v2
'

test_expect_success 'add outside sparse cone' '
init_repos &&
Expand Down
4 changes: 2 additions & 2 deletions unpack-trees.c
Original file line number Diff line number Diff line change
Expand Up @@ -1442,7 +1442,7 @@ static void debug_unpack_callback(int n,
* from the tree walk at the given traverse_info.
*/
static int is_sparse_directory_entry(struct cache_entry *ce,
struct name_entry *name,
const struct name_entry *name,
struct traverse_info *info)
{
if (!ce || !name || !S_ISSPARSEDIR(ce->ce_mode))
Expand Down Expand Up @@ -1581,7 +1581,7 @@ static int unpack_callback(int n, unsigned long mask, unsigned long dirmask, str
}
}

if (!is_sparse_directory_entry(src[0], names, info) &&
if (!is_sparse_directory_entry(src[0], p, info) &&
!is_new_sparse_dir &&
traverse_trees_recursive(n, dirmask, mask & ~dirmask,
names, info) < 0) {
Expand Down

0 comments on commit 2f75d8a

Please sign in to comment.