Subject: git grep: ^$ false match at end of file

From: Olly Betts <[email-masked]>
Date: Thu, 9 Jan 2025 23:52:55 +0000

git grep '^$' seems to match at the end of the file, reporting a line
number one greater than the number of lines in that file.  This does
not match the behaviour of grep.

To reproduce:

$ git init -q git-grep-bug
$ cd git-grep-bug
$ echo test > test.txt
$ git add test.txt
$ git commit -m test
[master (root-commit) 55b48b26] test
 1 file changed, 1 insertion(+)
 create mode 100644 test.txt
$ git grep -n '^$'
test.txt:2:
$ grep -n '^$' test.txt
$

(The -n option isn't required to trigger it.)

I'm using the git 1:2.47.1-1 packages from Debian unstable.  I can also
reproduce with git 1:2.48.0~rc1+next.20250101-1 from Debian
experimental.

Cheers,
    Olly