pathspec: only match across submodule boundaries when requested

Commit 74ed43711fd (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 74ed43711fd 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 https://github.com/git-for-windows/git/issues/1371. Signed-off-by: Brandon Williams <bmwill@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Brandon Williams committed Dec 4, 2017 at 16:07 UTC eef3df5a93784e4d709907ce03006374ffc3ea26
4 files changed +24 -2
builtin/grep.c
+1
@@ -1015,6 +1015,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
1015 prefix, argv + i);
1016 pathspec.max_depth = opt.max_depth;
1017 pathspec.recursive = 1;
1018 + pathspec.recurse_submodules = !!recurse_submodules;
1019
1020 #ifndef NO_PTHREADS
1021 if (list.nr || cached || show_in_pager)
pathspec.h
+1
@@ -24,6 +24,7 @@ struct pathspec {
24 int nr;
25 unsigned int has_wildcard:1;
26 unsigned int recursive:1;
27 + unsigned int recurse_submodules:1;
28 unsigned magic;
29 int max_depth;
30 struct pathspec_item {
t/t4208-log-magic-pathspec.sh
+19
@@ -93,4 +93,23 @@ test_expect_success 'command line pathspec parsing for "git log"' '
93 git log --merge -- a
94 '
95
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 +
115 test_done
tree-walk.c
+3 -2
@@ -1011,7 +1011,8 @@ static enum interesting do_match(const struct name_entry *entry,
1011 * character. More accurate matching can then
1012 * be performed in the submodule itself.
1013 */
1014 - if (ps->recursive && S_ISGITLINK(entry->mode) &&
1014 + if (ps->recurse_submodules &&
1015 + S_ISGITLINK(entry->mode) &&
1016 !ps_strncmp(item, match + baselen,
1017 entry->path,
1018 item->nowildcard_len - baselen))
@@ -1060,7 +1061,7 @@ match_wildcards:
1061 * character. More accurate matching can then
1062 * be performed in the submodule itself.
1063 */
1063 - if (ps->recursive && S_ISGITLINK(entry->mode) &&
1064 + if (ps->recurse_submodules && S_ISGITLINK(entry->mode) &&
1065 !ps_strncmp(item, match, base->buf + base_offset,
1066 item->nowildcard_len)) {
1067 strbuf_setlen(base, base_offset + baselen);