-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpre-commit
More file actions
92 lines (71 loc) · 2.65 KB
/
pre-commit
File metadata and controls
92 lines (71 loc) · 2.65 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
#!/bin/sh
#
# Hook used prior to commit to ensure that the branch being pushed to
# is not a 'protected' branch (develop or master).
# Also verifies that the local branch is up to date with it's upstream.
# A commit will be prevented if on develop or mater and/or the local branch is not up to date with it's remote.
#
#
read -r -d '' ERROR_IMG << "EOM"
__
_ ,___,-'",-=-.
__,-- _ _,-'_)_ (""`'-._\ `.
_,' __ |,' ,-' __) ,- /. |
,'_,--' | -' _)/ `\
,',' ,' ,-'_,` :
,' ,-' ,(,-( :
,' ,-' , _ ;
/ ,-._/`---' /
/ (____)(----. ) ,'
/ ( `.__, /\ /,
: ;-.___ /__\/|
| ,' `--. -,\ |
: / \ .__/
\ (__ \ |_
\ ,`-, * / _|,\
\ ,' `-. ,'_,-' \
(_\,-' ,'\")--,'-' __\
\ / // ,'| ,--' `-.
`-. `-/ \' | _,' `.
`-._ / `--'/ \
,' | \
/ | \
,-' | /
/ | -'
_____ ____ _ _ _
| __ \ / __ \ | | | | | |
| | | | | | | | | |__| | | |
| | | | | | | | | __ | | |
| |__| | | |__| | | | | | |_|
|_____/ \____/ |_| |_| (_)
EOM
readonly protected_branches=("develop" "master")
readonly current_branch=$(git rev-parse --abbrev-ref HEAD)
readonly status=$(git fetch && git status -sb)
readonly regex="behind ([0-9]*)"
is_current_branch_protected=false
for protected_branch in "${protected_branches[@]}"; do
if [[ $protected_branch = "$current_branch" ]]; then
is_current_branch_protected=true
echo "setting is branch to true"
fi
done
if $is_current_branch_protected; then
echo "$ERROR_IMG"
cat <<\EOM
Uh-oh spaghetti-o's! Attempt to commit directly to a protected branch
It is not advised to commit directly to develop or master.
Any changes to one of these branches should be made via a pull request.
EOM
exit 1
fi
if [[ $status =~ $regex ]]; then
echo "$ERROR_IMG"
cat <<\EOM
Uh-oh spaghetti-o's! Attempt to commit without latest remote changes
Your branch is a number of commits behind its remote tracking branch
Use "git pull" to merge remote changes into yours
EOM
exit 1
fi
exit 0