Skip to content

Commit e3c42a7

Browse files
chore(validator): add revert commit validation
1 parent 98614fa commit e3c42a7

File tree

3 files changed

+110
-18
lines changed

3 files changed

+110
-18
lines changed

README.md

+23-7
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,12 @@ Must be one of the following:
7979
- **refactor**: A code change that neither fixes a bug or adds a feature
8080
- **test**: Adding missing tests or correcting existing tests
8181
- **chore**: Changes to the build process or auxiliary tools and libraries such as distribution generation
82-
- **revert**: Reverts a previous commit
8382

8483
### Scope
8584

8685
The scope could be anything specifying place of the commit change. For example `notification', 'dropdown', etc.
8786
The scope must be written in [kebab-case](https://en.wikipedia.org/wiki/Letter_case#Special_case_styles).
8887

89-
If the commit reverts a previous commit, it should contains the reverted commit SHA1.
90-
9188
### Subject
9289

9390
A brief but meaningfull description of the change.
@@ -97,12 +94,9 @@ Here are some recommandation for writing your subject:
9794
- don't capitalize first letter
9895
- no "." (dot) at the end
9996

100-
If the commit reverts a previous commit, it should contains the title of the reverted commit.
101-
10297
### Body
10398

10499
The body should include the motivation for the change and contrast this with previous behavior.
105-
If the commit reverts a previous commit, explain why you reverted it.
106100

107101
### Footer
108102

@@ -125,6 +119,28 @@ first thing broken
125119
second thing broken
126120
```
127121

122+
### Revert
123+
124+
The proper for revert based on Angular commit message convention should be:
125+
126+
```
127+
revert: feat(toto-service): provide toto for all
128+
129+
This reverts commit <sha1>.
130+
```
131+
132+
However, the default git behavior, that cannot be easily overiden is:
133+
134+
```
135+
Revert "feat(toto-service): provide toto for all"
136+
137+
This reverts commit <sha1>.
138+
```
139+
140+
Thus we won't enforce one or the other, we will only enfore:
141+
* starting the commit title with revert (with a capitalized letter or not)
142+
* having the sentence "This reverts commit <sha1>"
143+
128144
### Built With
129145

