aboutsummaryrefslogtreecommitdiffstats
path: root/dir.c
diff options
context:
space:
mode:
authorMatheus Tavares <matheus.bernardino@usp.br>2021-10-28 11:21:11 -0300
committerJunio C Hamano <gitster@pobox.com>2021-10-28 08:46:07 -0700
commit20141e322c8c27054c105fca5c812043c2bc7440 (patch)
treed6e09f1ca6fca85d85d4a2388b0dbbc5cd5cb1bf /dir.c
parent6579e788c0a4b9468c5e2954a0868f9db0496e43 (diff)
downloadgit-20141e322c8c27054c105fca5c812043c2bc7440.tar.gz
add, rm, mv: fix bug that prevents the update of non-sparse dirs
These three commands recently learned to avoid updating paths outside the sparse checkout even if they are missing the SKIP_WORKTREE bit. This is done using path_in_sparse_checkout(), which checks whether a given path matches the current list of sparsity rules, similar to what clear_ce_flags() does when we run "git sparse checkout init" or "git sparse-checkout reapply". However, clear_ce_flags() uses a recursive approach, applying the match results from parent directories on paths that get the UNDECIDED result, whereas path_in_sparse_checkout() only attempts to match the full path and immediately considers UNDECIDED as NOT_MATCHED. This makes the function miss matches with leading directories. For example, if the user has the sparsity patterns "!/a" and "b/", add, rm, and mv will fail to update the path "a/b/c" and end up displaying a warning about it being outside the sparse checkout even though it isn't. This problem only occurs in full pattern mode as the pattern matching functions never return UNDECIDED for cone mode. To fix this, replicate the recursive behavior of clear_ce_flags() in path_in_sparse_checkout(), falling back to the parent directory match when a path gets the UNDECIDED result. (If this turns out to be too expensive in some cases, we may want to later add some form of caching to accelerate multiple queries within the same directory. This is not implemented in this patch, though.) Also add two tests for each affected command (add, rm, and mv) to check that they behave correctly with the recursive pattern matching. The first test would previously fail without this patch while the second already succeeded. It is added mostly to make sure that we are not breaking the existing pattern matching for directories that are really sparse, and also as a protection against any future regressions. Two other existing tests had to be changed as well: one test in t3602 checks that "git rm -r <dir>" won't remove sparse entries, but it didn't allow the non-sparse entries inside <dir> to be removed. The other one, in t7002, tested that "git mv" would correctly display a warning message for sparse paths, but it accidentally expected the message to include two non-sparse paths as well. Signed-off-by: Matheus Tavares <matheus.bernardino@usp.br> Acked-by: Derrick Stolee <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'dir.c')
-rw-r--r--dir.c29
1 files changed, 23 insertions, 6 deletions
diff --git a/dir.c b/dir.c
index 174d336c30..7bbf22a3e7 100644
--- a/dir.c
+++ b/dir.c
@@ -1504,8 +1504,9 @@ static int path_in_sparse_checkout_1(const char *path,
struct index_state *istate,
int require_cone_mode)
{
- const char *base;
int dtype = DT_REG;
+ enum pattern_match_result match = UNDECIDED;
+ const char *end, *slash;
/*
* We default to accepting a path if there are no patterns or
@@ -1516,11 +1517,27 @@ static int path_in_sparse_checkout_1(const char *path,
!istate->sparse_checkout_patterns->use_cone_patterns))
return 1;
- base = strrchr(path, '/');
- return path_matches_pattern_list(path, strlen(path), base ? base + 1 : path,
- &dtype,
- istate->sparse_checkout_patterns,
- istate) > 0;
+ /*
+ * If UNDECIDED, use the match from the parent dir (recursively), or
+ * fall back to NOT_MATCHED at the topmost level. Note that cone mode
+ * never returns UNDECIDED, so we will execute only one iteration in
+ * this case.
+ */
+ for (end = path + strlen(path);
+ end > path && match == UNDECIDED;
+ end = slash) {
+
+ for (slash = end - 1; slash > path && *slash != '/'; slash--)
+ ; /* do nothing */
+
+ match = path_matches_pattern_list(path, end - path,
+ slash > path ? slash + 1 : path, &dtype,
+ istate->sparse_checkout_patterns, istate);
+
+ /* We are going to match the parent dir now */
+ dtype = DT_DIR;
+ }
+ return match > 0;
}
int path_in_sparse_checkout(const char *path,