【问题标题】:git: number of lines *not* changed since specific commit?git:自特定提交以来*未*更改的行数?
【发布时间】:2016-09-09 18:22:58
【问题描述】:

有很多很好的命令行 fu 的答案来查找更改(或更改统计信息),但我想找到相反的答案:自一个特定的提交?

我能找到的最接近的是:How to find which files have not changed since commit?,但我想知道有多少行(理想情况下:每个文件中)保持不变,而不是哪些文件。

所以,基本上:除了插入和删除之外,git diff --stat 可以输出 unchanged 行吗?

或者,我想 git ls-files、git blame 和一些 awk 魔术可能会解决问题,但我还没有完全弄清楚。 -- 例如,不是用最后一次更改的提交号标记每一行,我可以让 git-blame 指示此更改是在给定提交之前还是之后发生的?再加上 grep 和 wc -l 这会让我到达那里。

【问题讨论】:

  • 计算所有行并减去更改的行。
  • @fedorqui 原则上是的,是的,但是我在每个文件都完成此操作时遇到了麻烦(也许我只是使用了正确的命令......) - 例如,我可以计算所有行(每个文件)通过 git ls-files | xargs wc -l;我可以通过 git diff --numstat HEAD 来计算更改的数量(每个文件),但是有没有一种连接两者的好方法?
  • git blame --date=raw 会给你一个原始形式的日期,可以很好地与 awk 配合使用。可以使用答案中显示的 rev-parse 技巧找到截止日期。

标签: git git-diff git-blame


【解决方案1】:
git diff HEAD~ HEAD && echo files that changed
git rev-parse HEAD && echo hash of current rev
wc -l <filename> && echo total lines
git blame <filename> | grep -v -c -e"<first8bytesofhash>"  && echo unchanged lines
git blame <filename> | grep -c -e"<first8bytesofhash>" && echo changed lines

【讨论】:

  • 感谢您的回复。但是,如果我没看错,这仅适用于由单个提交引入的更改(因为 git blame 的每行标签是“此提交”或“之前的所有内容”)。如果我想比较的提交是很久以前的,我将如何应用它——换句话说,我如何知道提交哈希是在“之前”还是“之后”类别中?
【解决方案2】:

我尝试使用 Python:

import commands
s,o=commands.getstatusoutput('git tag start')
s,o=commands.getstatusoutput('git log --pretty=%H --max-parents=0')
roots=o.split()
result=set()
for root in roots:
  s,o=commands.getstatusoutput('git reset root')
  s,o=commands.getstatusoutput('git ls-files')
  all=set(o.split())
  s,o=commands.getstatusoutput('git ls-files --modified')
  modified=set(o.split())
  unchanged=all-modified
  result=result|unchanged
print result
s,o=commands.getstatusoutput('git reset start --hard')

【讨论】:

    【解决方案3】:

    想通了。关键是 git blame 可以指定日期范围(参见https://git-scm.com/docs/git-blame,“指定范围”部分)。假设 123456 是我要比较的提交。与

    git blame 123456..
    

    “自范围边界 [...] 以来未更改的行被归咎于该范围边界提交”,也就是说,它将显示自该提交以来未更改的所有内容为“^123456”。因此,每个文件,我的问题的答案是

    git blame 123456.. $file | grep -P "^\^123456" | wc -l # unchanged since
    git blame 123456.. $file | grep -Pv "^\^123456" | wc -l # new since
    

    包装成 bash 脚本以检查 repo (git ls-files) 中的所有文件并打印漂亮:

    #!/bin/bash
    
    total_lines=0;
    total_lines_unchanged=0;
    total_lines_new=0;
    
    echo "--- total unchanged new filename ---"
    
    for file in `git ls-files | \
      <can do some filtering of files here with grep>`
    do
      # calculate stats for this file
      lines=`cat $file | wc -l`
      lines_unchanged=`git blame 123456.. $file | grep -P "^\^123456" | wc -l`
      lines_new=`git blame 123456.. $file | grep -Pv "^\^123456" | wc -l`
    
      # print pretty
      lines_pretty="$(printf "%6d" $lines)"
      lines_unchanged_pretty="$(printf "%6d" $lines_unchanged)"
      lines_new_pretty="$(printf "%6d" $lines_new)"
      echo "$lines_pretty $lines_unchanged_pretty $lines_new_pretty $file"
    
      # add to total
      total_lines=$(($total_lines + $lines))
      total_lines_unchanged=$(($total_lines_unchanged + $lines_unchanged))
      total_lines_new=$(($total_lines_new + $lines_new))
    done
    
    # print total
    echo "--- total unchanged new ---"
    
    lines_pretty="$(printf "%6d" $total_lines)"
    lines_unchanged_pretty="$(printf "%6d" $total_lines_unchanged)"
    lines_new_pretty="$(printf "%6d" $total_lines_new)"
    echo "$lines_pretty $lines_unchanged_pretty $lines_new_pretty TOTAL"
    

    感谢 Gregg 的回答,让我更多地研究了 git-blame 的选项!

    【讨论】:

      【解决方案4】:
      $ wc -l main.c
      718 main.c
      $ git diff --numstat v2.0.0 main.c
      152     70      main.c
      

      即从 v2.0.0 开始,当前 main.c 的 152 行被更改或添加,因此 566 行从那时起没有变化。

      lines-unchanged-in-since () {
              set -- $2 `wc -l $1` `git diff --numstat $2 $1` 
              echo $(($2-$4)) lines unchanged in $3 since $1
      }
      

      【讨论】:

        猜你喜欢
        • 2014-07-07
        • 1970-01-01
        • 1970-01-01
        • 2016-12-24
        • 2020-12-15
        • 2018-09-22
        • 2017-06-30
        • 2018-08-15
        相关资源
        最近更新 更多