Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1731837
  • 博文数量: 150
  • 博客积分: 660
  • 博客等级: 上士
  • 技术积分: 2480
  • 用 户 组: 普通用户
  • 注册时间: 2005-08-08 11:39
文章分类

全部博文(150)

文章存档

2019年(4)

2018年(36)

2017年(53)

2016年(7)

2015年(3)

2014年(3)

2013年(27)

2012年(2)

2011年(1)

2006年(1)

2005年(13)

分类: 系统运维

2016-02-23 13:49:38

一、简单说明
基于git log的输出统计;
按照月份统计,当然稍微改动也可以按照年月进行统计;
遍历所有的版本库,可以在统计的时候指定不同的分支。

二、脚本内容
脚本分为三部分部分,一部分为格式化输出,如下:

点击(此处)折叠或打开

  1. #!/bin/bash
  2. # 按照cdc.txt 中定义的目录统计个项目的总提交次数、增加、删除、留存代码行数
  3. # 统计按照自然月进行或者指定时段进行 $1 为月份(1-12)
  4. ### 当前目录###
  5. if [ $(echo $0 | grep '^/') ]; then
  6. cur_dir=$(dirname $0)
  7. else
  8. cur_dir=$(pwd)/$(dirname $0)
  9. fi
  10. ### 定义使用文件###
  11. repo_file=$cur_dir/cdc.txt #定义版本库目录文件
  12. everyone_file=$cur_dir/every.txt
  13. goluk_file=$cur_dir/goluk.csv
  14. ### 接收月份参数###
  15. Month=$1
  16. :>$goluk_file
  17. while read name project_dir
  18. do
  19. echo $name |awk '{printf "%-20s%1s%10s%1s%10s%1s%10s%1s%10s\n",$1, \
  20. "," , "提交次数" , "," , "增加代码" , "," , "减少代码" , "," , "留存代码"}' >> $goluk_file
  21. everyone_file=$cur_dir/$project_dir/every.txt
  22. ### 汇总计算各人的代码行数
  23. ### 删除空行
  24. awk '!/^$/' $everyone_file |\
  25. ### 计算
  26. 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}} \
  27. else{adds=adds+$1;dels=dels+$2;next} }' |\
  28. ### 汇总
  29. awk '{cnt[$1]++;name[$1]=$1;adds[$1]+=$2;dels[$1]+=$3}END{for(i in name) printf "%-20s%1s%10d%1s%10d%1s%10d%1s%10d\n",\
  30. name[i],",",cnt[i],",",adds[i],",",dels[i],",",adds[i]-dels[i]}' >> $goluk_file
  31. done < $repo_file
一部分为实际统计计算部分,代码如下:

