获取 git 中每个贡献者的代码行数

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

我一直在为班级开展一个小组项目,我们使用 git 作为协作工具。 我需要找出项目子文件夹之一中的每个协作者有多少行代码。 需要注意的是:我只想要当前部署(主分支)中的代码行。 GitHub 为我们提供了添加的行的全局计数,但没有考虑它们是否保留到此日期,因此它不符合我们的需求。 我们怎样才能做到这一点?

我尝试在 GitHub 上查找统计数据,但没有符合我们目的的数据。不

git github
1个回答
0
投票

忽略关于 LOC 指标有用性的侧面讨论,您可以使用

git blame
命令获得您想要的内容,该命令对于文件中的每一行代码都会打印有关最后修改它的人的信息。

您想要对存储库中的每个文件运行

git blame
,然后汇总结果。

git blame
的默认输出看起来像:

2245e60e (<[email protected]> 2021-08-28 12:36:11 -0400  27)      #include <stdio.h>
2245e60e (<[email protected]> 2021-08-28 12:36:11 -0400  28)      int main() {
2245e60e (<[email protected]> 2021-08-28 12:36:11 -0400  29)        printf("Hello, world\n");
968d2915 (<[email protected]> 2021-08-29 20:03:01 -0400  30)        return 0;
593207f4 (<[email protected]> 2022-02-03 16:38:56 -0500  31)      }

要获取“存储库中的所有文件”的列表,我们可以使用

git ls-files
。我们将使用
-z
标志,以便我们的代码正确处理包含空格的文件名。像这样的东西:

git ls-files -z |
  xargs -0 -n1 git -P blame |
  awk -F'[<>]' '
    {loc[$2]++}
    END {for (i in loc) print i, loc[i]}
  ' | sort -k2 -n

这将为每个作者的电子邮件地址生成 LOC。


如果您的存储库碰巧包含子模块,您可能需要添加额外的

find
过滤器,以避免尝试在目录上运行
git blame

git ls-files -z |
  xargs -0 -IPATH find PATH -maxdepth 0 -type f -print0 |
  xargs -0 -n1 git -P blame |
  awk -F'[<>]' '
    {loc[$2]++}
    END {for (i in loc) print i, loc[i]}
  ' | sort -k2 -n

`

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