Skip to content

Commit d217f02

Browse files
committed
Extract COMMIT_SUBJECT variable
Because: * The upcoming support for the autosquash flags will require modifying the subject. Best to do this in one place.
1 parent 1200e2f commit d217f02

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

hook.sh

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ validate_commit_message() {
110110
# reset warnings
111111
WARNINGS=()
112112

113+
# capture the subject
114+
COMMIT_SUBJECT=${COMMIT_MSG_LINES[0]}
115+
113116
# if the commit is empty there's nothing to validate, we can return here
114117
COMMIT_MSG_STR="${COMMIT_MSG_LINES[*]}"
115118
test -n "${COMMIT_MSG_STR[*]// }" || return;
@@ -123,19 +126,19 @@ validate_commit_message() {
123126
# 2. Limit the subject line to 50 characters
124127
# ------------------------------------------------------------------------------
125128

126-
test "${#COMMIT_MSG_LINES[0]}" -le 50
127-
test $? -eq 0 || add_warning 1 "Limit the subject line to 50 characters (${#COMMIT_MSG_LINES[0]} chars)"
129+
test "${#COMMIT_SUBJECT}" -le 50
130+
test $? -eq 0 || add_warning 1 "Limit the subject line to 50 characters (${#COMMIT_SUBJECT} chars)"
128131

129132
# 3. Capitalize the subject line
130133
# ------------------------------------------------------------------------------
131134

132-
[[ ${COMMIT_MSG_LINES[0]} =~ ^[[:blank:]]*([[:upper:]]{1}[[:lower:]]*|[[:digit:]]+)([[:blank:]]|[[:punct:]]|$) ]]
135+
[[ ${COMMIT_SUBJECT} =~ ^[[:blank:]]*([[:upper:]]{1}[[:lower:]]*|[[:digit:]]+)([[:blank:]]|[[:punct:]]|$) ]]
133136
test $? -eq 0 || add_warning 1 "Capitalize the subject line"
134137

135138
# 4. Do not end the subject line with a period
136139
# ------------------------------------------------------------------------------
137140

138-
[[ ${COMMIT_MSG_LINES[0]} =~ [^\.]$ ]]
141+
[[ ${COMMIT_SUBJECT} =~ [^\.]$ ]]
139142
test $? -eq 0 || add_warning 1 "Do not end the subject line with a period"
140143

141144
# 5. Use the imperative mood in the subject line
@@ -174,7 +177,7 @@ validate_commit_message() {
174177
shopt -s nocasematch
175178

176179
for BLACKLISTED_WORD in "${IMPERATIVE_MOOD_BLACKLIST[@]}"; do
177-
[[ ${COMMIT_MSG_LINES[0]} =~ $BLACKLISTED_WORD ]]
180+
[[ ${COMMIT_SUBJECT} =~ $BLACKLISTED_WORD ]]
178181
test $? -eq 0 && add_warning 1 "Use the imperative mood in the subject line, e.g 'fix' not 'fixes'" && break
179182
done
180183

@@ -200,14 +203,14 @@ validate_commit_message() {
200203
# 8. Do no write single worded commits
201204
# ------------------------------------------------------------------------------
202205

203-
COMMIT_SUBJECT_WORDS=(${COMMIT_MSG_LINES[0]})
206+
COMMIT_SUBJECT_WORDS=(${COMMIT_SUBJECT})
204207
test "${#COMMIT_SUBJECT_WORDS[@]}" -gt 1
205208
test $? -eq 0 || add_warning 1 "Do no write single worded commits"
206209

207210
# 9. Do not start the subject line with whitespace
208211
# ------------------------------------------------------------------------------
209212

210-
[[ ${COMMIT_MSG_LINES[0]} =~ ^[[:blank:]]+ ]]
213+
[[ ${COMMIT_SUBJECT} =~ ^[[:blank:]]+ ]]
211214
test $? -eq 1 || add_warning 1 "Do not start the subject line with whitespace"
212215
}
213216

0 commit comments

Comments
 (0)