我是否可以调用一个命令来计算Git存储库中特定作者更改的行数?我知道一定有方法来计算提交的数量,因为Github为他们的影响图这样做。


当前回答

这是一个伟大的回购,使您的生活更容易

git-quick-stats

在安装了brew的mac上

酿造安装git-quick-stats

Run

git-quick-stats

只需从这个列表中输入列出的数字并按enter键,选择您想要的选项。

 Generate:
    1) Contribution stats (by author)
    2) Contribution stats (by author) on a specific branch
    3) Git changelogs (last 10 days)
    4) Git changelogs by author
    5) My daily status
    6) Save git log output in JSON format

 List:
    7) Branch tree view (last 10)
    8) All branches (sorted by most recent commit)
    9) All contributors (sorted by name)
   10) Git commits per author
   11) Git commits per date
   12) Git commits per month
   13) Git commits per weekday
   14) Git commits per hour
   15) Git commits by author per hour

 Suggest:
   16) Code reviewers (based on git history)

其他回答

这是一个伟大的回购,使您的生活更容易

git-quick-stats

在安装了brew的mac上

酿造安装git-quick-stats

Run

git-quick-stats

只需从这个列表中输入列出的数字并按enter键,选择您想要的选项。

 Generate:
    1) Contribution stats (by author)
    2) Contribution stats (by author) on a specific branch
    3) Git changelogs (last 10 days)
    4) Git changelogs by author
    5) My daily status
    6) Save git log output in JSON format

 List:
    7) Branch tree view (last 10)
    8) All branches (sorted by most recent commit)
    9) All contributors (sorted by name)
   10) Git commits per author
   11) Git commits per date
   12) Git commits per month
   13) Git commits per weekday
   14) Git commits per hour
   15) Git commits by author per hour

 Suggest:
   16) Code reviewers (based on git history)

除了Charles Bailey的回答之外,您可能还想在命令中添加-C参数。否则,即使文件内容没有被修改,文件重命名也会被视为大量的添加和删除(与文件的行数一样多)。

为了说明,当使用git log——oneline——shortstat命令时,这里有一个从我的一个项目中移动的大量文件的提交:

9052459 Reorganized project structure
 43 files changed, 1049 insertions(+), 1000 deletions(-)

这里使用git log——oneline——shortstat -C命令来检测文件的复制和重命名:

9052459 Reorganized project structure
 27 files changed, 134 insertions(+), 85 deletions(-)

在我看来,后者给出了一个人对项目有多大影响的更现实的观点,因为重命名一个文件比从头开始写文件要小得多。

在看了Alex和Gerty3000的答案后,我试着缩短了一行字:

基本上,使用git log numstat,而不跟踪更改的文件数量。

Mac OSX上的Git 2.1.0版本:

git log --format='%aN' | sort -u | while read name; do echo -en "$name\t"; git log --author="$name" --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }' -; done

例子:

Jared Burrows   added lines: 6826, removed lines: 2825, total lines: 4001

我发现下面的方法对于查看当前代码库中谁拥有最多的行很有用:

git ls-files -z | xargs -0n1 git blame -w | ruby -n -e '$_ =~ /^.*\((.*?)\s[\d]{4}/; puts $1.strip' | sort -f | uniq -c | sort -n

其他答案主要集中在提交中更改的行,但如果提交无法存活并被覆盖,则它们可能只是被更改了。上面的咒语还可以让您按行对所有提交者进行排序,而不是一次只排序一个。您可以向git blame (-C -M)添加一些选项,以获得一些更好的数字,将文件移动和文件之间的行移动考虑在内,但如果这样做,该命令可能会运行更长时间。

同样,如果你正在为所有提交者寻找在所有提交中更改的行,下面的小脚本很有帮助:

http://git-wt-commit.rubyforge.org/#git-rank-contributors

@mmrobins @AaronM @ErikZ @JamesMishra提供的变体都有一个共同的问题:他们要求git生成不用于脚本使用的信息的混合物,包括来自存储库的行内容在同一行,然后用regexp匹配混乱。

当某些行不是有效的UTF-8文本时,以及当某些行恰好与regexp匹配时(这里发生了这种情况),就会出现问题。

这是一条修改过的线,没有这些问题。它要求git在单独的行上干净地输出数据,这使得它很容易过滤我们想要的内容:

git ls-files -z | xargs -0n1 git blame -w --line-porcelain | grep -a "^author " | sort -f | uniq -c | sort -n

您可以grep其他字符串,如author-mail, committer等。

也许首先要导出LC_ALL=C(假设是bash)以强制进行字节级处理(这碰巧也大大加快了来自基于utf -8的区域设置的grep的速度)。