Git:发现哪些提交触及了一系列的行 [英] Git: discover which commits ever touched a range of lines

查看:162
本文介绍了Git:发现哪些提交触及了一系列的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难弄清楚如何使用 git blame 来获取 触及给定范围的行的提交集。有类似的问题,如这一个,但接受的答案并没有带来更多的进展。

比方说,我有一个定义,从 foo.rb 的第1000行开始。它只有5行,但是改变这些行的提交数量是巨大的。如果我这样做

  git blame foo.rb -L ​​1000,+ 5 

我引用了(最多)五个不同的提交,这些提交改变了这些行,但我也对背后提交感兴趣。

同样,

  git rev-list HEAD  -  foo。 rb | xargs git log --oneline 

几乎是我想要的,但我无法指定行范围 git rev-list



我可以传递一个标志给 git blame 以获得曾经触及这五行的提交列表,或者构建提取此类信息的脚本的最快方法是什么?让我们暂时忽略定义曾经多于或少于5行的可能性。

解决方案

自从Git 1.8.4 git log has -L git blame ' (aa27064 ...))[mlm @ macbook:〜/ w / mlm / git]

  $ b $ git blame -L150,+ 11  -  git-web--browse.sh 
a180055a git-web--browse.sh(Giuseppe Bilotta 2010-12-03 17:47:36 +0100 150)死浏览器$浏览器不是
a180055a git-web--browse.sh(Giuseppe Bilotta 2010-12-03 17:47:36 +0100 151)fi
5d6491c7 git-browse-help。 sh(Christian Couder 2007-12-02 06:07:55 +0100 152)fi
5d6491c7 git-browse-help.sh(Christian Couder 2007-12-02 06:07:55 +0100 153)
5d6491c7 git-browse-help.sh(Christian C ouder 2007-12-02 06:07:55 +0100 154)在
81f42f11的情况下$ browsergit-web--browse.sh(Giuseppe Bilotta 2010-12-03 17:47:38 +0100 155 )firefox | iceweasel | seamonkey | iceape)
5d6491c7 git-browse-help.sh(Christian Couder 2007-12-02 06:07:55 +0100 156)#检查版本,因为firefox< 2.0 do
5d6491c7 git-browse-help.sh(Christian Couder 2007-12-02 06:07:55 +0100 157)vers = $(expr$($ browser_path -version)
5d6491c7 git-browse-help.sh(Christian Couder 2007-12-02 06:07:55 +0100 158)NEWTAB =' - new-tab'
5d6491c7 git-browse-help.sh(Christian Couder 2007-12 -02 06:07:55 +0100 159)测试$ vers-lt 2&& NEWTAB =''
a0685a4f git-web--browse.sh(Dmitry Potapov 2008-02-09 23: 22:22 -0800 160)$ browser_path$ NEWTAB$ @&

然后:

 <$ c 

然后:

  $ git log --topo-order --graph -u -L 155,155:git-web-- browse.sh 
* commit 81f42f11496b9117273939c98d270af273c8a463
|作者:Giuseppe Bilotta< giuseppe.bilotta@gmail.com>
|日期:星期五十二月3 17:47:38 2010 +0100
|
| web - browse:支持opera,seamonkey和elinks
|
| T他支持的浏览器列表也在文档中更新。
|
|签名:Giuseppe Bilotta< giuseppe.bilotta@gmail.com>
|签名:Junio C Hamano< gitster@pobox.com>
|
| diff --git a / git-web--browse.sh b / git-web--browse.sh
| --- a / git-web--browse.sh
| +++ b / git-web--browse.sh
| @@ -143,1 +143,1 @@
| -firefox | iceweasel)
| + firefox | iceweasel | seamonkey | iceape)
|
* commit a180055a47c6793eaaba6289f623cff32644215b
|作者:Giuseppe Bilotta< giuseppe.bilotta@gmail.com>
|日期:周五12月3日17:47:36 2010 +0100
|
|网页浏览:编码风格
|
|在病例陈述中提供缓解和替代选择。
|
|签名:Giuseppe Bilotta< giuseppe.bilotta@gmail.com>
|签名:Junio C Hamano< gitster@pobox.com>
|
| diff --git a / git-web--browse.sh b / git-web--browse.sh
| --- a / git-web--browse.sh
| +++ b / git-web--browse.sh
| @@ -142,1 +142,1 @@
| - firefox | iceweasel)
| + firefox | iceweasel)
|
* commit 5884f1fe96b33d9666a78e660042b1e3e5f9f4d9
作者:Christian Couder< chriscool@tuxfamily.org>
日期:星期六2月2日07:32:53 2008 +0100

将'git-help--browse.sh'重命名为'git-web--browse.sh'。

签名:Christian Couder< chriscool@tuxfamily.org>
签名:Junio C Hamano< gitster@pobox.com>

diff --git a / git-web--browse.sh b / git-web--browse.sh
--- / dev / null
+++ b / git-web--browse.sh
@@ -0,0 +127,1 @@
+ firefox | iceweasel)

如果您经常使用此功能,您可能会发现一个有用的git别名。为此,请输入〜/ .gitconfig

  [别名] 
#遵循文件中特定行的演变
#arg1 =文件,arg2 =第一行,arg3 =最后一行或仅为第一行的空白
follow =!sh - c'git log --topo-order -u -L $ 2,$ {3: - $ 2}:$ 1' -

现在您可以执行 git follow git-web--browse.sh 155


