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


当前回答

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

使用呆呆的:

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

其他回答

要统计给定作者(或所有作者)在给定分支上提交的数量,可以使用git-shortlog;特别是它的——编号和——摘要选项,例如在git存储库上运行时:

$ git shortlog v1.6.4 --numbered --summary
  6904  Junio C Hamano
  1320  Shawn O. Pearce
  1065  Linus Torvalds
    692  Johannes Schindelin
    443  Eric Wong

该问题要求提供关于特定作者的信息,但许多答案都是根据修改的代码行返回作者排名列表的解决方案。

这正是我想要的,但现有的解决方案并不完美。为了方便人们通过谷歌找到这个问题,我对它们进行了一些改进,并将其制成一个shell脚本,下面显示该脚本。

它不依赖于Perl或Ruby。此外,空格、重命名和行移动都在行更改计数中被考虑在内。只需将其放入一个文件中,并将Git存储库作为第一个参数传递。

#!/bin/bash
git --git-dir="$1/.git" log > /dev/null 2> /dev/null
if [ $? -eq 128 ]
then
    echo "Not a git repository!"
    exit 128
else
    echo -e "Lines  | Name\nChanged|"
    git --work-tree="$1" --git-dir="$1/.git" ls-files -z |\
    xargs -0n1 git --work-tree="$1" --git-dir="$1/.git" blame -C -M  -w |\
    cut -d'(' -f2 |\
    cut -d2 -f1 |\
    sed -e "s/ \{1,\}$//" |\
    sort |\
    uniq -c |\
    sort -nr
fi

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

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)

对于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

下面是一个简短的一行代码,用于生成所有作者的统计信息。它比Dan在https://stackoverflow.com/a/20414465/1102119上的解决方案快得多(我的解决方案的时间复杂度是O(N),而不是O(NM),其中N是提交的数量,M是作者的数量)。

git log --no-merges --pretty=format:%an --numstat | awk '/./ && !author { author = $0; next } author { ins[author] += $1; del[author] += $2 } /^$/ { author = ""; next } END { for (a in ins) { printf "%10d %10d %10d %s\n", ins[a] - del[a], ins[a], del[a], a } }' | sort -rn