Skip to content

Commit 7e2e4b3

Browse files
pcloudsgitster
authored andcommitted
dir: ignore trailing spaces in exclude patterns
Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 16402b9 commit 7e2e4b3

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

Documentation/gitignore.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ PATTERN FORMAT
7777
Put a backslash ("`\`") in front of the first hash for patterns
7878
that begin with a hash.
7979

80+
- Trailing spaces are ignored unless they are quoted with backlash
81+
("`\`").
82+
8083
- An optional prefix "`!`" which negates the pattern; any
8184
matching file excluded by a previous pattern will become
8285
included again. If a negated pattern matches, this will

dir.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -463,20 +463,23 @@ void clear_exclude_list(struct exclude_list *el)
463463
el->filebuf = NULL;
464464
}
465465

466-
static void check_trailing_spaces(const char *fname, char *buf)
466+
static void trim_trailing_spaces(char *buf)
467467
{
468-
int i, last_space = -1, len = strlen(buf);
468+
int i, last_space = -1, nr_spaces, len = strlen(buf);
469469
for (i = 0; i < len; i++)
470470
if (buf[i] == '\\')
471471
i++;
472-
else if (buf[i] == ' ')
473-
last_space = i;
474-
else
472+
else if (buf[i] == ' ') {
473+
if (last_space == -1) {
474+
last_space = i;
475+
nr_spaces = 1;
476+
} else
477+
nr_spaces++;
478+
} else
475479
last_space = -1;
476480

477-
if (last_space == len - 1)
478-
warning(_("%s: trailing spaces in '%s'. Please quote or remove them."),
479-
fname, buf);
481+
if (last_space != -1 && last_space + nr_spaces == len)
482+
buf[last_space] = '\0';
480483
}
481484

482485
int add_excludes_from_file_to_list(const char *fname,
@@ -530,7 +533,7 @@ int add_excludes_from_file_to_list(const char *fname,
530533
if (buf[i] == '\n') {
531534
if (entry != buf + i && entry[0] != '#') {
532535
buf[i - (i && buf[i-1] == '\r')] = 0;
533-
check_trailing_spaces(fname, entry);
536+
trim_trailing_spaces(entry);
534537
add_exclude(entry, base, baselen, el, lineno);
535538
}
536539
lineno++;

t/t0008-ignores.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -779,18 +779,18 @@ test_expect_success PIPE 'streaming support for --stdin' '
779779
#
780780
# test whitespace handling
781781

782-
test_expect_success 'trailing whitespace is warned' '
782+
test_expect_success 'trailing whitespace is ignored' '
783783
mkdir whitespace &&
784784
>whitespace/trailing &&
785785
>whitespace/untracked &&
786786
echo "whitespace/trailing " >ignore &&
787787
cat >expect <<EOF &&
788-
whitespace/trailing
789788
whitespace/untracked
790789
EOF
790+
: >err.expect &&
791791
git ls-files -o -X ignore whitespace >actual 2>err &&
792-
grep "ignore:.*'\''whitespace/trailing '\''" err &&
793-
test_cmp expect actual
792+
test_cmp expect actual &&
793+
test_cmp err.expect err
794794
'
795795

796796
test_expect_success 'quoting allows trailing whitespace' '

0 commit comments

Comments
 (0)