我希望能够查看给定Git历史分支中每个作者移除/添加行数的数量。有一个命令git shortlog -s
可以显示每个作者的提交数量。是否有类似的方法来获取总体的差异统计信息?
虽然这是一篇老文章,但如果还有人在寻找:
安装git extras
brew install git-extras
然后git summary --line
apt-get install git-extras
来安装git-extras。 - alexgit line-summary
可以工作,尽管文档中说它已经被弃用,推荐使用 --line
https://github.com/tj/git-extras/blob/master/Commands.md#git-line-summary - dav--line
。 - Sakari Cajanus一行代码(支持时间范围选择):
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
输出:
user-a: +5455 -3471
user-b: +5118 -1934
由于这个SO问题“如何计算Git存储库中特定作者更改的总行数?”不完全令人满意,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\)$"
-x
或--exclude
选项无效。从git ls-files
手册页中可以看出,如果将--others
或--ignored
添加到git ls-files
命令中,则git ls-files -x "*pdf" ...
仅排除未跟踪的内容。)git ls-files "*.py" "*.html" "*.css"
仅包含特定的文件类型。
然而,基于"git log
"的解决方案应该更好,例如:
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个提交),而不是每个分支的所有分支。
#!/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 ":"
/Users/slippyd/Desktop/git-authorship: line 3: declare: -A: invalid option declare: usage: declare [-afFirtx] [-p] [name[=value] ...]
- Slipp D. Thompson#!/bin/bash
代替#!/bin/sh
。 - Donatas Olsevičiusbash
,我遇到了和 Pizzaiola 相同的问题,我认为这是与空格有关的错误。 - Karthik T我在我的代码库中遇到了很多不良输出,因此这里提供一份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
以此类推。
以下命令的输出应该很容易发送到脚本中以累加总数:
git log --author="<authorname>" --oneline --shortstat
这将为当前 HEAD 上的所有提交提供统计信息。如果您想要累加其他分支中的统计信息,则必须将它们作为参数提供给 git log。
git shortlog abranch --numbered --summary
。 - VonCgit shortlog --numbered --summary
和git shortlog -s -n
是一样的,它只会显示提交次数,而不是更改的行数。 - knittl