git: identify all commits not complying with a pattern

1k views Asked by At

I am trying to identify all commits with a message not starting with [core]. These are my failed attempts:

  1. Simple approach

    git rev-list --grep '^(?!(\[core\]).).+' "branch1...branch2"
    
    • empty result
  2. Enable extended flag

    git rev-list -E --grep '^(?!(\[core\]).).+' "branch1...branch2"
    
    • Error message fatal: command line, '^(?!(\[core\]).).+': Invalid preceding regular expression.

    • It seems that a negative look-ahead is not supported by git grep.

  3. Comparing the list of all commits to the list of those with the tag (cf. this answer):

    git rev-list "branch1...branch2" | grep -Fxv <(git rev-list -E --grep '^\[core\].+' "branch1...branch2")
    
    • Results in sh: syntax error near unexpected token `('

P.S: I cannot upgrade to Git 2.x, so --invert-grep is not an option

2

There are 2 answers

4
Etan Reisner On

Something like this might work.

git rev-list "branch1...branch2" --not $(git rev-list --grep '^\[core\]')

Select the revisions in that list that match the pattern you don't want and then use --not to negate that list of revisions in another call to rev-list.

You could also avoid the process substitution in your third attempt by using an actual file like this:

git rev-list -E --grep '^\[core\].+' "branch1...branch2" > core.list
git rev-list "branch1...branch2" | grep -Fxvf core.list
4
PhilLab On

If it fits your requirements to use a batch file, this is one solution:

#!/bin/sh
range="branch1...branch2"
validCommits=`git rev-list -E --grep '^\[core\]' "$range"`
badCommits=`git rev-list "$range" | grep -Fxv "$validCommits"`