3df54be9e4
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
67 lines
2.3 KiB
JavaScript
67 lines
2.3 KiB
JavaScript
/**
|
|
* @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
|
|
*/
|
|
|
|
|
|
// tslint:disable:no-console
|
|
module.exports = (gulp) => () => {
|
|
try {
|
|
const validateCommitMessage = require('../validate-commit-message');
|
|
const shelljs = require('shelljs');
|
|
|
|
shelljs.set('-e'); // Break on error.
|
|
|
|
let baseBranch = 'master';
|
|
const currentVersion = require('semver').parse(require('../../package.json').version);
|
|
const baseHead =
|
|
shelljs
|
|
.exec(`git ls-remote --heads origin ${currentVersion.major}.${currentVersion.minor}.*`)
|
|
.trim()
|
|
.split('\n')
|
|
.pop();
|
|
if (baseHead) {
|
|
const match = /refs\/heads\/(.+)/.exec(baseHead);
|
|
baseBranch = match && match[1] || baseBranch;
|
|
}
|
|
|
|
// We need to fetch origin explicitly because it might be stale.
|
|
// I couldn't find a reliable way to do this without fetch.
|
|
const result = shelljs.exec(
|
|
`git fetch origin ${baseBranch} && git log --reverse --format=%s origin/${baseBranch}..HEAD`);
|
|
|
|
if (result.code) {
|
|
throw new Error(`Failed to fetch commits: ${result.stderr}`);
|
|
}
|
|
|
|
const commitsByLine = result.trim().split(/\n/).filter(line => line != '');
|
|
|
|
console.log(`Examining ${commitsByLine.length} commit(s) between ${baseBranch} and HEAD`);
|
|
|
|
if (commitsByLine.length == 0) {
|
|
console.log(`There are zero new commits between ${baseBranch} and HEAD`);
|
|
}
|
|
|
|
const disallowSquashCommits = true;
|
|
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(
|
|
'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);
|
|
process.exit(1);
|
|
}
|
|
};
|