Skip to content

Commit 16402b9

Browse files
pcloudsgitster
authored andcommitted
dir: warn about 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 3330a2c commit 16402b9

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

dir.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,22 @@ 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)
467+
{
468+
int i, last_space = -1, len = strlen(buf);
469+
for (i = 0; i < len; i++)
470+
if (buf[i] == '\\')
471+
i++;
472+
else if (buf[i] == ' ')
473+
last_space = i;
474+
else
475+
last_space = -1;
476+
477+
if (last_space == len - 1)
478+
warning(_("%s: trailing spaces in '%s'. Please quote or remove them."),
479+
fname, buf);
480+
}
481+
466482
int add_excludes_from_file_to_list(const char *fname,
467483
const char *base,
468484
int baselen,
@@ -514,6 +530,7 @@ int add_excludes_from_file_to_list(const char *fname,
514530
if (buf[i] == '\n') {
515531
if (entry != buf + i && entry[0] != '#') {
516532
buf[i - (i && buf[i-1] == '\r')] = 0;
533+
check_trailing_spaces(fname, entry);
517534
add_exclude(entry, base, baselen, el, lineno);
518535
}
519536
lineno++;

t/t0008-ignores.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -775,4 +775,35 @@ test_expect_success PIPE 'streaming support for --stdin' '
775775
echo "$response" | grep "^:: two"
776776
'
777777

778+
############################################################################
779+
#
780+
# test whitespace handling
781+
782+
test_expect_success 'trailing whitespace is warned' '
783+
mkdir whitespace &&
784+
>whitespace/trailing &&
785+
>whitespace/untracked &&
786+
echo "whitespace/trailing " >ignore &&
787+
cat >expect <<EOF &&
788+
whitespace/trailing
789+
whitespace/untracked
790+
EOF
791+
git ls-files -o -X ignore whitespace >actual 2>err &&
792+
grep "ignore:.*'\''whitespace/trailing '\''" err &&
793+
test_cmp expect actual
794+
'
795+
796+
test_expect_success 'quoting allows trailing whitespace' '
797+
rm -rf whitespace &&
798+
mkdir whitespace &&
799+
>"whitespace/trailing " &&
800+
>whitespace/untracked &&
801+
echo "whitespace/trailing\\ \\ " >ignore &&
802+
echo whitespace/untracked >expect &&
803+
: >err.expect &&
804+
git ls-files -o -X ignore whitespace >actual 2>err &&
805+
test_cmp expect actual &&
806+
test_cmp err.expect err
807+
'
808+
778809
test_done

0 commit comments

Comments
 (0)