如何取消`git log --grep =< pattern>`或如何显示不符合模式的git日志 [英] How to invert `git log --grep=<pattern>` or How to show git logs that don't match a pattern

查看:163
本文介绍了如何取消`git log --grep =< pattern>`或如何显示不符合模式的git日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 git log 来显示与给定模式不匹配的所有提交。我知道我可以使用以下来显示所有符合模式的提交:

;

如何反转匹配感?



编辑:我希望我的最终输出是相当详细的。

编辑:我希望我的最终输出是相当详细的。例如 git log --pretty --stat 。所以从> git log --format = oneline 的输出将不适用于我。

解决方案 div>

生成所有提交的列表,将包含违规模式的日志消息减去,并将结果以您所需的选项提供给 git log 。在最后阶段, git log 的选项很方便: - stdin

除了命令行中列出的 commit 之外,还可以从标准输入中读取它们。



- 不散步

只显示给定的转速,但不要穿越他们的祖先。

您可以通过一个管道和流程替换来完成它。

 #! / bin / bash 

if(($#< 1));然后
echo>& 2用法:$ 0 pattern [< since> ..< until>]
exit 1
fi

pattern = $ 1
转换

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

如果你不想使用bash,你可以用Perl来完成它。

 #! / usr / bin / env perl 

use strict;
使用警告;
没有警告exec;

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

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

打开我的$ fh, - |,git,log,--grep = $ pattern,--format =%H,@ARGV
或死于$ 0:exec:$!;
my%omit = map +($ _ => 1),< $ fh>;
%省略;
}

除非@ARGV> = 1;
my $ pattern = shift;

my%omit = commits_to_omit $ pattern;

打开我的$ all, - |,git,log,--format =%H,@ARGV
或者死掉$ 0:exec:$! ;

打开我的$ out,| - ,git,log,--pretty,--stat,--stdin,--no-walk
或死亡$ 0:exec:$!; $(< $ all>){
print $ out $ _除非$ omit {$ _};

;

$ / code>

假设上面的一个在PATH中是 git-log-vgrep 和一个形式的历史记录

 $ 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

我们可以说

 $ git log-vgrep X 

可以获得Z,Y,W和V.

你也可以登录其他分支,所以

 $ git log-vgrep一个tmp 

给出D,C,B和V;和

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

产量只有D。

以上实现的一个限制是,如果您使用匹配所有提交的模式,例如, ^ ,那么您将获得HEAD。这就是 git log 的工作方式:

 $ git log --stdin --no-walk  - -pretty = oneline< / dev / null 
83052e62f0dc1c6ddfc1aff3463504a4bf23e3c4 Z


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.

解决方案

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

这篇关于如何取消`git log --grep =&lt; pattern&gt;`或如何显示不符合模式的git日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