I'm having trouble figuring out how to use git blame for getting the set of commits that ever touched a given range of lines. There are similar questions like this one but the accepted answer doesn't bring me much further.

Let's say I have a definition that starts on line 1000 of foo.rb. It's only only 5 lines long, but the number of commits that ever changed those lines is enormous. If I do

git blame foo.rb -L 1000,+5

I get references to (at most) five distinct commits that changed these lines, but I'm also interested in the commits "behind them".

Similarly,

git rev-list HEAD -- foo.rb | xargs git log --oneline

is almost what I want, but I can't specify line ranges to git rev-list

Can I pass a flag to git blame to get the list of commits that ever touched those five lines, or what's the quickest way to build a script that extracts such information? Let's ignore for the moment the possibility that the definition once had more or less than 5 lines.

解决方案

Since Git 1.8.4, git log has -L to view the evolution of a range of lines.

For example, suppose you look at git blame's output:

((aa27064...))[mlm@macbook:~/w/mlm/git]
$ git blame -L150,+11 -- git-web--browse.sh
a180055a git-web--browse.sh (Giuseppe Bilotta 2010-12-03 17:47:36 +0100 150)            die "The browser $browser is not
a180055a git-web--browse.sh (Giuseppe Bilotta 2010-12-03 17:47:36 +0100 151)    fi
5d6491c7 git-browse-help.sh (Christian Couder 2007-12-02 06:07:55 +0100 152) fi
5d6491c7 git-browse-help.sh (Christian Couder 2007-12-02 06:07:55 +0100 153) 
5d6491c7 git-browse-help.sh (Christian Couder 2007-12-02 06:07:55 +0100 154) case "$browser" in
81f42f11 git-web--browse.sh (Giuseppe Bilotta 2010-12-03 17:47:38 +0100 155) firefox|iceweasel|seamonkey|iceape)
5d6491c7 git-browse-help.sh (Christian Couder 2007-12-02 06:07:55 +0100 156)    # Check version because firefox < 2.0 do
5d6491c7 git-browse-help.sh (Christian Couder 2007-12-02 06:07:55 +0100 157)    vers=$(expr "$($browser_path -version)" 
5d6491c7 git-browse-help.sh (Christian Couder 2007-12-02 06:07:55 +0100 158)    NEWTAB='-new-tab'
5d6491c7 git-browse-help.sh (Christian Couder 2007-12-02 06:07:55 +0100 159)    test "$vers" -lt 2 && NEWTAB=''
a0685a4f git-web--browse.sh (Dmitry Potapov   2008-02-09 23:22:22 -0800 160)    "$browser_path" $NEWTAB "$@" &

And you want to know the history of what is now line 155.

Then:

((aa27064...))[mlm@macbook:~/w/mlm/git]
$ git log --topo-order --graph -u -L 155,155:git-web--browse.sh
* commit 81f42f11496b9117273939c98d270af273c8a463
| Author: Giuseppe Bilotta <giuseppe.bilotta@gmail.com>
| Date:   Fri Dec 3 17:47:38 2010 +0100
| 
|     web--browse: support opera, seamonkey and elinks
|     
|     The list of supported browsers is also updated in the documentation.
|     
|     Signed-off-by: Giuseppe Bilotta <giuseppe.bilotta@gmail.com>
|     Signed-off-by: Junio C Hamano <gitster@pobox.com>
| 
| diff --git a/git-web--browse.sh b/git-web--browse.sh
| --- a/git-web--browse.sh
| +++ b/git-web--browse.sh
| @@ -143,1 +143,1 @@
| -firefox|iceweasel)
| +firefox|iceweasel|seamonkey|iceape)
|  
* commit a180055a47c6793eaaba6289f623cff32644215b
| Author: Giuseppe Bilotta <giuseppe.bilotta@gmail.com>
| Date:   Fri Dec 3 17:47:36 2010 +0100
| 
|     web--browse: coding style
|     
|     Retab and deindent choices in case statements.
|     
|     Signed-off-by: Giuseppe Bilotta <giuseppe.bilotta@gmail.com>
|     Signed-off-by: Junio C Hamano <gitster@pobox.com>
| 
| diff --git a/git-web--browse.sh b/git-web--browse.sh
| --- a/git-web--browse.sh
| +++ b/git-web--browse.sh
| @@ -142,1 +142,1 @@
| -    firefox|iceweasel)
| +firefox|iceweasel)
|  
* commit 5884f1fe96b33d9666a78e660042b1e3e5f9f4d9
  Author: Christian Couder <chriscool@tuxfamily.org>
  Date:   Sat Feb 2 07:32:53 2008 +0100

      Rename 'git-help--browse.sh' to 'git-web--browse.sh'.

      Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
      Signed-off-by: Junio C Hamano <gitster@pobox.com>

  diff --git a/git-web--browse.sh b/git-web--browse.sh
  --- /dev/null
  +++ b/git-web--browse.sh
  @@ -0,0 +127,1 @@
  +    firefox|iceweasel)

If you use this functionality frequently, you might find a git alias useful. To do that, put in your ~/.gitconfig:

[alias]
    # Follow evolution of certain lines in a file
    # arg1=file, arg2=first line, arg3=last line or blank for just the first line
    follow = "!sh -c 'git log --topo-order -u -L $2,${3:-$2}:"$1"'" -

And now you can just do git follow git-web--browse.sh 155.

这篇关于Git:发现哪些提交触及了一系列的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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