Skip to content

Instantly share code, notes, and snippets.

@hhhaiai
Forked from vra/pre-commit
Created January 20, 2021 03:23
Show Gist options
  • Save hhhaiai/7a175a2e247069dd41e36fff64dc9936 to your computer and use it in GitHub Desktop.
Save hhhaiai/7a175a2e247069dd41e36fff64dc9936 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