我如何“滥用”责备(或一些更合适的函数,和/或与shell命令结合)来给我一个关于当前存储库中有多少行(代码)来自每个提交者的统计数据?
示例输出:
Committer 1: 8046 Lines
Committer 2: 4378 Lines
我如何“滥用”责备(或一些更合适的函数,和/或与shell命令结合)来给我一个关于当前存储库中有多少行(代码)来自每个提交者的统计数据?
示例输出:
Committer 1: 8046 Lines
Committer 2: 4378 Lines
当前回答
请从http://gitstats.sourceforge.net/查看gitstats命令
其他回答
git shortlog -sec
这将显示每个作者的提交列表。
git ls-tree -r HEAD|sed -re 's/^.{53}//'|while read filename; do file "$filename"; done|grep -E ': .*text'|sed -r -e 's/: .*//'|while read filename; do git blame -w "$filename"; done|sed -r -e 's/.*\((.*)[0-9]{4}-[0-9]{2}-[0-9]{2} .*/\1/' -e 's/ +$//'|sort|uniq -c
逐级讲解:
列出版本控制下的所有文件
git ls-tree -r HEAD|sed -re 's/^.{53}//'
将列表修剪为仅文本文件
|while read filename; do file "$filename"; done|grep -E ': .*text'|sed -r -e 's/: .*//'
Git责备所有的文本文件,忽略空白的变化
|while read filename; do git blame -w "$filename"; done
找出作者的名字
|sed -r -e 's/.*\((.*)[0-9]{4}-[0-9]{2}-[0-9]{2} .*/\1/' -e 's/ +$//'
排序作者列表,并让uniq计算连续重复的行数
|sort|uniq -c
示例输出:
1334 Maneater
1924 Another guy
37195 Brian Ruby
1482 Anna Lambda
我有这个解决方案,计数在所有文本文件(不包括二进制文件,甚至版本化的):
IFS=$'\n'
for file in $(git ls-files); do
git blame `git symbolic-ref --short HEAD` --line-porcelain "$file" | \
grep "^author " | \
grep -v "Binary file (standard input) matches" | \
grep -v "Not Committed Yet" | \
cut -d " " -f 2-
done | \
sort | \
uniq -c | \
sort -nr
我自己的脚本是@nilbus和@Alex的组合
#!/bin/sh
for f in $(git ls-tree -r --name-only HEAD --);
do
j=$(file "$f" | grep -E ': .*text'| sed -r -e 's/: .*//');
if [ "$f" != "$j" ]; then
continue;
fi
git blame -w --line-porcelain HEAD "$f" | grep "^author " | sed 's/author //'`enter code here`
done | sort | uniq -c | sort -nr
下面是来自@Alex的回答的主要片段,它实际上做了聚集指责行的操作。我已经将其缩减为对单个文件而不是一组文件进行操作。
git blame --line-porcelain path/to/file.txt | grep "^author " | sort | uniq -c | sort -nr
我在这里发表这篇文章是因为我经常回到这个答案,重新阅读这篇文章,重新消化例子,以提取我重视的部分,这是很费力的。对于我的用例来说,它也不够通用;它的范围是整个C项目。
我喜欢列出每个文件的统计数据,通过使用bash for迭代器而不是xargs来实现,因为我发现xargs可读性较差,很难使用/记忆,xargs vs for的优点/缺点应该在其他地方讨论。
下面是一个实用的代码片段,它将单独显示每个文件的结果:
for file in $(git ls-files); do \
echo $file; \
git blame --line-porcelain $file \
| grep "^author " | sort | uniq -c | sort -nr; \
echo; \
done
我测试过,在bash shell中直接运行它是ctrl+c安全的,如果你需要把它放在bash脚本中,如果你想让用户能够打破你的for循环,你可能需要在SIGINT和SIGTERM上捕获。