-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpre-commit-hook
More file actions
executable file
·103 lines (82 loc) · 2.97 KB
/
pre-commit-hook
File metadata and controls
executable file
·103 lines (82 loc) · 2.97 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/bin/bash
DEBUG=0
ID=$(id -u)
# Check if we could detect the current user id
if [ -z "${ID}" ]; then
cat <<EOF >&2
Could not determine Unix user id.
Commit aborted!
EOF
exit 1
fi
ALLOWROOT=$(git config --get commit.allowroot)
# Check if we aren't currently logged in as root
if [ "${ID}" = "0" ] && [ "x${ALLOWROOT}" != "xyes" ]; then
cat <<EOF >&2
You are currently logged in as root. You should never commit as root.
Log in as the proper user for this repository. If you can't directly
SSH as the proper user, you might have to add your SSH key to the authorized_keys
file of the user.
Commit aborted!
EOF
exit 1
fi
# Check if we have a valid connection with the SSH agent
HASAGENT=$(ssh-add -l 2>/dev/null | head -n1 | grep -oE "[0-9a-zA-Z:]{47}|SHA256:[A-Za-z0-9+/=]{43}")
if [ -z "${HASAGENT}" ]; then
cat <<EOF >&2
Could not find a proper SSH key. This will prevent you from pushing later.
Commit aborted!
EOF
exit 1
fi
# Retrieve author information as Git sees it while commiting
AUTHORINFO=$(git var GIT_AUTHOR_IDENT) || exit 1
NAME=$(printf '%s\n' "${AUTHORINFO}" | sed -n 's/^\(.*\) <.*$/\1/p')
EMAIL=$(printf '%s\n' "${AUTHORINFO}" | sed -n 's/^.* <\(.*\)> .*$/\1/p')
if [ $DEBUG -eq 1 ]; then
printf "AUTHORINFO: %s\n" "${AUTHORINFO}"
printf "NAME: %s\n" "${NAME}"
printf "EMAIL: %s\n" "${EMAIL}"
fi
# Retrieve author information of the Repo user
REPONAME=$(git config --get user.name)
REPOEMAIL=$(git config --get user.email)
if [ $DEBUG -eq 1 ]; then
printf "REPONAME: %s\n" "${REPONAME}"
printf "REPOEMAIL: %s\n" "${REPOEMAIL}"
fi
$(git var GIT_AUTHOR_IDENT | grep -qv root)
AUTH_CHECK=$?
# If the author is the same as the Repo user or root
if [ "${NAME}" = "${REPONAME}" ] || [ "${EMAIL}" = "${REPOEMAIL}" ] || [ $AUTH_CHECK -ne 0 ]; then
cat <<EOF >&2
You have a SSH key set, but the author information is still set to the System user.
This probably means we could not match the email address in the SSH key to a valid Git
user.
If you think this is an error, add an entry to /etc/gitusers. If you want to commit
right now without doing this, you can use the below command to set an author for this
commit.
GIT_AUTHOR_NAME="Your Name" GIT_AUTHOR_EMAIL="your.name@email.com" git commit
Commit aborted!
EOF
exit 2
fi
## Lint checks
# PHP lint
PHP_EXT="\.php$|\.thtml$"
FAILED_LINT=$(git diff-index --cached HEAD | grep -v "0000000000000000000000000000000000000000 D" | awk '{ print $6 }' | egrep "$PHP_EXT" | xargs --replace -n 1 sh -c 'php -l "{}" > /dev/null 2> /dev/null || echo " {}"')
if [ -n "$FAILED_LINT" ]
then
echo -e "Some input files failed PHP lint, commit aborted: \n$FAILED_LINT"
exit 1
fi
# Ruby lint
RUBY_EXT="\.rb$"
FAILED_LINT=$(git diff-index --cached HEAD | grep -v "0000000000000000000000000000000000000000 D" | awk '{ print $6 }' | egrep "$RUBY_EXT" | xargs --replace -n 1 sh -c 'ruby -c "{}" > /dev/null 2> /dev/null || echo " {}"')
if [ -n "$FAILED_LINT" ]
then
echo -e "Some input files failed Ruby lint, commit aborted: \n$FAILED_LINT"
exit 1
fi
exit 0