Skip to content

Commit 97660b2

Browse files
committed
dir.c: do not be fooled by :(exclude) pathspec elements
When exclude_matches_pathspec() tries to determine if an otherwise excluded item matches the pathspec given, it goes through each pathspec element and declares a hit, without checking if the element is a negative ":(exclude)" element. Fix it be applying the usual "a path matches if it matches any one of positive pathspec element, and if it matches none of negative pathspec elements" rule in the function. Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent a1cf0cf commit 97660b2

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

dir.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2219,6 +2219,8 @@ static int exclude_matches_pathspec(const char *path, int pathlen,
22192219
const struct pathspec *pathspec)
22202220
{
22212221
int i;
2222+
int matches_exclude_magic = 0;
2223+
int matches_pathspec_elem = 0;
22222224

22232225
if (!pathspec || !pathspec->nr)
22242226
return 0;
@@ -2235,15 +2237,23 @@ static int exclude_matches_pathspec(const char *path, int pathlen,
22352237
for (i = 0; i < pathspec->nr; i++) {
22362238
const struct pathspec_item *item = &pathspec->items[i];
22372239
int len = item->nowildcard_len;
2240+
int *matches;
2241+
2242+
if (item->magic & PATHSPEC_EXCLUDE)
2243+
matches = &matches_exclude_magic;
2244+
else
2245+
matches = &matches_pathspec_elem;
22382246

22392247
if (len == pathlen &&
22402248
!ps_strncmp(item, item->match, path, pathlen))
2241-
return 1;
2249+
*matches = 1;
22422250
if (len > pathlen &&
22432251
item->match[pathlen] == '/' &&
22442252
!ps_strncmp(item, item->match, path, pathlen))
2245-
return 1;
2253+
*matches = 1;
22462254
}
2255+
if (matches_pathspec_elem && !matches_exclude_magic)
2256+
return 1;
22472257
return 0;
22482258
}
22492259

t/t2204-add-ignored.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,21 @@ do
8989
'
9090
done
9191

92+
test_expect_success "exclude magic would not interfere with .gitignore" '
93+
test_write_lines dir file sub ign err out "*.o" >.gitignore &&
94+
>foo.o &&
95+
>foo.c &&
96+
test_must_fail git add foo.o 2>err &&
97+
test_grep "are ignored by one" err &&
98+
test_grep "hint: Use -f" err &&
99+
100+
git add ":(exclude)foo.o" &&
101+
git ls-files >actual &&
102+
cat >expect <<-\EOF &&
103+
.gitignore
104+
foo.c
105+
EOF
106+
test_cmp expect actual
107+
'
108+
92109
test_done

0 commit comments

Comments
 (0)