Skip to content

Commit 0952e5b

Browse files
authored
Merge pull request #5 from tommarshall/4-handle-autosquash-flags
Handle autosquash flags
2 parents 1200e2f + cf9c350 commit 0952e5b

File tree

3 files changed

+45
-12
lines changed

3 files changed

+45
-12
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Offers an interactive prompt if any of the rules are detected to be broken.
2727
At the root of the repository, run:
2828

2929
```sh
30-
curl https://cdn.rawgit.com/tommarshall/git-good-commit/v0.3.0/hook.sh > .git/hooks/commit-msg && chmod +x .git/hooks/commit-msg
30+
curl https://cdn.rawgit.com/tommarshall/git-good-commit/v0.4.0/hook.sh > .git/hooks/commit-msg && chmod +x .git/hooks/commit-msg
3131
```
3232

3333
### Globally
@@ -37,14 +37,14 @@ To use the hook globally, you can use `git-init`'s template directory:
3737
```sh
3838
mkdir -p ~/.git-template/hooks
3939
git config --global init.templatedir '~/.git-template'
40-
curl https://cdn.rawgit.com/tommarshall/git-good-commit/v0.3.0/hook.sh > ~/.git-template/hooks/commit-msg && chmod +x ~/.git-template/hooks/commit-msg
40+
curl https://cdn.rawgit.com/tommarshall/git-good-commit/v0.4.0/hook.sh > ~/.git-template/hooks/commit-msg && chmod +x ~/.git-template/hooks/commit-msg
4141
```
4242

4343
The hook will now be present after any `git init` or `git clone`. You can [safely re-run `git init`](http://stackoverflow.com/a/5149861/885540) on any existing repositories to add the hook there.
4444

4545
---
4646

47-
_If you're security conscious, you may be reasonably suspicious of [curling executable files](https://www.seancassidy.me/dont-pipe-to-your-shell.html). In this case you're on HTTPS throughout, and not piping directly to execution, so you can check contents and the hash against MD5 `23a607325827a12482bb95a208c4a5ee` for v0.3.0._
47+
_If you're security conscious, you may be reasonably suspicious of [curling executable files](https://www.seancassidy.me/dont-pipe-to-your-shell.html). In this case you're on HTTPS throughout, and not piping directly to execution, so you can check contents and the hash against MD5 `1bbe22e756fe98dbcc8aae390e20367d` for v0.4.0._
4848

4949
## Usage
5050

hook.sh

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# git-good-commit(1) - Git hook to help you write good commit messages.
55
# Released under the MIT License.
66
#
7-
# Version 0.3.0
7+
# Version 0.4.0
88
#
99
# https://github.com/tommarshall/git-good-commit
1010
#
@@ -110,9 +110,15 @@ validate_commit_message() {
110110
# reset warnings
111111
WARNINGS=()
112112

113+
# capture the subject, and remove the 'squash! ' prefix if present
114+
COMMIT_SUBJECT=${COMMIT_MSG_LINES[0]/#squash! /}
115+
113116
# if the commit is empty there's nothing to validate, we can return here
114117
COMMIT_MSG_STR="${COMMIT_MSG_LINES[*]}"
115-
test -n "${COMMIT_MSG_STR[*]// }" || return;
118+
test -z "${COMMIT_MSG_STR[*]// }" && return;
119+
120+
# if the commit subject starts with 'fixup! ' there's nothing to validate, we can return here
121+
[[ $COMMIT_SUBJECT == 'fixup! '* ]] && return;
116122

117123
# 1. Separate subject from body with a blank line
118124
# ------------------------------------------------------------------------------
@@ -123,19 +129,19 @@ validate_commit_message() {
123129
# 2. Limit the subject line to 50 characters
124130
# ------------------------------------------------------------------------------
125131

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)"
132+
test "${#COMMIT_SUBJECT}" -le 50
133+
test $? -eq 0 || add_warning 1 "Limit the subject line to 50 characters (${#COMMIT_SUBJECT} chars)"
128134

129135
# 3. Capitalize the subject line
130136
# ------------------------------------------------------------------------------
131137

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

135141
# 4. Do not end the subject line with a period
136142
# ------------------------------------------------------------------------------
137143

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

141147
# 5. Use the imperative mood in the subject line
@@ -174,7 +180,7 @@ validate_commit_message() {
174180
shopt -s nocasematch
175181

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

@@ -200,14 +206,14 @@ validate_commit_message() {
200206
# 8. Do no write single worded commits
201207
# ------------------------------------------------------------------------------
202208

203-
COMMIT_SUBJECT_WORDS=(${COMMIT_MSG_LINES[0]})
209+
COMMIT_SUBJECT_WORDS=(${COMMIT_SUBJECT})
204210
test "${#COMMIT_SUBJECT_WORDS[@]}" -gt 1
205211
test $? -eq 0 || add_warning 1 "Do no write single worded commits"
206212

207213
# 9. Do not start the subject line with whitespace
208214
# ------------------------------------------------------------------------------
209215

210-
[[ ${COMMIT_MSG_LINES[0]} =~ ^[[:blank:]]+ ]]
216+
[[ ${COMMIT_SUBJECT} =~ ^[[:blank:]]+ ]]
211217
test $? -eq 1 || add_warning 1 "Do not start the subject line with whitespace"
212218
}
213219

test/validation.bats

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ EOF
2424
assert_success
2525
}
2626

27+
@test "validation: ignores commits with the fixup! autosquash flag" {
28+
echo "n" > $FAKE_TTY
29+
run git commit -m "$(cat <<EOF
30+
fixup! Add foo bar string to my_file - As requested by Jon
31+
Another line in the body that runs to longer than 72 characters in length
32+
EOF
33+
)"
34+
35+
assert_success
36+
}
37+
2738
# 0. Good commits - control
2839
# ------------------------------------------------------------------------------
2940

@@ -96,6 +107,14 @@ EOF
96107
assert_line --partial "Limit the subject line to 50 characters (51 chars)"
97108
}
98109

110+
@test "validation: ignores squash! prefix when checking subject line length" {
111+
echo "n" > $FAKE_TTY
112+
run git commit -m "squash! Add foo bar string to my_file - As requested by Jo"
113+
114+
assert_success
115+
refute_line --partial "Limit the subject line to 50 characters"
116+
}
117+
99118
# 3. Capitalize the subject line
100119
# ------------------------------------------------------------------------------
101120

@@ -131,6 +150,14 @@ EOF
131150
refute_line --partial "Capitalize the subject line"
132151
}
133152

153+
@test "validation: ignores squash! prefix when checking subject line capitalisation" {
154+
echo "n" > $FAKE_TTY
155+
run git commit -m "squash! Add foo bar string to my_file"
156+
157+
assert_success
158+
refute_line --partial "Capitalize the subject line"
159+
}
160+
134161
# 4. Do not end the subject line with a period
135162
# ------------------------------------------------------------------------------
136163

0 commit comments

Comments
 (0)