angular-cn/tools/gulp-tasks/validate-commit-message.js
George Kalpakas 6788d86709 build: fix validate-commit-message on Windows (#28780)
Previously, the `validate-commit-message` gulp task was using the
`git log ... ^r1 r2` syntax to list commits between the base branch and
the current head. This didn't work as expected on Windows, because `^`
is the escape character. As a result, the command was equivalent to
`git log ... r1 r2` on Windows, which essentially logs all commits
reachable from either `r1` or `r2`.

This commit fixes it by switching to git's
[double-dot range notation][1] (`r1..r2`), which is an alias for the
`^r1 r2` syntax and works correctly on all platforms.

[1]: https://git-scm.com/docs/gitrevisions#_dotted_range_notations

Fixes #16830

PR Close #28780
2019-02-19 12:37:57 -08:00

60 lines
1.9 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 someCommitsInvalid = !commitsByLine.every(validateCommitMessage);
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);
}
};