Skip to content

Instantly share code, notes, and snippets.

@ak1ra-komj
Created February 24, 2025 12:19
Show Gist options
  • Save ak1ra-komj/dbe71c0ad26637617a0fc0a51bde7c0e to your computer and use it in GitHub Desktop.
Save ak1ra-komj/dbe71c0ad26637617a0fc0a51bde7c0e to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e
# 可配置参数 ========================================
filesize=100 # 文件大小(MiB)
modify_bytes=8 # 每次修改的字节数
commit_times=10 # 提交次数
repo_without_lfs_dir="repo_without_lfs"
repo_with_lfs_dir="repo_with_lfs"
# ==================================================
# 生成大文件的函数
generate_bigfile() {
local file="$1"
echo "生成 ${filesize}MiB 文件: $file"
dd if=/dev/urandom of="$file" bs=1M count=$filesize status=none
}
# 修改文件的函数
modify_file() {
local file="$1"
local file_size_bytes=$((filesize * 1024 * 1024))
local max_offset=$((file_size_bytes - modify_bytes))
# 生成随机偏移量
local offset
offset=$(shuf -i 0-$max_offset -n 1)
# 在随机位置写入数据
dd if=/dev/urandom of="$file" bs=1 count=$modify_bytes \
seek="$offset" conv=notrunc status=none
echo "修改位置: $offset"
}
# 初始化仓库并执行测试的通用函数
run_test() {
local repo_dir="$1"
local use_lfs="$2"
echo "========================================"
echo "测试仓库: $repo_dir (LFS: $use_lfs)"
echo "========================================"
# 清理并初始化仓库
rm -rf "$repo_dir"
mkdir -p "$repo_dir"
cd "$repo_dir"
git init --quiet
# LFS 初始化
if [ "$use_lfs" = "yes" ]; then
git lfs install --local
echo "bigfile.bin filter=lfs diff=lfs merge=lfs -text" >.gitattributes
git add .gitattributes
git commit -m "Add LFS tracking" --quiet
fi
# 生成并提交初始文件
generate_bigfile "bigfile.bin"
git add bigfile.bin
git commit -m "Initial commit" --quiet
# 循环提交修改
for ((i = 1; i <= commit_times; i++)); do
modify_file "bigfile.bin"
git add bigfile.bin
git commit -m "Update $i" --quiet
echo "提交次数: $i/$commit_times"
done
# 对 Git repo 执行一遍 git gc
git gc --aggressive
# 输出存储信息
echo -e "\n存储统计:"
du -sh .git | awk '{print "Git 仓库: "$1}'
if [ "$use_lfs" = "yes" ]; then
du -sh .git/lfs/objects | awk '{print "LFS 存储: "$1}'
fi
cd ..
}
# 执行测试 ==========================================
run_test "$repo_without_lfs_dir" "no"
run_test "$repo_with_lfs_dir" "yes"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment