-
Notifications
You must be signed in to change notification settings - Fork 18
/
pre-commit.hook
executable file
·88 lines (72 loc) · 2.72 KB
/
pre-commit.hook
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/bin/bash
#
# The hook should exit with non-zero status after issuing
# an appropriate message if it wants to stop the commit.
# Strictly disallow uninitialized Variables
set -u
# Exit if a single command breaks and its failure is not handled accordingly
set -e
if git rev-parse --verify HEAD >/dev/null 2>&1; then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi
# If there are whitespace errors, print the offending file names and fail.
git diff-index --check --cached $against -- 1>&2 || echo -e "\033[0;31mThis commit contains whitespace issues!\033[0m"
# Usefull hooks for the Arch User Repository or Arch Build System
# Fix environment of githooks
unset GIT_DIR
# Allow to read user input below, assigns stdin to keyboard
exec < /dev/tty
# Iterate through folders with a PKGBUILD
# shellcheck disable=SC2046
for path in $(dirname $(git diff --name-only --cached) 2>/dev/null | sort -u); do
if [[ -f "${path}/PKGBUILD" ]]; then
echo -e "\033[1;32m==>\e[39;1m Entering ${path}...\e[0m"
# Ask whether updating the checksums is desired
updpkgsums_cmd="updpkgsums"
echo -en "\e[34;1m ->\e[39;1m Do you want to update the checksums using '${updpkgsums_cmd}'? [Y/n] \e[0m"
read -rn1 user_input
echo
if [[ ${user_input} =~ (Y|y) ]]; then
(cd "${path}" && ${updpkgsums_cmd})
# Update tracked PKGBUILD
git add -u "${path}/PKGBUILD" 1> /dev/null
fi
unset user_input
# Ask whether a rebuild is desired
makepkg_cmd="makepkg -sfc"
echo -en "\e[34;1m ->\e[39;1m Do you want to rebuild the package using '${makepkg_cmd}'? [Y/n] \e[0m"
read -rn1 user_input
echo
if [[ ${user_input} =~ (Y|y) ]]; then
cd "${path}" || exit 1
echo
if ! ${makepkg_cmd}; then
# Ask to abort the commit in case the build fails
echo -en "\n\e[34;1m ->\e[39;1m A failure occured while building the package. Do you want to abort this commit? [Y/n] \e[0m"
read -rn1 user_input_two
echo
if [[ ${user_input_two} =~ (Y|y) ]]; then
exit 2
fi
else
echo
fi
cd - 1>/dev/null
fi
unset user_input
echo -e "\e[34;1m ->\e[39;1m Generating .SRCINFO...\e[0m"
(cd "${path}" && makepkg --printsrcinfo > .SRCINFO)
echo -e "\e[34;1m ->\e[39;1m Checking PKGBUILD with namcap...\033[0m"
[[ ! -x "/usr/bin/namcap" ]] && echo -e "\e[0;31m ->\e[39;1m Unable to execute /usr/bin/namcap... aborting commit\e[0m" && exit 1
(cd "${path}" && namcap PKGBUILD)
# Add the .SRCINFO file if it is not tracked already
if ! git ls-files "${path}/.SRCINFO" --error-unmatch &> /dev/null; then
git add "${path}/.SRCINFO"
echo -e "\e[34;1m ->\e[39;1m Adding .SRCINFO to git tree...\033[0m"
fi
git add -u "${path}/.SRCINFO" 1>/dev/null
fi
done