Skip to content

Commit abc044e

Browse files
authored
rules: accept revert commit titles that were elongated by git (#99)
This change ensures that the commit message linter doesn't reject revert commits that originally had commit titles with an acceptable length but were elongated during the revert process by running `git revert`. Fixes: #97 Signed-off-by: Darshan Sen <raisinten@gmail.com>
1 parent 63c3f75 commit abc044e

File tree

2 files changed

+83
-3
lines changed

2 files changed

+83
-3
lines changed

lib/rules/title-length.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ module.exports = {
1717
max_length: 72
1818
},
1919
validate: (context, rule) => {
20-
const max = rule.options.max_length
20+
const isRevertCommit = /^Revert ".*"$/.test(context.title)
21+
const max = rule.options.max_length + (isRevertCommit ? 'Revert ""'.length : 0)
2122
if (context.title.length > max) {
2223
context.report({
2324
id: id,
@@ -31,7 +32,7 @@ module.exports = {
3132
return
3233
}
3334

34-
const len = rule.options.length
35+
const len = rule.options.length + (isRevertCommit ? 'Revert ""'.length : 0)
3536
if (context.title.length > len) {
3637
context.report({
3738
id: id,

test/validator.js

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const str2 = `commit b6475b9a9d0da0971eec7eb5559dff4d18a0e721
2525
Author: Evan Lucas <evanlucas@me.com>
2626
Date: Tue Mar 29 08:09:37 2016 -0500
2727
28-
Revert "tty: don't read from console stream upon creation"
28+
Revert "tty: do not read from the console stream upon creation"
2929
3030
This reverts commit 461138929498f31bd35bea61aa4375a2f56cceb7.
3131
@@ -127,6 +127,42 @@ Date: Thu Mar 3 10:10:46 2016 -0600
127127
test: Check memoryUsage properties.
128128
`
129129

130+
const str10 = `commit b04fe688d5859f707cf1a5e0206967268118bf7a
131+
Author: Darshan Sen <raisinten@gmail.com>
132+
Date: Sun May 1 21:10:21 2022 +0530
133+
134+
Revert "bootstrap: delay the instantiation of maps in per-context scripts"
135+
136+
The linked issue, https://bugs.chromium.org/p/v8/issues/detail?id=6593,
137+
is marked as "Fixed", so I think we can revert this now.
138+
139+
This reverts commit 08a9c4a996964aca909cd75fa8ecafd652c54885.
140+
141+
Signed-off-by: Darshan Sen <raisinten@gmail.com>
142+
PR-URL: https://github.com/nodejs/node/pull/42934
143+
Refs: https://bugs.chromium.org/p/v8/issues/detail?id=9187
144+
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
145+
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
146+
`
147+
148+
const str11 = `commit b04fe688d5859f707cf1a5e0206967268118bf7a
149+
Author: Darshan Sen <raisinten@gmail.com>
150+
Date: Sun May 1 21:10:21 2022 +0530
151+
152+
Revert "bootstrap: delay the instantiation of all maps in the per-context scripts"
153+
154+
The linked issue, https://bugs.chromium.org/p/v8/issues/detail?id=6593,
155+
is marked as "Fixed", so I think we can revert this now.
156+
157+
This reverts commit 08a9c4a996964aca909cd75fa8ecafd652c54885.
158+
159+
Signed-off-by: Darshan Sen <raisinten@gmail.com>
160+
PR-URL: https://github.com/nodejs/node/pull/42934
161+
Refs: https://bugs.chromium.org/p/v8/issues/detail?id=9187
162+
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
163+
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
164+
`
165+
130166
test('Validator - misc', (t) => {
131167
const v = new Validator()
132168

@@ -215,6 +251,49 @@ test('Validator - real commits', (t) => {
215251
})
216252
})
217253

254+
t.test('accept revert commit titles that are elongated by git', (tt) => {
255+
const v = new Validator()
256+
v.lint(str10)
257+
v.on('commit', (data) => {
258+
const c = data.commit.toJSON()
259+
tt.equal(c.sha, 'b04fe688d5859f707cf1a5e0206967268118bf7a', 'sha')
260+
tt.equal(c.date, 'Sun May 1 21:10:21 2022 +0530', 'date')
261+
tt.deepEqual(c.subsystems, ['bootstrap'], 'subsystems')
262+
tt.equal(c.prUrl, 'https://github.com/nodejs/node/pull/42934', 'pr')
263+
tt.equal(c.revert, true, 'revert')
264+
const msgs = data.messages
265+
const filtered = msgs.filter((item) => {
266+
return item.level === 'fail'
267+
})
268+
tt.equal(filtered.length, 0, 'messages.length')
269+
tt.end()
270+
})
271+
})
272+
273+
t.test('reject revert commit titles whose original titles are really long', (tt) => {
274+
const v = new Validator()
275+
v.lint(str11)
276+
v.on('commit', (data) => {
277+
const c = data.commit.toJSON()
278+
tt.equal(c.sha, 'b04fe688d5859f707cf1a5e0206967268118bf7a', 'sha')
279+
tt.equal(c.date, 'Sun May 1 21:10:21 2022 +0530', 'date')
280+
tt.deepEqual(c.subsystems, ['bootstrap'], 'subsystems')
281+
tt.equal(c.prUrl, 'https://github.com/nodejs/node/pull/42934', 'pr')
282+
tt.equal(c.revert, true, 'revert')
283+
const msgs = data.messages
284+
const filtered = msgs.filter((item) => {
285+
return item.level === 'fail'
286+
})
287+
tt.equal(filtered.length, 1, 'messages.length')
288+
const ids = filtered.map((item) => {
289+
return item.id
290+
})
291+
const exp = ['title-length']
292+
tt.deepEqual(ids.sort(), exp.sort(), 'message ids')
293+
tt.end()
294+
})
295+
})
296+
218297
t.test('invalid pr-url, missing subsystem', (tt) => {
219298
const v = new Validator()
220299
v.lint(str4)

0 commit comments

Comments
 (0)