Skip to content

Commit

Permalink
build: ensure fixup commits match an earlier, unmerged commit (#32023)
Browse files Browse the repository at this point in the history
Previously, `validate-commit-message` would treat `fixup! `-prefixed
commits like this:
- It would strip the `fixup! ` prefix.
- It would validate the rest of the commit message header as any other
  commit.

However, fixup commits are special in that they need to exactly match an
earlier commit message header (sans the `fixup! ` prefix) in order for
git to treat them correctly. Otherwise, they will not be squashed into
the original commits and will be merged as is. Fixup commits can end up
not matching their original commit for several reasons (e.g. accidental
typo, changing the original commit message, etc.).

This commit prevents invalid fixup commits to pass validation by
ensuring that they match an earlier (unmerged) commit (i.e. a commit
between the current HEAD and the BASE commit).

NOTE: This new behavior is currently not activated in the pre-commit git
      hook, that is used to validate commit messages (because the
      preceding, unmerged commits are not available there). It _is_
      activated in `gulp validate-commit-message`, which is run as part
      of the `lint` job on CI and thus will detect invalid commits,
      before their getting merged.

PR Close #32023
  • Loading branch information
gkalpak authored and kara committed Aug 9, 2019
1 parent edd6cb5 commit c67f490
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 4 deletions.
9 changes: 7 additions & 2 deletions tools/gulp-tasks/validate-commit-message.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,13 @@ module.exports = (gulp) => () => {
}

const disallowSquashCommits = true;
const someCommitsInvalid =
!commitsByLine.every(m => validateCommitMessage(m, disallowSquashCommits));
const isNonFixup = m => !validateCommitMessage.FIXUP_PREFIX_RE.test(m);
const someCommitsInvalid = !commitsByLine.every((m, i) => {
// `priorNonFixupCommits` is only needed if the current commit is a fixup commit.
const priorNonFixupCommits =
isNonFixup(m) ? undefined : commitsByLine.slice(0, i).filter(isNonFixup);
return validateCommitMessage(m, disallowSquashCommits, priorNonFixupCommits);
});

if (someCommitsInvalid) {
throw new Error(
Expand Down
20 changes: 18 additions & 2 deletions tools/validate-commit-message/validate-commit-message.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,33 @@ const FIXUP_PREFIX_RE = /^fixup! /i;
const SQUASH_PREFIX_RE = /^squash! /i;
const REVERT_PREFIX_RE = /^revert:? /i;

module.exports = (commitHeader, disallowSquash) => {
module.exports = (commitHeader, disallowSquash, nonFixupCommitHeaders) => {
if (REVERT_PREFIX_RE.test(commitHeader)) {
return true;
}

const {header, type, scope, isSquash} = parseCommitHeader(commitHeader);
const {header, type, scope, isFixup, isSquash} = parseCommitHeader(commitHeader);

if (isSquash && disallowSquash) {
error('The commit must be manually squashed into the target commit', commitHeader);
return false;
}

// If it is a fixup commit and `nonFixupCommitHeaders` is not empty, we only care to check whether
// there is a corresponding non-fixup commit (i.e. a commit whose header is identical to this
// commit's header after stripping the `fixup! ` prefix).
if (isFixup && nonFixupCommitHeaders) {
if (!nonFixupCommitHeaders.includes(header)) {
error(
'Unable to find match for fixup commit among prior commits: ' +
(nonFixupCommitHeaders.map(x => `\n ${x}`).join('') || '-'),
commitHeader);
return false;
}

return true;
}

if (header.length > config.maxLength) {
error(`The commit message header is longer than ${config.maxLength} characters`, commitHeader);
return false;
Expand Down Expand Up @@ -61,6 +76,7 @@ module.exports = (commitHeader, disallowSquash) => {
return true;
};

module.exports.FIXUP_PREFIX_RE = FIXUP_PREFIX_RE;
module.exports.config = config;

// Helpers
Expand Down
53 changes: 53 additions & 0 deletions tools/validate-commit-message/validate-commit-message.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,5 +177,58 @@ describe('validate-commit-message.js', () => {
});
});
});

describe('(fixup)', () => {

describe('without `nonFixupCommitHeaders`', () => {

it('should strip the `fixup! ` prefix and validate the rest', () => {
const errorMessageFor = header =>
`INVALID COMMIT MSG: ${header}\n => ERROR: The commit message header does not match the format of ` +
'\'<type>(<scope>): <subject>\' or \'Revert: "<type>(<scope>): <subject>"\'';

// Valid messages.
expect(validateMessage('fixup! feat(core): add feature')).toBe(VALID);
expect(validateMessage('fixup! fix: a bug')).toBe(VALID);

// Invalid messages.
expect(validateMessage('fixup! fix a typo')).toBe(INVALID);
expect(validateMessage('fixup! fixup! fix: a bug')).toBe(INVALID);
expect(errors).toEqual([
errorMessageFor('fixup! fix a typo'),
errorMessageFor('fixup! fixup! fix: a bug'),
]);
});
});

describe('with `nonFixupCommitHeaders`', () => {

it('should check that the fixup commit matches a non-fixup one', () => {
const msg = 'fixup! foo';

expect(validateMessage(msg, false, ['foo', 'bar', 'baz'])).toBe(VALID);
expect(validateMessage(msg, false, ['bar', 'baz', 'foo'])).toBe(VALID);
expect(validateMessage(msg, false, ['baz', 'foo', 'bar'])).toBe(VALID);

expect(validateMessage(msg, false, ['qux', 'quux', 'quuux'])).toBe(INVALID);
expect(errors).toEqual([
`INVALID COMMIT MSG: ${msg}\n` +
' => ERROR: Unable to find match for fixup commit among prior commits: \n' +
' qux\n' +
' quux\n' +
' quuux',
]);
});

it('should fail if `nonFixupCommitHeaders` is empty', () => {
expect(validateMessage('refactor(router): make reactive', false, [])).toBe(VALID);
expect(validateMessage('fixup! foo', false, [])).toBe(INVALID);
expect(errors).toEqual([
'INVALID COMMIT MSG: fixup! foo\n' +
' => ERROR: Unable to find match for fixup commit among prior commits: -',
]);
});
});
});
});
});

0 comments on commit c67f490

Please sign in to comment.