Merge branch 'nd/gitignore-trailing-whitespace'
authorJunio C Hamano <gitster@pobox.com>
Fri, 14 Mar 2014 21:23:37 +0000 (14:23 -0700)
committerJunio C Hamano <gitster@pobox.com>
Fri, 14 Mar 2014 21:23:37 +0000 (14:23 -0700)
Trailing whitespaces in .gitignore files, unless they are quoted for
fnmatch(3), e.g. "path\ ", are warned and ignored.

Strictly speaking, this is a backward incompatible change, but very
unlikely to bite any sane user and adjusting should be obvious and
easy.

* nd/gitignore-trailing-whitespace:
t0008: skip trailing space test on Windows
dir: ignore trailing spaces in exclude patterns
dir: warn about trailing spaces in exclude patterns

Documentation/gitignore.txt
dir.c
t/t0008-ignores.sh
index b08d34d84ec92eec6aa54572e3823062424c23bf..8734c1566ca918e8c62f759aebdf7d0b497e860b 100644 (file)
@@ -77,6 +77,9 @@ PATTERN FORMAT
    Put a backslash ("`\`") in front of the first hash for patterns
    that begin with a hash.
 
+ - Trailing spaces are ignored unless they are quoted with backlash
+   ("`\`").
+
  - An optional prefix "`!`" which negates the pattern; any
    matching file excluded by a previous pattern will become
    included again. It is not possible to re-include a file if a parent
diff --git a/dir.c b/dir.c
index 98bb50fbabb69d25443df8ca4d29e11dea746a60..6c6a5d13c2e65b02bfd9e3201c35b3a9f270d093 100644 (file)
--- a/dir.c
+++ b/dir.c
@@ -503,6 +503,25 @@ void clear_exclude_list(struct exclude_list *el)
        el->filebuf = NULL;
 }
 
+static void trim_trailing_spaces(char *buf)
+{
+       int i, last_space = -1, nr_spaces, len = strlen(buf);
+       for (i = 0; i < len; i++)
+               if (buf[i] == '\\')
+                       i++;
+               else if (buf[i] == ' ') {
+                       if (last_space == -1) {
+                               last_space = i;
+                               nr_spaces = 1;
+                       } else
+                               nr_spaces++;
+               } else
+                       last_space = -1;
+
+       if (last_space != -1 && last_space + nr_spaces == len)
+               buf[last_space] = '\0';
+}
+
 int add_excludes_from_file_to_list(const char *fname,
                                   const char *base,
                                   int baselen,
@@ -554,6 +573,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;
+                               trim_trailing_spaces(entry);
                                add_exclude(entry, base, baselen, el, lineno);
                        }
                        lineno++;
index b4d98e602f7422009599e58f0a7ae943476b1c7e..63beb99828cc17d2ec8218a6470b62c02a78806b 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 ignored' '
+       mkdir whitespace &&
+       >whitespace/trailing &&
+       >whitespace/untracked &&
+       echo "whitespace/trailing   " >ignore &&
+       cat >expect <<EOF &&
+whitespace/untracked
+EOF
+       : >err.expect &&
+       git ls-files -o -X ignore whitespace >actual 2>err &&
+       test_cmp expect actual &&
+       test_cmp err.expect err
+'
+
+test_expect_success !MINGW '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