我如何“滥用”责备(或一些更合适的功能,和/或与shell命令一起使用),以统计当前存储库中来自每个提交者的行数(代码)?
示例输出:
Committer 1: 8046 Lines
Committer 2: 4378 Lines
git ls-tree -r -z --name-only HEAD -- */*.c | xargs -0 -n1 git blame \
--line-porcelain HEAD |grep "^author "|sort|uniq -c|sort -nr
我在路上更新了一些东西。
为了方便起见,您也可以将其放入自己的命令中:
#!/bin/bash
# save as i.e.: git-authors and set the executable flag
git ls-tree -r -z --name-only HEAD -- $1 | xargs -0 -n1 git blame \
--line-porcelain HEAD |grep "^author "|sort|uniq -c|sort -nr
将此存储在您的路径中的某处或修改您的路径并像使用它一样
git authors '*/*.c' # look for all files recursively ending in .c
git authors '*/*.[ch]' # look for all files recursively ending in .c or .h
git authors 'Makefile' # just count lines of authors in the Makefile
虽然可接受的答案可以完成工作,但速度非常慢。
$ git ls-tree --name-only -z -r HEAD|egrep -z -Z -E '\.(cc|h|cpp|hpp|c|txt)$' \
|xargs -0 -n1 git blame --line-porcelain|grep "^author "|sort|uniq -c|sort -nr
几乎是瞬时的。
要获取当前跟踪的文件列表,可以使用
git ls-tree --name-only -r HEAD
此解决方案避免调用file
来确定文件类型,并出于性能原因使用grep匹配所需的扩展名。如果应包括所有文件,只需将其从行中删除。
grep -E '\.(cc|h|cpp|hpp|c)$' # for C/C++ files
grep -E '\.py$' # for Python files
如果文件可以包含空格,这对shell不利,则可以使用:
git ls-tree -z --name-only -r HEAD | egrep -Z -z '\.py'|xargs -0 ... # passes newlines as '\0'
给出文件列表(通过管道),可以使用xargs调用命令并分发参数。允许处理多个文件的命令忽略-n1
。在这种情况下,我们调用,git blame --line-porcelain
并且每次调用都使用1个参数。
xargs -n1 git blame --line-porcelain
然后,我们针对出现的“作者”过滤输出进行排序,并通过以下方式对重复的行进行计数:
grep "^author "|sort|uniq -c|sort -nr
其他答案实际上过滤掉仅包含空格的行。
grep -Pzo "author [^\n]*\n([^\n]*\n){10}[\w]*[^\w]"|grep "author "
上面的命令将打印包含至少一个非空白字符的行的作者。您还可以使用match \w*[^\w#]
,它还将排除第一个非空白字符不是a的行#
(在许多脚本语言中为注释)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句