Skip to content

Instantly share code, notes, and snippets.

@vra
Last active March 15, 2024 06:48
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save vra/ae91ade1e15ce31b42f6366a91d1ac17 to your computer and use it in GitHub Desktop.
Save vra/ae91ade1e15ce31b42f6366a91d1ac17 to your computer and use it in GitHub Desktop.
#!/bin/sh
hard_limit=$(git config hooks.filesizehardlimit)
soft_limit=$(git config hooks.filesizesoftlimit)
: ${hard_limit:=10000000}
: ${soft_limit:=1000000}
list_new_or_modified_files()
{
git diff --staged --name-status|sed -e '/^D/ d; /^D/! s/.\s\+//'
}
unmunge()
{
local result="${1#\"}"
result="${result%\"}"
env echo -e "$result"
}
check_file_size()
{
n=0
while read -r munged_filename
do
f="$(unmunge "$munged_filename")"
h=$(git ls-files -s "$f"|cut -d' ' -f 2)
s=$(git cat-file -s "$h")
if [ "$s" -gt $hard_limit ]
then
env echo -E 1>&2 "ERROR: hard size limit ($hard_limit) exceeded: $munged_filename ($s)"
n=$((n+1))
elif [ "$s" -gt $soft_limit ]
then
env echo -E 1>&2 "WARNING: soft size limit ($soft_limit) exceeded: $munged_filename ($s)"
fi
done
[ $n -eq 0 ]
}
list_new_or_modified_files | check_file_size
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment