git:行数*自特定提交以来未更改? [英] git: number of lines *not* changed since specific commit?

查看:115
本文介绍了git:行数*自特定提交以来未更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有许多很好的命令行选项可以找到更改(或更改统计信息),但我想发现相反的情况:自从以来, 没改变多少行(每个文件)特定的提交?

There are plenty of answers with great command line fu to find changes (or change statistics), but I'd like to find the opposite: how many lines (per file) have not changed since a particular commit?

我能找到的最接近的是:如何找出自提交以来未更改的文件?,但我想知道有多少行(理想情况下:每个文件中)未改变而存活了,而不是哪些文件.

The closest I could find is this: How to find which files have not changed since commit? but I'd like to know how many lines (ideally: in each file) have survived unchanged, not which files.

因此,基本上:除了插入和删除操作外,git diff --stat还可以输出未更改行吗?

So, basically: can git diff --stat output unchanged lines in addition to insertions and deletions?

或者,我想象git ls文件,git blame和一些awk魔术可能会解决问题,但是我还无法弄清楚. -例如,除了用最后更改的提交号标记每行外,我还可以用git-blame来指示此更改是在给定提交之前还是之后进行的?连同grep和wc -l一起带我到那里.

Alternatively, I'd imagine that git ls-files, git blame and some awk magic might do the trick, but I haven't been able to figure it out quite yet. -- For example, rather than label each line with the commit number of the last change, can I get git-blame to indicate if this change occurred before or after a given commit? Together with grep and wc -l that would get me there.

推荐答案

弄清楚了.关键是git blame可以指定日期范围(请参见 https://git-scm.com/docs/git-blame ,指定范围"部分).假设123456是我要比较的提交.使用

Figured it out. The key is that git blame can specify date ranges (see https://git-scm.com/docs/git-blame, section "SPECIFYING RANGES"). Assume 123456 is the commit I want to compare to. With

git blame 123456..

该范围边界提交归咎于自范围边界以来未更改的行",即,它将显示自该提交以来未更改的所有内容为"^ 123456".daccess-ods.un.org daccess-ods.un.org因此,每个文件,我的问题的答案是

"lines that have not changed since the range boundary [...] are blamed for that range boundary commit", that is, it will show everything that hasn't changed since that commit as "^123456". Thus, per file, the answer to my question is

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文件)并打印漂亮:

Wrapped into bash script to go over all files in repo (git ls-files) and printing pretty:

#!/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的选项!

Thanks to Gregg for his answer, which had me look into the options for git-blame more!

这篇关于git:行数*自特定提交以来未更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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