這篇文章主要介紹了Shell腳本實現的基於SVN的代碼提交量統計工具,本文直接給出實現腳本代碼,需要的朋友可以參考下
最近沒啥事,就用bash寫了一個基於svn的代碼統計小工具。 可以指定統計的目錄,默認遞歸統計子目錄。
目前還沒有屏蔽指定目錄的功能。哈 代碼比較粗糙。不過先曬出來。
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 #!/bin/bash - #"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" # FILE: lines.sh # # USAGE: ./lines.sh [dir] # AUTHOR: william # # DESCRIPTION: 基於SVN的代碼提交量統計工具 # OPTIONS: --- # CREATED: 06/05/2012 12:49:20 PM CST #""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" set -o nounset # Treat unset variables as an error # 關注的文件類型 後罪名 FILES_TYPE="*.cpp *.h *.lua" # 需要統計的人員,在這裡寫入需要統計的人,用空格隔開。哈還不智能 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 還沒寫哈 # 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 | %-7dn" ${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 | %-7dn" ${coder} ${!coder} fi done echo "--------------------------" } # main echo "-----開始統計,請耐心等待.... :) " action $1 total exit 0