aboutsummaryrefslogtreecommitdiff
path: root/.githooks
diff options
context:
space:
mode:
authorJakub Jirutka <jakub@jirutka.cz>2017-12-31 01:08:46 +0100
committerJakub Jirutka <jakub@jirutka.cz>2017-12-31 01:09:51 +0100
commit6b1401bfbcbd476aea75dcd136c1007913daffb1 (patch)
tree30c4a56e9f73fc0b992d6c557f8785b90835368d /.githooks
parent9a8fafdfdf6abbad63d5b26f6fdad386d63fdfa0 (diff)
downloadalpine_aports-6b1401bfbcbd476aea75dcd136c1007913daffb1.tar.bz2
alpine_aports-6b1401bfbcbd476aea75dcd136c1007913daffb1.tar.xz
alpine_aports-6b1401bfbcbd476aea75dcd136c1007913daffb1.zip
githooks: add check for file size into pre-commit hook
Diffstat (limited to '.githooks')
-rwxr-xr-x.githooks/pre-commit30
1 files changed, 26 insertions, 4 deletions
diff --git a/.githooks/pre-commit b/.githooks/pre-commit
index b286691a3d..3901bc09de 100755
--- a/.githooks/pre-commit
+++ b/.githooks/pre-commit
@@ -5,6 +5,10 @@
5# 5#
6set -eu 6set -eu
7 7
8# Maximal allowed size (in bytes) of a file.
9FILE_SIZE_LIMIT=262144 # 256 kiB
10
11
8if ! command -v sha512sum >/dev/null; then 12if ! command -v sha512sum >/dev/null; then
9 # macOS / BSDs (?) don't have sha512sum, but shasum. 13 # macOS / BSDs (?) don't have sha512sum, but shasum.
10 alias sha512sum='shasum -a 512' 14 alias sha512sum='shasum -a 512'
@@ -14,13 +18,13 @@ error() {
14 printf '\033[0;31mpre-commit:\033[0m %s\n' "$1" >&2 # red 18 printf '\033[0;31mpre-commit:\033[0m %s\n' "$1" >&2 # red
15} 19}
16 20
17# Prints paths of created or modified APKBUILDs being committed. 21# Prints paths of created or modified files being committed.
18changed_apkbuilds() { 22changed_files() {
19 git diff-index \ 23 git diff-index \
20 --name-only \ 24 --name-only \
21 --cached \ 25 --cached \
22 --diff-filter=ACMR HEAD \ 26 --diff-filter=ACMR HEAD \
23 -- '**/APKBUILD' 27 -- "$@"
24} 28}
25 29
26# Prints file names and checksums (in format <SHA-512>:<filename>) of local 30# Prints file names and checksums (in format <SHA-512>:<filename>) of local
@@ -79,7 +83,25 @@ check_local_sources() {
79 return $status 83 return $status
80} 84}
81 85
86# Checks if the file ($1) being committed is not bigger than FILE_SIZE_LIMIT.
87check_file_size() {
88 local path="$1"
89 local size
90
91 size=$(git cat-file -s ":$path")
92 if [ $size -gt $FILE_SIZE_LIMIT ]; then
93 local size_kb=$(( size / 1024 ))
82 94
83for apkbuild in $(changed_apkbuilds); do 95 error "file \"$path\" is quite big ($(( size / 1024 )) kiB), better to upload it to dev.alpinelinux.org"
96 return 1
97 fi
98}
99
100
101for apkbuild in $(changed_files '**/APKBUILD'); do
84 check_local_sources "$apkbuild" 102 check_local_sources "$apkbuild"
85done 103done
104
105for path in $(changed_files); do
106 check_file_size "$path"
107done