如何在 git 中显示作者贡献的统计数据?

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

我正在与一个团队合作开发 git 项目,我希望看到每个作者在编写的行数或编辑的行数等方面的贡献...我如何显示作者的统计数据?

git
6个回答
125
投票

很简单:

git shortlog -s -n



33
投票

对于那些更喜欢内置解决方案的人,此脚本只需使用

git
grep
awk
:

示例

$ git user-stats
Email                           Commits         Files           Insertions      Deletions       Total Lines
-----                           -------         -----           ----------      ---------       -----------
[email protected]            289             35              5361            3293            8654
[email protected]              142             17              2631            1756            4387
[email protected]              115             9               1407            1107            2514
$ git -C path/to/repo user-stats --since="1 week ago"
Email                           Commits         Files           Insertions      Deletions       Total Lines
-----                           -------         -----           ----------      ---------       -----------
[email protected]              20              3               83              634             717
[email protected]            21              2               242             110             352

使用方法

$ git [git options] user-stats [git-log options]

显示代码

#!/bin/bash
#
# Show user stats (commits, files modified, insertions, deletions, and total
# lines modified) for a repo

git_log_opts=( "$@" )

git log "${git_log_opts[@]}" --format='author: %ae' --numstat \
    | tr '[A-Z]' '[a-z]' \
    | grep -v '^$' \
    | grep -v '^-' \
    | awk '
        {
            if ($1 == "author:") {
                author = $2;
                commits[author]++;
            } else {
                insertions[author] += $1;
                deletions[author] += $2;
                total[author] += $1 + $2;
                # if this is the first time seeing this file for this
                # author, increment their file count
                author_file = author ":" $3;
                if (!(author_file in seen)) {
                    seen[author_file] = 1;
                    files[author]++;
                }
            }
        }
        END {
            # Print a header
            printf("%-30s\t%-10s\t%-10s\t%-10s\t%-10s\t%-10s\n",
                   "Email", "Commits", "Files",
                   "Insertions", "Deletions", "Total Lines");
            printf("%-30s\t%-10s\t%-10s\t%-10s\t%-10s\t%-10s\n",
                   "-----", "-------", "-----",
                   "----------", "---------", "-----------");
            
            # Print the stats for each user, sorted by total lines
            n = asorti(total, sorted_emails, "@val_num_desc");
            for (i = 1; i <= n; i++) {
                email = sorted_emails[i];
                printf("%-30s\t%-10s\t%-10s\t%-10s\t%-10s\t%-10s\n",
                       email, commits[email], files[email],
                       insertions[email], deletions[email], total[email]);
            }
        }
'

查看要点

安装

下载脚本,赋予其可执行权限,然后将其粘贴到路径中的某个位置。例如:

wget -O ~/bin/git-user-stats https://gist.githubusercontent.com/shitchell/783cc8a892ed1591eca2afeb65e8720a/raw/git-user-stats
chmod +x ~/bin/git-user-stats
cd ~/path/to/repo
git user-stats --since="1 week ago"

说明

基本上它使用

git log --format="author: %ae" --numstat
(减去任何空行或二进制文件)来生成如下所示的输出:

author: [email protected]
1       147     foo/bar.py
0       370     hello/world.py
author: [email protected]
7       6       foo/bar.py
author: [email protected]
1       0       super/sekrit.txt
author: [email protected]
2       1       hello/world.py

author: ...
开头的每个部分都是一次提交。
--numstat
的第一列是该文件的插入次数,第二列是删除次数。

然后它会用

awk
遍历每条线。每当它遇到以
author:
开头的行时,它都会将该行的第二列(该特定提交的作者的电子邮件地址)存储在变量
author
中,并增加该用户的提交总数。对于后续的每一行,它都会更新该用户的插入、删除和文件数,直到到达以
author:
开头的下一行。冲洗并重复直至完成。

最后,它按总行更改(插入+删除)排序并打印出所有收集的统计信息。如果您想按其他方式排序,只需将

total
数组替换为
asorti(...)
函数中的相关数组即可。例如,要按文件数量排序,您可以将该行更改为:

n = asorti(files, sorted_emails, "@val_num_desc");

注意 任何参数/选项都将传递给

git log
命令,并可用于过滤结果
git user-stats --since="2 weeks ago"

再详细一点

git log
输出经过:

  • tr '[A-Z]' '[a-z]'
    标准化电子邮件地址。我的公司按照
    [email protected]
    的方式将电子邮件地址大写,并且根据用户提交的位置/方式,该电子邮件可能会显示为大写或全部小写。这可确保特定电子邮件地址的所有实例始终分组在一起,无论大小写如何。
  • grep -v '^$'
    删除日志输出中默认显示的空行
  • grep -v '^-'
    删除二进制文件的
    --numstat
    信息,如下所示:
    -      -  foo/bar.png

此外,

git
的一个很酷的功能让我花了很长时间才发现,如果你将一个名为
git-some-command
的可执行文件放在你的路径中的文件夹中,git会检测到它,你可以通过
git some-command
使用它!这样做的另一个好处是能够在每个命令的基础上指定自定义配置设置,例如
git -c color.ui=always some-command | sed ...
。因此,如果您将此脚本放入
~/bin/git-user-stats
中,那么您就可以通过
git user-stats
使用它,如示例中所示 🙂


8
投票

您应该看看repoXplorer,这是我开发的一个开源项目。它能够计算一个项目(一组 git 存储库)以及一个贡献者和一组贡献者的统计数据。它提供了 REST 接口和 Web UI。 Web UI 提供了各种信息,例如给定贡献者的信息:

  • 提交、行更改和项目计数
  • 提交日期直方图
  • 按提交排名最高的项目
  • 排名靠前的项目发生了变化

但最好是查看演示实例这里

这是贡献者统计页面的屏幕截图(统计数据是在由 repoplorer 索引的所有存储库中计算的,但可以针对特定项目进行过滤):

enter image description here


2
投票

我建议Gitential。它测量:

  • 编码量
  • 编码时间
  • 生产力
  • 效率

并提供分析界面以在多个层面上可视化它们:

  • 项目
  • 团队
  • 回购
  • 开发商

它还可以删除重复的作者身份并过滤可疑的提交,以提供更好的了解。


0
投票

我发现的最简单的方法是运行此行,它针对当前文件夹执行此操作,因此您可以

cd
进入您感兴趣的文件夹并运行它。请注意,您还可以使用
':!package-lock.json'
:

排除目录
git log --format='%aN' | sort -u | while read name; do
    echo -en "$name\t"
    git log --author="$name" --pretty=tformat: --numstat -- . ':!node_modules' ':!package-lock.json' |
    awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }' -
done
© www.soinside.com 2019 - 2024. All rights reserved.