How to invert `git log --grep=` or How to

2019-01-11 05:49发布

I want to use git log to show all commits that do not match a given pattern. I know I can use the following to show all commits that do match a pattern:

git log --grep=<pattern>

How do I invert the sense of matching?

I am trying to ignore commits that have "bumped to version ..." in the message.

EDIT: I want my final output to be pretty verbose. e.g. git log --pretty --stat. So output from git log --format=oneline won't work for me.

标签: git git-log
8条回答
别忘想泡老子
2楼-- · 2019-01-11 06:29

As far as I can tell, this is not possible to do directly with a single command line; you'd have to do something like Justin Lilly suggests and then run 'git log' on the resulting list of hashes, e.g.,

git log --format="%h" | grep -v `git log -1 --grep="bumped to version" --format="%h"` > good-hashes
for h in `cat good-hashes`; do
    PAGER=cat git log -1 --pretty --stat $h
done

should do the trick.

查看更多
Summer. ? 凉城
3楼-- · 2019-01-11 06:31

As mentioned by VonC the best option is if you can update to Git 2.4.0 (which is currently on RC2). But even if you can't do that there is no reason for elaborate scripts. A (gnu) awk one-liner should do it. git log has the useful -z option to separate commits by a NUL-character which makes it easy to parse them:

git log -z --pretty --stat | awk 'BEGIN{ RS="\0"; FS="\n\n" } !match($2, /<pattern>/)'

If you don't have gnu awk, you probably should at least install that. Or port this script to your specific awk version, which I leave as an exercise for the reader ;-).

查看更多
时光不老,我们不散
4楼-- · 2019-01-11 06:31
git log --pretty --stat | grep -v "bumped to version"
查看更多
淡お忘
5楼-- · 2019-01-11 06:39

Generate a list of all commits, subtract those whose log messages contain the offending pattern, and feed the result to git log with your desired options. In the final stage, a couple of options to git log are handy:

--stdin
In addition to the commit listed on the command line, read them from the standard input.

--no-walk
Only show the given revs, but do not traverse their ancestors.

You can do it with a single pipeline and process substitution.

#! /bin/bash

if (( $# < 1 )); then
  echo >&2 "Usage: $0 pattern [<since>..<until>]"
  exit 1
fi

pattern=$1
shift

git log --format=%H $@ |
  grep -v -f <(git log --format=%H "--grep=$pattern" $@) |
  git log --pretty --stat --stdin --no-walk

If you don't want to use bash, you could do it with Perl.

#! /usr/bin/env perl

use strict;
use warnings;
no warnings "exec";

sub usage { "Usage: $0 pattern\n" }

sub commits_to_omit {
  my($pattern) = @_;

  open my $fh, "-|", "git", "log", "--grep=$pattern", "--format=%H", @ARGV
    or die "$0: exec: $!";
  my %omit = map +($_ => 1), <$fh>;
  %omit;
}

die usage unless @ARGV >= 1;
my $pattern = shift;

my %omit = commits_to_omit $pattern;

open my $all, "-|", "git", "log", "--format=%H", @ARGV
  or die "$0: exec: $!";

open my $out, "|-", "git", "log", "--pretty", "--stat", "--stdin", "--no-walk"
  or die "$0: exec: $!";

while (<$all>) {
  print $out $_ unless $omit{$_};
}

Assuming one of the above is in your PATH as git-log-vgrep and with a history of the form

$ git lola
* b0f2a28 (tmp, feature1) D
* 68f87b0 C
* d311c65 B
* a092126 A
| * 83052e6 (HEAD, origin/master, master) Z
| * 90c3d28 Y
| * 4165a42 X
| * 37844cb W
|/  
* f8ba9ea V

we could say

$ git log-vgrep X

to get Z, Y, W, and V.

You can also log other branches, so

$ git log-vgrep A tmp

gives D, C, B, and V; and

$ git log-vgrep C tmp~2..tmp

yields just D.

One limitation of the above implementations is if you use a pattern that matches all commits, e.g., . or ^, then you'll get HEAD. This is how git log works:

$ git log --stdin --no-walk --pretty=oneline </dev/null
83052e62f0dc1c6ddfc1aff3463504a4bf23e3c4 Z
查看更多
【Aperson】
6楼-- · 2019-01-11 06:39

This will be possible with Git 2.4+ (Q2 2015): see commit 22dfa8a by Christoph Junghans (junghans):

log: teach --invert-grep option

"git log --grep=<string>" shows only commits with messages that match the given string, but sometimes it is useful to be able to show only commits that do not have certain messages (e.g. "show me ones that are not FIXUP commits").

Originally, we had the invert-grep flag in grep_opt, but because "git grep --invert-grep" does not make sense except in conjunction with "--files-with-matches", which is already covered by "--files-without-matches", it was moved it to revisions structure.
To have the flag there expresses the function to the feature better.

When the newly inserted two tests run, the history would have commits with messages "initial", "second", "third", "fourth", "fifth", "sixth" and "Second", committed in this order.
The commits that does not match either "th" or "Sec" is "second" and "initial". For the case insensitive case only "initial" matches.

--invert-grep

Limit the commits output to ones with log message that do not match the pattern specified with --grep=<pattern>.

Example:

I first grep message with "sequencer" in them:

vonc@voncm C:\Users\vonc\prog\git\git

> git log -2 --pretty="tformat:%s" --grep=sequencer
Merge branch 'js/sequencer-wo-die'
sequencer: ensure to release the lock when we could not read the index

If I want messages with no sequencer:

> git log -2 --pretty="tformat:%s" --grep=sequencer --invert-grep
Second batch for 2.11
Merge branch 'js/git-gui-commit-gpgsign'
查看更多
萌系小妹纸
7楼-- · 2019-01-11 06:41

A relatively simple method with a lot of flexibility is to use git log with the -z option piped to awk. The -z option adds nulls between commit records, and so makes it easy parse with awk:

git log --color=always -z | awk -v RS=\\0

(color=always is required to keep coloring when the output is a pipe). Then, its simple to add any boolean expression you want that works on each field. For example, this will print all entries where the author email is not from fugly.com, and the day of the commit was Sunday:

git log --color=always -z | awk -v RS=\\0 '!/Author:.*fugly.com>/ && /Date:.* Sun /'

Another nice thing is its you can add in any formatting option or revision range to the git log, and it still works.

One last thing, if you want to paginate it, use "less -r" to keep the colors.

EDIT: changed to use -v in awk to make it a little simpler.

查看更多
登录 后发表回答