Skip to content

grepdiff: invert match #36

Open
Open

Description

@camsteffen

There should be a flag like -v, --invert-match in grep.

See https://stackoverflow.com/questions/57628550/grepdiff-find-hunks-that-dont-match-a-given-regexp (someone else's question).

My use case is to "unstage hunks that do not match" in git:

git diff --staged -U1 | grepdiff foo --invert-match --output-matching=hunk | git apply --cached -R

Activity

  1. twaugh commented on Dec 6, 2019

    @twaugh
    Owner

    --invert-match sounds like the right parameter name for this. Probably needs an invert_regexecs() similar to match regexecs(), and a way of switching between them appropriately (maybe a function pointer).

    I don't have time to code this myself right now. Want to have a go at it?

  2. camsteffen commented on Dec 7, 2019

    @camsteffen
    Author

    Thanks for your response. I'm also a little too busy to claim this right now.

  3. jansmets commented on Apr 11, 2021

    @jansmets

    Would be useful indeed.

  4. twaugh commented on Aug 27, 2025

    @twaugh
    Owner

    Should --invert-match and --output-matching=hunk show individual hunks that don't match, or entire files that have at least one non-matching hunk?

    How should --invert-match interact with --only-match=...? Should it invert the pattern matching or the line-type filtering? For example, with a hunk like this:

    @@ -1,3 +1,3 @@
     unchanged
    -removed_line_with_foo
    +added_line_with_bar
    +added_line_with_foo
    

    should grepdiff --invert-match --only-match=add foo not show this file because the addition lines match 'foo'?

    I'm particularly interested in which workflows an --invert-match option enables. I'm thinking something like "using git, unstage hunks that do not match", but what others are there?

  5. camsteffen commented on Aug 29, 2025

    @camsteffen
    Author

    Should --invert-match and --output-matching=hunk show individual hunks that don't match, or entire files that have at least one non-matching hunk?

    The former. It fits the use case. It's the corollary of grep --invert-match. The latter could be derived from it if needed but that seems like a pretty unlikely use case.

    How should --invert-match interact with --only-match=...? Should it invert the pattern matching or the line-type filtering?

    This seems like a question of order of operations. I think the sensible thing is to apply --only-match and then apply --invert-match. That way the --only-match is part of what gets inverted and so you get more functionality with the combined semantics. If you wanted to apply the --only-match filtering at the end, I think you could use a subsequent invocation of grepdiff.

    For example, ...should grepdiff --invert-match --only-match=add foo not show this file because the addition lines match 'foo'?

    Right, except I'd say "should not show this hunk".

    I'm particularly interested in which workflows an --invert-match option enables. I'm thinking something like "using git, unstage hunks that do not match", but what others are there?

    Yeah that is the use case I thought of. I can't think of a very different use case. A slightly different use case is to create a patch that partially reverts a commit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions