Skip to content

Commit b7845ce

Browse files
pcloudsgitster
authored andcommitted
tree-walk.c: fix overoptimistic inclusion in :(exclude) matching
tree_entry_interesting() is used for matching pathspec on a tree. The interesting thing about this function is that, because the tree entries are known to be sorted, this function can return more than just "yes, matched" and "no, not matched". It can also say "yes, this entry is matched and so is the remaining entries in the tree". This is where I made a mistake when matching exclude pathspec. For exclude pathspec, we do matching twice, one with positive patterns and one with negative ones, then a rule table is applied to determine the final "include or exclude" result. Note that "matched" does not necessarily mean include. For negative patterns, "matched" means exclude. This particular rule is too eager to include everything. Rule 8 says that "if all entries are positively matched" and the current entry is not negatively matched (i.e. not excluded), then all entries are positively matched and therefore included. But this is not true. If the _current_ entry is not negatively matched, it does not mean the next one will not be and we cannot conclude right away that all remaining entries are positively matched and can be included. Rules 8 and 18 are now updated to be less eager. We conclude that the current entry is positively matched and included. But we say nothing about remaining entries. tree_entry_interesting() will be called again for those entries where we will determine entries individually. Reported-by: Christophe Bliard <[email protected]> Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 268fbcd commit b7845ce

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

t/t6132-pathspec-exclude.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,4 +194,21 @@ test_expect_success 'multiple exclusions' '
194194
test_cmp expect actual
195195
'
196196

197+
test_expect_success 't_e_i() exclude case #8' '
198+
git init case8 &&
199+
(
200+
cd case8 &&
201+
echo file >file1 &&
202+
echo file >file2 &&
203+
git add file1 file2 &&
204+
git commit -m twofiles &&
205+
git grep -l file HEAD :^file2 >actual &&
206+
echo HEAD:file1 >expected &&
207+
test_cmp expected actual &&
208+
git grep -l file HEAD :^file1 >actual &&
209+
echo HEAD:file2 >expected &&
210+
test_cmp expected actual
211+
)
212+
'
213+
197214
test_done

tree-walk.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,7 +1105,7 @@ enum interesting tree_entry_interesting(const struct name_entry *entry,
11051105
* 5 | file | 1 | 1 | 0
11061106
* 6 | file | 1 | 2 | 0
11071107
* 7 | file | 2 | -1 | 2
1108-
* 8 | file | 2 | 0 | 2
1108+
* 8 | file | 2 | 0 | 1
11091109
* 9 | file | 2 | 1 | 0
11101110
* 10 | file | 2 | 2 | -1
11111111
* -----+-------+----------+----------+-------
@@ -1116,7 +1116,7 @@ enum interesting tree_entry_interesting(const struct name_entry *entry,
11161116
* 15 | dir | 1 | 1 | 1 (*)
11171117
* 16 | dir | 1 | 2 | 0
11181118
* 17 | dir | 2 | -1 | 2
1119-
* 18 | dir | 2 | 0 | 2
1119+
* 18 | dir | 2 | 0 | 1
11201120
* 19 | dir | 2 | 1 | 1 (*)
11211121
* 20 | dir | 2 | 2 | -1
11221122
*
@@ -1132,7 +1132,12 @@ enum interesting tree_entry_interesting(const struct name_entry *entry,
11321132

11331133
negative = do_match(entry, base, base_offset, ps, 1);
11341134

1135-
/* #3, #4, #7, #8, #13, #14, #17, #18 */
1135+
/* #8, #18 */
1136+
if (positive == all_entries_interesting &&
1137+
negative == entry_not_interesting)
1138+
return entry_interesting;
1139+
1140+
/* #3, #4, #7, #13, #14, #17 */
11361141
if (negative <= entry_not_interesting)
11371142
return positive;
11381143

0 commit comments

Comments
 (0)