Skip to content

Commit 646c1b0

Browse files
committed
fix: add support for branch-only commit message types.
1 parent 274c1a4 commit 646c1b0

File tree

3 files changed

+87
-50
lines changed

3 files changed

+87
-50
lines changed

scripts/test-commit-messages.js

+61-30
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1+
'use strict';
2+
13
require('../lib/bootstrap-local');
24

5+
const fs = require('fs');
6+
const path = require('path');
37
const validateCommitMessage = require('./validate-commit-message');
4-
const exec = require('child_process').exec;
8+
const execSync = require('child_process').execSync;
59
const chalk = require('chalk');
610
const Logger = require('@ngtools/logger').Logger;
11+
const configPath = path.resolve(__dirname, './validate-commit-message/commit-message.json');
12+
const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
713
require('rxjs/add/operator/filter');
814

915
// Configure logger
@@ -23,39 +29,64 @@ logger.subscribe((entry) => {
2329
});
2430

2531
logger
26-
.filter((entry) => entry.level == 'fatal')
32+
.filter((entry) => entry.level === 'fatal')
2733
.subscribe(() => {
2834
process.stderr.write('A fatal error happened. See details above.');
2935
process.exit(1);
3036
});
3137

3238
// Note: This is based on the gulp task found in the angular/angular repository
39+
execSync('git fetch origin');
3340

34-
exec(
35-
'git fetch origin master:master && git log --reverse --format=%s master.. --no-merges',
36-
(error, stdout, stderr) => {
37-
if (error) {
38-
logger.fatal(stderr);
39-
return;
40-
}
41-
42-
const output = stdout.trim();
43-
if (output.length == 0) {
44-
logger.warn('There are zero new commits between this HEAD and master');
45-
return;
46-
}
47-
48-
const commitsByLine = output.split(/\n/);
49-
50-
logger.info(`Examining ${commitsByLine.length} commit(s) between HEAD and master`);
51-
52-
const someCommitsInvalid = !commitsByLine.every(validateCommitMessage);
53-
54-
if (someCommitsInvalid) {
55-
logger.error('Please fix the failing commit messages before continuing...');
56-
logger.fatal(
57-
'Commit message guidelines: https://github.com/angular/angular-cli/blob/master/CONTRIBUTING.md#commit');
58-
} else {
59-
logger.info('All commit messages are valid.');
60-
}
61-
});
41+
// Find the branch
42+
const branchRefs = {};
43+
for (const name of config['branches']) {
44+
const output = execSync(`git show-ref --hash ${name}`, { encoding: 'utf-8' });
45+
if (output) {
46+
branchRefs[name] = output;
47+
}
48+
}
49+
logger.info(`Found refs for branches:\n ${Object.entries(branchRefs).forEach(([key, value]) => {
50+
return `${key} => ${value}`;
51+
}).join('\n ')}`);
52+
53+
54+
const output = execSync('git log --format="%H %s" --no-merges', { encoding: 'utf-8' });
55+
56+
if (output.length === 0) {
57+
logger.warn('There are zero new commits between this HEAD and master');
58+
return;
59+
}
60+
61+
const commitByLines = [];
62+
let branch = null;
63+
64+
// Finding the closest branch marker.
65+
for (const line of output.split(/n/)) {
66+
const [hash, ...messageArray] = line.split(/ /);
67+
const message = messageArray.join(' ');
68+
69+
const maybeBranch = Object.keys(branchRefs).find(branchName => branchRefs[branchName] == hash);
70+
if (maybeBranch) {
71+
branch = maybeBranch;
72+
break;
73+
}
74+
commitByLines.push(message);
75+
}
76+
77+
if (!branch) {
78+
logger.fatal('Something wrong happened.');
79+
return;
80+
}
81+
82+
logger.info(`Examining ${commitsByLine.length} commit(s) between HEAD and ${branch}`);
83+
84+
const someCommitsInvalid = !commitsByLine.every(validateCommitMessage);
85+
86+
if (someCommitsInvalid) {
87+
logger.error('Please fix the failing commit messages before continuing...');
88+
logger.fatal(
89+
'Commit message guidelines: https://github.com/angular/angular-cli/blob/master/CONTRIBUTING.md#commit');
90+
} else {
91+
logger.info('All commit messages are valid.');
92+
}
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
{
22
"maxLength": 100,
3-
"types": [
4-
"build",
5-
"ci",
6-
"docs",
7-
"feat",
8-
"fix",
9-
"perf",
10-
"refactor",
11-
"style",
12-
"test",
13-
"tool"
3+
"types": {
4+
"build": "",
5+
"ci": "",
6+
"docs": "",
7+
"feat": "origin/1.1.x",
8+
"fix": "",
9+
"perf": "",
10+
"refactor": "",
11+
"style": "",
12+
"test": "",
13+
"tool": ""
14+
},
15+
"branches": [
16+
"origin/master",
17+
"origin/1.1.x"
1418
]
1519
}

scripts/validate-commit-message/validate-commit-message.js

+11-9
Original file line numberDiff line numberDiff line change
@@ -24,33 +24,35 @@ const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
2424
const { packages, tools } = require('../../lib/packages');
2525
const PATTERN = /^(revert\: )?(\w+)(?:\(([^)]+)\))?\: (.+)$/;
2626

27-
module.exports = function(commitSubject) {
27+
module.exports = function(commitSubject, branch) {
2828
if (commitSubject.length > config['maxLength']) {
2929
error(`The commit message is longer than ${config['maxLength']} characters`, commitSubject);
3030
return false;
3131
}
3232

3333
const match = PATTERN.exec(commitSubject);
3434
if (!match || match[2] === 'revert') {
35-
error(
36-
'The commit message does not match the format of "<type>(<scope>): <subject> OR revert: type(<scope>): <subject>"',
37-
commitSubject);
35+
error('The commit message does not match the format of "<type>(<scope>): <subject> '
36+
+ 'OR revert: type(<scope>): <subject>"', commitSubject);
3837
return false;
3938
}
4039

4140
const type = match[2];
42-
if (config['types'].indexOf(type) === -1) {
43-
error(
44-
`${type} is not an allowed type.\n => TYPES: ${config['types'].join(', ')}`, commitSubject);
41+
const types = Object.keys(config['types']);
42+
if (!(type in types)) {
43+
error(`${type} is not an allowed type.\n => TYPES: ${types.join(', ')}`, commitSubject);
44+
return false;
45+
}
46+
if (types[type] !== "" && types[type] !== branch) {
47+
error(`${type} is not allowed to be on branch ${branch}.`, commitSubject);
4548
return false;
4649
}
4750

4851
const scope = match[3];
4952
const allScopes = Object.keys(packages).concat(Object.keys(tools));
5053

5154
if (scope && !allScopes.includes(scope)) {
52-
error(
53-
`"${scope}" is not an allowed scope.\n => SCOPES: ${config['scopes'].join(', ')}`,
55+
error(`"${scope}" is not an allowed scope.\n => SCOPES: ${allScopes.join(', ')}`,
5456
commitSubject);
5557
return false;
5658
}

0 commit comments

Comments
 (0)