-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgit-hook-pre-push.sh
94 lines (79 loc) · 2.76 KB
/
git-hook-pre-push.sh
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
#!/bin/sh
# This hook will PREVENT a GIT push where ..
# * The build is passing AND a commit message starts with WIP
# * The build is failing AND a commit message does not start with WIP
# * The build is failing AND the branch is master or develop
RED='\033[0;31m';
YELLOW='\033[0;33m';
GREEN='\033[0;32m';
NC='\033[0m'; # No color
echo;
echo -e "pre-push: Executing pre-push git hook";
echo -e "pre-push: Before pushing lets try building ..";
echo;
npm run build -s;
if [ $? -eq 0 ]; then
build=true;
echo;
echo -e "pre-push: Build passed";
else
build=false;
echo;
echo -e "pre-push: Build failed";
fi
remote="$1";
url="$2";
z40=0000000000000000000000000000000000000000;
wip=false;
branch=`git symbolic-ref --short HEAD`;
prevent=false;
while read local_ref local_sha remote_ref remote_sha; do
if [ "$local_sha" = ${z40} ]; then
# Handle delete
:
else
if [ "$remote_sha" = ${z40} ]; then
# New branch, examine all commits
range="$local_sha";
else
# Update to existing branch, examine new commits
range="$remote_sha..$local_sha";
fi
# Check for WIP commit
commit=`git rev-list -n 1 --grep '^WIP' "$range"`;
if [ -n "$commit" ]; then
echo >&2 "pre-push: Found WIP commit";
wip=true;
fi
fi
done
echo -e "pre-push: SUMMARY: build=${build}, WIP=${wip}, branch=${branch}";
if [ "${build}" == "true" ] && [ "${wip}" == "true" ]; then
echo -e "pre-push: ${RED}Push prevented .. The build is passing AND a commit message starts with WIP";
echo -e " TIP: Change your commit messages with ..";
echo -e " git commit --amend -m \"new message\" OR git rebase -i (then use reword (r))${NC}";
prevent=true;
fi
if [ "${build}" == "false" ] && [ "${wip}" == "false" ]; then
echo -e "pre-push: ${RED}Push prevented .. The build is failing AND a commit message does not start with WIP";
echo -e " TIP: Fix the build (preferred) OR change your commit messages with ..";
echo -e " git commit --amend -m \"new message\" OR git rebase -i (then use reword (r))${NC}";
prevent=true;
fi
if [ "${build}" == "false" ] && [ "${branch}" == "master" ]; then
echo -e "pre-push: ${RED}Push prevented .. The build is failing AND the branch is master${NC}";
prevent=true;
fi
if [ "${build}" == "false" ] && [ "${branch}" == "develop" ]; then
echo -e "pre-push: ${RED}Push prevented .. The build is failing AND the branch is develop${NC}";
prevent=true;
fi
if [ "${prevent}" == "false" ] && [ "${wip}" == "true" ]; then
echo -e "pre-push: ${YELLOW}Push permitted but it is against policy to push failing code - Please fix ASAP${NC}";
fi
if [ "${prevent}" == "false" ] && [ "${wip}" == "false" ]; then
echo -e "pre-push: ${GREEN}Push permitted${NC}";
fi
test "${prevent}" == "false";
exitCode=$?;
exit ${exitCode};