@matheusbsbs / matheus-bagliano / commits / 7f45ab2dca
Fork 0

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 <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Elijah Newren committed Apr 1, 2020 at 04:17 UTC 7f45ab2dca04c5b76958843120c6bd6d3a033043
1 file changed +19 -19
dir.c
+19 -19
@@ -1665,6 +1665,7 @@ static enum path_treatment treat_directory(struct dir_struct *dir,
1665 * you CAN'T DO BOTH.
1666 */
1667 enum path_treatment state;
1668 + int matches_how = 0;
1669 int nested_repo = 0, check_only, stop_early;
1670 int old_ignored_nr, old_untracked_nr;
1671 /* The "len-1" is to strip the final '/' */
@@ -1677,6 +1678,22 @@ static enum path_treatment treat_directory(struct dir_struct *dir,
1678 if (status != index_nonexistent)
1679 BUG("Unhandled value for directory_exists_in_index: %d\n", status);
1680
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 +
1697 if ((dir->flags & DIR_SKIP_NESTED_GIT) ||
1698 !(dir->flags & DIR_NO_GITLINKS)) {
1699 struct strbuf sb = STRBUF_INIT;
@@ -1724,13 +1741,8 @@ static enum path_treatment treat_directory(struct dir_struct *dir,
1741 * 'subdir/some/deep/path/file' or 'subdir/widget-*.c'), then we
1742 * need to recurse.
1743 */
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;
1746
1747 /*
1748 * 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,
1862 if (state == path_none && !(dir->flags & DIR_HIDE_EMPTY_DIRECTORIES))
1863 state = excluded ? path_excluded : path_untracked;
1864
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 -
1865 return state;
1866 }
1867