aboutsummaryrefslogtreecommitdiff
path: root/.githooks
diff options
context:
space:
mode:
authorJakub Jirutka <jakub@jirutka.cz>2017-12-28 22:56:46 +0100
committerJakub Jirutka <jakub@jirutka.cz>2017-12-28 23:00:27 +0100
commitae664fe0d034529e7c4819c1dd1758877dd77500 (patch)
tree4df71ec9bfbf02191073e4b1dd3b08fb4e18f03d /.githooks
parent9e7f9462b65ca3119c091f1e7f9e2665ddbb4627 (diff)
downloadalpine_aports-ae664fe0d034529e7c4819c1dd1758877dd77500.tar.bz2
alpine_aports-ae664fe0d034529e7c4819c1dd1758877dd77500.tar.xz
alpine_aports-ae664fe0d034529e7c4819c1dd1758877dd77500.zip
githooks: add pre-commit hook
Diffstat (limited to '.githooks')
-rwxr-xr-x.githooks/pre-commit84
1 files changed, 84 insertions, 0 deletions
diff --git a/.githooks/pre-commit b/.githooks/pre-commit
new file mode 100755
index 0000000000..1960d863ba
--- /dev/null
+++ b/.githooks/pre-commit
@@ -0,0 +1,84 @@
1#!/bin/sh
2#
3# This hook checks that all local sources specified in the staged APKBUILDs
4# are staged too or already committed and that checksums are correct.
5#
6set -eu
7
8if ! command -v sha512sum >/dev/null; then
9 # macOS / BSDs (?) don't have sha512sum, but shasum.
10 alias sha512sum='shasum -a 512'
11fi
12
13error() {
14 printf '\033[0;31mpre-commit:\033[0m %s\n' "$1" >&2 # red
15}
16
17# Prints paths of created or modified APKBUILDs being committed.
18changed_apkbuilds() {
19 git diff-index \
20 --name-only \
21 --cached \
22 --diff-filter=ACMR HEAD \
23 -- '**/APKBUILD'
24}
25
26# Prints file names and checksums (in format <SHA-512>:<filename>) of local
27# sources specified in the APKBUILD ($1).
28abuild_local_sources() {
29 apkbuild="$1"
30
31 set +eu
32 . "$apkbuild" || {
33 error "$apkbuild is invalid"
34 return 1
35 }
36 set -eu
37
38 status=0
39 for src in $source; do
40 # Skip remote sources.
41 case "$src" in */*) continue;; esac
42
43 echo "$sha512sums" | awk -v src="$src" '
44 { if ($2 == src) { ok=1; print($2 ":" $1) } }
45 END { if (ok != 1) exit 1 }' || {
46 status=1
47 error "${apkbuild%/*}: file \"$src\" is missing in \$sha512sums (hint: run abuild checksum)"
48 }
49 done
50
51 return $status
52}
53
54# Checks that all local sources specified in the APKBUILD file ($1) are
55# available in git tree and checksums are correct.
56check_local_sources() {
57 local apkbuild="$1"
58 local startdir="${apkbuild%/*}"
59 local status=0
60 local checksum content filename line sources
61
62 sources=$(abuild_local_sources "$apkbuild")
63 for line in $sources; do
64 filename=${line%%:*}
65 checksum=${line#*:}
66
67 content=$(git show ":$startdir/$filename" 2>/dev/null) || {
68 error "$startdir: missing file \"$filename\""
69 status=1
70 continue
71 }
72 [ "$(printf '%s\n' "$content" | sha512sum)" = "$checksum -" ] || {
73 error "$startdir: bad checksum for file \"$filename\" (hint: run abuild checksum)"
74 status=1
75 }
76 done
77
78 return $status
79}
80
81
82for apkbuild in $(changed_apkbuilds); do
83 check_local_sources "$apkbuild"
84done