mirror of
https://gitlab.alpinelinux.org/alpine/aports.git
synced 2025-08-05 05:17:07 +02:00
githooks: add check for file size into pre-commit hook
This commit is contained in:
parent
9a8fafdfdf
commit
6b1401bfbc
@ -5,6 +5,10 @@
|
|||||||
#
|
#
|
||||||
set -eu
|
set -eu
|
||||||
|
|
||||||
|
# Maximal allowed size (in bytes) of a file.
|
||||||
|
FILE_SIZE_LIMIT=262144 # 256 kiB
|
||||||
|
|
||||||
|
|
||||||
if ! command -v sha512sum >/dev/null; then
|
if ! command -v sha512sum >/dev/null; then
|
||||||
# macOS / BSDs (?) don't have sha512sum, but shasum.
|
# macOS / BSDs (?) don't have sha512sum, but shasum.
|
||||||
alias sha512sum='shasum -a 512'
|
alias sha512sum='shasum -a 512'
|
||||||
@ -14,13 +18,13 @@ error() {
|
|||||||
printf '\033[0;31mpre-commit:\033[0m %s\n' "$1" >&2 # red
|
printf '\033[0;31mpre-commit:\033[0m %s\n' "$1" >&2 # red
|
||||||
}
|
}
|
||||||
|
|
||||||
# Prints paths of created or modified APKBUILDs being committed.
|
# Prints paths of created or modified files being committed.
|
||||||
changed_apkbuilds() {
|
changed_files() {
|
||||||
git diff-index \
|
git diff-index \
|
||||||
--name-only \
|
--name-only \
|
||||||
--cached \
|
--cached \
|
||||||
--diff-filter=ACMR HEAD \
|
--diff-filter=ACMR HEAD \
|
||||||
-- '**/APKBUILD'
|
-- "$@"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Prints file names and checksums (in format <SHA-512>:<filename>) of local
|
# Prints file names and checksums (in format <SHA-512>:<filename>) of local
|
||||||
@ -79,7 +83,25 @@ check_local_sources() {
|
|||||||
return $status
|
return $status
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Checks if the file ($1) being committed is not bigger than FILE_SIZE_LIMIT.
|
||||||
|
check_file_size() {
|
||||||
|
local path="$1"
|
||||||
|
local size
|
||||||
|
|
||||||
for apkbuild in $(changed_apkbuilds); do
|
size=$(git cat-file -s ":$path")
|
||||||
|
if [ $size -gt $FILE_SIZE_LIMIT ]; then
|
||||||
|
local size_kb=$(( size / 1024 ))
|
||||||
|
|
||||||
|
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"
|
check_local_sources "$apkbuild"
|
||||||
done
|
done
|
||||||
|
|
||||||
|
for path in $(changed_files); do
|
||||||
|
check_file_size "$path"
|
||||||
|
done
|
||||||
|
Loading…
Reference in New Issue
Block a user