Skip to content

Commit f1a2ddb

Browse files
pcloudsgitster
authored andcommitted
tree_entry_interesting(): optimize wildcard matching when base is matched
If base is already matched, skip that part when calling fnmatch(). This happens quite often if users start a command from worktree's subdirectory and prefix is usually prepended to all pathspecs. Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d38f280 commit f1a2ddb

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

t/t4010-diff-pathspec.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,22 @@ test_expect_success 'diff-tree -r with wildcard' '
8484
test_cmp expected result
8585
'
8686

87+
test_expect_success 'diff-tree with wildcard shows dir also matches' '
88+
git diff-tree --name-only $tree $tree2 -- "path1/f*" >result &&
89+
echo path1 >expected &&
90+
test_cmp expected result
91+
'
92+
93+
test_expect_success 'diff-tree -r with wildcard from beginning' '
94+
git diff-tree -r --name-only $tree $tree2 -- "path1/*file1" >result &&
95+
echo path1/file1 >expected &&
96+
test_cmp expected result
97+
'
98+
99+
test_expect_success 'diff-tree -r with wildcard' '
100+
git diff-tree -r --name-only $tree $tree2 -- "path1/f*" >result &&
101+
echo path1/file1 >expected &&
102+
test_cmp expected result
103+
'
104+
87105
test_done

tree-walk.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,20 @@ int tree_entry_interesting(const struct name_entry *entry,
595595
match + baselen, matchlen - baselen,
596596
&never_interesting))
597597
return 1;
598+
599+
if (ps->items[i].has_wildcard) {
600+
if (!fnmatch(match + baselen, entry->path, 0))
601+
return 1;
602+
603+
/*
604+
* Match all directories. We'll try to
605+
* match files later on.
606+
*/
607+
if (ps->recursive && S_ISDIR(entry->mode))
608+
return 1;
609+
}
610+
611+
continue;
598612
}
599613

600614
match_wildcards:

0 commit comments

Comments
 (0)