Skip to content

Commit 7f45ab2

Browse files
newrengitster
authored andcommitted
dir: replace double pathspec matching with single in treat_directory()
treat_directory() had a call to both do_match_pathspec() and match_pathspec(). These calls have migrated through the code somewhat since their introduction, but we don't actually need both. Replace the two calls with one, and while at it, move the check earlier in order to reduce the need for callers of fill_directory() to do post-filtering of results. The next patch will address post-filtering more forcefully and provide more relevant history and context. Signed-off-by: Elijah Newren <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1684644 commit 7f45ab2

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

dir.c

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1665,6 +1665,7 @@ static enum path_treatment treat_directory(struct dir_struct *dir,
16651665
* you CAN'T DO BOTH.
16661666
*/
16671667
enum path_treatment state;
1668+
int matches_how = 0;
16681669
int nested_repo = 0, check_only, stop_early;
16691670
int old_ignored_nr, old_untracked_nr;
16701671
/* The "len-1" is to strip the final '/' */
@@ -1677,6 +1678,22 @@ static enum path_treatment treat_directory(struct dir_struct *dir,
16771678
if (status != index_nonexistent)
16781679
BUG("Unhandled value for directory_exists_in_index: %d\n", status);
16791680

1681+
/*
1682+
* We don't want to descend into paths that don't match the necessary
1683+
* patterns. Clearly, if we don't have a pathspec, then we can't check
1684+
* for matching patterns. Also, if (excluded) then we know we matched
1685+
* the exclusion patterns so as an optimization we can skip checking
1686+
* for matching patterns.
1687+
*/
1688+
if (pathspec && !excluded) {
1689+
matches_how = do_match_pathspec(istate, pathspec, dirname, len,
1690+
0 /* prefix */, NULL /* seen */,
1691+
DO_MATCH_LEADING_PATHSPEC);
1692+
if (!matches_how)
1693+
return path_none;
1694+
}
1695+
1696+
16801697
if ((dir->flags & DIR_SKIP_NESTED_GIT) ||
16811698
!(dir->flags & DIR_NO_GITLINKS)) {
16821699
struct strbuf sb = STRBUF_INIT;
@@ -1724,13 +1741,8 @@ static enum path_treatment treat_directory(struct dir_struct *dir,
17241741
* 'subdir/some/deep/path/file' or 'subdir/widget-*.c'), then we
17251742
* need to recurse.
17261743
*/
1727-
if (pathspec) {
1728-
int ret = do_match_pathspec(istate, pathspec, dirname, len,
1729-
0 /* prefix */, NULL /* seen */,
1730-
DO_MATCH_LEADING_PATHSPEC);
1731-
if (ret == MATCHED_RECURSIVELY_LEADING_PATHSPEC)
1732-
return path_recurse;
1733-
}
1744+
if (matches_how == MATCHED_RECURSIVELY_LEADING_PATHSPEC)
1745+
return path_recurse;
17341746

17351747
/*
17361748
* Other than the path_recurse case immediately above, we only need
@@ -1850,18 +1862,6 @@ static enum path_treatment treat_directory(struct dir_struct *dir,
18501862
if (state == path_none && !(dir->flags & DIR_HIDE_EMPTY_DIRECTORIES))
18511863
state = excluded ? path_excluded : path_untracked;
18521864

1853-
/*
1854-
* We can recurse into untracked directories that don't match any
1855-
* of the given pathspecs when some file underneath the directory
1856-
* might match one of the pathspecs. If so, we should make sure
1857-
* to note that the directory itself did not match.
1858-
*/
1859-
if (pathspec &&
1860-
!match_pathspec(istate, pathspec, dirname, len,
1861-
0 /* prefix */, NULL,
1862-
0 /* do NOT special case dirs */))
1863-
state = path_none;
1864-
18651865
return state;
18661866
}
18671867

0 commit comments

Comments
 (0)