Skip to content

Commit cbf4e02

Browse files
committed
Merge branch 'pb/trim-trailing-spaces' into maint
* pb/trim-trailing-spaces: t0008: do not depend on 'echo' handling backslashes specially dir.c:trim_trailing_spaces(): fix for " \ " sequence
2 parents f35392b + 97c1364 commit cbf4e02

File tree

2 files changed

+44
-15
lines changed

2 files changed

+44
-15
lines changed

dir.c

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -508,21 +508,25 @@ void clear_exclude_list(struct exclude_list *el)
508508

509509
static void trim_trailing_spaces(char *buf)
510510
{
511-
int i, last_space = -1, nr_spaces, len = strlen(buf);
512-
for (i = 0; i < len; i++)
513-
if (buf[i] == '\\')
514-
i++;
515-
else if (buf[i] == ' ') {
516-
if (last_space == -1) {
517-
last_space = i;
518-
nr_spaces = 1;
519-
} else
520-
nr_spaces++;
521-
} else
522-
last_space = -1;
523-
524-
if (last_space != -1 && last_space + nr_spaces == len)
525-
buf[last_space] = '\0';
511+
char *p, *last_space = NULL;
512+
513+
for (p = buf; *p; p++)
514+
switch (*p) {
515+
case ' ':
516+
if (!last_space)
517+
last_space = p;
518+
break;
519+
case '\\':
520+
p++;
521+
if (!*p)
522+
return;
523+
/* fallthrough */
524+
default:
525+
last_space = NULL;
526+
}
527+
528+
if (last_space)
529+
*last_space = '\0';
526530
}
527531

528532
int add_excludes_from_file_to_list(const char *fname,

t/t0008-ignores.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -806,4 +806,29 @@ test_expect_success !MINGW 'quoting allows trailing whitespace' '
806806
test_cmp err.expect err
807807
'
808808

809+
test_expect_success NOT_MINGW,NOT_CYGWIN 'correct handling of backslashes' '
810+
rm -rf whitespace &&
811+
mkdir whitespace &&
812+
>"whitespace/trailing 1 " &&
813+
>"whitespace/trailing 2 \\\\" &&
814+
>"whitespace/trailing 3 \\\\" &&
815+
>"whitespace/trailing 4 \\ " &&
816+
>"whitespace/trailing 5 \\ \\ " &&
817+
>"whitespace/trailing 6 \\a\\" &&
818+
>whitespace/untracked &&
819+
sed -e "s/Z$//" >ignore <<-\EOF &&
820+
whitespace/trailing 1 \ Z
821+
whitespace/trailing 2 \\\\Z
822+
whitespace/trailing 3 \\\\ Z
823+
whitespace/trailing 4 \\\ Z
824+
whitespace/trailing 5 \\ \\\ Z
825+
whitespace/trailing 6 \\a\\Z
826+
EOF
827+
echo whitespace/untracked >expect &&
828+
>err.expect &&
829+
git ls-files -o -X ignore whitespace >actual 2>err &&
830+
test_cmp expect actual &&
831+
test_cmp err.expect err
832+
'
833+
809834
test_done

0 commit comments

Comments
 (0)