Files
2026-05-08 23:40:42 +08:00

42 lines
1.1 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
limit_bytes="${GIT_MAX_FILE_SIZE_BYTES:-52428800}"
limit_mb=$((limit_bytes / 1024 / 1024))
failed=0
format_size() {
local bytes="$1"
awk -v bytes="$bytes" 'BEGIN {
if (bytes >= 1073741824) {
printf "%.1fGB", bytes / 1073741824
} else if (bytes >= 1048576) {
printf "%.1fMB", bytes / 1048576
} else if (bytes >= 1024) {
printf "%.1fKB", bytes / 1024
} else {
printf "%dB", bytes
}
}'
}
while IFS= read -r -d '' path; do
size="$(git cat-file -s ":$path" 2>/dev/null || printf '0')"
if [ "$size" -gt "$limit_bytes" ]; then
if [ "$failed" -eq 0 ]; then
printf '\nLarge files are not allowed in git. Limit: %sMB\n\n' "$limit_mb" >&2
fi
printf ' %s %s\n' "$(format_size "$size")" "$path" >&2
failed=1
fi
done < <(git diff --cached --name-only -z --diff-filter=ACMR)
if [ "$failed" -ne 0 ]; then
cat >&2 <<'EOF'
Move large files to S3/CDN or local storage, then commit only the URL/metadata.
To override the limit locally for one commit, set GIT_MAX_FILE_SIZE_BYTES, but avoid doing that for media/archive files.
EOF
exit 1
fi