点击(此处)折叠或打开

  1. #!/bin/bash
  2. # 统计后台的总的提交次数、增加、删除、留存代码行数
  3. # 统计按照自然月进行或者指定时段进行 $1 为月份(1-12)
  4. #### 定义分支 ####
  5. if [ $2 = "" ] ; then
  6. Branch=develop
  7. else
  8. Branch=$2
  9. fi
  10. #### 定义版本库 ####
  11. #git_repo=cdc.txt
  12. ### 当前目录###
  13. if [ $(echo $0 | grep '^/') ]; then
  14. cur_dir=$(dirname $0)
  15. else
  16. cur_dir=$(pwd)/$(dirname $0)
  17. fi
  18. ### 定义使用文件###
  19. repo_file=$cur_dir/cdc.txt #版本库定义
  20. commit_file=$cur_dir/commit.txt #提交次数明细
  21. total_file=$cur_dir/total.txt #每人提交次数汇总
  22. detail_file=$cur_dir/detail.txt #每人提交行数明细
  23. everyone_file=$cur_dir/every.txt
  24. ### 接收月份参数###
  25. Month=$1
  26. ### 初始化中间文件###
  27. :>$commit_file
  28. :>$detail_file
  29. :>$everyone_file
  30. ### 首先统计每个人的提交次数,记录到中间文件
  31. function Count() {
  32. while read git_url
  33. do
  34. echo $git_url
  35. goluk_repo=`echo $git_url |awk -F/ '{print $NF}'`
  36. cd $goluk_repo
  37. git checkout $Branch
  38. git pull
  39. git log --pretty='%aN' --since ==2016-$Month-01 --until=2016-$Month-31 | sort | uniq -c | sort -k1 -n -r >> $commit_file
  40. cd ../
  41. done < $repo_file
  42. }
  43. ### 代码提交行数
  44. function Codelines() {
  45. while read git_url
  46. do
  47. echo $git_url
  48. goluk_repo=`echo $git_url |awk -F/ '{print $NF}'`
  49. cd $goluk_repo
  50. git pull
  51. git checkout $Branch
  52. # 统计各版本总行数
  53. git log --author=^.* --pretty=tformat: --numstat --since=2016-$Month-01 --until=2016-$Month-31 |\
  54. awk '{ add += $1 ; subs += $2 ; loc += $1 - $2 } \
  55. END { print add,subs,loc ,repo_name }' repo_name=$goluk_repo - >> $detail_file
  56. ### debug begin
  57. ###git log --author=^.* --pretty=tformat:%aN --numstat --since=2016-$Month-01 --until=2016-$Month-31 |\
  58. ### awk '!/^$/' >> $cur_dir/every2.txt
  59. ## debug end
  60. # 记录各人代码、增加行数、删除行数明细
  61. git log --pretty='tformat:%aN' --numstat --since=2016-$Month-01 --until=2016-$Month-31 >>$everyone_file
  62. cd ../
  63. done < $repo_file
  64. }
  65. #awk '{sum[$2]+=$1}END{for(i in sum)print i ,sum[i]}' scrope.txt |sort -k2 -nr >
  66. Count $Month
  67. ### 计算总提交次数
  68. awk '{sum[$2]+=$1}END{for(i in sum)print i ,sum[i]}' $commit_file |sort -k2 -nr > $total_file
  69. Codelines $Month
  70. ### 汇总提交数
  71. awk '{cnt+=$2}END{printf "%-20d%10d\n",Mon,cnt}' Mon=$Month $total_file
  72. ### 汇总代码行数
  73. #awk '{adds+=$1;removes+=$2;saves+=$3}END{print adds,removes,saves}' $detail_file
  74. ### 汇总计算各人的代码行数
  75. ### 删除空行
  76. awk '!/^$/' $everyone_file |\
  77. ### 计算
  78. 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}} \
  79. else{adds=adds+$1;dels=dels+$2;next} }' |\
  80. ### 汇总
  81. 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]}'
最后一部分脚本,是首次git clone版本库用的


点击(此处)折叠或打开

  1. #!/bin/bash
  2. #### 定义分支 ####
  3. Branch=release
  4. #### 定义版本库 ####
  5. git_repo=cdc.txt
  6. while read repo
  7. do
  8. git clone $repo
  9. done < $git_repo


三、使用注意事项

1、三部分独立成三个脚本文件比较好
2、统计机器必须要有所有版本库的读权限,否则没法clone。
3、版本库定义文件格式,文件末尾不能留空行

点击(此处)折叠或打开

  1. git@1.1.1.1:users/p1/cdc/authority
  2. git@1.1.1.1:users/p2/cdc/business

4、关于多项目的统计目录结构



点击(此处)折叠或打开

  1. .

  2. ├── GetStat.sh #第一部分脚本
  3. ├── android #项目目录
  4. │   ├── GetAllByMon.sh #第二部分脚本
  5. │   ├── cdc.txt #本项目源码的git地址
  6. │   ├── commit.txt
  7. │   ├── detail.txt
  8. │   ├── every.txt
  9. │   ├── total.txt
  10. │   └── workspace-goluk #项目源码

  11. ├── cdc.txt # 项目名称和目录文件,以空格分隔

  12. ├── firmware #结构同上目录
  13. │   ├── GetAllByMon.sh
  14. │   ├── Getrepo.sh
  15. │   ├── cdc.txt
  16. │   ├── commit.txt
  17. │   ├── detail.txt
  18. │   ├── every.txt
  19. │   ├── goluk_src
  20. │   ├── s2l_linux_sdk
  21. │   └── total.txt


阅读(16805) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~