threads / bug / 66690

[BUG] check-ignore matches trailing-slash paths against CRLF blank lines

Subject: [BUG] check-ignore matches trailing-slash paths against CRLF blank lines

## tl;dr

4 messages between Oct 28, 2026 and Nov 21, 2026.

replies: 3people: 2as markdown or json

Mike Beaton· Oct 28, 2026, 12:49 UTC · lore
## 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.

Aniket Gupta· Nov 6, 2026, 18:29 UTC · re: Mike Beaton · lore

[PATCH] dir: do not turn blank ignore-file lines into patterns

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

← back to recent threads