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 <newren@gmail.com> Signed-off-by: Victoria Dye <vdye@github.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Victoria Dye committed Mar 1, 2022 at 20:24 UTC 749703924121b9eb2750b4313b2c769113c8b310
3 files changed +46 -3
builtin/read-tree.c
+1 -2
@@ -217,8 +217,7 @@ int cmd_read_tree(int argc, const char **argv, const char *cmd_prefix)
217 if (opts.merge && !opts.index_only)
218 setup_work_tree();
219
220 - /* TODO: audit sparse index behavior in unpack_trees */
221 - if (opts.skip_sparse_checkout || opts.prefix)
220 + if (opts.skip_sparse_checkout)
221 ensure_full_index(&the_index);
222
223 if (opts.merge) {
t/t1092-sparse-checkout-compatibility.sh
+7 -1
@@ -1417,7 +1417,13 @@ test_expect_success 'sparse index is not expanded: read-tree' '
1417 do
1418 ensure_not_expanded read-tree -mu $MERGE_TREES &&
1419 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
1427 '
1428
1429 test_expect_success 'ls-files' '
unpack-trees.c
+38
@@ -1693,6 +1693,41 @@ static void populate_from_existing_patterns(struct unpack_trees_options *o,
1693 o->pl = pl;
1694 }
1695
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 +}
1731
1732 static int verify_absent(const struct cache_entry *,
1733 enum unpack_trees_error_types,
@@ -1739,6 +1774,9 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options
1774 setup_standard_excludes(o->dir);
1775 }
1776
1777 + if (o->prefix)
1778 + update_sparsity_for_prefix(o->prefix, o->src_index);
1779 +
1780 if (!core_apply_sparse_checkout || !o->update)
1781 o->skip_sparse_checkout = 1;
1782 if (!o->skip_sparse_checkout && !o->pl) {