Skip to content

Commit 7497039

Browse files
vdyegitster
authored andcommitted
read-tree: narrow scope of index expansion for '--prefix'
When 'git read-tree' is provided with a prefix, expand the index only if the prefix is equivalent to a sparse directory or contained within one. If the index is not expanded in these cases, 'ce_in_traverse_path' will indicate that the relevant sparse directory is not in the prefix/traverse path, skipping past it and not unpacking the appropriate tree(s). If the prefix is in-cone, its sparse subdirectories (if any) will be traversed correctly without index expansion. The behavior of 'git read-tree' with prefixes 1) inside of cone, 2) equal to a sparse directory, and 3) inside a sparse directory are all tested as part of the 't/t1092-sparse-checkout-compatibility.sh' test 'read-tree --prefix', ensuring that the sparse index case works the way it did prior to this change as well as matching non-sparse index sparse-checkout. Helped-by: Elijah Newren <[email protected]> Signed-off-by: Victoria Dye <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2c66a7c commit 7497039

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

builtin/read-tree.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,7 @@ int cmd_read_tree(int argc, const char **argv, const char *cmd_prefix)
217217
if (opts.merge && !opts.index_only)
218218
setup_work_tree();
219219

220-
/* TODO: audit sparse index behavior in unpack_trees */
221-
if (opts.skip_sparse_checkout || opts.prefix)
220+
if (opts.skip_sparse_checkout)
222221
ensure_full_index(&the_index);
223222

224223
if (opts.merge) {

t/t1092-sparse-checkout-compatibility.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1417,7 +1417,13 @@ test_expect_success 'sparse index is not expanded: read-tree' '
14171417
do
14181418
ensure_not_expanded read-tree -mu $MERGE_TREES &&
14191419
ensure_not_expanded reset --hard || return 1
1420-
done
1420+
done &&
1421+
1422+
rm -rf sparse-index/deep/deeper2 &&
1423+
ensure_not_expanded add . &&
1424+
ensure_not_expanded commit -m "test" &&
1425+
1426+
ensure_not_expanded read-tree --prefix=deep/deeper2 -u deepest
14211427
'
14221428

14231429
test_expect_success 'ls-files' '

unpack-trees.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1693,6 +1693,41 @@ static void populate_from_existing_patterns(struct unpack_trees_options *o,
16931693
o->pl = pl;
16941694
}
16951695

1696+
static void update_sparsity_for_prefix(const char *prefix,
1697+
struct index_state *istate)
1698+
{
1699+
int prefix_len = strlen(prefix);
1700+
struct strbuf ce_prefix = STRBUF_INIT;
1701+
1702+
if (!istate->sparse_index)
1703+
return;
1704+
1705+
while (prefix_len > 0 && prefix[prefix_len - 1] == '/')
1706+
prefix_len--;
1707+
1708+
if (prefix_len <= 0)
1709+
BUG("Invalid prefix passed to update_sparsity_for_prefix");
1710+
1711+
strbuf_grow(&ce_prefix, prefix_len + 1);
1712+
strbuf_add(&ce_prefix, prefix, prefix_len);
1713+
strbuf_addch(&ce_prefix, '/');
1714+
1715+
/*
1716+
* If the prefix points to a sparse directory or a path inside a sparse
1717+
* directory, the index should be expanded. This is accomplished in one
1718+
* of two ways:
1719+
* - if the prefix is inside a sparse directory, it will be expanded by
1720+
* the 'ensure_full_index(...)' call in 'index_name_pos(...)'.
1721+
* - if the prefix matches an existing sparse directory entry,
1722+
* 'index_name_pos(...)' will return its index position, triggering
1723+
* the 'ensure_full_index(...)' below.
1724+
*/
1725+
if (!path_in_cone_mode_sparse_checkout(ce_prefix.buf, istate) &&
1726+
index_name_pos(istate, ce_prefix.buf, ce_prefix.len) >= 0)
1727+
ensure_full_index(istate);
1728+
1729+
strbuf_release(&ce_prefix);
1730+
}
16961731

16971732
static int verify_absent(const struct cache_entry *,
16981733
enum unpack_trees_error_types,
@@ -1739,6 +1774,9 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options
17391774
setup_standard_excludes(o->dir);
17401775
}
17411776

1777+
if (o->prefix)
1778+
update_sparsity_for_prefix(o->prefix, o->src_index);
1779+
17421780
if (!core_apply_sparse_checkout || !o->update)
17431781
o->skip_sparse_checkout = 1;
17441782
if (!o->skip_sparse_checkout && !o->pl) {

0 commit comments

Comments
 (0)