From: Phillip Wood <phillip.wood123@gmail.com>
To: "René Scharfe" <l.s.r@web.de>,
"Brandon Chinn" <brandonchinn178@gmail.com>
Cc: git@vger.kernel.org, Taylor Blau <me@ttaylorr.com>,
Jeff King <peff@peff.net>, Junio C Hamano <gitster@pobox.com>
Subject: Re: git grep bug with --column and --only-matching
Date: Thu, 23 Apr 2026 10:44:31 +0100 [thread overview]
Message-ID: <033622e1-1f61-4f9d-b56f-fafc7850d275@gmail.com> (raw)
In-Reply-To: <3ce1906a-85e3-4fb1-9ebc-a5639f3194c9@web.de>
On 21/04/2026 21:33, René Scharfe wrote:
> On 4/21/26 7:03 AM, Brandon Chinn wrote:
>> I'm encountering a bug when using `git grep` with both `--column` and
>> `--only-matching`, is this a known limitation?
>>
>> Repro:
>>
>> ```
>> $ echo 'x x x' > repro.txt
>>
>> $ grep -bo x repro.txt
>> 0:x
>> 4:x
>> 8:x
>>
>> $ git grep --no-index -o -n --column x repro.txt
>> repro.txt:1: 1:x
>> repro.txt:1: 2:x
>> repro.txt:1: 6:x
>
> The first column value matches (1-based for git grep, 0-based for
> grep(1)), the remaining ones are different. grep(1) shows the offset
> like for the first match, but git grep adds the relative position of the
> end of the previous match.
I'm having a hard time understanding what git is doing. Looking at your
example with the ruler
> (`man gitcvs-migration` or `git help cvs-migration` if git is
> ^^^ ^^^ ^^^
> 1111111111222222222233333333334444444444555555555566
> 123456789 123456789 123456789 123456789 123456789 123456789 1
>
> 7:git
> 16:git
> 38:git
The first match ends at column 9 and the second match starts at column
29 so how do we end up with 16 as the "column" of the second match when
the difference between them is 20?
> I don't know how to use the resulting numbers and I agree that it seems
> like a bug -- showing the column of each match within its line makes
> more sense for an option named --column.
Indeed
> However, the original submission of this feature in
> https://lore.kernel.org/git/cover.1529961706.git.me@ttaylorr.com/
> gave similar examples in its commit message and its tests, called
> them "as one would expect" and was not challenged on that, so I may be
> missing something.
I wonder how hard anyone looked at the examples and tests, the
discussion seemed to have focused on other things.
Thanks
Phillip
> Here's its first example with underlined matches, a ruler and the
> intended output (without unnecessary headers):
>
> (`man gitcvs-migration` or `git help cvs-migration` if git is
> ^^^ ^^^ ^^^
> 1111111111222222222233333333334444444444555555555566
> 123456789 123456789 123456789 123456789 123456789 123456789 1
>
> 7:git
> 16:git
> 38:git
>
> And here's the last line of the test file from t7810 with underlined
> matches (the other four lines are very similar), a ruler and the
> expected output (unncessary headers removed):
>
> foo_mmap bar mmap baz
> ^^^^ ^^^^
> 111111111122
> 123456789 123456789 1
>
> 5:mmap
> 13:mmap
>
> If we wanted to show the column of matches 2 and beyond then we could do
> something like this:
>
>
> diff --git a/grep.c b/grep.c
> index c7e1dc1e0e..a54e5d86a9 100644
> --- a/grep.c
> +++ b/grep.c
> @@ -1267,6 +1267,7 @@ static void show_line(struct grep_opt *opt,
> regmatch_t match;
> enum grep_context ctx = GREP_CONTEXT_BODY;
> int eflags = 0;
> + const char *start = bol;
>
> if (want_color(opt->color)) {
> if (sign == ':')
> @@ -1285,6 +1286,7 @@ static void show_line(struct grep_opt *opt,
> if (match.rm_so == match.rm_eo)
> break;
>
> + cno = bol - start + match.rm_so + 1;
> if (opt->only_matching)
> show_line_header(opt, name, lno, cno, sign);
> else
> @@ -1294,7 +1296,6 @@ static void show_line(struct grep_opt *opt,
> if (opt->only_matching)
> opt->output(opt, "\n", 1);
> bol += match.rm_eo;
> - cno += match.rm_eo;
> rest -= match.rm_eo;
> eflags = REG_NOTBOL;
> }
> diff --git a/t/t7810-grep.sh b/t/t7810-grep.sh
> index 64ac4f04ee..bd439563d6 100755
> --- a/t/t7810-grep.sh
> +++ b/t/t7810-grep.sh
> @@ -322,11 +322,11 @@ do
> ${HC}file:1:5:mmap
> ${HC}file:2:5:mmap
> ${HC}file:3:5:mmap
> - ${HC}file:3:13:mmap
> + ${HC}file:3:14:mmap
> ${HC}file:4:5:mmap
> - ${HC}file:4:13:mmap
> + ${HC}file:4:14:mmap
> ${HC}file:5:5:mmap
> - ${HC}file:5:13:mmap
> + ${HC}file:5:14:mmap
> EOF
> git grep --column -n -o -e mmap $H >actual &&
> test_cmp expected actual
>
>
next prev parent reply other threads:[~2026-04-23 9:44 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-21 5:03 git grep bug with --column and --only-matching Brandon Chinn
2026-04-21 20:33 ` René Scharfe
2026-04-23 9:44 ` Phillip Wood [this message]
2026-04-24 21:04 ` [PATCH] grep: fix --column --only-match for 2nd and later matches René Scharfe
2026-05-04 13:10 ` Phillip Wood
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=033622e1-1f61-4f9d-b56f-fafc7850d275@gmail.com \
--to=phillip.wood123@gmail.com \
--cc=brandonchinn178@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=l.s.r@web.de \
--cc=me@ttaylorr.com \
--cc=peff@peff.net \
--cc=phillip.wood@dunelm.org.uk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox