溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何實(shí)現(xiàn)Shell腳本基于SVN的代碼提交量統(tǒng)計(jì)工具

發(fā)布時(shí)間:2021-09-28 11:06:16 來(lái)源:億速云 閱讀:165 作者:iii 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要講解了“如何實(shí)現(xiàn)Shell腳本基于SVN的代碼提交量統(tǒng)計(jì)工具”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“如何實(shí)現(xiàn)Shell腳本基于SVN的代碼提交量統(tǒng)計(jì)工具”吧!

#!/bin/bash -  
#"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 
#     FILE: lines.sh 
#  
#     USAGE: ./lines.sh [dir] 
#     AUTHOR: william 
#  
#  DESCRIPTION: 基于SVN的代碼提交量統(tǒng)計(jì)工具 
#    OPTIONS: --- 
#    CREATED: 06/05/2012 12:49:20 PM CST 
#""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 
 
set -o nounset               # Treat unset variables as an error 
 
 
# 關(guān)注的文件類型 后罪名 
FILES_TYPE="*.cpp *.h *.lua" 
 
# 需要統(tǒng)計(jì)的人員,在這里寫(xiě)入需要統(tǒng)計(jì)的人,用空格隔開(kāi)。哈還不智能 
declare -r CODER_LIST="coder1 coder2" 
declare -i coder1 
declare -i coder2 
 
 
declare -r USAGE="Usage: $0 [dir]. default dir is current dir.\n" 
 
# ERROR CODES; 
declare -r E_BAD_PATH=1 
declare -r E_INVAILED_ARGU=2 
declare -r E_NOT_SVN_DIR=3 
 
 
#TODO 屏蔽一些dir 還沒(méi)寫(xiě)哈 
# TODO other way get path not with / end  
getpath() 
{ 
  #debug 
  #echo dir_name: ${dir_name} 
  #echo base_name: ${base_name} 
  if [ $dir_name == "/" ] || [ $base_name == "/" ]; then 
    work_path="/" 
  else 
    work_path=${dir_name}/${base_name} 
  fi 
} 
 
statistic_codelines() 
{ 
  if [ -z "$1" ]; then 
    echo "ERROR statistic_codelines not argument" 
    return 
  fi 
  local pwd_length=${#PWD} 
  echo "--------------------------" 
  echo "${PWD}" 
  for coder in $CODER_LIST; do 
    local num=$(echo "$1" | grep ${coder} | wc -l) 
    (( ${coder} += num )) 
    if [ $num -ne 0 ]; then 
      printf "%10s | %-7d\n" ${coder} $num 
    fi 
  done 
  echo "--------------------------" 
} 
 
 
# init check argument set work_path 
init_work_path() 
{ 
  if [ $# -eq 1 ]; then 
    if [ $1 == "-h" ]; then # is help 
        echo -e "$USAGE" 
    elif [ -d $1 ]; then 
      dir_name=$(dirname ${1}) 
      base_name=$(basename ${1}) 
      getpath; 
    else 
      echo -e "An invailed argument" 
      echo -e "Use -h get help." 
      exit $E_INVAILED_ARGU 
    fi 
  fi 
} 
 
# check work_path 
check_work_path() 
{ 
  if [ -z $work_path ] || [ ! -d $work_path ]; then 
    exit $E_BADPATH; 
  fi 
} 
 
# enter work_path 
enter_work_path() 
{ 
  cd ${work_path} 
  if [ ! $? ]; then 
    echo "Can not enter ${work_path} " 
  fi 
} 
 
# check work_pat is a svn dir 
is_svn_dir() 
{ 
  ( 
  # check if current dir is asvn dir 
  svn info &> /dev/null 
  exit $? 
  ) 
  return $? 
} 
 
action() 
{ 
  local dir_name=. 
  local base_name= 
  local work_path=$dir_name 
 
  init_work_path $1 
  check_work_path 
  enter_work_path #todo can't enter 
 
  #echo "NOW DIR: $PWD, OLD DIR $OLDPWD" 
  is_svn_dir 
  #todo to next dir 
  local ret=$? 
  if [ $ret -ne 0 ] 
  then 
    echo -e "Current dir \"${work_path}\" not a svn dir." 
    exit $E_NOT_SVN_DIR 
  fi 
 
  # get source files 
  local files=$(ls ${FILES_TYPE} 2> /dev/null) 
 
  if [ -n "$files" ]; then 
   local namelist=$(echo -n ${files} | xargs -n 1 svn blame | awk '{print $2}') 
   #svn blame $files #| grep $1 | wc -l 
   statistic_codelines "$namelist" 
  fi 
 
  local sub_dirs=$(find -maxdepth 1 -type d -name "[^.]*" 2>/dev/null) 
 
  if [ -n "$sub_dirs" ]; then 
    for dir in $sub_dirs ; do 
      action "$dir" 
    done 
  fi 
 
  cd .. 
} 
 
total() 
{ 
  echo "-------- TOTOAL ----------" 
  echo "   NAME | lines    "  
  echo "--------------------------" 
  for coder in $CODER_LIST; do 
    if [ ${!coder} -ne 0 ]; then 
      printf "%10s | %-7d\n" ${coder} ${!coder} 
    fi 
  done 
  echo "--------------------------" 
} 
 
# main 
echo "-----開(kāi)始統(tǒng)計(jì),請(qǐng)耐心等待.... :) " 
action $1 
total 
 
exit 0

感謝各位的閱讀,以上就是“如何實(shí)現(xiàn)Shell腳本基于SVN的代碼提交量統(tǒng)計(jì)工具”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)如何實(shí)現(xiàn)Shell腳本基于SVN的代碼提交量統(tǒng)計(jì)工具這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI