Created
November 1, 2014 07:11
-
-
Save freemo/43996dbc393a1dc09fc6 to your computer and use it in GitHub Desktop.
A GIT hook for limiting the size of files committed to a repository.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
#echo "" | |
#echo "==== Remote update-hooks/file-hooks/limit-size.sh ====" | |
#Initialize | |
ref="$1" | |
old_rev="$2" | |
new_rev="$3" | |
tree_path="$4" | |
tree_file="$5" | |
#if the file isnt a regular file then lets just pass | |
if [ ! -f ${tree_path}/${tree_file} ]; then | |
exit 0 | |
fi | |
repo_path=$PWD | |
large_size=10000 | |
max_size=50000000 | |
#file_size=`ls -la ${tree_path}/${tree_file} | sed -e "s/^.\{10\} [0-9]* [^ ]* [^ ]* //g" | sed -e "s/ .*$//g"` | |
file_size="`cat "${tree_path}/${tree_file}" | gzip -c | wc -c`" | |
#try to get large and max file size from config | |
config_large_size=`git config --get hook.update.limit-size.large_size` | |
if [ "$config_large_size" -gt "0" 2>> /dev/null ]; then | |
large_size=$config_large_size | |
fi | |
config_max_size=`git config --get hook.update.limit-size.max_size` | |
if [ "$config_max_size" -gt "0" 2>> /dev/null ]; then | |
max_size=$config_max_size | |
fi | |
#print some information that might be useful | |
#echo "ref: ${ref} - old: ${old_rev} - new: ${new_rev} - tree: ${tree_path} - file: ${tree_file}" | |
cd $tree_path | |
large_attr=`git --git-dir=${repo_path} --work-tree=${tree_path} check-attr large -- ${tree_file} | sed -e "s/^[^:]*: large: //g"` | |
#echo "large attr: $large_attr" | |
is_large="false" | |
if [ $large_attr = "set" ]; then | |
is_large="true" | |
elif [ $large_attr = "unset" ]; then | |
is_large="false" | |
elif [ $large_attr = "unspecified" ]; then | |
is_large="false" | |
elif [ "$large_attr" -gt "$large_size" ]; then | |
is_large="true" | |
max_size=$large_attr | |
elif [ "$large_attr" -lt "$large_size" ]; then | |
is_large="true" | |
max_size=$large_attr | |
large_size=$large_attr | |
fi | |
#we dont need to do anything at all if the file isnt large | |
if [ "$file_size" -lt "$large_size" ]; then | |
exit 0 | |
fi | |
#since we got this far its size is large enough to be large, so this needs to agree with is_large to pass | |
if [ $is_large = "false" ]; then | |
echo "ERROR: The file located at '${tree_file}' in commit ${new_rev} which you are trying to push is too large. It has a file size of ${file_size} bytes yet this type of file has a maximum allowable size of ${large_size} bytes. Please adjust your .gitattributes file." | |
exit 1 | |
elif [ "$file_size" -gt "$max_size" ]; then | |
echo "ERROR: The file located at '${tree_file}' in commit ${new_rev} which you are trying to push is too large. It has a file size of ${file_size} bytes yet this type of file has a maximum allowable size of ${max_size} bytes. Please adjust your .gitattributes file." | |
exit 1 | |
fi | |
#clean up | |
cd ${repo_path} | |
#echo "==== Exited Remote update-hooks/file-hooks/limit-size.sh ====" | |
#echo "" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment