如何统计当前代码库各用户贡献行数并忽略指定文件类型
Git仓库按作者统计代码行数(支持忽略指定文件类型)
可直接运行的基础统计命令
该命令可直接输出每个作者的新增行数、删除行数、净行数,默认自动跳过二进制文件统计:
git log --pretty=format:'#AUTHOR:%an' --numstat | awk ' BEGIN { print "作者\t新增行数\t删除行数\t净行数" } /^#AUTHOR:/ { current_author = substr($0, 9) next } { # 过滤二进制文件记录 if ($1 == "-" || $2 == "-") next add[current_author] += $1 subs[current_author] += $2 loc[current_author] += $1 - $2 } END { for (author in add) { printf "%s\t%d\t%d\t%d\n", author, add[author], subs[author], loc[author] } }'
自定义忽略特定文件/文件类型
如需忽略指定后缀的文件或者目录,在git log命令末尾添加排除规则即可,示例为忽略所有.md、.yaml后缀文件、dist目录下所有文件的统计:
git log --pretty=format:'#AUTHOR:%an' --numstat -- ':(exclude)*.md' ':(exclude)*.yaml' ':(exclude)dist/*' | awk ' BEGIN { print "作者\t新增行数\t删除行数\t净行数" } /^#AUTHOR:/ { current_author = substr($0, 9) next } { if ($1 == "-" || $2 == "-") next add[current_author] += $1 subs[current_author] += $2 loc[current_author] += $1 - $2 } END { for (author in add) { printf "%s\t%d\t%d\t%d\n", author, add[author], subs[author], loc[author] } }'
补充说明
- 统计范围默认为当前分支全量提交记录,如需限定统计时间段,可给
git log增加--since、--until参数,示例:增加--since="2024-01-01" --until="2024-06-30"即可统计2024年上半年的代码变更 - 净行数=新增行数-删除行数,仅做参考,实际代码贡献量建议结合提交内容综合判断
内容的提问来源于stack exchange,提问作者pizza_rat_matt
相关产品推荐
相关产品推荐

