Skip to content

Commit 314847e

Browse files
chore(validator): add an option to allow temporary commits
1 parent 52d99b4 commit 314847e

File tree

3 files changed

+63
-3
lines changed

3 files changed

+63
-3
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ in `.git/hooks` directory of your repository.
200200
### OPTIONS
201201

202202
- if `COMMIT_VALIDATOR_NO_JIRA` environment variable is set, no validation is done on JIRA refs.
203+
- if `COMMIT_VALIDATOR_ALLOW_TEMP` environment variable is set, no validation is done on `fixup!` and `squash!` commits.
203204

204205
<!-- ROADMAP -->
205206

@@ -219,7 +220,7 @@ See the [open issues](https://github.com/lumapps/commit-msg-validator/issues) fo
219220
- [x] enforce the BROKEN part length
220221
- [x] avoid trailing space
221222
- [x] allow automated revert commit
222-
- [ ] allow fixup! and squash! commit with an option
223+
- [x] allow fixup! and squash! commit with an option
223224
- [x] allow to not check JIRA reference with an option
224225
- [ ] enforce subject length (3 words at least)
225226

validator.bats

+54
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,26 @@ BROKEN:
264264
[ "$status" -eq $ERROR_HEADER ]
265265
}
266266

267+
@test "header overall should allow fixup if env set" {
268+
COMMIT_VALIDATOR_ALLOW_TEMP= validate_header "fixup! plopezrr"
269+
[[ $GLOBAL_TYPE == "temp" ]]
270+
}
271+
272+
@test "header overall should allow squash if env set" {
273+
COMMIT_VALIDATOR_ALLOW_TEMP= validate_header "squash! plopezrr"
274+
[[ $GLOBAL_TYPE == "temp" ]]
275+
}
276+
277+
@test "header overall should reject fixup if env not set" {
278+
run validate_header "fixup! plopezrr"
279+
[[ "$status" -eq $ERROR_HEADER ]]
280+
}
281+
282+
@test "header overall should reject squash if env not set" {
283+
run validate_header "squash! plopezrr"
284+
[[ "$status" -eq $ERROR_HEADER ]]
285+
}
286+
267287
@test "header overall should allow 'revert: type(scope): message'" {
268288
validate_header "revert: type(scope): message"
269289
[[ $GLOBAL_TYPE == "revert" ]]
@@ -631,3 +651,37 @@ BROKEN:
631651
run validate "$MESSAGE"
632652
[[ "$status" -eq 0 ]]
633653
}
654+
655+
@test "overall fixup! validation" {
656+
MESSAGE='fixup! plepozkfopezr
657+
658+
Commit about stuff\"plop \" dezd
659+
660+
12345678901234567890123456789012345678901234567890
661+
12345678901234567890123456789012345678901234567890
662+
663+
LUM-2345
664+
BROKEN:
665+
- plop
666+
- plop'
667+
668+
COMMIT_VALIDATOR_ALLOW_TEMP= run validate "$MESSAGE"
669+
[[ "$status" -eq 0 ]]
670+
}
671+
672+
@test "overall fixup! rejection" {
673+
MESSAGE='fixup! plepozkfopezr
674+
675+
Commit about stuff\"plop \" dezd
676+
677+
12345678901234567890123456789012345678901234567890
678+
12345678901234567890123456789012345678901234567890
679+
680+
LUM-2345
681+
BROKEN:
682+
- plop
683+
- plop'
684+
685+
run validate "$MESSAGE"
686+
[[ "$status" -eq $ERROR_HEADER ]]
687+
}

validator.sh

+7-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ readonly BROKE_PATTERN="^BROKEN:$"
1313
readonly TRAILING_SPACE_PATTERN=" +$"
1414
readonly REVERT_HEADER_PATTERN="^[R|r]evert[: ].*$"
1515
readonly REVERT_COMMIT_PATTERN="^This reverts commit ([a-f0-9]+)"
16+
readonly TEMP_HEADER_PATTERN="^(fixup!|squash!).*$"
1617

1718
readonly ERROR_STRUCTURE=1
1819
readonly ERROR_HEADER=2
@@ -124,7 +125,9 @@ validate_overall_structure() {
124125
validate_header() {
125126
local HEADER="$1"
126127

127-
if [[ $HEADER =~ $REVERT_HEADER_PATTERN ]]; then
128+
if [[ -v COMMIT_VALIDATOR_ALLOW_TEMP && $HEADER =~ $TEMP_HEADER_PATTERN ]]; then
129+
GLOBAL_TYPE="temp"
130+
elif [[ $HEADER =~ $REVERT_HEADER_PATTERN ]]; then
128131
GLOBAL_TYPE="revert"
129132
elif [[ $HEADER =~ $HEADER_PATTERN ]]; then
130133
GLOBAL_TYPE=${BASH_REMATCH[1]}
@@ -268,7 +271,9 @@ validate() {
268271
local SCOPE="$GLOBAL_SCOPE"
269272
local SUBJECT="$GLOBAL_SUBJECT"
270273

271-
if [[ $TYPE = "revert" ]]; then
274+
if [[ $TYPE = "temp" ]]; then
275+
echo "ignoring temporary commit"
276+
elif [[ $TYPE = "revert" ]]; then
272277
validate_revert "$BODY"
273278
else
274279
validate_header_length "$HEADER"

0 commit comments

Comments
 (0)