Shell脚本实现的基于SVN的代码提交量统计工具

所属分类: 脚本专栏 / linux shell 阅读数: 96
收藏 0 赞 0 分享

最近没啥事,就用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 

更多精彩内容其他人还在看

Linux shell中的printf的详细用法

这篇文章主要介绍了Linux shell中的printf的详细用法的相关资料,希望能通过本文能帮助到大家,需要的朋友可以参考下
收藏 0 赞 0 分享

shell字符截取命令之cut命令的实例详解

这篇文章主要介绍了shell字符截取命令之cut命令的实例详解的相关资料,希望通过本文大家能够掌握这部分内容,需要的朋友可以参考下
收藏 0 赞 0 分享

Linux 在Bash脚本中怎么关闭文件描述符的实例

这篇文章主要介绍了Linux 在Bash脚本中怎么关闭文件描述符的实例的相关资料,希望通过本文能帮助到大家实现这样的功能,需要的朋友可以参考下
收藏 0 赞 0 分享

Linux使用文本编辑器vi常用命令

vi就是一种功能强大的文本编辑器,而vim则是高级版的vi,不但可以用不同颜色显示文字内容,还能进行诸如shell脚本、C语言程序编辑等功能,可以作为程序编辑器。下面通过本文给大家介绍linux 文本编辑器vi常用命令,一起看看吧
收藏 0 赞 0 分享

Linux 中unzip解压时中文乱码的解决办法

这篇文章主要介绍了Linux 中unzip解压时中文乱码的解决办法的相关资料,这里提供两种解决办法,希望能通过本文帮助到大家,需要的朋友可以参考下
收藏 0 赞 0 分享

Linux shell ftp命令根据文件日期下载文件的方法

最近做项目遇到这样的需求要求ftp获取远程数据的文件,根据文件的创建时间点下载文件。下面小编给大家分享知识点小结,感兴趣的朋友要求看看吧
收藏 0 赞 0 分享

Linux下服务器重启的脚本命令

Linux关闭和重启系统一般使用相同的命令可以实现。下面脚本之家小编给大家带来了Linux下服务器重启的脚本命令,感兴趣的朋友一起看看吧
收藏 0 赞 0 分享

Linux中的特殊符号与正则表达式

这篇文章主要介绍了Linux中的特殊符号与正则表达式,需要的朋友可以参考下
收藏 0 赞 0 分享

利用shell命令统计日志的方法详解

这篇文章主要给大家介绍了关于利用shell命令统计日志的方法,通过这个命令将会对大家的学习或者工作具有一定的参考学习价值,文中给出了详细的示例代码,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧。
收藏 0 赞 0 分享

Linux中的Syslog命令

syslog是Linux系统默认的日志守护进程,默认的syslog配置文件是/etc/syslog.conf文件。接下来通过本文给大家分享Linux中的Syslog命令,感兴趣的朋友一起看看吧
收藏 0 赞 0 分享
查看更多