-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathupdatehook
executable file
·71 lines (60 loc) · 2.17 KB
/
updatehook
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
#!/bin/bash
# hook to make sure back merges are not accidentally added
ref_name="$1"
old_rev="$2"
new_rev="$3"
#echo "ref_name: ${ref_name}"
#echo "old_rev: ${old_rev}"
#echo "new_rev: ${new_rev}"
# only check commits made after this date
check_from_date="2017-01-27"
zero="0000000000000000000000000000000000000000"
back_merge_regex="Merge (remote-tracking )?branch '(.*/)?master' into"
#branch \'*./master\'"
#echo "only run on feature branches. Checking if master is pushed."
if [ "${ref_name}" == "refs/heads/master" ]; then
echo "No need to check for back-merges. Pushing master."
exit 0
fi
# ignore deletion of branches
if [ "${new_rev}" == "${zero}" ]; then
echo "No need to check for back-merges. Deleting branch."
exit 0
fi
#echo "checking ${ref_name}"
# if it's a new branch use the merge base for the range to check
if [ "${old_rev}" == "${zero}" ]; then
old_rev=$(git merge-base ${new_rev} refs/heads/master)
fi
#echo "checking if force pushed to older commit"
if git merge-base --is-ancestor ${new_rev} ${old_rev}; then
echo "Force pushed to erase some commits. No need to check anything."
exit 0
fi
#echo "checking if force pushed new commits"
if ! git merge-base --is-ancestor ${old_rev} ${new_rev}; then
echo "Force pushed new commits. Need to check all commits to branch point."
old_rev=$(git merge-base ${new_rev} refs/heads/master)
# echo "old_ref is now: $old_rev"
fi
#echo "grabbing commit references to check"
# get the commits to check
commits=$(git rev-list --since=${check_from_date} ${old_rev}..${new_rev})
#echo "will check the following commits for this pattern '${back_merge_regex}'"
#echo ${commits}
for commit in ${commits}; do
# echo "inspecting commit ${commit}"
message=$(git log -1 --format="%s" ${commit})
# echo "this is the commit message: ${message}"
if [[ "${message}" =~ $back_merge_regex ]]; then
echo "****"
echo "**** Back merges are a bad practice. If this needs to be overridden, change the commit message."
echo "****"
echo "**** $commit $message"
echo "****"
echo "**** More information here: https://github.com/Adaptech/developer-guidelines#abstain-from-back-merges "
echo "****"
exit 1
fi
done
exit 0