Skip to content

Instantly share code, notes, and snippets.

@eikevons
Last active June 26, 2023 13:20
Show Gist options
  • Select an option

  • Save eikevons/6c4d490bcf6d36422ae624f6aa01bf0d to your computer and use it in GitHub Desktop.

Select an option

Save eikevons/6c4d490bcf6d36422ae624f6aa01bf0d to your computer and use it in GitHub Desktop.
git pre-commit hook to prevent checking in large files
#!/bin/sh
# Maximum file size limit in MiB
limitMiB=${GIT_MAX_MIB:-5} # Default 5MiB
if [ $limitMiB -le 0 ]; then
exit 0
fi
# Go to repo root folder
cd "$(git rev-parse --show-toplevel)"
git diff-index --cached --name-only HEAD | {
fail=0
while read file; do
sizeMiB=$(( $(stat -c '%s' "$file") / 1024 / 1024 ))
if [ $sizeMiB -gt $limitMiB ]; then
if [ $fail = 0 ]; then
echo "Some files exceed size limit of $limitMiB MiB:" 1>&2
echo 1>&2
fail=1
fi
echo "$file ($sizeMiB MiB)" 1>&2
fi
done
if [ "$fail" = 1 ];then
echo 1>&2
echo "(Use 'GIT_MAX_MIB=... git commit ...' to use a different limit. GIT_MAX_MIB=0 disables this hook.)" 1>&2
exit 1
fi
}
fail=$?
exit $fail
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment