Commit 6b1401bf authored by Jakub Jirutka's avatar Jakub Jirutka

githooks: add check for file size into pre-commit hook

parent 9a8fafdf
......@@ -5,6 +5,10 @@
#
set -eu
# Maximal allowed size (in bytes) of a file.
FILE_SIZE_LIMIT=262144 # 256 kiB
if ! command -v sha512sum >/dev/null; then
# macOS / BSDs (?) don't have sha512sum, but shasum.
alias sha512sum='shasum -a 512'
......@@ -14,13 +18,13 @@ error() {
printf '\033[0;31mpre-commit:\033[0m %s\n' "$1" >&2 # red
}
# Prints paths of created or modified APKBUILDs being committed.
changed_apkbuilds() {
# Prints paths of created or modified files being committed.
changed_files() {
git diff-index \
--name-only \
--cached \
--diff-filter=ACMR HEAD \
-- '**/APKBUILD'
-- "$@"
}
# Prints file names and checksums (in format <SHA-512>:<filename>) of local
......@@ -79,7 +83,25 @@ check_local_sources() {
return $status
}
# Checks if the file ($1) being committed is not bigger than FILE_SIZE_LIMIT.
check_file_size() {
local path="$1"
local size
size=$(git cat-file -s ":$path")
if [ $size -gt $FILE_SIZE_LIMIT ]; then
local size_kb=$(( size / 1024 ))
for apkbuild in $(changed_apkbuilds); do
error "file \"$path\" is quite big ($(( size / 1024 )) kiB), better to upload it to dev.alpinelinux.org"
return 1
fi
}
for apkbuild in $(changed_files '**/APKBUILD'); do
check_local_sources "$apkbuild"
done
for path in $(changed_files); do
check_file_size "$path"
done
Markdown is supported
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