Thread View: linux.kernel.git
19 messages
19 total messages
Started by Mike Beaton
Wed, 28 Oct 2026 12:49
[BUG] check-ignore matches trailing-slash paths against CRLF blank lines
Author: Mike Beaton
Date: Wed, 28 Oct 2026 12:49
Date: Wed, 28 Oct 2026 12:49
115 lines
14475 bytes
14475 bytes
## 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.
[PATCH] dir: do not turn blank ignore-file lines into patterns
Author: Aniket Gupta
Date: Fri, 06 Nov 2026 18:29
Date: Fri, 06 Nov 2026 18:29
52 lines
12418 bytes
12418 bytes
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
Re: [PATCH] dir: do not turn blank ignore-file lines into patterns
Author: Aniket Gupta
Date: Thu, 19 Nov 2026 17:19
Date: Thu, 19 Nov 2026 17:19
2 lines
10707 bytes
10707 bytes
Friendly ping. Just wanted to make sure this does not get lost.
Re: [PATCH] dir: do not turn blank ignore-file lines into patterns
Author: Aniket Gupta
Date: Sat, 21 Nov 2026 09:45
Date: Sat, 21 Nov 2026 09:45
4 lines
10802 bytes
10802 bytes
> Do I need to do anything Nope. It is still waiting for a review.
Re: [PATCH] dir: do not turn blank ignore-file lines into patterns
Author: Aniket Gupta
Date: Fri, 27 Nov 2026 16:21
Date: Fri, 27 Nov 2026 16:21
8 lines
10867 bytes
10867 bytes
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
Re: [PATCH] dir: do not turn blank ignore-file lines into patterns
Author: "Matt Hunter"
Date: Sat, 28 Nov 2026 05:24
Date: Sat, 28 Nov 2026 05:24
86 lines
11248 bytes
11248 bytes
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 &&
Re: [PATCH] dir: do not turn blank ignore-file lines into patterns
Author: Junio C Hamano
Date: Sat, 28 Nov 2026 06:54
Date: Sat, 28 Nov 2026 06:54
33 lines
13844 bytes
13844 bytes
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...?
Re: [PATCH] dir: do not turn blank ignore-file lines into patterns
Author: Aniket Gupta
Date: Sat, 28 Nov 2026 07:28
Date: Sat, 28 Nov 2026 07:28
34 lines
11984 bytes
11984 bytes
> 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.
Re: [PATCH] dir: do not turn blank ignore-file lines into patterns
Author: Aniket Gupta
Date: Sat, 28 Nov 2026 07:28
Date: Sat, 28 Nov 2026 07:28
22 lines
11796 bytes
11796 bytes
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.
Re: [PATCH] dir: do not turn blank ignore-file lines into patterns
Author: "Matt Hunter"
Date: Sat, 28 Nov 2026 09:38
Date: Sat, 28 Nov 2026 09:38
21 lines
8516 bytes
8516 bytes
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 &&
Re: [PATCH] dir: do not turn blank ignore-file lines into patterns
Author: Aniket Gupta
Date: Sat, 28 Nov 2026 10:12
Date: Sat, 28 Nov 2026 10:12
8 lines
10968 bytes
10968 bytes
> 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.
Re: [PATCH] dir: do not turn blank ignore-file lines into patterns
Author: Aniket Gupta
Date: Sat, 28 Nov 2026 10:47
Date: Sat, 28 Nov 2026 10:47
16 lines
11571 bytes
11571 bytes
> 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().
Re: [PATCH] dir: do not turn blank ignore-file lines into patterns
Author: Junio C Hamano
Date: Sun, 29 Nov 2026 03:21
Date: Sun, 29 Nov 2026 03:21
32 lines
14411 bytes
14411 bytes
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.
Re: [PATCH] dir: do not turn blank ignore-file lines into patterns
Author: Junio C Hamano
Date: Sun, 29 Nov 2026 04:01
Date: Sun, 29 Nov 2026 04:01
44 lines
14662 bytes
14662 bytes
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?
[PATCH v2] dir: ignore CRLF-empty and space-only lines in pattern files
Author: Aniket Gupta
Date: Sun, 29 Nov 2026 06:00
Date: Sun, 29 Nov 2026 06:00
83 lines
13987 bytes
13987 bytes
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)
Re: [PATCH v2] dir: ignore CRLF-empty and space-only lines in pattern files
Author: "Matt Hunter"
Date: Sun, 29 Nov 2026 11:14
Date: Sun, 29 Nov 2026 11:14
73 lines
10896 bytes
10896 bytes
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.
Re: [PATCH v2] dir: ignore CRLF-empty and space-only lines in pattern files
Author: Aniket Gupta
Date: Sun, 29 Nov 2026 11:42
Date: Sun, 29 Nov 2026 11:42
19 lines
11404 bytes
11404 bytes
> 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.
[PATCH v3] dir: ignore CRLF-empty and space-only lines in pattern files
Author: Aniket Gupta
Date: Tue, 01 Dec 2026 08:00
Date: Tue, 01 Dec 2026 08:00
73 lines
13445 bytes
13445 bytes
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 <mjsbeaton@gmail.com>
Signed-off-by: Aniket Gupta <aniketgupta3001@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)
Re: [PATCH v3] dir: ignore CRLF-empty and space-only lines in pattern files
Author: Aniket Gupta
Date: Tue, 01 Dec 2026 19:11
Date: Tue, 01 Dec 2026 19:11
46 lines
13100 bytes
13100 bytes
> Huh, affected by what? At this point I have no idea what the issue > even is, the only thing I know is that we have an empty pattern now. The problem is a line in a pattern file that "looks empty" but isnt. It is either a 2 byte "\r\n" line in a file with CRLF line endings, or a line that holds only spaces. Git skips an empty line by default, however, these 2 lines only become empty after git strips the "\r" and the trailing spaces, and by then, it passes the empty string to add_pattern() which records a zero length pattern. A zero length pattern matches a path argument that ends in a slash because its basename is empty, so "git check-ignore foo/" says an untracked foo/ is ignored because of that line. In a cone-mode sparse-checkout file, the same line makes git print 2 warnings and disable cone matching. In the patch I try to skip a line which is empty after trimming. Allow me to start the message this way in v4. > Okay. With git-check-ignore(1) we can append that slash ourselves, and > thus it may trigger the bug. And this is in contrast to git-status(1) > and git-add(1) seemingly, which never have that trailing slash. True. status and add match a directory before they add the slash. check-ignore can be given a path argument that ends in one. > Sorry for repeating all of that, but I'm trying to deciper this AI > message and am having a hard time doing so. Claude discovered the problem while I was trying to understand the code around it. Since I am new to open source and English not being my first language, I use it to review my code and to correct my messages. I realize I am only making it more complicated for your understanding. > Okay. Is this a cosmetic issue, or is it something that results in > wrong behaviour? It does not check out wrong files, but it is more than cosmetic. With one such line in a cone-mode sparse-checkout file, git prints 2 warnings and disables cone matching. Because of that, the sparse index is expanded to a full one, and "git sparse-checkout list" shows raw patterns like "/a/" instead of the directory name "a". I can explain like this in v4. > Would it make sense to maybe clarify the rule to say "A blank line or > a line that consists of spaces, only, matches no files"? Yeah I think that would help. I will change it in gitignore.adoc in v4.
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads