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


当前回答

除了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(-)

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

其他回答

AaronM使用shell一行程序得到的答案很好,但实际上,还有另一个错误,如果用户名和日期之间有不同数量的空格,空格会破坏用户名。损坏的用户名将给出多行用户计数,您必须自己将它们相加。

这个小小的改变解决了我的问题:

git ls-files -z | xargs -0n1 git blame -w --show-email | perl -n -e '/^.*?\((.*?)\s+[\d]{4}/; print $1,"\n"' | sort -f | uniq -c | sort -n

注意\s后面的+,它将占用从名称到日期的所有空白。

实际上,添加这个答案既是为了帮助别人,也是为了我自己的记忆,因为这至少是我第二次谷歌这个主题:)

增加了——show-email to git blame -w来聚合email,因为有些人在不同的计算机上使用不同的Name格式,有时两个同名的人在同一个git中工作。

这是最好的方法,它还可以让您清楚地了解所有用户提交的总数

git shortlog -s -n

对于windows用户,可以使用以下批处理脚本计算指定作者添加/删除的行数

@echo off

set added=0
set removed=0

for /f "tokens=1-3 delims= " %%A in ('git log --pretty^=tformat: --numstat --author^=%1') do call :Count %%A %%B %%C

@echo added=%added%
@echo removed=%removed%
goto :eof

:Count
  if NOT "%1" == "-" set /a added=%added% + %1
  if NOT "%2" == "-" set /a removed=%removed% + %2
goto :eof

https://gist.github.com/zVolodymyr/62e78a744d99d414d56646a5e8a1ff4f

这个脚本可以做到这一点。把它放入authorship.sh, chmod +x,就完成了。

#!/bin/sh
declare -A map
while read line; do
    if grep "^[a-zA-Z]" <<< "$line" > /dev/null; then
        current="$line"
        if [ -z "${map[$current]}" ]; then 
            map[$current]=0
        fi
    elif grep "^[0-9]" <<<"$line" >/dev/null; then
        for i in $(cut -f 1,2 <<< "$line"); do
            map[$current]=$((map[$current] + $i))
        done
    fi
done <<< "$(git log --numstat --pretty="%aN")"

for i in "${!map[@]}"; do
    echo -e "$i:${map[$i]}"
done | sort -nr -t ":" -k 2 | column -t -s ":"

到目前为止,我确定的最好的工具是gitinspector。它给每个用户,每周等设置报告 你可以像下面这样用npm安装

NPM install -g gitinspector

这些链接可以获得更多详细信息

https://www.npmjs.com/package/gitinspector

https://github.com/ejwa/gitinspector/wiki/Documentation

https://github.com/ejwa/gitinspector

命令示例如下

gitinspector -lmrTw 
gitinspector --since=1-1-2017 etc