Git:统计单个提交的文件体积变化

1、进入仓库目录

cd path/to/repo

2、创建脚本 git-file-size-stat.sh

#!/bin/bash
# 
# 统计 Git 仓库指定提交的文件体积文件变化
# 参数1: commitID
# 
# 示例 
# cd path/to/repo
# bash /path/to/git-file-size-stat.sh 419fd3846fff2c41bd44ecbbf42d27f436f5a8c8


# 脚本用 bash 启动
shellEnv="$3"
if [[ "${shellEnv}" != "bash" ]]; then
    bash "$0" "$1" "$2" "bash"
    exit 0;
fi

git status 2>/dev/null

if [[ $? != 0 ]]; then
    echo -e "\033[31m请先 cd 到 git 仓库后再执行此脚本 \033[0m"
    exit 1
fi

newCommitID="$1"
if [[ -z "$newCommitID" ]]; then
    echo -e "\033[31mcommitID: 参数1不存在 \033[0m"
    exit 2
fi


oldCommitID=$(git rev-list --max-count=2 "${newCommitID}" | tail -n 1)
if [[ -z "$oldCommitID" ]]; then
    echo -e "\033[31m无法获取 ${newCommitID} 的上一个 CommitID \033[0m"
    exit 3
fi

# 新添的文件
insertFileTotal=0;
insertFileCount=0;
# 更新的文件
updateFileTotal=0;
updateFileCount=0
# 删除的文件
removeFileTotal=0;
removeFileCount=0;

echo ""
echo -e "\033[36m☕️ ${oldCommitID} => ${newCommitID} \033[0m"
echo ""

while IFS= read -r line; do
    # 逆转义字符:Git会将路径中特殊字符转义
    line="$(printf "$line")"
    # 删除头尾的双引号
    line="${line#\"}"
    line="${line%\"}"
    # 获取文件大小,2>/dev/null 屏蔽错误输出
    newSize=$(git show $newCommitID:"$line" 2>/dev/null | wc -c);
    oldSize=$(git show $oldCommitID:"$line" 2>/dev/null | wc -c);

    if [[ ${oldSize} -eq 0 ]]; then
        printf "\033[32m[+] %-10d ${line} \033[0m\n" "${newSize}";
        insertFileCount=$((insertFileCount + 1));
        insertFileTotal=$((insertFileTotal + newSize));
    elif [[ ${newSize} -eq 0 ]]; then
        printf "\033[31m[-] %-10d ${line} \033[0m\n" "${oldSize}";
        removeFileCount=$((removeFileCount + 1));
        removeFileTotal=$((removeFileTotal + oldSize));
    else
        delSize=$((newSize - oldSize));
        printf "\033[34m[*] %-10d ${line}  \033[0m\n" "${delSize}"
        updateFileCount=$((updateFileCount + 1));
        updateFileTotal=$((updateFileTotal + delSize));
    fi
done < <(git show --name-only --format=tformat: "${newCommitID}" | sort | uniq)

formatFileSize() {
    echo $(echo $1 | numfmt --to=iec --suffix=B --format="%.2f")
}

fileTotal=$((insertFileTotal - removeFileTotal + updateFileTotal));
fileCount=$((insertFileCount + removeFileCount + updateFileCount));

echo ""
echo -e "\033[36m🎉 统计结果 \033[0m"
echo ""
printf "\033[36m新增 %-5d 个文件,共 %-10d 字节,$(formatFileSize $insertFileTotal) \033[0m\n" "${insertFileCount}" "${insertFileTotal}"
printf "\033[36m删除 %-5d 个文件,共 %-10d 字节,$(formatFileSize $removeFileTotal) \033[0m\n" "${removeFileCount}" "${removeFileTotal}"
printf "\033[36m更新 %-5d 个文件,共 %-10d 字节,$(formatFileSize $updateFileTotal) \033[0m\n" "${updateFileCount}" "${updateFileTotal}"
printf "\033[36m-------------------------------------------------                \033[0m\n"
printf "\033[36m总计 %-5d 个文件,共 %-10d 字节,$(formatFileSize $fileTotal)       \033[0m\n" "${fileCount}"       "${fileTotal}"
echo ""

3、执行脚本,参数为提交的 commit ID 

bash /path/to/git-file-size-stat.sh 419fd3846fff2c41bd44ecbbf42d27f436f5a8c8

4、输出结果

☕️ 723862c86ef449ebcc535abdcc4b308e0b60d159 => 419fd3846fff2c41bd44ecbbf42d27f436f5a8c8 

[-] 161        file1.txt
[-] 238        file2.txt
[*] 1435       file3.txt
[*] 33         file4.txt
[+] 237        file5.txt
[+] 595        file6.txt
[*] 12850      file7.txt
[+] 1588       file8.txt
[*] 0          file9.txt
[*] 303        file0.txt
[*] -91        fileA.txt
[*] 586        fileB.txt
[*] 1944       fileC.txt
[*] 3          fileD.txt
[*] 6          fileE.txt  

🎉 统计结果 

新增 56    个文件,共 94017      字节,91.82KB 
删除 71    个文件,共 438873     字节,428.59KB 
更新 6     个文件,共 4795       字节,4.69KB 
---------------------------------------------------------------- 
总计 133   个文件,共 -340061    字节,-332.10KB 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值