status: skip sparse-checkout percentage with sparse-index

'git status' began reporting a percentage of populated paths when sparse-checkout is enabled in 051df3cf (wt-status: show sparse checkout status as well, 2020-07-18). This percentage is incorrect when the index has sparse directories. It would also be expensive to calculate as we would need to parse trees to count the total number of possible paths. Avoid the expensive computation by simplifying the output to only report that a sparse checkout exists, without the percentage. This change is the reason we use 'git status --porcelain=v2' in t1092-sparse-checkout-compatibility.sh. We don't want to ensure that this message is equal across both modes, but instead just the important information about staged, modified, and untracked files are compared. Reviewed-by: Elijah Newren <newren@gmail.com> Signed-off-by: Derrick Stolee <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Jul 14, 2021 at 13:12 UTC bf48e5acdbf25a98fd142d9c90157c10b427119a
3 files changed +20 -3
t/t1092-sparse-checkout-compatibility.sh
+8
@@ -218,6 +218,14 @@ test_expect_success 'status with options' '
218 test_all_match git status --porcelain=v2 -uno
219 '
220
221 +test_expect_success 'status reports sparse-checkout' '
222 + init_repos &&
223 + git -C sparse-checkout status >full &&
224 + git -C sparse-index status >sparse &&
225 + test_i18ngrep "You are in a sparse checkout with " full &&
226 + test_i18ngrep "You are in a sparse checkout." sparse
227 +'
228 +
229 test_expect_success 'add, commit, checkout' '
230 init_repos &&
231
wt-status.c
+11 -3
@@ -1493,9 +1493,12 @@ static void show_sparse_checkout_in_use(struct wt_status *s,
1493 if (s->state.sparse_checkout_percentage == SPARSE_CHECKOUT_DISABLED)
1494 return;
1495
1496 - status_printf_ln(s, color,
1497 - _("You are in a sparse checkout with %d%% of tracked files present."),
1498 - s->state.sparse_checkout_percentage);
1496 + if (s->state.sparse_checkout_percentage == SPARSE_CHECKOUT_SPARSE_INDEX)
1497 + status_printf_ln(s, color, _("You are in a sparse checkout."));
1498 + else
1499 + status_printf_ln(s, color,
1500 + _("You are in a sparse checkout with %d%% of tracked files present."),
1501 + s->state.sparse_checkout_percentage);
1502 wt_longstatus_print_trailer(s);
1503 }
1504
@@ -1653,6 +1656,11 @@ static void wt_status_check_sparse_checkout(struct repository *r,
1656 return;
1657 }
1658
1659 + if (r->index->sparse_index) {
1660 + state->sparse_checkout_percentage = SPARSE_CHECKOUT_SPARSE_INDEX;
1661 + return;
1662 + }
1663 +
1664 for (i = 0; i < r->index->cache_nr; i++) {
1665 struct cache_entry *ce = r->index->cache[i];
1666 if (ce_skip_worktree(ce))
wt-status.h
+1
@@ -78,6 +78,7 @@ enum wt_status_format {
78 };
79
80 #define SPARSE_CHECKOUT_DISABLED -1
81 +#define SPARSE_CHECKOUT_SPARSE_INDEX -2
82
83 struct wt_status_state {
84 int merge_in_progress;