Shell脚本实现的基于SVN的代码提交量统计工具
最近没啥事,就用bash写了一个基于svn的代码统计小工具。可以指定统计的目录,默认递归统计子目录。
目前还没有屏蔽指定目录的功能。哈代码比较粗糙。不过先晒出来。
#!/bin/bash-
#""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
#FILE:lines.sh
#
#USAGE:./lines.sh[dir]
#AUTHOR:william
#
#DESCRIPTION:基于SVN的代码提交量统计工具
#OPTIONS:---
#CREATED:06/05/201212:49:20PMCST
#"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
set-onounset#Treatunsetvariablesasanerror
#关注的文件类型后罪名
FILES_TYPE="*.cpp*.h*.lua"
#需要统计的人员,在这里写入需要统计的人,用空格隔开。哈还不智能
declare-rCODER_LIST="coder1coder2"
declare-icoder1
declare-icoder2
declare-rUSAGE="Usage:$0[dir].defaultdiriscurrentdir.\n"
#ERRORCODES;
declare-rE_BAD_PATH=1
declare-rE_INVAILED_ARGU=2
declare-rE_NOT_SVN_DIR=3
#TODO屏蔽一些dir还没写哈
#TODOotherwaygetpathnotwith/end
getpath()
{
#debug
#echodir_name:${dir_name}
#echobase_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"ERRORstatistic_codelinesnotargument"
return
fi
localpwd_length=${#PWD}
echo"--------------------------"
echo"${PWD}"
forcoderin$CODER_LIST;do
localnum=$(echo"$1"|grep${coder}|wc-l)
((${coder}+=num))
if[$num-ne0];then
printf"%10s|%-7d\n"${coder}$num
fi
done
echo"--------------------------"
}
#initcheckargumentsetwork_path
init_work_path()
{
if[$#-eq1];then
if[$1=="-h"];then#ishelp
echo-e"$USAGE"
elif[-d$1];then
dir_name=$(dirname${1})
base_name=$(basename${1})
getpath;
else
echo-e"Aninvailedargument"
echo-e"Use-hgethelp."
exit$E_INVAILED_ARGU
fi
fi
}
#checkwork_path
check_work_path()
{
if[-z$work_path]||[!-d$work_path];then
exit$E_BADPATH;
fi
}
#enterwork_path
enter_work_path()
{
cd${work_path}
if[!$?];then
echo"Cannotenter${work_path}"
fi
}
#checkwork_patisasvndir
is_svn_dir()
{
(
#checkifcurrentdirisasvndir
svninfo&>/dev/null
exit$?
)
return$?
}
action()
{
localdir_name=.
localbase_name=
localwork_path=$dir_name
init_work_path$1
check_work_path
enter_work_path#todocan'tenter
#echo"NOWDIR:$PWD,OLDDIR$OLDPWD"
is_svn_dir
#todotonextdir
localret=$?
if[$ret-ne0]
then
echo-e"Currentdir\"${work_path}\"notasvndir."
exit$E_NOT_SVN_DIR
fi
#getsourcefiles
localfiles=$(ls${FILES_TYPE}2>/dev/null)
if[-n"$files"];then
localnamelist=$(echo-n${files}|xargs-n1svnblame|awk'{print$2}')
#svnblame$files#|grep$1|wc-l
statistic_codelines"$namelist"
fi
localsub_dirs=$(find-maxdepth1-typed-name"[^.]*"2>/dev/null)
if[-n"$sub_dirs"];then
fordirin$sub_dirs;do
action"$dir"
done
fi
cd..
}
total()
{
echo"--------TOTOAL----------"
echo"NAME|lines"
echo"--------------------------"
forcoderin$CODER_LIST;do
if[${!coder}-ne0];then
printf"%10s|%-7d\n"${coder}${!coder}
fi
done
echo"--------------------------"
}
#main
echo"-----开始统计,请耐心等待....:)"
action$1
total
exit0