2017-09-22 13:51:03 -04: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-10-12 07:28:43 -04:00
|
|
|
|
2017-09-22 13:51:03 -04:00
|
|
|
// tslint:disable:no-console
|
2020-01-29 12:47:54 -05:00
|
|
|
module.exports = (gulp) => async() => {
|
2019-02-17 03:57:31 -05:00
|
|
|
try {
|
|
|
|
const validateCommitMessage = require('../validate-commit-message');
|
|
|
|
const shelljs = require('shelljs');
|
2020-01-29 12:47:54 -05:00
|
|
|
const getRefsAndShasForTarget = require('../utils/get-refs-and-shas-for-target');
|
2019-02-17 03:57:31 -05:00
|
|
|
|
2020-01-29 18:19:37 -05:00
|
|
|
// Break on error.
|
|
|
|
shelljs.set('-e');
|
|
|
|
|
2019-02-17 03:57:31 -05:00
|
|
|
|
2020-01-29 18:19:37 -05:00
|
|
|
let target = {};
|
|
|
|
if (process.env['CI'] === 'true') {
|
|
|
|
// Validation of commit messages on CI
|
|
|
|
if (process.env['CI_PULL_REQUEST'] === 'false') {
|
|
|
|
// Skip commit message validation on CI for non-PR branches as we are not testing new
|
|
|
|
// unreviewed commits. By enforcing correctness on the incoming changes in the PR
|
|
|
|
// branches, we are able to render this check unnecessary on non-PR branches.
|
|
|
|
console.info(
|
|
|
|
`Since valid commit messages are enforced by PR linting on CI,\n` +
|
|
|
|
`we do not need to validate commit messages on CI runs on upstream branches.\n\n` +
|
|
|
|
`Skipping validate-commit-message check`);
|
|
|
|
process.exit();
|
|
|
|
}
|
|
|
|
target = await getRefsAndShasForTarget(process.env['CI_PULL_REQUEST']);
|
|
|
|
} else {
|
|
|
|
// Validation of commit messages locally
|
|
|
|
const baseRef = 'master';
|
|
|
|
const headRef = shelljs.exec('git symbolic-ref --short HEAD', {silent: true}).trim();
|
|
|
|
const baseSha = shelljs.exec(`git rev-parse origin/master`, {silent: true}).trim();
|
|
|
|
const headSha = shelljs.exec(`git rev-parse HEAD`, {silent: true}).trim();
|
|
|
|
const commonAncestorSha =
|
|
|
|
shelljs.exec(`git merge-base origin/master ${headSha}`, {silent: true}).trim();
|
|
|
|
target = {
|
|
|
|
base: {
|
|
|
|
ref: baseRef,
|
|
|
|
sha: baseSha,
|
|
|
|
},
|
|
|
|
head: {
|
|
|
|
ref: headRef,
|
|
|
|
sha: headSha,
|
|
|
|
},
|
|
|
|
commonAncestorSha: commonAncestorSha,
|
|
|
|
latestShaOfTargetBranch: baseSha,
|
|
|
|
latestShaOfPrBranch: headSha,
|
|
|
|
};
|
|
|
|
}
|
2019-02-17 03:57:31 -05:00
|
|
|
|
|
|
|
const result = shelljs.exec(
|
2020-01-29 18:19:37 -05:00
|
|
|
`git log --reverse --format=%s ${target.commonAncestorSha}..${target.latestShaOfPrBranch}`,
|
|
|
|
{silent: true});
|
2019-02-17 03:57:31 -05:00
|
|
|
|
|
|
|
if (result.code) {
|
|
|
|
throw new Error(`Failed to fetch commits: ${result.stderr}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
const commitsByLine = result.trim().split(/\n/).filter(line => line != '');
|
|
|
|
|
2020-01-29 12:47:54 -05:00
|
|
|
console.log(`Examining ${commitsByLine.length} commit(s) between ${target.base.ref} and HEAD`);
|
2019-02-17 03:57:31 -05:00
|
|
|
|
|
|
|
if (commitsByLine.length == 0) {
|
2020-01-29 12:47:54 -05:00
|
|
|
console.log(`There are zero new commits between ${target.base.ref} and HEAD`);
|
2019-02-17 03:57:31 -05:00
|
|
|
}
|
|
|
|
|
2019-08-06 13:24:10 -04:00
|
|
|
const disallowSquashCommits = 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 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);
|
|
|
|
});
|
2019-02-17 03:57:31 -05:00
|
|
|
|
|
|
|
if (someCommitsInvalid) {
|
|
|
|
throw new Error(
|
|
|
|
'Please fix the failing commit messages before continuing...\n' +
|
|
|
|
'Commit message guidelines: https://github.com/angular/angular/blob/master/CONTRIBUTING.md#-commit-message-guidelines');
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
2017-10-12 07:28:43 -04:00
|
|
|
process.exit(1);
|
|
|
|
}
|
2017-02-03 03:10:41 -05:00
|
|
|
};
|