Git development
 help / color / mirror / Atom feed
* [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; 4+ 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] 4+ 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
  0 siblings, 1 reply; 4+ 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] 4+ 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>
  0 siblings, 1 reply; 4+ 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] 4+ 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; 4+ 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] 4+ messages in thread

end of thread, other threads:[~2026-11-21  9:45 UTC | newest]

Thread overview: 4+ 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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox