2010-05-07 43 views
24

我想查看刪除/添加行的數量,按照作者爲git歷史中給定分支分組的數量。有git shortlog -s,它顯示了每個作者的提交數量。有沒有類似的東西可以得到一個完整的diffstat?顯示每個作者在git中更改的行數

+1

會http://stackoverflow.com/questions/1265040/how-to-count-total-lines-changed-by-a-specific-author-in-a-git-repository幫助嗎?正如'git短日曆 - 編號--summary' – VonC 2010-05-07 08:42:38

+1

@VonC,我'git shortlog --numbered --summary'與'git shortlog -s -n'相同,它只會顯示提交數量,沒有改變的行 – knittl 2010-05-07 08:53:43

回答

18

由於the SO question "How to count total lines changed by a specific author in a Git repository?"並不完全令人滿意,commandlinefu有替代品(而不是每個分支雖然):

git ls-files | while read i; do git blame $i | sed -e 's/^[^(]*(//' -e 's/^\([^[:digit:]]*\)[[:space:]]\+[[:digit:]].*/\1/'; done | sort | uniq -ic | sort -nr 

它包括二進制文件,這是不好的,所以你可以(除去真正隨機二進制文件):

git ls-files | grep -v "\.\(pdf\|psd\|tif\)$" 

(注:如由commentedtrcarden,一個-x--exclude選項是行不通的
git ls-files man pagegit ls-files -x "*pdf" ...只會排除未經跟蹤內容,如果--others--ignored添加到git ls-files命令)

或者:

git ls-files "*.py" "*.html" "*.css" 

只包括特定的文件類型。


儘管如此,"git log"-based solution應該會更好,如:

git log --numstat --pretty="%H" --author="Your Name" commit1..commit2 | awk 'NF==3 {plus+=$1; minus+=$2} END {printf("+%d, -%d\n", plus, minus)}' 

但同樣,這也是一個路徑(這裏是2提交),而不是每枝所有分支。

+1

git log是唯一一個不會讓我失望的好主意! – jjxtra 2012-08-21 22:42:35

+0

您實際上不能通過指定的方法忽略二進制文件。 ls-files上的-x命令僅適用於「未跟蹤文件」常見錯誤。 – trcarden 2014-02-12 09:30:42

+0

@trcarden非常好的一點。我編輯了答案,並提出了另一種排除二進制文件的方法。 – VonC 2014-02-12 09:51:24

2

這裏的腳本將做到這一點。把它放入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 ":" 
+3

在Mac OS X 10.6.8和Debian Linux 5.0.8上使用這個:'/ Users/slippyd/Desktop/git-authorship:line 3:declare:-A:invalid option declare:usage:declare [-afFirtx] [-p] [name [= value] ...]' – 2012-01-27 17:10:06

+0

這個腳本非常好用。謝謝! – ColinM 2012-03-07 17:43:31

+0

在mac上得到與slipp相同的錯誤,我將-A更改爲-a(如錯誤消息中指定的那樣),但腳本無論如何都失敗了,顯然它沒有處理名稱中的空格(如第一個和第姓氏)​​,仍然看起來有效的解決方案,你可能會認爲這是許多人需要的東西,以獲得晉升:),顯然不是。好吧,我只會說90%的老闆! – 2013-09-28 00:03:12

31

這是一個老的文章,但如果有人仍然在尋找它:

安裝git的額外

brew install git-extras 

然後

git summary --line 

https://github.com/tj/git-extras

+2

apt-get install git-extras' for Linux users – alex 2016-07-26 09:51:09

+1

'致命的:無法識別的參數:-line'我認爲他們已經刪除了最新版本 – M2X 2016-07-29 21:07:57

+0

@ M2X的選項,它看起來像'git line-summary'雖然它在文檔中說過,它的作品不贊成使用'--line' https://github.com/tj/git-extras/blob/master/Commands.md#git-line-summary – dav 2016-08-05 17:15:04

1

在我的回購協議我已經在這裏漂浮的單線隊員獲得了很多垃圾輸出是一個Python腳本做是正確的:

import subprocess 
import collections 
import sys 


def get_lines_from_call(command): 
    return subprocess.check_output(command).splitlines() 

def get_files(paths=()): 
    command = ['git', 'ls-files'] 
    command.extend(paths) 
    return get_lines_from_call(command) 

def get_blame(path): 
    return get_lines_from_call(['git', 'blame', path]) 


def extract_name(line): 
    """ 
    Extract the author from a line of a standard git blame 
    """ 
    return line.split('(', 1)[1].split(')', 1)[0].rsplit(None, 4)[0] 


def get_file_authors(path): 
    return [extract_name(line) for line in get_blame(path)] 


def blame_stats(paths=()): 
    counter = collections.Counter() 
    for filename in get_files(paths): 
     counter.update(get_file_authors(filename)) 
    return counter 


def main(): 
    counter = blame_stats(sys.argv[1:]) 
    max_width = len(str(counter.most_common(1)[0][1])) 
    for name, count in reversed(counter.most_common()): 
     print('%s %s' % (str(count).rjust(max_width), name)) 

if __name__ == '__main__': 
    main() 

注意,參數的腳本將被傳遞到git ls-files,所以如果你只是想顯示Python文件: blame_stats.py '**/*.py'

如果你只是想在一個子目錄中顯示文件:blame_stats.py some_dir

依此類推。