Commit f8badc3d authored by Brad King's avatar Brad King 💬
Browse files

pre-commit: add support for the ghostflow hooks-max-size attribute

parent d033ed35
......@@ -163,6 +163,8 @@ check_mode() {
size_max_KiB=$(git config hooks.MaxObjectKiB)
test -n "$size_max_KiB" || size_max_KiB=1024
size_max_bytes=$(git config hooks.max-size)
test -n "$size_max_bytes" || size_max_bytes=$(("${size_max_KiB}" * 1024))
size_too_large_once=""
size_too_large_once() {
test -z "$size_too_large_once" || return ; size_too_large_once=done
......@@ -170,42 +172,61 @@ size_too_large_once() {
We prefer to keep large files out of the main source tree, especially
binary files that do not compress well. This hook disallows large files
by default but can be configured. A limit for specific files or patterns
may be set in ".gitattributes" with the "hooks.MaxObjectKiB" attribute.
may be set in ".gitattributes" with the "hooks-max-size" attribute.
For example, the line
*.c hooks.MaxObjectKiB=2048
*.c hooks-max-size=1500000
sets a limit of 2048 KiB for C source files. See "git help attributes"
sets a limit of 1500000 bytes for C source files. See "git help attributes"
for details on the .gitattributes format. If no attribute has been set
for a given file then its size is limited by the local default. Run
git config hooks.MaxObjectKiB $KiB
git config hooks.max-size $bytes
to set the local default limit (0 to disable).
'
}
size_too_large() {
size_too_large_once
echo "The path '$file' has size $file_KiB KiB, greater than allowed $max_KiB KiB."
echo "The path '$file' has size $file_bytes bytes, greater than allowed $max_bytes bytes."
}
size_validate_max_KiB() {
test "$max_KiB" -ge "0" 2>/dev/null && return 0
echo "The path '$file' has invalid attribute \"hooks-MaxObjectKiB=$max_KiB\"."
echo "The path '$file' has invalid attribute \"hooks.MaxObjectKiB=$max_KiB\"."
return 1
}
size_validate_max_bytes() {
test "$max_bytes" -ge "0" 2>/dev/null && return 0
echo "The path '$file' has invalid attribute \"hooks-max-size=$max_bytes\"."
return 1
}
check_size() {
test "$dst_obj" != "$zero" || return
max_KiB=$(git check-attr hooks.MaxObjectKiB -- "$file" |
sed 's/^[^:]*: hooks.MaxObjectKiB: //')
max_KiB=$(git check-attr hooks.MaxObjectKiB -- "$file" | sed 's/^[^:]*: hooks.MaxObjectKiB: //')
case "$max_KiB" in
'unset') return ;; # No maximum for this object.
'set') max_KiB="$size_max_KiB" ;; # Use local default.
'unspecified') max_KiB="$size_max_KiB" ;; # Use local default.
'unset') ;;
'set') ;;
'unspecified') ;;
*) size_validate_max_KiB || return ;;
esac
if test "$max_KiB" -gt "0"; then
file_KiB=$(expr '(' $(git cat-file -s "$dst_obj") + 1023 ')' / 1024)
test "$file_KiB" -le "$max_KiB" || size_too_large
max_bytes=$(git check-attr hooks-max-size -- "$file" | sed 's/^[^:]*: hooks-max-size: //')
case "$max_bytes" in
'unset') return ;; # No maximum for this object.
'set') max_bytes="$size_max_bytes" ;; # Use local default.
'unspecified')
# Fall back to max KiB setting.
case "$max_KiB" in
'unset') return ;; # No maximum for this object.
'set') max_bytes=$(("${size_max_KiB}" * 1024)) ;; # Use local default.
'unspecified') max_bytes="$size_max_bytes" ;; # Use local default.
*) max_bytes=$(("${max_KiB}" * 1024)) ;;
esac
;;
*) size_validate_max_bytes || return ;;
esac
if test "$max_bytes" -gt "0"; then
file_bytes=$(git cat-file -s "$dst_obj")
test "$file_bytes" -le "$max_bytes" || size_too_large
fi
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment