dir: ignore trailing spaces in exclude patterns

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Feb 9, 2014 at 07:26 UTC 7e2e4b37d3d57a95a525ba1a18224ba04f858768
3 files changed +19 -13
Documentation/gitignore.txt
+3
@@ -77,6 +77,9 @@ PATTERN FORMAT
77 Put a backslash ("`\`") in front of the first hash for patterns
78 that begin with a hash.
79
80 + - Trailing spaces are ignored unless they are quoted with backlash
81 + ("`\`").
82 +
83 - An optional prefix "`!`" which negates the pattern; any
84 matching file excluded by a previous pattern will become
85 included again. If a negated pattern matches, this will
dir.c
+12 -9
@@ -463,20 +463,23 @@ void clear_exclude_list(struct exclude_list *el)
463 el->filebuf = NULL;
464 }
465
466 -static void check_trailing_spaces(const char *fname, char *buf)
466 +static void trim_trailing_spaces(char *buf)
467 {
468 - int i, last_space = -1, len = strlen(buf);
468 + int i, last_space = -1, nr_spaces, 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
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
479 last_space = -1;
480
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';
483 }
484
485 int add_excludes_from_file_to_list(const char *fname,
@@ -530,7 +533,7 @@ int add_excludes_from_file_to_list(const char *fname,
533 if (buf[i] == '\n') {
534 if (entry != buf + i && entry[0] != '#') {
535 buf[i - (i && buf[i-1] == '\r')] = 0;
533 - check_trailing_spaces(fname, entry);
536 + trim_trailing_spaces(entry);
537 add_exclude(entry, base, baselen, el, lineno);
538 }
539 lineno++;
t/t0008-ignores.sh
+4 -4
@@ -779,18 +779,18 @@ test_expect_success PIPE 'streaming support for --stdin' '
779 #
780 # test whitespace handling
781
782 -test_expect_success 'trailing whitespace is warned' '
782 +test_expect_success 'trailing whitespace is ignored' '
783 mkdir whitespace &&
784 >whitespace/trailing &&
785 >whitespace/untracked &&
786 echo "whitespace/trailing " >ignore &&
787 cat >expect <<EOF &&
788 -whitespace/trailing
788 whitespace/untracked
789 EOF
790 + : >err.expect &&
791 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
794 '
795
796 test_expect_success 'quoting allows trailing whitespace' '