add_patterns_from_buffer() skips an empty line which starts right at the '\n', before it strips the '\r' or any trailing spaces. A two byte "\r\n" line in a CRLF file, or a line that holds only spaces (with or without the '\r') is not empty at that point. Stripping the '\r' and calling trim_trailing_spaces() reduces it to an empty string, which is then passed to add_pattern(). parse_path_pattern() turns that empty string into a 0 length pattern with PATTERN_FLAG_NODIR. The directory walk checks a directory before it appends the trailing slash. Thus, status and a plain add are not affected. A path argument that ends in a slash is a different scenario. For eg, with "git check-ignore foo/", the basename is empty and match_basename() matches the 0 length pattern and "check-ignore -v" names that line as the reason foo/ is ignored. Cone-mode sparse-checkout is affected as well. For such a line, add_pattern_to_hashsets() sees a pattern shorter than 2 characters, warns "unrecognized pattern: ''" and disables cone pattern matching. Skip calling add_pattern() when the trimmed line is empty. This follows gitignore(5) which says that trailing spaces are ignored unless quoted with backslash. A file name made of spaces can still be matched by quoting the spaces. Reported-by: Mike Beaton <mjsb…@gmail.com> Signed-off-by: Aniket Gupta <anik…@gmail.com> --- Changes in v3: - Say "path argument" instead of "path" and drop "like before" in the log message (Matt). dir.c | 3 ++- t/t0008-ignores.sh | 7 +++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/dir.c b/dir.c index d896e7be4b..5a8a28afef 100644 --- a/dir.c +++ b/dir.c @@ -1249,7 +1249,8 @@ int add_patterns_from_buffer(char *buf, size_t size, if (entry != buf + i && entry[0] != '#') { buf[i - (i && buf[i-1] == '\r')] = 0; trim_trailing_spaces(entry); - add_pattern(entry, base, baselen, pl, lineno); + if (*entry) + add_pattern(entry, base, baselen, pl, lineno); } lineno++; entry = buf + i + 1; diff --git a/t/t0008-ignores.sh b/t/t0008-ignores.sh index ed95faf327..9884f67650 100755 --- a/t/t0008-ignores.sh +++ b/t/t0008-ignores.sh @@ -875,6 +875,13 @@ EOF test_must_be_empty err ' +test_expect_success 'trailing-slash directory is not ignored by CRLF-empty or space-only lines' ' + printf "\r\n \n \r\n" >empty-lines && + test_expect_code 1 git -c core.excludesfile=empty-lines \ + check-ignore dir/ >actual && + test_must_be_empty actual +' + test_expect_success !MINGW 'quoting allows trailing whitespace' ' rm -rf whitespace && mkdir whitespace && base-commit: 03b41523c576f26ad34085378717a0c1b287470f
-- 2.54.0 (Apple Git-157)