有很多答案都可以使用出色的命令行 fu 来查找更改(或更改统计信息),但我想找到相反的内容:从那时起,有多少行(每个文件)没有更改特定的提交?
我能找到的最接近的是:How to find which files have not changed since commit?但我想知道有多少行(理想情况下:每个文件中)保持不变,而不是哪些文件。
那么,基本上:除了插入和删除之外, git diff --stat 还可以输出不变行吗?
或者,我想象 git ls-files、git Blame 和一些 awk 魔法可能会成功,但我还没有弄清楚。 -- 例如,我可以让 git-blame 来指示此更改是发生在给定提交之前还是之后,而不是用最后一次更改的提交编号来标记每一行?与 grep 和 wc -l 一起,我就能到达那里。
最佳答案
想通了。关键是 gitblame 可以指定日期范围(请参阅 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 脚本中以查看存储库中的所有文件(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 的选项!
关于git:自特定提交以来*未*更改的行数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37211955/