美文网首页
git统计代码量

git统计代码量

作者: 杯中怎可无酒 | 来源:发表于2023-09-17 17:46 被阅读0次

记录一下
这个脚本应用场景是读取某个文件夹下所有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统计项目总行数 查看git上个人代码量(需要修改usernam...

  • git 代码量统计

    git代码量统计 代码量按人员统计 代码量按人员和时间统计 markdown语法入门

  • GIT服务器查询代码贡献量

    1.统计git上面的每个人代码贡献量 2..统计git上面的个人代码贡献量 3.统计git上面提交(commit)...

  • 2019-03-01git 统计提交次数及总共代码量

    git查看commit提交次数 统计代码量

  • git 代码量统计

    增加的代码行数量 git log --stat|perl -ne 'END { print $c } $c += ...

  • git 统计代码量

    进入项目目录下(包含.git的目录) 1.统计sujing在某个时间段内的git新增/删除代码行数 2.统计该项目...

  • git统计代码量

    参数:分支名:比如master,可选参数author:作者名,可选参数since:开始日期,可选参数until:截...

  • 统计代码量

    按作者统计代码量: git log --author=myname --format='%aN' | sort -...

  • Git log 统计

    git log使用方式参考Git使用---git log,下面给出一些常用的统计。 1、统计某人的代码提交量,包括...

  • 项目代码行数统计

    一、git命令统计 1、统计某人代码提交量 gitlog--author="mengfanxiao"--prett...

网友评论

      本文标题:git统计代码量

      本文链接:https://www.haomeiwen.com/subject/cuewvdtx.html