#!/bin/bash
# 统计后台的总的提交次数、增加、删除、留存代码行数
# 统计按照自然月进行或者指定时段进行 $1 为月份(1-12)
#### 定义分支 ####
if [ $2 = "" ] ; then
Branch=develop
else
Branch=$2
fi
#### 定义版本库 ####
#git_repo=cdc.txt
### 当前目录###
if [ $(echo $0 | grep '^/') ]; then
cur_dir=$(dirname $0)
else
cur_dir=$(pwd)/$(dirname $0)
fi
### 定义使用文件###
repo_file=$cur_dir/cdc.txt #版本库定义
commit_file=$cur_dir/commit.txt #提交次数明细
total_file=$cur_dir/total.txt #每人提交次数汇总
detail_file=$cur_dir/detail.txt #每人提交行数明细
everyone_file=$cur_dir/every.txt
### 接收月份参数###
Month=$1
### 初始化中间文件###
:>$commit_file
:>$detail_file
:>$everyone_file
### 首先统计每个人的提交次数,记录到中间文件
function Count() {
while read git_url
do
echo $git_url
goluk_repo=`echo $git_url |awk -F/ '{print $NF}'`
cd $goluk_repo
git checkout $Branch
git pull
git log --pretty='%aN' --since ==2016-$Month-01 --until=2016-$Month-31 | sort | uniq -c | sort -k1 -n -r >> $commit_file
cd ../
done < $repo_file
}
### 代码提交行数
function Codelines() {
while read git_url
do
echo $git_url
goluk_repo=`echo $git_url |awk -F/ '{print $NF}'`
cd $goluk_repo
git pull
git checkout $Branch
# 统计各版本总行数
git log --author=^.* --pretty=tformat: --numstat --since=2016-$Month-01 --until=2016-$Month-31 |\
awk '{ add += $1 ; subs += $2 ; loc += $1 - $2 } \
END { print add,subs,loc ,repo_name }' repo_name=$goluk_repo - >> $detail_file
### debug begin
###git log --author=^.* --pretty=tformat:%aN --numstat --since=2016-$Month-01 --until=2016-$Month-31 |\
### awk '!/^$/' >> $cur_dir/every2.txt
## debug end
# 记录各人代码、增加行数、删除行数明细
git log --pretty='tformat:%aN' --numstat --since=2016-$Month-01 --until=2016-$Month-31 >>$everyone_file
cd ../
done < $repo_file
}
#awk '{sum[$2]+=$1}END{for(i in sum)print i ,sum[i]}' scrope.txt |sort -k2 -nr >
Count $Month
### 计算总提交次数
awk '{sum[$2]+=$1}END{for(i in sum)print i ,sum[i]}' $commit_file |sort -k2 -nr > $total_file
Codelines $Month
### 汇总提交数
awk '{cnt+=$2}END{printf "%-20d%10d\n",Mon,cnt}' Mon=$Month $total_file
### 汇总代码行数
#awk '{adds+=$1;removes+=$2;saves+=$3}END{print adds,removes,saves}' $detail_file
### 汇总计算各人的代码行数
### 删除空行
awk '!/^$/' $everyone_file |\
### 计算
awk '{if($1 ~ /^[a-zA-Z]+$/) {if(NR==1){printf "%20s",$1 }else {printf "\n%20s%8d%8d",$1,adds,dels;adds=0;dels=0}} \
else{adds=adds+$1;dels=dels+$2;next} }' |\
### 汇总
awk '{cnt[$1]++;name[$1]=$1;adds[$1]+=$2;dels[$1]+=$3}END{for(i in name) printf "%-20s%10d%10d%10d%10d\n", name[i],cnt[i],adds[i],dels[i],adds[i]-dels[i]}'