Skip to content

Commit b664ae1

Browse files
committed
Fix imperative mood false positives
Because: * The imperative mood check is incorrectly reporting a warning when a commit subject is in imperative mood, but contains a word on the blacklist, e.g. "Remove the temporary fixes to foo" * This is because the check is currently checking for the presence of the blacklist words anywhere in the string. This change: * Add test for the false positive case. * Fix the issue by having the check test only the first word of the subject against the blacklist. Notes: * This may lead to some false negatives, but it's best effort, and the false positives are coming up relatively frequently.
1 parent 0952e5b commit b664ae1

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

hook.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ validate_commit_message() {
180180
shopt -s nocasematch
181181

182182
for BLACKLISTED_WORD in "${IMPERATIVE_MOOD_BLACKLIST[@]}"; do
183-
[[ ${COMMIT_SUBJECT} =~ $BLACKLISTED_WORD ]]
183+
[[ ${COMMIT_SUBJECT} =~ ^[[:blank:]]*$BLACKLISTED_WORD ]]
184184
test $? -eq 0 && add_warning 1 "Use the imperative mood in the subject line, e.g 'fix' not 'fixes'" && break
185185
done
186186

test/validation.bats

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,30 +172,38 @@ EOF
172172
# 5. Use the imperative mood in the subject line
173173
# ------------------------------------------------------------------------------
174174

175-
@test "validation: subject line with 'fixes' shows warning" {
175+
@test "validation: subject line starting with 'fixes' shows warning" {
176176
echo "n" > $FAKE_TTY
177-
run git commit -m "More fixes for broken stuff"
177+
run git commit -m "Fixes for broken stuff"
178178

179179
assert_failure
180180
assert_line --partial "Use the imperative mood in the subject line"
181181
}
182182

183-
@test "validation: subject line with 'fixed' shows warning" {
183+
@test "validation: subject line starting with 'fixed' shows warning" {
184184
echo "n" > $FAKE_TTY
185185
run git commit -m "Fixed bug with Y"
186186

187187
assert_failure
188188
assert_line --partial "Use the imperative mood in the subject line"
189189
}
190190

191-
@test "validation: subject line with 'fixing' shows warning" {
191+
@test "validation: subject line starting with 'fixing' shows warning" {
192192
echo "n" > $FAKE_TTY
193193
run git commit -m "Fixing behavior of X"
194194

195195
assert_failure
196196
assert_line --partial "Use the imperative mood in the subject line"
197197
}
198198

199+
@test "validation: subject line in imperative mood with 'fixes' does not show warning" {
200+
echo "n" > $FAKE_TTY
201+
run git commit -m "Remove the temporary fixes to Y"
202+
203+
assert_success
204+
refute_line --partial "Use the imperative mood in the subject line"
205+
}
206+
199207
@test "validation: body with 'fixes', 'fixed', 'fixing' does not show warning" {
200208
run git commit -m "$(cat <<EOF
201209
Add foo bar string to my_file

0 commit comments

Comments
 (0)