在git中显示每位作者更改的行数

问题描述 投票:26回答:6

我想看看删除/添加行的数量,按作者分组给git历史中的给定分支。有git shortlog -s,它显示了每位作者的提交数量。是否有类似的东西获得整体diffstat?

git diff history logging
6个回答
38
投票

这是一个老帖子,但如果有人还在寻找它:

安装git extras

brew install git-extras

然后

git summary --line

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


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\)$"

(注意:由commented作为trcarden-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

但同样,这是针对一条路径(此处为2次提交),而不是针对每个分支的所有分支。


6
投票

一行代码(支持时间范围选择):

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

说明:

git log --since=4.weeks --numstat --pretty="%ae %H" | sed 's/@.*//g' | awk '{ if (NF == 1){ name = $1}; if(NF == 3) {plus[name] += $1; minus[name] += $2}} END { for (name in plus) {print name": +"plus[name]" -"minus[name]}}' | sort -k2 -gr

输出:

git log --since=4.weeks --numstat --pretty="%ae %H" \
    | sed 's/@.*//g'  \
    | awk '{ if (NF == 1){ name = $1}; if(NF == 3) {plus[name] += $1; minus[name] += $2}} END { for (name in plus) {print name": +"plus[name]" -"minus[name]}}' \
    | sort -k2 -gr

# query log by time range
# get author email prefix
# count plus / minus lines
# sort result

2
投票

这个脚本在这里就可以了。把它放入authorship.sh,chmod + x它,你就完成了。

user-a: +5455 -3471
user-b: +5118 -1934

2
投票

来自#!/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 ":"

以下命令的输出应该相当容易发送到脚本以添加总计:

How to count total lines changed by a specific author in a Git repository?

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


1
投票

在我的回购中,我已经从浮动的单行中获得了大量的垃圾输出,所以这是一个Python脚本来做正确的:

git log --author="<authorname>" --oneline --shortstat

请注意,脚本的参数将传递给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() ,因此如果您只想显示Python文件:git ls-files

如果您只想在一个子目录中显示文件:blame_stats.py '**/*.py'

等等。

© www.soinside.com 2019 - 2024. All rights reserved.