Skip to content

Commit eef3df5

Browse files
bmwillgitster
authored andcommitted
pathspec: only match across submodule boundaries when requested
Commit 74ed437 (grep: enable recurse-submodules to work on <tree> objects, 2016-12-16) taught 'tree_entry_interesting()' to be able to match across submodule boundaries in the presence of wildcards. This is done by performing literal matching up to the first wildcard and then punting to the submodule itself to perform more accurate pattern matching. Instead of introducing a new flag to request this behavior, commit 74ed437 overloaded the already existing 'recursive' flag in 'struct pathspec' to request this behavior. This leads to a bug where whenever any other caller has the 'recursive' flag set as well as a pathspec with wildcards that all submodules will be indicated as matches. One simple example of this is: git init repo cd repo git init submodule git -C submodule commit -m initial --allow-empty touch "[bracket]" git add "[bracket]" git commit -m bracket git add submodule git commit -m submodule git rev-list HEAD -- "[bracket]" Fix this by introducing the new flag 'recurse_submodules' in 'struct pathspec' and using this flag to determine if matches should be allowed to cross submodule boundaries. This fixes #1371. Signed-off-by: Brandon Williams <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9560e62 commit eef3df5

File tree

4 files changed

+24
-2
lines changed

4 files changed

+24
-2
lines changed

builtin/grep.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,6 +1015,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
10151015
prefix, argv + i);
10161016
pathspec.max_depth = opt.max_depth;
10171017
pathspec.recursive = 1;
1018+
pathspec.recurse_submodules = !!recurse_submodules;
10181019

10191020
#ifndef NO_PTHREADS
10201021
if (list.nr || cached || show_in_pager)

pathspec.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ struct pathspec {
2424
int nr;
2525
unsigned int has_wildcard:1;
2626
unsigned int recursive:1;
27+
unsigned int recurse_submodules:1;
2728
unsigned magic;
2829
int max_depth;
2930
struct pathspec_item {

t/t4208-log-magic-pathspec.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,23 @@ test_expect_success 'command line pathspec parsing for "git log"' '
9393
git log --merge -- a
9494
'
9595

96+
test_expect_success 'tree_entry_interesting does not match past submodule boundaries' '
97+
test_when_finished "rm -rf repo submodule" &&
98+
git init submodule &&
99+
test_commit -C submodule initial &&
100+
git init repo &&
101+
>"repo/[bracket]" &&
102+
git -C repo add "[bracket]" &&
103+
test_tick &&
104+
git -C repo commit -m bracket &&
105+
git -C repo rev-list HEAD -- "[bracket]" >expect &&
106+
107+
git -C repo submodule add ../submodule &&
108+
test_tick &&
109+
git -C repo commit -m submodule &&
110+
111+
git -C repo rev-list HEAD -- "[bracket]" >actual &&
112+
test_cmp expect actual
113+
'
114+
96115
test_done

tree-walk.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,7 +1011,8 @@ static enum interesting do_match(const struct name_entry *entry,
10111011
* character. More accurate matching can then
10121012
* be performed in the submodule itself.
10131013
*/
1014-
if (ps->recursive && S_ISGITLINK(entry->mode) &&
1014+
if (ps->recurse_submodules &&
1015+
S_ISGITLINK(entry->mode) &&
10151016
!ps_strncmp(item, match + baselen,
10161017
entry->path,
10171018
item->nowildcard_len - baselen))
@@ -1060,7 +1061,7 @@ static enum interesting do_match(const struct name_entry *entry,
10601061
* character. More accurate matching can then
10611062
* be performed in the submodule itself.
10621063
*/
1063-
if (ps->recursive && S_ISGITLINK(entry->mode) &&
1064+
if (ps->recurse_submodules && S_ISGITLINK(entry->mode) &&
10641065
!ps_strncmp(item, match, base->buf + base_offset,
10651066
item->nowildcard_len)) {
10661067
strbuf_setlen(base, base_offset + baselen);

0 commit comments

Comments
 (0)