forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commit-msg
executable file
·163 lines (139 loc) · 4.5 KB
/
commit-msg
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/env bash
#=============================================================================
# Copyright 2010-2011 Kitware, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#=============================================================================
# Prepare a copy of the message:
# - strip comment lines
# - stop at "diff --git" (git commit -v)
# Later versions of git gui on Windows don't set this properly
if [ -z "$GIT_DIR" ]; then
GIT_DIR=$(git rev-parse --git-dir)
fi
commit_msg="$GIT_DIR/COMMIT_MSG"
sed -n -e '/^#/d' -e '/^diff --git/q' -e 'p;d' "$1" > "$commit_msg"
die_advice='
To continue editing, run the command
git commit -e -F '"$commit_msg"'
(assuming your working directory is at the top).'
die() {
echo 'commit-msg hook failure' 1>&2
echo '-----------------------' 1>&2
echo '' 1>&2
echo "$@" 1>&2
test -n "$die_advice" && echo "$die_advice" 1>&2
exit 1
}
#-----------------------------------------------------------------------------
# Check the commit message layout with a simple state machine.
msg_is_merge() {
test -f "$GIT_DIR/MERGE_HEAD" &&
echo "$line" | grep "^Merge " >/dev/null 2>&1
}
msg_is_revert() {
echo "$line" | grep "^Revert " >/dev/null 2>&1
}
# This method taken from http://github.com/stephenh/git-central
msg_trac() {
grep -i '\(\(re\|refs\|qa\) #[0-9]\+\)\|\(no ticket\)' "$commit_msg" > /dev/null
if [ $? -ne 0 ]
then
die 'Please reference a trac ticket via "Re(fs)"'
fi
}
msg_first() {
len=$(echo -n "$line" | wc -c)
if test $len -eq 0; then
# not yet first line
return
elif test $len -lt 10; then
die 'Please make the first line at least 10 characters (yours is '$len'):
-------------------|
'"$line"'
-------------------|'
elif test $len -gt 70 && ! msg_is_merge && ! msg_is_revert; then
die 'The first line may be at most 70 characters (to fit on github summary page):
---------------------------------------------------------------------|
'"$line"'
---------------------------------------------------------------------|'
elif echo "$line" | grep "^[ ]\|[ ]$" >/dev/null 2>&1; then
die 'The first line may not have leading or trailing space:
['"$line"']'
else
# first line okay
state=second
fi
}
msg_second() {
if test "x$line" != "x"; then
die 'The second line must be empty:
'"$line"
else
state=rest
fi
}
msg_rest() {
if echo "$line" | grep -q "^Change-Id:"; then
state=gerrit
fi
}
msg_gerrit() {
test "x$line" = "x" && return
echo "$line" | grep -q '^[A-Za-z0-9-][A-Za-z0-9-]*:' && return
die 'The Change-Id line must appear in a footer at the bottom.'
}
# First check that a ticket is referenced - disable for now
#msg_trac
# Pipe commit message into the state machine.
state=first
cat "$commit_msg" |
while IFS='' read line; do
msg_$state || break
done &&
rm -f "$commit_msg" || exit 1
die_advice='' # No more temporary message file.
#-----------------------------------------------------------------------------
# Optionally run Gerrit's commit-msg hook to add a Change-Id line.
gerrit_advice() {
gerrits=$(git config -l |grep '^remote\.[^=]\+url=.*review.*$')
test "x$gerrits" != "x" || return 0
echo 'Some config values look like Gerrit Code Review URLs:'
echo ''
echo "$gerrits" | sed 's/^/ /'
echo '
This hook can automatically add a "Change-Id" footer to commit messages
to make interaction with Gerrit easier. To enable this feature, run
git config hooks.GerritId true
Then run "git commit --amend" to fix this commit. Otherwise, run
git config hooks.GerritId false
to disable the feature and this message.'
}
gerrit_error() {
die 'non-bool config value hooks.GerritId = '"$hooks_GerritId"
}
gerrit_hook() {
"$GIT_DIR/hooks/gerrit/commit-msg" "$@" ||
die 'gerrit/commit-msg failed'
}
hooks_GerritId=$(git config --get hooks.GerritId)
case "$hooks_GerritId" in
'true') gerrit_hook "$@" ;;
'false') ;;
'') gerrit_advice ;;
*) gerrit_error ;;
esac
#-----------------------------------------------------------------------------
# Chain to project-specific hook.
#. "$GIT_DIR/hooks/hooks-chain.bash"
#hooks_chain commit-msg "$@"