From 16402b992e0332d2ac68106f4488b47175bf0a13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Sun, 9 Feb 2014 07:26:37 +0700 Subject: [PATCH 1/3] dir: warn about trailing spaces in exclude patterns MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- dir.c | 17 +++++++++++++++++ t/t0008-ignores.sh | 31 +++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/dir.c b/dir.c index 23b6de4703..065491bb4b 100644 --- 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++; diff --git a/t/t0008-ignores.sh b/t/t0008-ignores.sh index b4d98e602f..9e1d64c6ac 100755 --- a/t/t0008-ignores.sh +++ b/t/t0008-ignores.sh @@ -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 <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 From 7e2e4b37d3d57a95a525ba1a18224ba04f858768 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Sun, 9 Feb 2014 07:26:38 +0700 Subject: [PATCH 2/3] dir: ignore trailing spaces in exclude patterns MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- Documentation/gitignore.txt | 3 +++ dir.c | 21 ++++++++++++--------- t/t0008-ignores.sh | 8 ++++---- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/Documentation/gitignore.txt b/Documentation/gitignore.txt index f971960512..aa776234e1 100644 --- a/Documentation/gitignore.txt +++ b/Documentation/gitignore.txt @@ -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. If a negated pattern matches, this will diff --git a/dir.c b/dir.c index 065491bb4b..f6743b36cd 100644 --- a/dir.c +++ b/dir.c @@ -463,20 +463,23 @@ void clear_exclude_list(struct exclude_list *el) el->filebuf = NULL; } -static void check_trailing_spaces(const char *fname, char *buf) +static void trim_trailing_spaces(char *buf) { - int i, last_space = -1, len = strlen(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] == ' ') - last_space = i; - else + else if (buf[i] == ' ') { + if (last_space == -1) { + last_space = i; + nr_spaces = 1; + } else + nr_spaces++; + } else last_space = -1; - if (last_space == len - 1) - warning(_("%s: trailing spaces in '%s'. Please quote or remove them."), - fname, buf); + if (last_space != -1 && last_space + nr_spaces == len) + buf[last_space] = '\0'; } int add_excludes_from_file_to_list(const char *fname, @@ -530,7 +533,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); + trim_trailing_spaces(entry); add_exclude(entry, base, baselen, el, lineno); } lineno++; diff --git a/t/t0008-ignores.sh b/t/t0008-ignores.sh index 9e1d64c6ac..bbaf6b5e9e 100755 --- a/t/t0008-ignores.sh +++ b/t/t0008-ignores.sh @@ -779,18 +779,18 @@ test_expect_success PIPE 'streaming support for --stdin' ' # # test whitespace handling -test_expect_success 'trailing whitespace is warned' ' +test_expect_success 'trailing whitespace is ignored' ' mkdir whitespace && >whitespace/trailing && >whitespace/untracked && echo "whitespace/trailing " >ignore && cat >expect <err.expect && git ls-files -o -X ignore whitespace >actual 2>err && - grep "ignore:.*'\''whitespace/trailing '\''" err && - test_cmp expect actual + test_cmp expect actual && + test_cmp err.expect err ' test_expect_success 'quoting allows trailing whitespace' ' From 35e4d775875603c748a127c82909f5d62a410ac8 Mon Sep 17 00:00:00 2001 From: Johannes Sixt Date: Tue, 11 Mar 2014 08:46:40 +0100 Subject: [PATCH 3/3] t0008: skip trailing space test on Windows The Windows API does not preserve file names with trailing spaces (and dots), but rather strips them. Our tools (MSYS bash, git) base the POSIX emulation on the Windows API. As a consequence, it is impossible for bash on Windows to allocate a file whose name has trailing spaces, and for git to stat such a file. Both operate on a file whose name has the spaces stripped. Skip the test that needs such a file name. Note that we do not use (another incarnation of) prerequisite FUNNYNAMES. The reason is that FUNNYNAMES is intended to represent a property of the file system. But the inability to have trailing spaces in a file name is a property of the Windows API. The file system (NTFS) does not have this limitation. Signed-off-by: Johannes Sixt Signed-off-by: Junio C Hamano --- t/t0008-ignores.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/t/t0008-ignores.sh b/t/t0008-ignores.sh index bbaf6b5e9e..63beb99828 100755 --- a/t/t0008-ignores.sh +++ b/t/t0008-ignores.sh @@ -793,7 +793,7 @@ EOF test_cmp err.expect err ' -test_expect_success 'quoting allows trailing whitespace' ' +test_expect_success !MINGW 'quoting allows trailing whitespace' ' rm -rf whitespace && mkdir whitespace && >"whitespace/trailing " &&