Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

--max-count has precedence over --after-context and --context #402

Closed
chelmertz opened this issue Mar 10, 2017 · 1 comment
Closed

--max-count has precedence over --after-context and --context #402

chelmertz opened this issue Mar 10, 2017 · 1 comment
Labels
bug A bug. libripgrep An issue related to modularizing ripgrep into libraries.
Milestone

Comments

@chelmertz
Copy link

chelmertz commented Mar 10, 2017

I think --after-context (-A) and --context (-C) should have the same precedence as --before-context (-B), when being combined with --max-count (-m), but they do not:

$ cat > rgtest.txt <<EOF
> a
> b
> c
> d
> e
> EOF
$ rg -B2 c rgtest.txt
1-a
2-b
3:c
$ rg -C2 c rgtest.txt
1-a
2-b
3:c
4-d
5-e
$ rg -A2 c rgtest.txt
3:c
4-d
5-e
$ # and now with --max-count:
$ rg -B2 c -m1 rgtest.txt
1-a
2-b
3:c
$ rg -C2 c -m1 rgtest.txt
1-a
2-b
3:c
$ rg -A2 c -m1 rgtest.txt
3:c

From my expectations, -B works correctly, -C works like -B which is unexpected, and -A has no effect at all.

My use case is to show a colleague one instance of many repeated matches, but also with some context. If -A combined with -m would yield the same result as with -B, then my use case would have been fulfilled.

I am using ripgrep-0.3.2-1.fc24.x86_64, packaged for Fedora through copr (by carlgeorge-ripgrep).

Thanks for a great program! (edit: removed trailing whitespace in code block)

@BurntSushi
Copy link
Owner

Thanks for the bug report. This is definitely a bug. But I don't think it has anything to do with precedence specifically. For example:

[andrew@Cheetah ripgrep-402] cat > rgtest.txt <<EOF
> a
> b
> c
> d
> e
> a
> b
> c
> d
> e
> EOF
[andrew@Cheetah ripgrep-402] rg -C2 c -m1 rgtest.txt
1-a
2-b
3:c
[andrew@Cheetah ripgrep-402] rg -C2 c -m2 rgtest.txt
1-a
2-b
3:c
4-d
5-e
6-a
7-b
8:c

Notice that in the last example, the "after" context for the first match is printed correctly. Only the last match loses it.

This is probably just a bug in the search code where it neglects to print the context after the last match if it decides to quit early. I'll note that grep handles this correctly:

[andrew@Cheetah ripgrep-402] grep -C2 c -m2 rgtest.txt
a
b
c
d
e
a
b
c
d
e

This could be a little tricky to fix. Not sure.

@BurntSushi BurntSushi added bug A bug. libripgrep An issue related to modularizing ripgrep into libraries. labels Mar 10, 2017
@BurntSushi BurntSushi added this to the libripgrep milestone Mar 10, 2017
Detegr added a commit to Detegr/ripgrep that referenced this issue Oct 1, 2017
Print the context if there's a context left to be printed after
returning early from a search (because of --max-count).

Fixes BurntSushi#402.
BurntSushi pushed a commit that referenced this issue Oct 8, 2017
Print the context if there's a context left to be printed after
returning early from a search (because of --max-count).

Fixes #402.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A bug. libripgrep An issue related to modularizing ripgrep into libraries.
Projects
None yet
Development

No branches or pull requests

2 participants