add: update --chmod to skip sparse paths

We added checks for path_in_sparse_checkout() to portions of 'git add' that add warnings and prevent staging a modification, but we skipped the --chmod mode. Update chmod_pathspec() to ignore cache entries whose path is outside of the sparse-checkout cone (unless --sparse is provided). Add a test in t3705. Signed-off-by: Derrick Stolee <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Sep 24, 2021 at 15:39 UTC 63b60b3add7527c202124982e0b14a6a512f0450
2 files changed +12 -2
builtin/add.c
+3 -1
@@ -47,7 +47,9 @@ static int chmod_pathspec(struct pathspec *pathspec, char flip, int show_only)
47 struct cache_entry *ce = active_cache[i];
48 int err;
49
50 - if (!include_sparse && ce_skip_worktree(ce))
50 + if (!include_sparse &&
51 + (ce_skip_worktree(ce) ||
52 + !path_in_sparse_checkout(ce->name, &the_index)))
53 continue;
54
55 if (pathspec && !ce_path_match(&the_index, ce, pathspec, NULL))
t/t3705-add-sparse-checkout.sh
+9 -1
@@ -169,11 +169,19 @@ test_expect_success 'git add fails outside of sparse-checkout definition' '
169 test_must_fail git add sparse_entry &&
170 test_sparse_entry_unstaged &&
171
172 + test_must_fail git add --chmod=+x sparse_entry &&
173 + test_sparse_entry_unstaged &&
174 +
175 # Avoid munging CRLFs to avoid an error message
176 git -c core.autocrlf=input add --sparse sparse_entry 2>stderr &&
177 test_must_be_empty stderr &&
178 test-tool read-cache --table >actual &&
176 - grep "^100644 blob.*sparse_entry\$" actual
179 + grep "^100644 blob.*sparse_entry\$" actual &&
180 +
181 + git add --sparse --chmod=+x sparse_entry 2>stderr &&
182 + test_must_be_empty stderr &&
183 + test-tool read-cache --table >actual &&
184 + grep "^100755 blob.*sparse_entry\$" actual
185 '
186
187 test_expect_success 'add obeys advice.updateSparsePath' '