GIT: filter log by group of authors
Asked Answered
U

2

12

Our company is divided in teams and we perform code reviews before sending our code to the QA guys.

Usually, any code we touch is reviewed by someone inside the same team (A), but in some cases we also need to touch code from another team (B).

For those cases, the code review should be two-fold: one part reviewed by team A, and another part reviewed by team B.

Our problem is that many times the team A does not send the code to be reviewed by team B and this causes team B to be ignorant of this new code.

What I'd like to do is to periodically run a script to find commits inside certain parts of the code (usually subdirectories with modules our team handles) made by any person not belonging to our team, but unfortunately the --not argument in git log does not seem to work with --author so I cannot happily run git log --not --author "John" --author "Paul" --author "Ringo" module/A module/B module/C

My requirement translates to something like "list all commits touching module/A, module/B or module/C done by someone that's not John, Paul or Ringo"

Is there any way to do this using "official" tools? I would be interested in an open-source script too if it means doing this results in a one-liner or close to that.

Unknit answered 9/4, 2014 at 16:35 Comment(0)
G
18

EDIT: This should show commits with author names that do not contain "John" "Paul" or "Ringo". Requires git to be built with PCRE.

git log --perl-regexp --author='^(?!(.*(John|Paul|Ringo)))'

I'll leave previous variants below as they still may be of some use:


You should be able to do it using negative look-behind assertion, if your git is compiled with PCRE support. Something like this should do what you want:

git log --perl-regexp --author='(?<!(john|paul|ringo))@'

Note that the regex is actually looking for @ not preceded by john|paul|ringo. Getting this to work with names will be tricker as there may not be a convenient way to find an 'anchor' that's not preceded by the name you want to exclude.

You can try anchoring off beginning of the author line and show only commits that do not start with John|Paul|Ringo (i.e commits with John as the middle name would still be shown):

git log --perl-regexp --author='^(?!(John|Paul|Ringo))'

Glasshouse answered 9/4, 2014 at 18:29 Comment(4)
Didn't know about git built with PCRE, nice answer! I'll wait a little longer to see if anyone else can give another solution which doesn't require a custom GIT compilation.Unknit
For what it's worth, on ubuntu prebuilt git packages do come with PCRE enabled.Glasshouse
Tried on Ubuntu 13.10, and it does come with PCRE enabled. But it doesn't on 12.04, that is where I actually need it :(Unknit
You can get more up-to-date git version (1.9.x) for ubuntu 12.04 here: launchpad.net/~git-core/+archive/ppa It does contain perl regex support.Glasshouse
S
7

Though this question has been answered 7 years ago, it's worth sharing another solution, that became feasible roughly a year later (with git 2.4, in 2015).

As mentioned in the answer to a similar question and the answer to a slightly different problem still involving regexes, it is possible to invert a regex pattern by specifying --invert-grep.

So, to address the specific situation you mention, you'll need to run:

git log --perl-regexp --invert-grep --author='(John|Paul|Ringo)'

The solution by @ArtemB is of course still valid. The only benefit I can see in this solution is increased readability (by ditching the ?! construct).

Schumann answered 8/9, 2021 at 12:15 Comment(1)
--invert-grep saved my dayRibosome

© 2022 - 2024 — McMap. All rights reserved.