]> git.ipfire.org Git - thirdparty/git.git/commitdiff
dir: replace double pathspec matching with single in treat_directory()
authorElijah Newren <newren@gmail.com>
Wed, 1 Apr 2020 04:17:44 +0000 (04:17 +0000)
committerJunio C Hamano <gitster@pobox.com>
Wed, 1 Apr 2020 18:11:31 +0000 (11:11 -0700)
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 <newren@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
dir.c

diff --git a/dir.c b/dir.c
index 8be31df58c211dda6a7a5954ac63bc6860b838b3..a67930dcff6414bebe99e161f8b30c05c8e87dd6 100644 (file)
--- a/dir.c
+++ b/dir.c
@@ -1665,6 +1665,7 @@ static enum path_treatment treat_directory(struct dir_struct *dir,
         *          you CAN'T DO BOTH.
         */
        enum path_treatment state;
+       int matches_how = 0;
        int nested_repo = 0, check_only, stop_early;
        int old_ignored_nr, old_untracked_nr;
        /* The "len-1" is to strip the final '/' */
@@ -1677,6 +1678,22 @@ static enum path_treatment treat_directory(struct dir_struct *dir,
        if (status != index_nonexistent)
                BUG("Unhandled value for directory_exists_in_index: %d\n", status);
 
+       /*
+        * We don't want to descend into paths that don't match the necessary
+        * patterns.  Clearly, if we don't have a pathspec, then we can't check
+        * for matching patterns.  Also, if (excluded) then we know we matched
+        * the exclusion patterns so as an optimization we can skip checking
+        * for matching patterns.
+        */
+       if (pathspec && !excluded) {
+               matches_how = do_match_pathspec(istate, pathspec, dirname, len,
+                                               0 /* prefix */, NULL /* seen */,
+                                               DO_MATCH_LEADING_PATHSPEC);
+               if (!matches_how)
+                       return path_none;
+       }
+
+
        if ((dir->flags & DIR_SKIP_NESTED_GIT) ||
                !(dir->flags & DIR_NO_GITLINKS)) {
                struct strbuf sb = STRBUF_INIT;
@@ -1724,13 +1741,8 @@ static enum path_treatment treat_directory(struct dir_struct *dir,
         * 'subdir/some/deep/path/file' or 'subdir/widget-*.c'), then we
         * need to recurse.
         */
-       if (pathspec) {
-               int ret = do_match_pathspec(istate, pathspec, dirname, len,
-                                           0 /* prefix */, NULL /* seen */,
-                                           DO_MATCH_LEADING_PATHSPEC);
-               if (ret == MATCHED_RECURSIVELY_LEADING_PATHSPEC)
-                       return path_recurse;
-       }
+       if (matches_how == MATCHED_RECURSIVELY_LEADING_PATHSPEC)
+               return path_recurse;
 
        /*
         * 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,
        if (state == path_none && !(dir->flags & DIR_HIDE_EMPTY_DIRECTORIES))
                state = excluded ? path_excluded : path_untracked;
 
-       /*
-        * We can recurse into untracked directories that don't match any
-        * of the given pathspecs when some file underneath the directory
-        * might match one of the pathspecs.  If so, we should make sure
-        * to note that the directory itself did not match.
-        */
-       if (pathspec &&
-           !match_pathspec(istate, pathspec, dirname, len,
-                           0 /* prefix */, NULL,
-                           0 /* do NOT special case dirs */))
-               state = path_none;
-
        return state;
 }