* [BUG] check-ignore matches trailing-slash paths against CRLF blank lines
@ 2026-10-28 12:49 Mike Beaton
2026-11-06 18:29 ` [PATCH] dir: do not turn blank ignore-file lines into patterns Aniket Gupta
0 siblings, 1 reply; 17+ messages in thread
From: Mike Beaton @ 2026-10-28 12:49 UTC (permalink / raw)
To: git
## Summary
With a .gitignore containing a single blank CRLF line, `git check-ignore -v`
reports paths ending in `/` as ignored by an empty pattern. Replacing that
blank line with an LF-only blank line removes the match.
This occurs with both ordinary names and dot-prefixed names. In the tests
below, real files inside those directories remain visible to `git status`
and eligible for `git add`. The observed issue is therefore an incorrect
ignore result for the trailing-slash path spelling, rather than demonstrated
loss of files from normal status or staging operations.
## Versions and environment
Reproduced on Linux using Git 2.56.0 built from the official source tarball,
and also using Git 2.51.1.
The 2.56.0 build used:
```sh
make -j8 NO_RUST=YesPlease NO_GETTEXT=YesPlease NO_TCLTK=YesPlease \
NO_CURL=YesPlease NO_EXPAT=YesPlease git
```
The 2.56.0 tests disabled system and global Git configuration using
`GIT_CONFIG_NOSYSTEM=1` and `GIT_CONFIG_GLOBAL=/dev/null`.
Originally observed independently in a WSL working tree under /mnt/c.
## Minimal reproduction
Run in a shell with the Git executable to be tested on PATH:
```sh
repro_dir=$(mktemp -d)
cd "$repro_dir" || exit 1
export GIT_CONFIG_NOSYSTEM=1
export GIT_CONFIG_GLOBAL=/dev/null
git --version
git init -q --template=
# Exactly one blank CRLF line; no ignore patterns.
printf '\r\n' > .gitignore
git check-ignore -v foo/ .foo/
printf 'exit status: %s\n' "$?"
# Same paths without trailing slashes.
git check-ignore -v foo .foo
printf 'exit status: %s\n' "$?"
# Replace CRLF with LF and repeat the first check.
printf '\n' > .gitignore
git check-ignore -v foo/ .foo/
printf 'exit status: %s\n' "$?"
```
## Actual result
With CRLF, the trailing-slash check reports the following (there is a tab
between the final colon and each pathname):
```text
.gitignore:1: foo/
.gitignore:1: .foo/
exit status: 0
```
Without trailing slashes, it prints no matching patterns and returns 1.
After changing the blank line to LF, the trailing-slash check also prints no
matching patterns and returns 1.
The behaviour also reproduces when `foo` and `.foo` are existing directories
containing files.
## Expected result
Neither path should match a blank line, regardless of the line-ending style
or whether the pathname has a trailing slash. Both checks should print no
matches and return 1.
The gitignore documentation states: "A blank line matches no files, so it
can serve as a separator for readability."
https://git-scm.com/docs/gitignore
## Checks with actual files
In the same temporary repository:
```sh
printf '\r\n' > .gitignore
mkdir foo .foo
printf 'hello\n' > foo/test.txt
printf 'hello\n' > .foo/test.txt
git check-ignore -v foo/ .foo/ foo .foo foo/test.txt .foo/test.txt
git status --porcelain --untracked-files=all
git add --dry-run -- foo/test.txt .foo/test.txt
```
Only `foo/` and `.foo/` receive the unexpected ignore matches.
`git status` lists both test files as untracked, and `git add --dry-run`
reports that both would be added.
## Reporting references
Git's reporting instructions direct bug reports to git@vger.kernel.org:
https://git-scm.com/community
This report concerns upstream Git, as it reproduces on Linux with an
upstream source build; it is not specific to Git for Windows.
^ permalink raw reply [flat|nested] 17+ messages in thread
* [PATCH] dir: do not turn blank ignore-file lines into patterns
2026-10-28 12:49 [BUG] check-ignore matches trailing-slash paths against CRLF blank lines Mike Beaton
@ 2026-11-06 18:29 ` Aniket Gupta
2026-11-19 17:19 ` Aniket Gupta
` (5 more replies)
0 siblings, 6 replies; 17+ messages in thread
From: Aniket Gupta @ 2026-11-06 18:29 UTC (permalink / raw)
To: git; +Cc: Junio C Hamano, Mike Beaton
A blank line in an ignore file matches nothing. But
add_patterns_from_buffer() only skips a line when it is completely
empty. A line that holds just a carriage return i.e a blank line in a
file with CRLF line endings or just spaces, is not empty at that
point. Once the carriage return and the trailing spaces are removed,
what is left is an empty pattern, which matches any path that ends in
a slash. "git check-ignore -v foo/" then names the blank line as the
pattern that ignores foo/. Skip the line when nothing is left of it
after trimming and add a test.
Reported-by: Mike Beaton <mjsbeaton@gmail.com>
Signed-off-by: Aniket Gupta <aniketgupta3001@gmail.com>
---
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..199abbcdf3 100755
--- a/t/t0008-ignores.sh
+++ b/t/t0008-ignores.sh
@@ -875,6 +875,13 @@ EOF
test_must_be_empty err
'
+test_expect_success 'blank lines are not patterns' '
+ mkdir blank-dir &&
+ printf "\r\n \n \r\n" >blank-ignore &&
+ test_expect_code 1 git -c core.excludesfile=blank-ignore check-ignore blank-dir/ >actual &&
+ test_must_be_empty actual
+'
+
test_expect_success !MINGW 'quoting allows trailing whitespace' '
rm -rf whitespace &&
mkdir whitespace &&
--
2.43.0
^ permalink raw reply related [flat|nested] 17+ messages in thread
* Re: [PATCH] dir: do not turn blank ignore-file lines into patterns
2026-11-06 18:29 ` [PATCH] dir: do not turn blank ignore-file lines into patterns Aniket Gupta
@ 2026-11-19 17:19 ` Aniket Gupta
[not found] ` <CAHzAAWQo89ox+S_jtPX9DOtzfr-+3uMbWHN+HXdi9Aw2q57+Ng@mail.gmail.com>
2026-11-27 16:21 ` Aniket Gupta
` (4 subsequent siblings)
5 siblings, 1 reply; 17+ messages in thread
From: Aniket Gupta @ 2026-11-19 17:19 UTC (permalink / raw)
To: git; +Cc: Junio C Hamano, Mike Beaton
Friendly ping. Just wanted to make sure this does not get lost.
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH] dir: do not turn blank ignore-file lines into patterns
[not found] ` <CAHzAAWQo89ox+S_jtPX9DOtzfr-+3uMbWHN+HXdi9Aw2q57+Ng@mail.gmail.com>
@ 2026-11-21 9:45 ` Aniket Gupta
0 siblings, 0 replies; 17+ messages in thread
From: Aniket Gupta @ 2026-11-21 9:45 UTC (permalink / raw)
To: Mike Beaton; +Cc: git, Junio C Hamano
> Do I need to do anything
Nope. It is still waiting for a review.
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH] dir: do not turn blank ignore-file lines into patterns
2026-11-06 18:29 ` [PATCH] dir: do not turn blank ignore-file lines into patterns Aniket Gupta
2026-11-19 17:19 ` Aniket Gupta
@ 2026-11-27 16:21 ` Aniket Gupta
2026-11-28 5:24 ` Matt Hunter
` (3 subsequent siblings)
5 siblings, 0 replies; 17+ messages in thread
From: Aniket Gupta @ 2026-11-27 16:21 UTC (permalink / raw)
To: git; +Cc: Phillip Wood, Junio C Hamano, Mike Beaton
Hi Phillip,
Adding you here since you reviewed the non UTF-8 exclude files patch in
January. This one is a small fix for blank lines in ignore files. Would
you be able to take a look?
Thanks
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH] dir: do not turn blank ignore-file lines into patterns
2026-11-06 18:29 ` [PATCH] dir: do not turn blank ignore-file lines into patterns Aniket Gupta
2026-11-19 17:19 ` Aniket Gupta
2026-11-27 16:21 ` Aniket Gupta
@ 2026-11-28 5:24 ` Matt Hunter
2026-11-28 7:28 ` Aniket Gupta
2026-11-28 10:47 ` Aniket Gupta
2026-11-28 6:54 ` Junio C Hamano
` (2 subsequent siblings)
5 siblings, 2 replies; 17+ messages in thread
From: Matt Hunter @ 2026-11-28 5:24 UTC (permalink / raw)
To: Aniket Gupta, git; +Cc: Junio C Hamano, Mike Beaton, Phillip Wood
Hi Aniket, Mike
I've been looking into this patch as well, but have been slow to
respond, sorry. Just a couple of thoughts below.
On Fri Nov 6, 2026 at 1:29 PM EST, Aniket Gupta wrote:
> A blank line in an ignore file matches nothing. But
> add_patterns_from_buffer() only skips a line when it is completely
> empty. A line that holds just a carriage return i.e a blank line in a
> file with CRLF line endings or just spaces, is not empty at that
> point. Once the carriage return and the trailing spaces are removed,
> what is left is an empty pattern, which matches any path that ends in
> a slash. "git check-ignore -v foo/" then names the blank line as the
> pattern that ignores foo/. Skip the line when nothing is left of it
> after trimming and add a test.
Given these two points from gitignore(5):
- A blank line matches no files, so it can serve as a separator
for readability.
- Trailing spaces are ignored unless they are quoted with
backslash ("`\`").
this definitely seems like a bug worth addressing.
Something that initially confused me while attempting to reproduce the
problem was that git-status and git-add behave correctly. Even running
'git add some-directory/' with the trailing slash present, stages the
expected files. I went back to the original bug report and saw that
these details were commented on. It would be helpful to mention in the
commit log message if the problem is specific to check-ignore.
>
> Reported-by: Mike Beaton <mjsbeaton@gmail.com>
> Signed-off-by: Aniket Gupta <aniketgupta3001@gmail.com>
> ---
> 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);
This avoids adding a pattern which becomes empty after stripping a
carriage return and any spaces. I wonder if it is also worth fixing up
the parsing responsible for interpreting such "whitespace only" patterns
to match ".*/"? Either way, I think this patch is an improvement as-is.
> }
> lineno++;
> entry = buf + i + 1;
> diff --git a/t/t0008-ignores.sh b/t/t0008-ignores.sh
> index ed95faf327..199abbcdf3 100755
> --- a/t/t0008-ignores.sh
> +++ b/t/t0008-ignores.sh
> @@ -875,6 +875,13 @@ EOF
> test_must_be_empty err
> '
>
> +test_expect_success 'blank lines are not patterns' '
Should this test description be more about the spelling of the
(inadvertently) ignored directory? That might better explain the
specific check-ignore assertion that is made.
> + mkdir blank-dir &&
> + printf "\r\n \n \r\n" >blank-ignore &&
> + test_expect_code 1 git -c core.excludesfile=blank-ignore check-ignore blank-dir/ >actual &&
> + test_must_be_empty actual
> +'
> +
> test_expect_success !MINGW 'quoting allows trailing whitespace' '
> rm -rf whitespace &&
> mkdir whitespace &&
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH] dir: do not turn blank ignore-file lines into patterns
2026-11-06 18:29 ` [PATCH] dir: do not turn blank ignore-file lines into patterns Aniket Gupta
` (2 preceding siblings ...)
2026-11-28 5:24 ` Matt Hunter
@ 2026-11-28 6:54 ` Junio C Hamano
2026-11-28 7:28 ` Aniket Gupta
2026-11-28 9:38 ` Matt Hunter
2026-11-29 6:00 ` [PATCH v2] dir: ignore CRLF-empty and space-only lines in pattern files Aniket Gupta
5 siblings, 1 reply; 17+ messages in thread
From: Junio C Hamano @ 2026-11-28 6:54 UTC (permalink / raw)
To: Aniket Gupta; +Cc: git, Mike Beaton
Aniket Gupta <aniketgupta3001@gmail.com> writes:
> A blank line in an ignore file matches nothing. But
> add_patterns_from_buffer() only skips a line when it is completely
> empty.
Well, isn't that the definition of an empty line?
Or are you saying that a line with some whitespace characters on it is
"a blank line" that should not match anything? It is entirely possible
that a misguided or loosely written documentation page claims that a
blank line is ignored when it should have said that an empty line is
ignored, but I am afraid that such a position may be a bit hard to
justify, as there won't be a way to match a file with a funny name,
like three spaces, e.g.,
$ echo three spaces >" "
no?
> A line that holds just a carriage return i.e a blank line in a
> file with CRLF line endings or just spaces, is not empty at that
> point.
"A line that holds just a CR in a file with CRLF line endings" is a
confusing phrasing. Do you mean a three-byte line "\r\r\n"? That is
hardly an empty line (it has a lone CR on it). On the other hand, by
definition, a line with two bytes "\r\n" in a file with CRLF line
endings is an empty line. Without making it clear which one you mean,
it is hard for readers to agree or disagree with the statement.
So...?
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH] dir: do not turn blank ignore-file lines into patterns
2026-11-28 6:54 ` Junio C Hamano
@ 2026-11-28 7:28 ` Aniket Gupta
2026-11-29 4:01 ` Junio C Hamano
0 siblings, 1 reply; 17+ messages in thread
From: Aniket Gupta @ 2026-11-28 7:28 UTC (permalink / raw)
To: Junio C Hamano; +Cc: git, Mike Beaton, Matt Hunter, Phillip Wood
> Well, isn't that the definition of an empty line?
>
> Or are you saying that a line with some whitespace characters on it is
> "a blank line" that should not match anything? It is entirely possible
Yes, the latter. What I mean is a line that is empty once its trailing
spaces are trimmed. trim_trailing_spaces() reduces a line of only spaces
to "". add_pattern() registers that as empty pattern and an empty
pattern matches any path that ends in a slash. A line that is empty to
begin with is skipped already. It was wrong to write "blank" in the log
message.
> justify, as there won't be a way to match a file with a funny name,
> like three spaces, e.g.,
There still is, by quoting the spaces with backslashes. "\ \ \ " in an
ignore file ignores a file named with 3 spaces, with and without the
patch. An unquoted line of 3 spaces never matched it as it is trimmed to
nothing. Nothing is lost.
> "A line that holds just a CR in a file with CRLF line endings" is a
> confusing phrasing. Do you mean a three-byte line "\r\r\n"? That is
> hardly an empty line (it has a lone CR on it). On the other hand, by
> definition, a line with two bytes "\r\n" in a file with CRLF line
> endings is an empty line. Without making it clear which one you mean,
I meant the two byte "\r\n". By your definition it is an empty line but
add_patterns_from_buffer() tests "entry != buf + i" before it drops the
CR, so the line is not skipped and an empty pattern is added from it.
The three-byte "\r\r\n" is not involved.
I will describe it in these terms in the log message. Apologies if I was
unclear before.
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH] dir: do not turn blank ignore-file lines into patterns
2026-11-28 5:24 ` Matt Hunter
@ 2026-11-28 7:28 ` Aniket Gupta
2026-11-28 10:47 ` Aniket Gupta
1 sibling, 0 replies; 17+ messages in thread
From: Aniket Gupta @ 2026-11-28 7:28 UTC (permalink / raw)
To: Matt Hunter; +Cc: git, Junio C Hamano, Mike Beaton, Phillip Wood
Thanks a lot for looking into this, Matt.
> Something that initially confused me while attempting to reproduce the
> problem was that git-status and git-add behave correctly. Even running
> 'git add some-directory/' with the trailing slash present, stages the
> expected files. I went back to the original bug report and saw that
> these details were commented on. It would be helpful to mention in the
> commit log message if the problem is specific to check-ignore.
You are right. I should have said that from the start. I tried status,
add, clean and ls-files and all behave correctly. In my tests only
check-ignore shows a problem when given a path that ends in a slash, for
eg "foo/". I will say so in the log message.
> Should this test description be more about the spelling of the
> (inadvertently) ignored directory? That might better explain the
> specific check-ignore assertion that is made.
Good point. The name does not say what is being checked. It should be
renamed to say that a directory spelled with a trailing slash shouldnt
be ignored.
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH] dir: do not turn blank ignore-file lines into patterns
2026-11-06 18:29 ` [PATCH] dir: do not turn blank ignore-file lines into patterns Aniket Gupta
` (3 preceding siblings ...)
2026-11-28 6:54 ` Junio C Hamano
@ 2026-11-28 9:38 ` Matt Hunter
2026-11-28 10:12 ` Aniket Gupta
2026-11-29 6:00 ` [PATCH v2] dir: ignore CRLF-empty and space-only lines in pattern files Aniket Gupta
5 siblings, 1 reply; 17+ messages in thread
From: Matt Hunter @ 2026-11-28 9:38 UTC (permalink / raw)
To: Aniket Gupta, git; +Cc: Junio C Hamano, Mike Beaton
One more thing I saw...
On Fri Nov 6, 2026 at 1:29 PM EST, Aniket Gupta wrote:
>
> +test_expect_success 'blank lines are not patterns' '
> + mkdir blank-dir &&
> + printf "\r\n \n \r\n" >blank-ignore &&
> + test_expect_code 1 git -c core.excludesfile=blank-ignore check-ignore blank-dir/ >actual &&
The test doesn't actually need to create 'blank-dir'. git-check-ignore
seems to just check its input as _candidate_ filenames against the
ignore rules.
> + test_must_be_empty actual
> +'
> +
> test_expect_success !MINGW 'quoting allows trailing whitespace' '
> rm -rf whitespace &&
> mkdir whitespace &&
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH] dir: do not turn blank ignore-file lines into patterns
2026-11-28 9:38 ` Matt Hunter
@ 2026-11-28 10:12 ` Aniket Gupta
0 siblings, 0 replies; 17+ messages in thread
From: Aniket Gupta @ 2026-11-28 10:12 UTC (permalink / raw)
To: Matt Hunter; +Cc: git, Junio C Hamano, Mike Beaton, Phillip Wood
> The test doesn't actually need to create 'blank-dir'. git-check-ignore
> seems to just check its input as _candidate_ filenames against the
> ignore rules.
Checked this. Without the fix check-ignore still reports "blank-dir/"
and with the fix it reports nothing. Will drop it in v2.
Thanks.
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH] dir: do not turn blank ignore-file lines into patterns
2026-11-28 5:24 ` Matt Hunter
2026-11-28 7:28 ` Aniket Gupta
@ 2026-11-28 10:47 ` Aniket Gupta
2026-11-29 3:21 ` Junio C Hamano
1 sibling, 1 reply; 17+ messages in thread
From: Aniket Gupta @ 2026-11-28 10:47 UTC (permalink / raw)
To: Matt Hunter; +Cc: git, Junio C Hamano, Mike Beaton, Phillip Wood
> This avoids adding a pattern which becomes empty after stripping a
> carriage return and any spaces. I wonder if it is also worth fixing up
> the parsing responsible for interpreting such "whitespace only" patterns
> to match ".*/"? Either way, I think this patch is an improvement as-is.
I tried that. The empty pattern matches "foo/" because the basename of a
path with a trailing slash is empty and match_basename() compares it
with the empty pattern. Making match_basename() return 0 for an empty
pattern fixes blank line and also the lines "!", "/" and "!/", which my
patch leaves alone. With only "/" in the ignore file, "git check-ignore
-v foo/" still names that line. status, add, clean and ls-files give the
same output as before.
If this sits fine with you and Junio, I will do this in v2 rather than
skipping the line in add_patterns_from_buffer().
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH] dir: do not turn blank ignore-file lines into patterns
2026-11-28 10:47 ` Aniket Gupta
@ 2026-11-29 3:21 ` Junio C Hamano
0 siblings, 0 replies; 17+ messages in thread
From: Junio C Hamano @ 2026-11-29 3:21 UTC (permalink / raw)
To: Aniket Gupta; +Cc: Matt Hunter, git, Mike Beaton, Phillip Wood
Aniket Gupta <aniketgupta3001@gmail.com> writes:
>> This avoids adding a pattern which becomes empty after stripping a
>> carriage return and any spaces. I wonder if it is also worth fixing up
>> the parsing responsible for interpreting such "whitespace only" patterns
>> to match ".*/"? Either way, I think this patch is an improvement as-is.
>
> I tried that. The empty pattern matches "foo/" because the basename of a
> path with a trailing slash is empty and match_basename() compares it
> with the empty pattern. Making match_basename() return 0 for an empty
> pattern fixes blank line and also the lines "!", "/" and "!/", which my
> patch leaves alone. With only "/" in the ignore file, "git check-ignore
> -v foo/" still names that line. status, add, clean and ls-files give the
> same output as before.
add_patterns_from_buffer() is shared with sparse-checkout through
add_patterns_from_file_to_list() and add_patterns_from_blob_to_list().
Before any matching runs, add_pattern() calls add_pattern_to_hashsets().
When cone mode is active, add_pattern_to_hashsets() checks whether
patternlen is less than 2, and if so it emits "warning: unrecognized
pattern: ''" and disables cone pattern matching.
Changing only match_basename() would leave cone-mode sparse-checkout
broken on "\r\n" and space-only lines, and would still allocate unused
path_pattern entries for every such line in ignore files.
Making match_basename() reject zero-length patterns may still be a
worthwhile complementary change (for "!", "/", and "!/", or an empty
"--exclude=" pattern), though that can be a separate decision from
skipping empty trimmed lines here.
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH] dir: do not turn blank ignore-file lines into patterns
2026-11-28 7:28 ` Aniket Gupta
@ 2026-11-29 4:01 ` Junio C Hamano
0 siblings, 0 replies; 17+ messages in thread
From: Junio C Hamano @ 2026-11-29 4:01 UTC (permalink / raw)
To: Aniket Gupta; +Cc: git, Mike Beaton, Matt Hunter, Phillip Wood
Aniket Gupta <aniketgupta3001@gmail.com> writes:
> There still is, by quoting the spaces with backslashes. "\ \ \ " in an
> ignore file ignores a file named with 3 spaces, with and without the
> patch. An unquoted line of 3 spaces never matched it as it is trimmed to
> nothing. Nothing is lost.
> ...
> I will describe it in these terms in the log message. Apologies if I was
> unclear before.
Tracing through dir.c confirms the issue, and the clarifications you
gave in these messages can be used to make the log message describe the
issues clearly.
The commit message could be rewritten along these lines, dropping
"blank" in favor of explaining how "\r\n" and space-only lines turn into
"", like
dir: ignore CRLF-empty and space-only lines in pattern files
add_patterns_from_buffer() skips a line before stripping '\r' or
trailing spaces when the line starts right at '\n'. A two-byte
"\r\n" line in a CRLF file or a line containing only spaces is not
empty at that check, so stripping '\r' and calling
trim_trailing_spaces() reduces it to an empty string "", which is
then passed to add_pattern().
parse_path_pattern() records that "" as a zero-length pattern with
PATTERN_FLAG_NODIR. Normal directory walks via treat_path() and
prep_exclude() strip trailing slashes before matching, so commands
like status and add are unaffected.
However, normalize_path_copy_len() preserves a trailing '/' in
pathspec elements passed to "git check-ignore foo/", leaving an
empty basename after the slash that matches patternlen == 0 in
match_basename(). In addition, cone-mode sparse-checkout feeds
add_patterns_from_buffer() into add_pattern_to_hashsets(), where a
zero-length pattern warns and disables cone matching.
Skip calling add_pattern() in add_patterns_from_buffer() when the
trimmed line is empty.
or something like that, perhaps?
^ permalink raw reply [flat|nested] 17+ messages in thread
* [PATCH v2] dir: ignore CRLF-empty and space-only lines in pattern files
2026-11-06 18:29 ` [PATCH] dir: do not turn blank ignore-file lines into patterns Aniket Gupta
` (4 preceding siblings ...)
2026-11-28 9:38 ` Matt Hunter
@ 2026-11-29 6:00 ` Aniket Gupta
2026-11-29 11:14 ` Matt Hunter
5 siblings, 1 reply; 17+ messages in thread
From: Aniket Gupta @ 2026-11-29 6:00 UTC (permalink / raw)
To: git; +Cc: Junio C Hamano, Matt Hunter, Mike Beaton, Phillip Wood
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 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, like before.
Reported-by: Mike Beaton <mjsbeaton@gmail.com>
Signed-off-by: Aniket Gupta <aniketgupta3001@gmail.com>
---
Changes in v2:
- Rewrite the log message without the word "blank" (Junio).
- Explain in the log message why status and add are not affected,
with check-ignore as the example (Matt), and mention that
cone-mode sparse-checkout warns on such lines (Junio).
- Rename the test and drop the mkdir (Matt).
- Keep the fix in add_patterns_from_buffer() instead of moving it
into match_basename() as I suggested to Matt. Cone mode warns
before any matching runs (Junio), so the line has to be skipped
here. Making match_basename() reject empty patterns can be a
separate change.
Thanks Junio and Matt for the review.
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)
^ permalink raw reply related [flat|nested] 17+ messages in thread
* Re: [PATCH v2] dir: ignore CRLF-empty and space-only lines in pattern files
2026-11-29 6:00 ` [PATCH v2] dir: ignore CRLF-empty and space-only lines in pattern files Aniket Gupta
@ 2026-11-29 11:14 ` Matt Hunter
2026-11-29 11:42 ` Aniket Gupta
0 siblings, 1 reply; 17+ messages in thread
From: Matt Hunter @ 2026-11-29 11:14 UTC (permalink / raw)
To: Aniket Gupta, git; +Cc: Junio C Hamano, Mike Beaton, Phillip Wood
On Sun Nov 29, 2026 at 1:00 AM EST, Aniket Gupta wrote:
> 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 that ends in a slash is a different scenario. For eg,
s/path/path argument/ ?
Outside of a user's spelling, a directory name would not actually
contain a slash, right?
> 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, like before.
Perhaps drop the "like before", since "still be matched" suggests
unchanged behavior.
>
> Reported-by: Mike Beaton <mjsbeaton@gmail.com>
> Signed-off-by: Aniket Gupta <aniketgupta3001@gmail.com>
> ---
> Changes in v2:
>
> - Rewrite the log message without the word "blank" (Junio).
> - Explain in the log message why status and add are not affected,
> with check-ignore as the example (Matt), and mention that
> cone-mode sparse-checkout warns on such lines (Junio).
> - Rename the test and drop the mkdir (Matt).
> - Keep the fix in add_patterns_from_buffer() instead of moving it
> into match_basename() as I suggested to Matt. Cone mode warns
> before any matching runs (Junio), so the line has to be skipped
> here.
I think this is the better choice.
> Making match_basename() reject empty patterns can be a
> separate change.
>
> @@ -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' '
I debated suggesting to say "patterns" instead of "lines" here. But the
word "lines" is used much more consistently throughout the patch (and
doing so makes more sense). So never mind?
> + 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
The updated test looks good.
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH v2] dir: ignore CRLF-empty and space-only lines in pattern files
2026-11-29 11:14 ` Matt Hunter
@ 2026-11-29 11:42 ` Aniket Gupta
0 siblings, 0 replies; 17+ messages in thread
From: Aniket Gupta @ 2026-11-29 11:42 UTC (permalink / raw)
To: Matt Hunter, git; +Cc: Junio C Hamano, Mike Beaton, Phillip Wood
> s/path/path argument/ ?
>
> Outside of a user's spelling, a directory name would not actually
> contain a slash, right?
In this case the "/" only comes from what the user types. That
is clearer, will change.
> Perhaps drop the "like before", since "still be matched" suggests
> unchanged behavior.
Will drop it in v3.
> I debated suggesting to say "patterns" instead of "lines" here.
> But the word "lines" is used much more consistently throughout the
> patch (and doing so makes more sense). So never mind?
Sounds good. Thanks for reviewing.
^ permalink raw reply [flat|nested] 17+ messages in thread
end of thread, other threads:[~2026-11-29 11:43 UTC | newest]
Thread overview: 17+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-10-28 12:49 [BUG] check-ignore matches trailing-slash paths against CRLF blank lines Mike Beaton
2026-11-06 18:29 ` [PATCH] dir: do not turn blank ignore-file lines into patterns Aniket Gupta
2026-11-19 17:19 ` Aniket Gupta
[not found] ` <CAHzAAWQo89ox+S_jtPX9DOtzfr-+3uMbWHN+HXdi9Aw2q57+Ng@mail.gmail.com>
2026-11-21 9:45 ` Aniket Gupta
2026-11-27 16:21 ` Aniket Gupta
2026-11-28 5:24 ` Matt Hunter
2026-11-28 7:28 ` Aniket Gupta
2026-11-28 10:47 ` Aniket Gupta
2026-11-29 3:21 ` Junio C Hamano
2026-11-28 6:54 ` Junio C Hamano
2026-11-28 7:28 ` Aniket Gupta
2026-11-29 4:01 ` Junio C Hamano
2026-11-28 9:38 ` Matt Hunter
2026-11-28 10:12 ` Aniket Gupta
2026-11-29 6:00 ` [PATCH v2] dir: ignore CRLF-empty and space-only lines in pattern files Aniket Gupta
2026-11-29 11:14 ` Matt Hunter
2026-11-29 11:42 ` Aniket Gupta
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox