2017-01-19 19:24:47 -05:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
2017-01-23 16:03:40 -05:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2017-01-19 19:24:47 -05:00
|
|
|
/**
|
|
|
|
* GIT commit message format enforcement
|
|
|
|
*
|
|
|
|
* Note: this script was originally written by Vojta for AngularJS :-)
|
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2019-08-06 13:17:17 -04:00
|
|
|
const config = require('./commit-message.json');
|
2019-08-06 13:24:10 -04:00
|
|
|
const FIXUP_PREFIX_RE = /^fixup! /i;
|
|
|
|
const SQUASH_PREFIX_RE = /^squash! /i;
|
2019-08-06 13:17:17 -04:00
|
|
|
const REVERT_PREFIX_RE = /^revert:? /i;
|
|
|
|
|
build: ensure fixup commits match an earlier, unmerged commit (#32023)
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
2019-08-06 13:33:20 -04:00
|
|
|
module.exports = (commitHeader, disallowSquash, nonFixupCommitHeaders) => {
|
2019-08-06 13:17:17 -04:00
|
|
|
if (REVERT_PREFIX_RE.test(commitHeader)) {
|
2018-04-20 21:03:41 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
build: ensure fixup commits match an earlier, unmerged commit (#32023)
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
2019-08-06 13:33:20 -04:00
|
|
|
const {header, type, scope, isFixup, isSquash} = parseCommitHeader(commitHeader);
|
2019-08-06 13:24:10 -04:00
|
|
|
|
|
|
|
if (isSquash && disallowSquash) {
|
|
|
|
error('The commit must be manually squashed into the target commit', commitHeader);
|
|
|
|
return false;
|
|
|
|
}
|
2019-08-06 13:17:17 -04:00
|
|
|
|
build: ensure fixup commits match an earlier, unmerged commit (#32023)
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
2019-08-06 13:33:20 -04:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2019-08-06 13:17:17 -04:00
|
|
|
if (header.length > config.maxLength) {
|
|
|
|
error(`The commit message header is longer than ${config.maxLength} characters`, commitHeader);
|
2017-01-19 19:24:47 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-08-06 13:17:17 -04:00
|
|
|
if (!type) {
|
|
|
|
const format = '<type>(<scope>): <subject>';
|
2017-01-19 19:24:47 -05:00
|
|
|
error(
|
2019-08-06 13:17:17 -04:00
|
|
|
`The commit message header does not match the format of '${format}' or 'Revert: "${format}"'`,
|
|
|
|
commitHeader);
|
2017-01-19 19:24:47 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-08-06 13:17:17 -04:00
|
|
|
if (!config.types.includes(type)) {
|
|
|
|
error(`'${type}' is not an allowed type.\n => TYPES: ${config.types.join(', ')}`, commitHeader);
|
2017-01-19 19:24:47 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-08-06 13:17:17 -04:00
|
|
|
if (scope && !config.scopes.includes(scope)) {
|
2017-01-19 19:24:47 -05:00
|
|
|
error(
|
2019-08-06 13:17:17 -04:00
|
|
|
`'${scope}' is not an allowed scope.\n => SCOPES: ${config.scopes.join(', ')}`,
|
|
|
|
commitHeader);
|
2017-01-19 19:24:47 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
build: ensure fixup commits match an earlier, unmerged commit (#32023)
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
2019-08-06 13:33:20 -04:00
|
|
|
module.exports.FIXUP_PREFIX_RE = FIXUP_PREFIX_RE;
|
2019-08-06 13:17:17 -04:00
|
|
|
module.exports.config = config;
|
|
|
|
|
|
|
|
// Helpers
|
|
|
|
function error(errorMessage, commitHeader) {
|
|
|
|
console.error(`INVALID COMMIT MSG: ${commitHeader}\n => ERROR: ${errorMessage}`);
|
2017-01-19 19:24:47 -05:00
|
|
|
}
|
2018-01-09 16:46:19 -05:00
|
|
|
|
2019-08-06 13:17:17 -04:00
|
|
|
function parseCommitHeader(header) {
|
2019-08-06 13:24:10 -04:00
|
|
|
const isFixup = FIXUP_PREFIX_RE.test(header);
|
|
|
|
const isSquash = SQUASH_PREFIX_RE.test(header);
|
|
|
|
header = header.replace(FIXUP_PREFIX_RE, '').replace(SQUASH_PREFIX_RE, '');
|
2019-08-06 13:17:17 -04:00
|
|
|
|
|
|
|
const match = /^(\w+)(?:\(([^)]+)\))?\: (.+)$/.exec(header) || [];
|
|
|
|
|
|
|
|
return {
|
|
|
|
header,
|
|
|
|
type: match[1],
|
|
|
|
scope: match[2],
|
2019-08-06 13:24:10 -04:00
|
|
|
subject: match[3], isFixup, isSquash,
|
2019-08-06 13:17:17 -04:00
|
|
|
};
|
|
|
|
}
|