dir: warn about 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 16402b992e0332d2ac68106f4488b47175bf0a13
2 files changed +48
dir.c
+17
@@ -463,6 +463,22 @@ 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)
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 +
482 int add_excludes_from_file_to_list(const char *fname,
483 const char *base,
484 int baselen,
@@ -514,6 +530,7 @@ int add_excludes_from_file_to_list(const char *fname,
530 if (buf[i] == '\n') {
531 if (entry != buf + i && entry[0] != '#') {
532 buf[i - (i && buf[i-1] == '\r')] = 0;
533 + check_trailing_spaces(fname, entry);
534 add_exclude(entry, base, baselen, el, lineno);
535 }
536 lineno++;
t/t0008-ignores.sh
+31
@@ -775,4 +775,35 @@ test_expect_success PIPE 'streaming support for --stdin' '
775 echo "$response" | grep "^:: two"
776 '
777
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 +
809 test_done