shell腳本實現統計git上個人代碼量


gerrit代碼統計腳本


背景

  1. 自動創建git文件夾存放拉取項目及代碼統計結果。
  2. 添加統計單一分支代碼量功能。
  3. 可以只統計個人的單一/所有項目分支代碼量。

實現

統計所有人單一/所有分支代碼量

將開始時間、截止時間、被統計人gerrit名稱從命令行作為參數輸入,比如在Terminal中運行sudo ./git-test.sh 2022-2-01 2022-3-01 zjc命令。
若要統計所有人的代碼量,則被統計人名稱為“all”,即sudo ./git-test.sh 2022-2-01 2022-3-01 all
在出現的選取列表中輸入每個分支前序號統計該分支代碼量。若要統計所有分支代碼量,則選擇“all”。
統計完畢選擇“quit”退出。

#!/bin/bash
# author:zjc
​
export start_time=$1
export end_time=$2
export author_name=$3
PS3="Select a branch or get all info: "
rm -rf ~/git/total-test3.txt
#分支名
array=("xxx")
# 先創建git文件夾用於存儲統計文件
dir="/home/ubuntu/git"
if [ ! -d "$dir" ];then mkdir $dir; echo “創建git文件夾”; else echo "git文件夾已存在"; fi
echo "start time:"$1" end time:"$2>~/git/total-test3.txt
# 拉取項目,找到對應分支,計算代碼量並寫入文件
function gitStatisticsFunc(){
    echo "#########################"${array[$i]}>>~/git/total-test3.txt
    str=${array[$i]}
    strs=($str)
    echo ${array[$i]}
    git clone http://IP:Port/${strs[0]} ~/git/${strs[0]}-${strs[1]} 
    sudo chmod 777 ~/git/${strs[0]}-${strs[1]}
    cd ~/git/${strs[0]}-${strs[1]};
    git checkout ${strs[1]};
    git pull;
    pwd |awk -F '/' '{print "["$NF"]:\t"}' >> ~/git/total-test3.txt;
    if [ $author_name == "all" ]; then
        git log --since=$start_time --before=$end_time --format='%aN' | sort -u| while read name; do echo -en "$name\t";  
        git log --since=$start_time --before=$end_time --author="$name" --pretty=tformat: --numstat | awk '{add+= $1; subs+= $2; loc+= $1-$2 } END {printf " added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc}';
        done>>~/git/total-test3.txt;
    else
        echo -en "$author_name\t">>~/git/total-test.txt
        git log --since=$start_time --before=$end_time --author="$author_name" --pretty=tformat: --numstat | awk '{add+= $1; subs+= $2; loc+= $1-$2 } END {printf " added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc}'>>~/git/total-test3.txt;
    fi
}
# 統計完后刪除拉取項目
function deleteGit(){
    for((i=0;i<${#array[*]};i++))
    do
        str=${array[$i]}
        strs=($str)
        rm -rf ~/git/${strs[0]}-${strs[1]};
    done
}

modules=("${array[@]}")
modules+=("all")
modules+=("quit")
select module in "${modules[@]}";do
    case $module in 
        "quit")
            echo "User requested exit"
            deleteGit
            exit
            ;;
        *)      
            # 輸入all則全部統計
            if [ "$module" == "all" ]; then
                for((i=0;i<${#array[*]};i++))
                do
                    gitStatisticsFunc
                done
            else
            # 輸入對應項目及分支名則只統計對應項目分支
                for((i=0;i<${#array[*]};i++))
                do
                    if [ "$module" == "${array[$i]}" ]; then
                 gitStatisticsFunc
                    fi
                done
            fi
            deleteGit
    esac
done



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM