Skip to content

Commit f1f061e

Browse files
newrengitster
authored andcommitted
dir: fix treatment of negated pathspecs
do_match_pathspec() started life as match_pathspec_depth_1() and for correctness was only supposed to be called from match_pathspec_depth(). match_pathspec_depth() was later renamed to match_pathspec(), so the invariant we expect today is that do_match_pathspec() has no direct callers outside of match_pathspec(). Unfortunately, this intention was lost with the renames of the two functions, and additional calls to do_match_pathspec() were added in commits 75a6315 ("ls-files: add pathspec matching for submodules", 2016-10-07) and 89a1f4a ("dir: if our pathspec might match files under a dir, recurse into it", 2019-09-17). Of course, do_match_pathspec() had an important advantge over match_pathspec() -- match_pathspec() would hardcode flags to one of two values, and these new callers needed to pass some other value for flags. Also, although calling do_match_pathspec() directly was incorrect, there likely wasn't any difference in the observable end output, because the bug just meant that fill_diretory() would recurse into unneeded directories. Since subsequent does-this-path-match checks on individual paths under the directory would cause those extra paths to be filtered out, the only difference from using the wrong function was unnecessary computation. The second of those bad calls to do_match_pathspec() was involved -- via either direct movement or via copying+editing -- into a number of later refactors. See commits 777b420 ("dir: synchronize treat_leading_path() and read_directory_recursive()", 2019-12-19), 8d92fb2 ("dir: replace exponential algorithm with a linear one", 2020-04-01), and 95c11ec ("Fix error-prone fill_directory() API; make it only return matches", 2020-04-01). The last of those introduced the usage of do_match_pathspec() on an individual file, and thus resulted in individual paths being returned that shouldn't be. The problem with calling do_match_pathspec() instead of match_pathspec() is that any negated patterns such as ':!unwanted_path` will be ignored. Add a new match_pathspec_with_flags() function to fulfill the needs of specifying special flags while still correctly checking negated patterns, add a big comment above do_match_pathspec() to prevent others from misusing it, and correct current callers of do_match_pathspec() to instead use either match_pathspec() or match_pathspec_with_flags(). One final note is that DO_MATCH_LEADING_PATHSPEC needs special consideration when working with DO_MATCH_EXCLUDE. The point of DO_MATCH_LEADING_PATHSPEC is that if we have a pathspec like */Makefile and we are checking a directory path like src/module/component that we want to consider it a match so that we recurse into the directory because it _might_ have a file named Makefile somewhere below. However, when we are using an exclusion pattern, i.e. we have a pathspec like :(exclude)*/Makefile we do NOT want to say that a directory path like src/module/component is a (negative) match. While there *might* be a file named 'Makefile' somewhere below that directory, there could also be other files and we cannot pre-emptively rule all the files under that directory out; we need to recurse and then check individual files. Adjust the DO_MATCH_LEADING_PATHSPEC logic to only get activated for positive pathspecs. Reported-by: John Millikin <[email protected]> Signed-off-by: Elijah Newren <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c0af173 commit f1f061e

File tree

2 files changed

+66
-17
lines changed

2 files changed

+66
-17
lines changed

dir.c

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,8 @@ static int match_pathspec_item(const struct index_state *istate,
364364
return MATCHED_FNMATCH;
365365

366366
/* Perform checks to see if "name" is a leading string of the pathspec */
367-
if (flags & DO_MATCH_LEADING_PATHSPEC) {
367+
if ( (flags & DO_MATCH_LEADING_PATHSPEC) &&
368+
!(flags & DO_MATCH_EXCLUDE)) {
368369
/* name is a literal prefix of the pathspec */
369370
int offset = name[namelen-1] == '/' ? 1 : 0;
370371
if ((namelen < matchlen) &&
@@ -401,6 +402,10 @@ static int match_pathspec_item(const struct index_state *istate,
401402
}
402403

403404
/*
405+
* do_match_pathspec() is meant to ONLY be called by
406+
* match_pathspec_with_flags(); calling it directly risks pathspecs
407+
* like ':!unwanted_path' being ignored.
408+
*
404409
* Given a name and a list of pathspecs, returns the nature of the
405410
* closest (i.e. most specific) match of the name to any of the
406411
* pathspecs.
@@ -486,13 +491,12 @@ static int do_match_pathspec(const struct index_state *istate,
486491
return retval;
487492
}
488493

489-
int match_pathspec(const struct index_state *istate,
490-
const struct pathspec *ps,
491-
const char *name, int namelen,
492-
int prefix, char *seen, int is_dir)
494+
static int match_pathspec_with_flags(const struct index_state *istate,
495+
const struct pathspec *ps,
496+
const char *name, int namelen,
497+
int prefix, char *seen, unsigned flags)
493498
{
494499
int positive, negative;
495-
unsigned flags = is_dir ? DO_MATCH_DIRECTORY : 0;
496500
positive = do_match_pathspec(istate, ps, name, namelen,
497501
prefix, seen, flags);
498502
if (!(ps->magic & PATHSPEC_EXCLUDE) || !positive)
@@ -503,6 +507,16 @@ int match_pathspec(const struct index_state *istate,
503507
return negative ? 0 : positive;
504508
}
505509

510+
int match_pathspec(const struct index_state *istate,
511+
const struct pathspec *ps,
512+
const char *name, int namelen,
513+
int prefix, char *seen, int is_dir)
514+
{
515+
unsigned flags = is_dir ? DO_MATCH_DIRECTORY : 0;
516+
return match_pathspec_with_flags(istate, ps, name, namelen,
517+
prefix, seen, flags);
518+
}
519+
506520
/**
507521
* Check if a submodule is a superset of the pathspec
508522
*/
@@ -511,11 +525,11 @@ int submodule_path_match(const struct index_state *istate,
511525
const char *submodule_name,
512526
char *seen)
513527
{
514-
int matched = do_match_pathspec(istate, ps, submodule_name,
515-
strlen(submodule_name),
516-
0, seen,
517-
DO_MATCH_DIRECTORY |
518-
DO_MATCH_LEADING_PATHSPEC);
528+
int matched = match_pathspec_with_flags(istate, ps, submodule_name,
529+
strlen(submodule_name),
530+
0, seen,
531+
DO_MATCH_DIRECTORY |
532+
DO_MATCH_LEADING_PATHSPEC);
519533
return matched;
520534
}
521535

@@ -1686,9 +1700,11 @@ static enum path_treatment treat_directory(struct dir_struct *dir,
16861700
* for matching patterns.
16871701
*/
16881702
if (pathspec && !excluded) {
1689-
matches_how = do_match_pathspec(istate, pathspec, dirname, len,
1690-
0 /* prefix */, NULL /* seen */,
1691-
DO_MATCH_LEADING_PATHSPEC);
1703+
matches_how = match_pathspec_with_flags(istate, pathspec,
1704+
dirname, len,
1705+
0 /* prefix */,
1706+
NULL /* seen */,
1707+
DO_MATCH_LEADING_PATHSPEC);
16921708
if (!matches_how)
16931709
return path_none;
16941710
}
@@ -2120,9 +2136,9 @@ static enum path_treatment treat_path(struct dir_struct *dir,
21202136
if (excluded)
21212137
return path_excluded;
21222138
if (pathspec &&
2123-
!do_match_pathspec(istate, pathspec, path->buf, path->len,
2124-
0 /* prefix */, NULL /* seen */,
2125-
0 /* flags */))
2139+
!match_pathspec(istate, pathspec, path->buf, path->len,
2140+
0 /* prefix */, NULL /* seen */,
2141+
0 /* is_dir */))
21262142
return path_none;
21272143
return path_untracked;
21282144
}

t/t6132-pathspec-exclude.sh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,4 +211,37 @@ test_expect_success 't_e_i() exclude case #8' '
211211
)
212212
'
213213

214+
test_expect_success 'grep --untracked PATTERN' '
215+
# This test is not an actual test of exclude patterns, rather it
216+
# is here solely to ensure that if any tests are inserted, deleted, or
217+
# changed above, that we still have untracked files with the expected
218+
# contents for the NEXT two tests.
219+
cat <<-\EOF >expect-grep &&
220+
actual
221+
expect
222+
sub/actual
223+
sub/expect
224+
EOF
225+
git grep -l --untracked file -- >actual-grep &&
226+
test_cmp expect-grep actual-grep
227+
'
228+
229+
test_expect_success 'grep --untracked PATTERN :(exclude)DIR' '
230+
cat <<-\EOF >expect-grep &&
231+
actual
232+
expect
233+
EOF
234+
git grep -l --untracked file -- ":(exclude)sub" >actual-grep &&
235+
test_cmp expect-grep actual-grep
236+
'
237+
238+
test_expect_success 'grep --untracked PATTERN :(exclude)*FILE' '
239+
cat <<-\EOF >expect-grep &&
240+
actual
241+
sub/actual
242+
EOF
243+
git grep -l --untracked file -- ":(exclude)*expect" >actual-grep &&
244+
test_cmp expect-grep actual-grep
245+
'
246+
214247
test_done

0 commit comments

Comments
 (0)