build: autosquashes SHAs as part of merge-pr script (#21791)
To support `git checkin --fixup` and `git checkin —squash` we need to make sure that `merge-pr` squashes the sepecial commits before they are merged. For more details see: https://robots.thoughtbot.com/autosquashing-git-commits PR Close #21791
This commit is contained in:

committed by
Misko Hevery

parent
27ecd077d4
commit
637515e71b
@ -20,30 +20,35 @@ const fs = require('fs');
|
||||
const path = require('path');
|
||||
const configPath = path.resolve(__dirname, './commit-message.json');
|
||||
const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
|
||||
const PATTERN = /^(revert\: )?(\w+)(?:\(([^)]+)\))?\: (.+)$/;
|
||||
const PATTERN = /^(\w+)(?:\(([^)]+)\))?\: (.+)$/;
|
||||
const FIXUP_SQUASH = /^(fixup|squash)\! /i;
|
||||
const REVERT = /^revert: (\"(.*)\"|(.*))?$/i;
|
||||
|
||||
module.exports = function(commitSubject) {
|
||||
commitSubject = commitSubject.replace(FIXUP_SQUASH, '');
|
||||
commitSubject = commitSubject.replace(REVERT, function(m, g1, g2, g3) { return g2 || g3; });
|
||||
|
||||
if (commitSubject.length > config['maxLength']) {
|
||||
error(`The commit message is longer than ${config['maxLength']} characters`, commitSubject);
|
||||
return false;
|
||||
}
|
||||
|
||||
const match = PATTERN.exec(commitSubject);
|
||||
if (!match || match[2] === 'revert') {
|
||||
if (!match) {
|
||||
error(
|
||||
`The commit message does not match the format of "<type>(<scope>): <subject> OR revert: type(<scope>): <subject>"`,
|
||||
`The commit message does not match the format of '<type>(<scope>): <subject>' OR 'Revert: "type(<scope>): <subject>"'`,
|
||||
commitSubject);
|
||||
return false;
|
||||
}
|
||||
|
||||
const type = match[2];
|
||||
const type = match[1];
|
||||
if (config['types'].indexOf(type) === -1) {
|
||||
error(
|
||||
`${type} is not an allowed type.\n => TYPES: ${config['types'].join(', ')}`, commitSubject);
|
||||
return false;
|
||||
}
|
||||
|
||||
const scope = match[3];
|
||||
const scope = match[2];
|
||||
|
||||
if (scope && !config['scopes'].includes(scope)) {
|
||||
error(
|
||||
|
@ -9,6 +9,7 @@
|
||||
describe('validate-commit-message.js', function() {
|
||||
var validateMessage = require('./validate-commit-message');
|
||||
var SCOPES = validateMessage.config.scopes.join(', ');
|
||||
var TYPES = validateMessage.config.types.join(', ');
|
||||
var errors = [];
|
||||
var logs = [];
|
||||
|
||||
@ -40,6 +41,10 @@ describe('validate-commit-message.js', function() {
|
||||
expect(validateMessage('test(packaging): something')).toBe(VALID);
|
||||
expect(validateMessage('release: something')).toBe(VALID);
|
||||
expect(validateMessage('release(packaging): something')).toBe(VALID);
|
||||
expect(validateMessage('release(packaging): something')).toBe(VALID);
|
||||
expect(validateMessage('fixup! release(packaging): something')).toBe(VALID);
|
||||
expect(validateMessage('squash! release(packaging): something')).toBe(VALID);
|
||||
expect(validateMessage('Revert: "release(packaging): something"')).toBe(VALID);
|
||||
expect(errors).toEqual([]);
|
||||
});
|
||||
|
||||
@ -88,21 +93,24 @@ describe('validate-commit-message.js', function() {
|
||||
|
||||
expect(validateMessage(msg)).toBe(INVALID);
|
||||
expect(errors).toEqual([
|
||||
'INVALID COMMIT MSG: "not correct format"\n => ERROR: The commit message does not match the format of "<type>(<scope>): <subject> OR revert: type(<scope>): <subject>"'
|
||||
'INVALID COMMIT MSG: "not correct format"\n => ERROR: The commit message does not match the format of \'<type>(<scope>): <subject>\' OR \'Revert: "type(<scope>): <subject>"\''
|
||||
]);
|
||||
});
|
||||
|
||||
|
||||
it('should support "revert: type(scope):" syntax and reject "revert(scope):" syntax', function() {
|
||||
let correctMsg = 'revert: fix(compiler): reduce generated code payload size by 65%';
|
||||
expect(validateMessage(correctMsg)).toBe(VALID);
|
||||
it('should support "revert: type(scope):" syntax and reject "revert(scope):" syntax',
|
||||
function() {
|
||||
let correctMsg = 'revert: fix(compiler): reduce generated code payload size by 65%';
|
||||
expect(validateMessage(correctMsg)).toBe(VALID);
|
||||
|
||||
let incorretMsg = 'revert(compiler): reduce generated code payload size by 65%';
|
||||
expect(validateMessage(incorretMsg)).toBe(INVALID);
|
||||
expect(errors).toEqual([
|
||||
'INVALID COMMIT MSG: "revert(compiler): reduce generated code payload size by 65%"\n => ERROR: The commit message does not match the format of "<type>(<scope>): <subject> OR revert: type(<scope>): <subject>"'
|
||||
]);
|
||||
});
|
||||
let incorretMsg = 'revert(compiler): reduce generated code payload size by 65%';
|
||||
expect(validateMessage(incorretMsg)).toBe(INVALID);
|
||||
expect(errors).toEqual([
|
||||
'INVALID COMMIT MSG: "revert(compiler): reduce generated code payload size by 65%"\n' +
|
||||
' => ERROR: revert is not an allowed type.\n' +
|
||||
' => TYPES: ' + TYPES
|
||||
]);
|
||||
});
|
||||
|
||||
|
||||
it('should validate type', function() {
|
||||
@ -110,7 +118,7 @@ describe('validate-commit-message.js', function() {
|
||||
expect(errors).toEqual(
|
||||
['INVALID COMMIT MSG: "weird($filter): something"\n' +
|
||||
' => ERROR: weird is not an allowed type.\n' +
|
||||
' => TYPES: build, ci, docs, feat, fix, perf, refactor, release, style, test']);
|
||||
' => TYPES: ' + TYPES]);
|
||||
});
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user