git统计代码量

记录一下
这个脚本应用场景是读取某个文件夹下所有git仓库的log,然后根据author 和 开始结束日期计算代码量(已在MacOS13.0验证)

#!/bin/bash
# author: zhangsan
# desc: 统计代码量

repository="/Users/xxx/Desktop/workspace/Apple/Migu_lib"

# 定义ANSI转义码
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
CYAN='\033[0;36m'
NC='\033[0m' # 重置颜色

author="zhangsan"

#入参传开始和结束时间 如2023-09-01 2023-09-30  不传则默认获取当月的代码统计
if [[ $# -eq 2 ]]; then

    # 获取日期的时间戳
    timestamp1=$(date -j -f "%Y-%m-%d" "$1" "+%s")
    timestamp2=$(date -j -f "%Y-%m-%d" "$2" "+%s")

    if [[ $timestamp1 -lt $timestamp2 ]]; then
        #"$1 在 $2 之前"
        start_date=$1
        end_date=$2
    elif [[ $timestamp1 -gt $timestamp2 ]]; then
        #"$1 在 $2 之后"
        start_date=$2
        end_date=$1
    else
        #$1 和 $2 相同 获取当月的
        start_date=$(date -j -v1d -v+1m -v-1d "+%Y-%m-01")
        end_date=$(date -j -v1d -v+1m -v-1d "+%Y-%m-%d")
    fi
    
else
    #不传日期则默认取当前月
    start_date=$(date -j -v1d -v+1m -v-1d "+%Y-%m-01")
    end_date=$(date -j -v1d -v+1m -v-1d "+%Y-%m-%d")
fi

# 搜索指定路径下的一级文件夹,存入列表
for folder in "$repository"/*; do
    if [[ -d "$folder" ]]; then
        list+=("$folder")
    fi
done

cd "$repository" || exit 1

read -p "是否忽略远端仓库日志(y/n):" input

for repo in "${list[@]}"; do
    cd "$repo" || exit 1
    
    if [ ! -d ".git" ]; then
        continue
    fi
    
    # 提取文件夹名字
    folder=$(basename "$repo")
    
    #同步提交记录 统计自己的代码一般不需要同步,除非你在别的电脑上提交过代码
    input=$(echo "$input" | tr '[:upper:]' '[:lower:]') # 将输入转换为小写字母

    #修改行数
    changed_lines=$(git log --all --branches --author="$author" --since="$start_date" --until="$end_date"  --pretty=tformat: --numstat | awk '{changed_lines+=$1+$2} END {print changed_lines}')
    #提交次数
    commits=$(git log --all --branches --author="$author" --since="$start_date" --until="$end_date"  --oneline | wc -l)
    
    #没改动的库不打印
    if [[ -z changed_lines ]] || [[ changed_lines -le 0 ]]; then
        cd - >/dev/null || exit 1
        continue
    fi
    
    #读取输入查询日志前是否需要pull
    if [[ "$input" == "y" || "$input" == "yes" || -z "$input" ]]; then
        echo ""
    elif [[ "$input" == "n" || "$input" == "no" ]]; then
        echo ""
        git pull >/dev/null
    else
        echo -e "\n${RED}输入y(yes)或者n(no) 白给这乱输入${NC}\n"
        exit 1
    fi
    
    # 删除所有空格
    commits=$(echo "$commits" | sed 's/ //g')
    
    git log --all --branches --author="$author" --since="$start_date" --until="$end_date" --pretty=tformat: --numstat | \
    awk -v commits="$commits" -v author="$author" -v folder="$folder" '
      BEGIN {
        added_lines = 0
        deleted_lines = 0
        modified_files = 0
      }
      NF == 3 {
        added_lines += $1
        deleted_lines += $2
        modified_files += 1
      }
      END {
        printf "代码仓库:\033[32m  %s \033[0m\n", folder
        printf "提交作者:\033[33m  %s \033[0m\n", author
        printf "新增行数:\033[32m  %d \033[0m\n", added_lines
        printf "删除行数:\033[31m  %d \033[0m\n", deleted_lines
        printf "改动行数:\033[33m  %d \033[0m\n", added_lines + deleted_lines
        printf "改动文件:\033[33m  %d \033[0m\n", modified_files
        printf "提交次数:\033[32m  %s \033[0m\n", commits
      }
    '
    
    #新增行
    additions=$(git log --all --branches --author="$author" --since="$start_date" --until="$end_date"  --pretty=tformat: --numstat | awk '{additions+=$1} END {print additions}')
    #删除行
    deletions=$(git log --all --branches --author="$author" --since="$start_date" --until="$end_date"  --pretty=tformat: --numstat | awk '{deletions+=$2} END {print deletions}')
    #修改文件
    changed_files=$(git log --all --branches --author="$author" --since="$start_date" --until="$end_date"  --pretty=tformat: --numstat | awk 'END {print NR}')
    
    #统计数据累加
    total_additions=$((total_additions + additions))
    total_commits=$((total_commits + commits))
    total_deletions=$((total_deletions + deletions))
    total_changed_lines=$((total_changed_lines + changed_lines))
    total_changed_files=$((total_changed_files + changed_files))
    
    cd - >/dev/null || exit 1
done


# 判断新增总代码行数是否大于1000
if [ $total_additions -gt 1000 ]; then
    echo -e "\n😂😂😂--------------------😂😂😂\n"
else
#需要补文档了老铁
    echo -e "\n😱😱😱--------------------😱😱😱\n"
fi

echo -e "${CYAN}总计:($start_date ~ $end_date) $NC"
echo -e "${YELLOW}提交作者: $author $NC"
echo -e "${GREEN}总新增行数: $total_additions $NC"
echo -e "${RED}总删除行数: $total_deletions $NC"
echo -e "${YELLOW}总改动行数: $total_changed_lines $NC"
echo -e "${YELLOW}总改动文件: $total_changed_files $NC"
echo -e "${GREEN}总提交次数: $total_commits $NC"



效果图如下:


xiaoguo.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • GIT统计代码量 Git统计个人提交代码行数 Git统计项目总行数 查看git上个人代码量(需要修改usernam...
    jeffrey_hjf阅读 11,557评论 0 4
  • Git 是一个开源的分布式版本控制系统,用于敏捷高效地处理git任何或小或大的项目。 Git 是 Linus To...
    LeoLongl阅读 281评论 0 0
  • 参数:分支名:比如master,可选参数author:作者名,可选参数since:开始日期,可选参数until:截...
    xjkstar阅读 1,258评论 0 1
  • 概要 64学时 3.5学分 章节安排 电子商务网站概况 HTML5+CSS3 JavaScript Node 电子...
    阿啊阿吖丁阅读 9,430评论 0 3
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,269评论 19 139