赞
踩
脚本统计的是当月时间范围修改,需要放置到仓库的上一级目录运行,或者根据实际情况修改代码。
运行情况如下:
代码如下:
- #!/bin/bash
-
-
- # 提取当前年份和月份
- year=$(date +%Y)
- month=$(date +%m)
- # 计算本月的第一天和最后一天
- first_day="${year}-${month}-01"
- last_day=$(date -d "${first_day} +1 month -1 day" +%Y-%m-%d)
-
-
- # 命令提示符的默认配置
- GREEN='\033[0;32m' # 绿色
- PURPLE='\033[0;35m' # 紫色
- YELLOW='\033[0;33m' # 黄色
- AQUA='\033[0;36m' # 青色
- NC='\033[0m' # 重置颜色
-
- username=$(whoami)
- hostname=$(hostname)
- msystem=$MSYSTEM
-
-
- com_printf_format="新增行数:%s,删除行数:%s,总行数:%s"
-
-
- # 仓库全路径
- function getPath() {
- local path=$(pwd)
-
- if [[ $path == */ ]]; then
- path=${path%?} # 删除最后一个字符
- fi
-
- echo $path
- }
-
- # 由于无法直接获取命令行提示符内容,通过参数拼接模拟命令行提示符
- function getPrompt() {
- echo "$GREEN$username@$hostname $PURPLE$msystem $YELLOW$(getPath) $AQUA($(git rev-parse --abbrev-ref HEAD))$NC"
- }
-
- # git命令复用
- function getCmdGit() {
- echo "git log --all --author=$(git config user.name) --numstat --pretty=tformat: --after=$first_day --before=$last_day"
- }
-
- # 显示的命令内容
- function getShowCmd() {
- echo "$ $(getCmdGit) | awk '{ adds += \$1; subs += \$2; loc += \$1 + \$2 } END { printf \"$com_printf_format\\\n\", adds, subs, loc }'"
- }
-
- # 执行命令,过滤掉无添加代码的仓库,打印代码统计结果
- function runCMD() {
- $(getCmdGit) | awk -v title="$(getPrompt)" -v show_cmd="$(getShowCmd)" -v com_printf_format="$com_printf_format" '
- { adds += $1; subs += $2; loc += $1 + $2 }
- END { if (adds > 0) { printf "%s\n%s\n'$com_printf_format'\n\n", title, show_cmd, adds, subs, loc } }'
- }
-
- #遍历当前路径下文件
- for dir in `ls`
- do
- # 判断是否为目录
- if [ -d $dir/.git ]; then
- cd $dir
- runCMD
- cd ..
- fi
- done
-
- # 添加换行
- echo -e "\n"
-
- # 等待用户按下回车键
- read -p "Press enter to exit..."

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。