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


当前回答

在看了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 log --author="<authorname>" --oneline --shortstat

这将提供当前HEAD上所有提交的统计信息。如果你想在其他分支中添加统计数据,你必须将它们作为参数提供给git log。

对于传递到脚本,即使删除“一行”格式也可以使用空日志格式完成,正如Jakub narabulbski所评论的那样,——numstat是另一种替代方法。它生成每个文件而不是每个行统计数据,但更容易解析。

git log --author="<authorname>" --pretty=tformat: --numstat

在看了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

这个脚本可以做到这一点。把它放入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 ":"

这给出了关于作者的一些统计信息,可以根据需要修改。

使用呆呆的:

git log --author="_Your_Name_Here_" --pretty=tformat: --numstat \
| gawk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s removed lines: %s total lines: %s\n", add, subs, loc }' -

在Mac OSX上使用Awk:

git log --author="_Your_Name_Here_" --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 }' -

使用count-lines git-alias:

简单地创建计数行别名(每个系统一次),像这样:

git config --global alias.count-lines "! git log --author=\"\$1\" --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 }' #"

以后再用,比如:

git count-lines email@example.com

对于Windows,将Git-Bash添加到PATH(环境变量)后生效。 对于Linux,可能用gawk替换awk部分。 对于MacOS,无需任何更改即可正常工作。

使用现有脚本(更新2017)

在github上有一个新包,看起来很光滑,并使用bash作为依赖项(在linux上测试)。它更适合直接使用,而不是脚本。

它是git-quick-stats (github链接)。

复制git-quick-stats到一个文件夹,并将该文件夹添加到path。

mkdir ~/source
cd ~/source
git clone git@github.com:arzzen/git-quick-stats.git
mkdir ~/bin
ln -s ~/source/git-quick-stats/git-quick-stats ~/bin/git-quick-stats
chmod +x ~/bin/git-quick-stats
export PATH=${PATH}:~/bin

用法:

git-quick-stats

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

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