add: ignore outside the sparse-checkout in refresh()

Since b243012 (refresh_index(): add flag to ignore SKIP_WORKTREE entries, 2021-04-08), 'git add --refresh <path>' will output a warning message when the path is outside the sparse-checkout definition. The implementation of this warning happened in parallel with the sparse-index work to add ensure_full_index() calls throughout the codebase. Update this loop to have the proper logic that checks to see if the pathspec is outside the sparse-checkout definition. This avoids the need to expand the sparse directory entry and determine if the path is tracked, untracked, or ignored. We simply avoid updating the stat() information because there isn't even an entry that matches the path! 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 29, 2021 at 14:52 UTC 939fa07582a2c9455e71f599263e2dcbe1d655b5
2 files changed +10 -6
builtin/add.c
+9 -1
@@ -192,13 +192,21 @@ static int refresh(int verbose, const struct pathspec *pathspec)
192 struct string_list only_match_skip_worktree = STRING_LIST_INIT_NODUP;
193 int flags = REFRESH_IGNORE_SKIP_WORKTREE |
194 (verbose ? REFRESH_IN_PORCELAIN : REFRESH_QUIET);
195 + struct pattern_list pl = { 0 };
196 + int sparse_checkout_enabled = !get_sparse_checkout_patterns(&pl);
197
198 seen = xcalloc(pathspec->nr, 1);
199 refresh_index(&the_index, flags, pathspec, seen,
200 _("Unstaged changes after refreshing the index:"));
201 for (i = 0; i < pathspec->nr; i++) {
202 if (!seen[i]) {
201 - if (matches_skip_worktree(pathspec, i, &skip_worktree_seen)) {
203 + const char *path = pathspec->items[i].original;
204 + int dtype = DT_REG;
205 +
206 + if (matches_skip_worktree(pathspec, i, &skip_worktree_seen) ||
207 + (sparse_checkout_enabled &&
208 + !path_matches_pattern_list(path, strlen(path), NULL,
209 + &dtype, &pl, &the_index))) {
210 string_list_append(&only_match_skip_worktree,
211 pathspec->items[i].original);
212 } else {
t/t1092-sparse-checkout-compatibility.sh
+1 -5
@@ -339,11 +339,7 @@ test_expect_success 'status/add: outside sparse cone' '
339
340 # Adding the path outside of the sparse-checkout cone should fail.
341 test_sparse_match test_must_fail git add folder1/a &&
342 -
343 - test_must_fail git -C sparse-checkout add --refresh folder1/a 2>sparse-checkout-err &&
344 - test_must_fail git -C sparse-index add --refresh folder1/a 2>sparse-index-err &&
345 - # NEEDSWORK: A sparse index changes the error message.
346 - ! test_cmp sparse-checkout-err sparse-index-err &&
342 + test_sparse_match test_must_fail git add --refresh folder1/a &&
343
344 # NEEDSWORK: Adding a newly-tracked file outside the cone succeeds
345 test_sparse_match git add folder1/new &&