130146
- [bash](https://www.gnu.org/software/bash/)
@@ -198,7 +214,7 @@ See the [open issues](https://github.com/lumapps/commit-msg-validator/issues) fo
198214
- [x] enforce the JIRA reference
199215
- [x] enforce the BROKEN part length
200216
- [x] avoid trailing space
201-
- [ ] allow automated revert commit
217+
- [x] allow automated revert commit
202218
- [ ] allow fixup! and squash! commit with an option
203219

204220
<!-- CONTRIBUTING -->

validator.bats

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

267+
@test "header overall should allow 'revert: type(scope): message'" {
268+
validate_header "revert: type(scope): message"
269+
[[ $GLOBAL_TYPE == "revert" ]]
270+
}
271+
272+
@test "header overall should allow 'Revert \#type(scope): message\"'" {
273+
validate_header "Revert \"type(scope): message\""
274+
[[ $GLOBAL_TYPE == "revert" ]]
275+
}
276+
267277
@test "header overall should allow 'type(scope): message'" {
268278
validate_header "type(scope): message"
269279
[[ $GLOBAL_TYPE == "type" ]]
@@ -425,6 +435,26 @@ LUM-2345'
425435
[[ "$status" -eq 0 ]]
426436
}
427437

438+
@test "revert body without commit sha1 should be refused" {
439+
MESSAGE='rerer
440+
441+
LUM-2345'
442+
443+
run validate_revert "$MESSAGE"
444+
[[ "$status" -eq $ERROR_REVERT ]]
445+
}
446+
447+
@test "revert body with commit sha1 should be valid" {
448+
MESSAGE='rerer
449+
450+
This reverts commit 1234567890.
451+
452+
LUM-2345'
453+
454+
run validate_revert "$MESSAGE"
455+
[[ "$status" -eq 0 ]]
456+
}
457+
428458
@test "features and fixes commits need jira reference" {
429459
[[ `need_jira "feat"` -eq 1 ]]
430460
[[ `need_jira "fix"` -eq 1 ]]
@@ -578,3 +608,21 @@ BROKEN:
578608
run validate "$MESSAGE"
579609
[[ "$status" -eq 0 ]]
580610
}
611+
612+
@test "overall revert validation" {
613+
MESSAGE='Revert "feat(scope1): subject"
614+
615+
This reverts commit 12345678900.
616+
Commit about stuff\"plop \" dezd
617+
618+
12345678901234567890123456789012345678901234567890
619+
12345678901234567890123456789012345678901234567890
620+
621+
LUM-2345
622+
BROKEN:
623+
- plop
624+
- plop'
625+
626+
run validate "$MESSAGE"
627+
[[ "$status" -eq 0 ]]
628+
}

validator.sh

+39-11
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ if [[ $ZSH_NAME != "" ]]; then
77
fi
88

99
readonly HEADER_PATTERN="^([^\(]+)\(([^\)]+)\): (.+)$"
10-
readonly TYPE_PATTERN="^(feat|fix|docs|refactor|test|chore|revert)$"
10+
readonly TYPE_PATTERN="^(feat|fix|docs|refactor|test|chore)$"
1111
readonly SCOPE_PATTERN="^([a-z][a-z0-9]*)(-[a-z0-9]+)*$"
1212
readonly SUBJECT_PATTERN="^([a-z0-9].*[^ ^\.])$"
1313
readonly JIRA_PATTERN="^([A-Z]{2,4}-[0-9]{1,6} ?)+$"
1414
readonly BROKE_PATTERN="^BROKEN:$"
1515
readonly TRAILING_SPACE_PATTERN=" +$"
16+
readonly REVERT_HEADER_PATTERN="^[R|r]evert[: ].*$"
17+
readonly REVERT_COMMIT_PATTERN="^This reverts commit ([a-f0-9]+)"
1618

1719
readonly ERROR_STRUCTURE=1
1820
readonly ERROR_HEADER=2
@@ -23,6 +25,7 @@ readonly ERROR_SUBJECT=6
2325
readonly ERROR_BODY_LENGTH=7
2426
readonly ERROR_TRAILING_SPACE=8
2527
readonly ERROR_JIRA=9
28+
readonly ERROR_REVERT=10
2629

2730
GLOBAL_HEADER=""
2831
GLOBAL_BODY=""
@@ -123,7 +126,9 @@ validate_overall_structure() {
123126
validate_header() {
124127
local HEADER="$1"
125128

126-
if [[ $HEADER =~ $HEADER_PATTERN ]]; then
129+
if [[ $HEADER =~ $REVERT_HEADER_PATTERN ]]; then
130+
GLOBAL_TYPE="revert"
131+
elif [[ $HEADER =~ $HEADER_PATTERN ]]; then
127132
GLOBAL_TYPE=${BASH_REMATCH[1]}
128133
GLOBAL_SCOPE=${BASH_REMATCH[2]}
129134
GLOBAL_SUBJECT=${BASH_REMATCH[3]}
@@ -223,6 +228,24 @@ validate_jira() {
223228
fi
224229
}
225230

231+
validate_revert() {
232+
local BODY=$1
233+
local LINE=""
234+
local REVERTED_COMMIT=""
235+
236+
while IFS= read -r LINE ;
237+
do
238+
if [[ $LINE =~ $REVERT_COMMIT_PATTERN ]]; then
239+
REVERTED_COMMIT=${BASH_REMATCH[1]}
240+
fi
241+
done <<< "$BODY"
242+
243+
if [[ "$REVERTED_COMMIT" = "" ]]; then
244+
echo -e "revert commit should contain the reverted sha1"
245+
exit $ERROR_REVERT
246+
fi
247+
}
248+
226249
validate() {
227250
local COMMIT_MSG="$1"
228251

@@ -234,21 +257,26 @@ validate() {
234257
local FOOTER="$GLOBAL_FOOTER"
235258

236259
validate_header "$HEADER"
237-
validate_header_length "$HEADER"
238260

239261
local TYPE="$GLOBAL_TYPE"
240262
local SCOPE="$GLOBAL_SCOPE"
241263
local SUBJECT="$GLOBAL_SUBJECT"
242264

243-
validate_type "$TYPE"
244-
validate_scope "$SCOPE"
245-
validate_subject "$SUBJECT"
265+
if [[ $TYPE = "revert" ]]; then
266+
validate_revert "$BODY"
267+
else
268+
validate_header_length "$HEADER"
269+
270+
validate_type "$TYPE"
271+
validate_scope "$SCOPE"
272+
validate_subject "$SUBJECT"
246273

247-
validate_body_length "$BODY"
248-
validate_body_length "$FOOTER"
274+
validate_body_length "$BODY"
275+
validate_body_length "$FOOTER"
249276

250-
validate_trailing_space "$BODY"
251-
validate_trailing_space "$FOOTER"
277+
validate_trailing_space "$BODY"
278+
validate_trailing_space "$FOOTER"
252279

253-
validate_jira "$TYPE" "$JIRA"
280+
validate_jira "$TYPE" "$JIRA"
281+
fi
254282
}

0 commit comments

Comments
 (0)