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>
This commit is contained in:
Derrick Stolee 2021-07-29 14:52:06 +00:00 коммит произвёл Junio C Hamano
Родитель 4eaffd81a5
Коммит 939fa07582
2 изменённых файлов: 10 добавлений и 6 удалений

Просмотреть файл

@ -192,13 +192,21 @@ static int refresh(int verbose, const struct pathspec *pathspec)
struct string_list only_match_skip_worktree = STRING_LIST_INIT_NODUP; struct string_list only_match_skip_worktree = STRING_LIST_INIT_NODUP;
int flags = REFRESH_IGNORE_SKIP_WORKTREE | int flags = REFRESH_IGNORE_SKIP_WORKTREE |
(verbose ? REFRESH_IN_PORCELAIN : REFRESH_QUIET); (verbose ? REFRESH_IN_PORCELAIN : REFRESH_QUIET);
struct pattern_list pl = { 0 };
int sparse_checkout_enabled = !get_sparse_checkout_patterns(&pl);
seen = xcalloc(pathspec->nr, 1); seen = xcalloc(pathspec->nr, 1);
refresh_index(&the_index, flags, pathspec, seen, refresh_index(&the_index, flags, pathspec, seen,
_("Unstaged changes after refreshing the index:")); _("Unstaged changes after refreshing the index:"));
for (i = 0; i < pathspec->nr; i++) { for (i = 0; i < pathspec->nr; i++) {
if (!seen[i]) { if (!seen[i]) {
if (matches_skip_worktree(pathspec, i, &skip_worktree_seen)) { const char *path = pathspec->items[i].original;
int dtype = DT_REG;
if (matches_skip_worktree(pathspec, i, &skip_worktree_seen) ||
(sparse_checkout_enabled &&
!path_matches_pattern_list(path, strlen(path), NULL,
&dtype, &pl, &the_index))) {
string_list_append(&only_match_skip_worktree, string_list_append(&only_match_skip_worktree,
pathspec->items[i].original); pathspec->items[i].original);
} else { } else {

Просмотреть файл

@ -339,11 +339,7 @@ test_expect_success 'status/add: outside sparse cone' '
# Adding the path outside of the sparse-checkout cone should fail. # Adding the path outside of the sparse-checkout cone should fail.
test_sparse_match test_must_fail git add folder1/a && test_sparse_match test_must_fail git add folder1/a &&
test_sparse_match test_must_fail git add --refresh folder1/a &&
test_must_fail git -C sparse-checkout add --refresh folder1/a 2>sparse-checkout-err &&
test_must_fail git -C sparse-index add --refresh folder1/a 2>sparse-index-err &&
# NEEDSWORK: A sparse index changes the error message.
! test_cmp sparse-checkout-err sparse-index-err &&
# NEEDSWORK: Adding a newly-tracked file outside the cone succeeds # NEEDSWORK: Adding a newly-tracked file outside the cone succeeds
test_sparse_match git add folder1/new && test_sparse_match git add folder1/new &&