统计Git版本库每个人提交次数和代码的增加和删除行数脚本
一、简单说明基于git log的输出统计;
按照月份统计,当然稍微改动也可以按照年月进行统计;
遍历所有的版本库,可以在统计的时候指定不同的分支。
二、脚本内容
脚本分为三部分部分,一部分为格式化输出,如下:
<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>#!/bin/bash</li><li># 按照cdc.txt 中定义的目录统计个项目的总提交次数、增加、删除、留存代码行数</li><li># 统计按照自然月进行或者指定时段进行 $1 为月份(1-12)</li><li>### 当前目录###</li><li>if [ $(echo $0 | grep '^/') ]; then</li><li>cur_dir=$(dirname $0)</li><li>else</li><li>cur_dir=$(pwd)/$(dirname $0)</li><li>fi</li><li></li><li>### 定义使用文件###</li><li>repo_file=$cur_dir/cdc.txt #定义版本库目录文件</li><li>everyone_file=$cur_dir/every.txt</li><li>goluk_file=$cur_dir/goluk.csv</li><li>### 接收月份参数###</li><li>Month=$1</li><li>:>$goluk_file</li><li>while read name project_dir</li><li>do</li><li></li><li>echo $name |awk '{printf "%-20s%1s%10s%1s%10s%1s%10s%1s%10s\n",$1, \</li><li>"," , "提交次数" , "," , "增加代码" , "," , "减少代码" , "," , "留存代码"}' >> $goluk_file</li><li>everyone_file=$cur_dir/$project_dir/every.txt</li><li>### 汇总计算各人的代码行数</li><li>### 删除空行</li><li>awk '!/^$/' $everyone_file |\</li><li>### 计算</li><li>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}} \</li><li>else{adds=adds+$1;dels=dels+$2;next} }' |\</li><li>### 汇总</li><li>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",\</li><li>name[i],",",cnt[i],",",adds[i],",",dels[i],",",adds[i]-dels[i]}' >> $goluk_file</li><li>done < $repo_file</li></ol>一部分为实际统计计算部分,代码如下:
<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>#!/bin/bash</li><li># 统计后台的总的提交次数、增加、删除、留存代码行数</li><li># 统计按照自然月进行或者指定时段进行 $1 为月份(1-12)</li><li>#### 定义分支 ####</li><li>if [ $2 = "" ] ; then</li><li>Branch=develop</li><li>else</li><li>Branch=$2</li><li>fi</li><li>#### 定义版本库 ####</li><li>#git_repo=cdc.txt</li><li>### 当前目录###</li><li>if [ $(echo $0 | grep '^/') ]; then</li><li>cur_dir=$(dirname $0)</li><li>else</li><li>cur_dir=$(pwd)/$(dirname $0)</li><li>fi</li><li></li><li>### 定义使用文件###</li><li>repo_file=$cur_dir/cdc.txt #版本库定义</li><li>commit_file=$cur_dir/commit.txt #提交次数明细</li><li>total_file=$cur_dir/total.txt #每人提交次数汇总</li><li>detail_file=$cur_dir/detail.txt #每人提交行数明细</li><li>everyone_file=$cur_dir/every.txt</li><li>### 接收月份参数###</li><li>Month=$1</li><li>### 初始化中间文件###</li><li>:>$commit_file</li><li>:>$detail_file</li><li>:>$everyone_file</li><li></li><li>### 首先统计每个人的提交次数,记录到中间文件</li><li>function Count() {</li><li>while read git_url</li><li>do</li><li>echo $git_url</li><li>goluk_repo=`echo $git_url |awk -F/ '{print $NF}'`</li><li>cd $goluk_repo</li><li>git checkout $Branch</li><li>git pull</li><li>git log --pretty='%aN' --since ==2016-$Month-01 --until=2016-$Month-31 | sort | uniq -c | sort -k1 -n -r >> $commit_file</li><li>cd ../</li><li>done < $repo_file</li><li>}</li><li>### 代码提交行数</li><li>function Codelines() {</li><li>while read git_url</li><li>do</li><li>echo $git_url</li><li>goluk_repo=`echo $git_url |awk -F/ '{print $NF}'`</li><li>cd $goluk_repo</li><li>git pull</li><li>git checkout $Branch</li><li># 统计各版本总行数</li><li>git log --author=^.* --pretty=tformat: --numstat --since=2016-$Month-01 --until=2016-$Month-31 |\</li><li>awk '{ add += $1 ; subs += $2 ; loc += $1 - $2 } \</li><li>END { print add,subs,loc ,repo_name }' repo_name=$goluk_repo - >> $detail_file</li><li>### debug begin</li><li>###git log --author=^.* --pretty=tformat:%aN --numstat --since=2016-$Month-01 --until=2016-$Month-31 |\</li><li>### awk '!/^$/' >> $cur_dir/every2.txt</li><li>## debug end</li><li># 记录各人代码、增加行数、删除行数明细</li><li>git log --pretty='tformat:%aN' --numstat --since=2016-$Month-01 --until=2016-$Month-31 >>$everyone_file</li><li>cd ../</li><li>done < $repo_file</li><li>}</li><li>#awk '{sum[$2]+=$1}END{for(i in sum)print i ,sum[i]}' scrope.txt |sort -k2 -nr ></li><li>Count $Month</li><li>### 计算总提交次数</li><li>awk '{sum[$2]+=$1}END{for(i in sum)print i ,sum[i]}' $commit_file |sort -k2 -nr > $total_file</li><li>Codelines $Month</li><li>### 汇总提交数</li><li>awk '{cnt+=$2}END{printf "%-20d%10d\n",Mon,cnt}' Mon=$Month $total_file</li><li>### 汇总代码行数</li><li>#awk '{adds+=$1;removes+=$2;saves+=$3}END{print adds,removes,saves}' $detail_file</li><li>### 汇总计算各人的代码行数</li><li>### 删除空行</li><li>awk '!/^$/' $everyone_file |\</li><li>### 计算</li><li>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}} \</li><li>else{adds=adds+$1;dels=dels+$2;next} }' |\</li><li>### 汇总</li><li>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]}'</li></ol>最后一部分脚本,是首次git clone版本库用的
<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>#!/bin/bash</li><li></li><li>#### 定义分支 ####</li><li>Branch=release</li><li>#### 定义版本库 ####</li><li>git_repo=cdc.txt</li><li>while read repo</li><li>do</li><li>git clone $repo</li><li>done < $git_repo</li></ol>
三、使用注意事项
1、三部分独立成三个脚本文件比较好
2、统计机器必须要有所有版本库的读权限,否则没法clone。
3、版本库定义文件格式,文件末尾不能留空行
<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>git@1.1.1.1:users/p1/cdc/authority</li><li>git@1.1.1.1:users/p2/cdc/business</li></ol>
4、关于多项目的统计目录结构
<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>.<br /></li><li><br /></li><li>├── GetStat.sh #第一部分脚本<br /></li><li>├── android #项目目录<br /></li><li>│ ├── GetAllByMon.sh #第二部分脚本<br /></li><li>│ ├── cdc.txt #本项目源码的git地址<br /></li><li>│ ├── commit.txt<br /></li><li>│ ├── detail.txt<br /></li><li>│ ├── every.txt<br /></li><li>│ ├── total.txt<br /></li><li>│ └── workspace-goluk #项目源码<br /></li><li><br /></li><li>├── cdc.txt # 项目名称和目录文件,以空格分隔<br /></li><li><br /></li><li>├── firmware #结构同上目录<br /></li><li>│ ├── GetAllByMon.sh<br /></li><li>│ ├── Getrepo.sh<br /></li><li>│ ├── cdc.txt<br /></li><li>│ ├── commit.txt<br /></li><li>│ ├── detail.txt<br /></li><li>│ ├── every.txt<br /></li><li>│ ├── goluk_src<br /></li><li>│ ├── s2l_linux_sdk<br /></li><li>│ └── total.txt</li></ol>

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools