Skip to content

Instantly share code, notes, and snippets.

@fkei
Created February 26, 2014 04:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fkei/9223602 to your computer and use it in GitHub Desktop.
Save fkei/9223602 to your computer and use it in GitHub Desktop.
git#pre-commit : This command prevent commit if total file size exceeds error is occured.
$ cat .git/hooks/pre-commit
#!/bin/bash
#
# This command prevent commit if total file size exceeds error is occured.
#

if [ -z "$filesizelimit" ]; then
  #### TODO: custom change
  #filesizelimit=1000 # 1M
  filesizelimit=400 # 400K
fi

total=0
for fname in `git diff-index --cached HEAD --name-only`; do
  if [ -f $fname ]; then
    size=`ls -Al $fname | awk '{ print $5 }'`
    total=`expr $total + $size`
  fi  
done

filesize=`expr $total / 1024`


if [ $filesize -gt $filesizelimit ]; then

  filename=`git diff-index -z --cached HEAD --name-only`

  echo "# =================" >&2
  echo "Error: Too large commit attempted." >&2
  echo  >&2
  echo "File's size limit is $filesizelimit KB" >&2
  echo "File's named $filename of size $filesize KB." >&2
  exit 1
fi

exit 0

test

$ git commit -a 
# =================
Error: Too large commit attempted.

File's size limit is 400 KB
File's named 1.png2.png of size 443 KB.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment