dir: warn about trailing spaces in exclude patterns
authorNguyễn Thái Ngọc Duy <pclouds@gmail.com>
Sun, 9 Feb 2014 00:26:37 +0000 (07:26 +0700)
committerJunio C Hamano <gitster@pobox.com>
Mon, 10 Feb 2014 19:49:53 +0000 (11:49 -0800)
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
dir.c
t/t0008-ignores.sh
diff --git a/dir.c b/dir.c
index 23b6de47036839ca4297a9b93d1a9794547ccff1..065491bb4bc5d4c16a6259b594ed18643d4d3ee7 100644 (file)
--- a/dir.c
+++ b/dir.c
@@ -463,6 +463,22 @@ void clear_exclude_list(struct exclude_list *el)
        el->filebuf = NULL;
 }
 
+static void check_trailing_spaces(const char *fname, char *buf)
+{
+       int i, last_space = -1, len = strlen(buf);
+       for (i = 0; i < len; i++)
+               if (buf[i] == '\\')
+                       i++;
+               else if (buf[i] == ' ')
+                       last_space = i;
+               else
+                       last_space = -1;
+
+       if (last_space == len - 1)
+               warning(_("%s: trailing spaces in '%s'. Please quote or remove them."),
+                       fname, buf);
+}
+
 int add_excludes_from_file_to_list(const char *fname,
                                   const char *base,
                                   int baselen,
@@ -514,6 +530,7 @@ int add_excludes_from_file_to_list(const char *fname,
                if (buf[i] == '\n') {
                        if (entry != buf + i && entry[0] != '#') {
                                buf[i - (i && buf[i-1] == '\r')] = 0;
+                               check_trailing_spaces(fname, entry);
                                add_exclude(entry, base, baselen, el, lineno);
                        }
                        lineno++;
index b4d98e602f7422009599e58f0a7ae943476b1c7e..9e1d64c6ac5ba2d652a7657aca4411da6d5a08aa 100755 (executable)
@@ -775,4 +775,35 @@ test_expect_success PIPE 'streaming support for --stdin' '
        echo "$response" | grep "^::    two"
 '
 
+############################################################################
+#
+# test whitespace handling
+
+test_expect_success 'trailing whitespace is warned' '
+       mkdir whitespace &&
+       >whitespace/trailing &&
+       >whitespace/untracked &&
+       echo "whitespace/trailing   " >ignore &&
+       cat >expect <<EOF &&
+whitespace/trailing
+whitespace/untracked
+EOF
+       git ls-files -o -X ignore whitespace >actual 2>err &&
+       grep "ignore:.*'\''whitespace/trailing   '\''" err &&
+       test_cmp expect actual
+'
+
+test_expect_success 'quoting allows trailing whitespace' '
+       rm -rf whitespace &&
+       mkdir whitespace &&
+       >"whitespace/trailing  " &&
+       >whitespace/untracked &&
+       echo "whitespace/trailing\\ \\ " >ignore &&
+       echo whitespace/untracked >expect &&
+       : >err.expect &&
+       git ls-files -o -X ignore whitespace >actual 2>err &&
+       test_cmp expect actual &&
+       test_cmp err.expect err
+'
+
 test_done