1. 程式人生 > 其它 >shell指令碼實現統計git上個人程式碼量

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