Skip to content

Commit e37f266

Browse files
josephperrottAndrewKushnir
authored andcommitted
build: require a commit body in commit messages (angular#36632)
Enforces a requirement that all PR commit messages contain a body of at least 100 characters. This is meant to encourage commits within the repo to be more descriptive of each change. PR Close angular#36632
1 parent 5e579c4 commit e37f266

File tree

3 files changed

+27
-44
lines changed

3 files changed

+27
-44
lines changed

.dev-infra.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"commitMessage": {
33
"maxLength": 120,
4-
"minBodyLength": 0,
4+
"minBodyLength": 100,
55
"types": [
66
"build",
77
"ci",
@@ -44,4 +44,4 @@
4444
"zone.js"
4545
]
4646
}
47-
}
47+
}

dev-infra/commit-message/validate.spec.ts

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -160,21 +160,12 @@ describe('validate-commit-message.js', () => {
160160
});
161161

162162
describe('(squash)', () => {
163-
it('should strip the `squash! ` prefix and validate the rest', () => {
164-
const errorMessage = `The commit message header does not match the expected format.`;
165-
166-
// Valid messages.
167-
expect(validateCommitMessage('squash! feat(core): add feature')).toBe(VALID);
168-
expect(validateCommitMessage('squash! fix: a bug', {disallowSquash: false})).toBe(VALID);
169-
170-
// Invalid messages.
171-
expect(validateCommitMessage('squash! fix a typo', {disallowSquash: false})).toBe(INVALID);
172-
expect(lastError).toContain('squash! fix a typo');
173-
expect(lastError).toContain(errorMessage);
174-
175-
expect(validateCommitMessage('squash! squash! fix: a bug')).toBe(INVALID);
176-
expect(lastError).toContain('squash! squash! fix: a bug');
177-
expect(lastError).toContain(errorMessage);
163+
describe('without `disallowSquash`', () => {
164+
it('should return commits as valid', () => {
165+
expect(validateCommitMessage('squash! feat(core): add feature')).toBe(VALID);
166+
expect(validateCommitMessage('squash! fix: a bug')).toBe(VALID);
167+
expect(validateCommitMessage('squash! fix a typo')).toBe(VALID);
168+
});
178169
});
179170

180171
describe('with `disallowSquash`', () => {
@@ -191,21 +182,10 @@ describe('validate-commit-message.js', () => {
191182

192183
describe('(fixup)', () => {
193184
describe('without `nonFixupCommitHeaders`', () => {
194-
it('should strip the `fixup! ` prefix and validate the rest', () => {
195-
const errorMessage = `The commit message header does not match the expected format.`;
196-
197-
// Valid messages.
185+
it('should return commits as valid', () => {
198186
expect(validateCommitMessage('fixup! feat(core): add feature')).toBe(VALID);
199187
expect(validateCommitMessage('fixup! fix: a bug')).toBe(VALID);
200-
201-
// Invalid messages.
202-
expect(validateCommitMessage('fixup! fix a typo')).toBe(INVALID);
203-
expect(lastError).toContain('fixup! fix a typo');
204-
expect(lastError).toContain(errorMessage);
205-
206-
expect(validateCommitMessage('fixup! fixup! fix: a bug')).toBe(INVALID);
207-
expect(lastError).toContain('fixup! fixup! fix: a bug');
208-
expect(lastError).toContain(errorMessage);
188+
expect(validateCommitMessage('fixup! fixup! fix: a bug')).toBe(VALID);
209189
});
210190
});
211191

dev-infra/commit-message/validate.ts

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -82,20 +82,29 @@ export function validateCommitMessage(
8282
////////////////////////////////////
8383
// Checking revert, squash, fixup //
8484
////////////////////////////////////
85+
86+
// All revert commits are considered valid.
8587
if (commit.isRevert) {
8688
return true;
8789
}
8890

89-
if (commit.isSquash && options.disallowSquash) {
90-
error('The commit must be manually squashed into the target commit');
91-
return false;
91+
// All squashes are considered valid, as the commit will be squashed into another in
92+
// the git history anyway, unless the options provided to not allow squash commits.
93+
if (commit.isSquash) {
94+
if (options.disallowSquash) {
95+
error('The commit must be manually squashed into the target commit');
96+
return false;
97+
}
98+
return true;
9299
}
93100

94-
// If it is a fixup commit and `nonFixupCommitHeaders` is not empty, we only care to check whether
95-
// there is a corresponding non-fixup commit (i.e. a commit whose header is identical to this
96-
// commit's header after stripping the `fixup! ` prefix).
97-
if (commit.isFixup && options.nonFixupCommitHeaders) {
98-
if (!options.nonFixupCommitHeaders.includes(commit.header)) {
101+
// Fixups commits are considered valid, unless nonFixupCommitHeaders are provided to check
102+
// against. If `nonFixupCommitHeaders` is not empty, we check whether there is a corresponding
103+
// non-fixup commit (i.e. a commit whose header is identical to this commit's header after
104+
// stripping the `fixup! ` prefix), otherwise we assume this verification will happen in another
105+
// check.
106+
if (commit.isFixup) {
107+
if (options.nonFixupCommitHeaders && !options.nonFixupCommitHeaders.includes(commit.header)) {
99108
error(
100109
'Unable to find match for fixup commit among prior commits: ' +
101110
(options.nonFixupCommitHeaders.map(x => `\n ${x}`).join('') || '-'));
@@ -132,12 +141,6 @@ export function validateCommitMessage(
132141
// Checking commit body //
133142
//////////////////////////
134143

135-
// Commit bodies are not checked for fixups and squashes as they will be squashed into
136-
// another commit in the history anyway.
137-
if (commit.isFixup || commit.isSquash) {
138-
return true;
139-
}
140-
141144
if (commit.bodyWithoutLinking.trim().length < config.minBodyLength) {
142145
error(`The commit message body does not meet the minimum length of ${
143146
config.minBodyLength} characters`);

0 commit comments

Comments
 (0)