Shell脚本实现的基于SVN的代码提交量统计工具
程序员文章站
2023-02-14 08:51:36
最近没啥事,就用bash写了一个基于svn的代码统计小工具。 可以指定统计的目录,默认递归统计子目录。
目前还没有屏蔽指定目录的功能。哈 代码比较粗糙。不过先晒出来。...
最近没啥事,就用bash写了一个基于svn的代码统计小工具。 可以指定统计的目录,默认递归统计子目录。
目前还没有屏蔽指定目录的功能。哈 代码比较粗糙。不过先晒出来。
#!/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 | %-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 "-----开始统计,请耐心等待.... :) " action $1 total exit 0