]> git.ipfire.org Git - thirdparty/git.git/commitdiff
match_pathname(): reorder prefix-match check
authorJeff King <peff@peff.net>
Sun, 26 Oct 2025 15:41:46 +0000 (11:41 -0400)
committerJunio C Hamano <gitster@pobox.com>
Sun, 26 Oct 2025 23:30:39 +0000 (16:30 -0700)
As an optimization, we use fspathncmp() to match a prefix of the pattern
that does not contain any wildcards, and then pass the remainder to
fnmatch(). If it has matched the whole thing, we can return early.

Let's shift this early-return check to before we tweak the pattern and
name strings. That will gives us more flexibility with that tweaking.

It might also save a few instructions, but I couldn't measure any
improvement in doing so (and I wouldn't be surprised if an optimizing
compiler could figure that out itself).

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
dir.c

diff --git a/dir.c b/dir.c
index dfb4d40103fb4a7d38c3748c2ab4acd8e1be1930..130fa987660a01cbefb373bb06b6379ad4d01e1d 100644 (file)
--- a/dir.c
+++ b/dir.c
@@ -1360,18 +1360,19 @@ int match_pathname(const char *pathname, int pathlen,
 
                if (fspathncmp(pattern, name, prefix))
                        return 0;
-               pattern += prefix;
-               patternlen -= prefix;
-               name    += prefix;
-               namelen -= prefix;
 
                /*
                 * If the whole pattern did not have a wildcard,
                 * then our prefix match is all we need; we
                 * do not need to call fnmatch at all.
                 */
-               if (!patternlen && !namelen)
+               if (patternlen == prefix && namelen == prefix)
                        return 1;
+
+               pattern += prefix;
+               patternlen -= prefix;
+               name    += prefix;
+               namelen -= prefix;
        }
 
        return fnmatch_icase_mem(pattern, patternlen,