一、如何衡量程序员KPI
作为码农,大家或多或少都遇到过用代码量来衡量贡献度的,所以有时候需要自己去统计参与开发项目的代码提交行数,下面是我写的运行于Windows上的代码量统计脚本。脚本比较简单,我也就不多说了,放出来给需要的兄弟借鉴。
#!/bin/bash
#set -e
set -x
# 统计代码提交的时间范围
startTime="$1"
endTime="$2"
if [ ! $startTime ]; then
startTime="2023-10-01"
echo "the default startTime is: $startTime"
else
echo "the specify startTime is: $startTime"
fi
if [ ! $endTime ]; then
endTime="2023-12-31"
echo "the default endTime is: $endTime"
else
echo "the specify endTime is: $endTime"
fi
cd repo_tmp
git init -q
OLDIFS="$IFS" #备份旧的IFS变量
IFS=$'\n\r' #修改分隔符为换行符
for i in `cat ../git_authors.txt`;
do
addSum=0
subSum=0
locSum=0
for j in `cat ../git_projects.txt`;
do
#echo "开发者--> $i"
#echo "项目名--> $j"
git remote add origin ${j}
if [[ $? -ne 0 ]]; then
echo "报错: git remote add origin ${j}"
git remote remove origin
fi
git fetch -q
git log --remotes --author="$i" --since=--$startTime --until=--$endTime --pretty=tformat: --numstat | awk -v auth="$i" -v proj="$j" '{ add += $1; subs += $2; loc += $1 - $2 } END { if(add=="") add=0;if(subs=="") subs=0;if(loc=="") loc=0;printf "开发者: %s, 项目: %s, 新增: %s, 删除: %s, 净增: %s\n", auth, proj, add, subs, loc }'
addSum=$(($addSum + $(git log --remotes --author="$i" --since=--$startTime --until=--$endTime --pretty=tformat: --numstat | awk '{ add += $1} END { if(add=="") add=0; print add }')))
#echo "新增代码行:addSum=$addSum"
subSum=$(($subSum + $(git log --remotes --author="$i" --since=--$startTime --until=--$endTime --pretty=tformat: --numstat | awk '{ subs += $2} END { if(subs=="") subs=0; print subs }')))
#echo "删除代码行:subSum=$subSum"
locSum=$(($locSum + $(git log --remotes --author="$i" --since=--$startTime --until=--$endTime --pretty=tformat: --numstat | awk '{ loc += $1 - $2} END { if(loc=="") loc=0; print loc }')))
#echo "净增代码行:locSum=$locSum"
git remote remove origin
done
echo "开发者: $i, 项目: ALL_PROJECT, 新增: $addSum, 删除: $subSum, 净增: $locSum"
done
IFS="$OLDIFS" #还原IFS变量