我们在参与开源项目,贡献代码之后,时间久了之后,或多或少会想要知道自己在某个开源项目中贡献过多少行代码,贡献代码量占比是多少。这样我们在后面面试或者其他用途的时候,才能更加有底气。

统计开源项目贡献代码量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/bin/bash

# 设置作者名称
AUTHOR="haiqing chen"

# 获取特定作者增加和删除的行数
author_stats=$(git log --author="$AUTHOR" --pretty=tformat: --numstat | awk '{ added += $1; removed += $2 } END { print added, removed }')
author_added=$(echo $author_stats | cut -d ' ' -f1)
author_removed=$(echo $author_stats | cut -d ' ' -f2)

# 获取整个项目的增加和删除行数
total_stats=$(git log --pretty=tformat: --numstat | awk '{ added += $1; removed += $2 } END { print added, removed }')
total_added=$(echo $total_stats | cut -d ' ' -f1)
total_removed=$(echo $total_stats | cut -d ' ' -f2)

# 计算贡献百分比
added_percentage=$(awk "BEGIN {printf \"%.2f\", ($author_added/$total_added)*100}")
removed_percentage=$(awk "BEGIN {printf \"%.2f\", ($author_removed/$total_removed)*100}")

# 打印结果
echo "Total lines added by $AUTHOR: $author_added (${added_percentage}%)"
echo "Total lines removed by $AUTHOR: $author_removed (${removed_percentage}%)"

将AUTHOR修改成自己的名字,在你的需要统计的项目根目录执行上述脚本。就能看到相应的结果啦