diff --git a/scripts/test-commit-messages.js b/scripts/test-commit-messages.js index edbbc44eb199..a173e6105763 100644 --- a/scripts/test-commit-messages.js +++ b/scripts/test-commit-messages.js @@ -41,13 +41,17 @@ execSync('git fetch origin'); // Find the branch const branchRefs = {}; for (const name of config['branches']) { - const output = execSync(`git show-ref --hash ${name}`, { encoding: 'utf-8' }); - if (output) { - branchRefs[name] = output; + try { + const output = execSync(`git show-ref --hash ${name}`, { encoding: 'utf-8' }); + if (output) { + branchRefs[name] = output.replace(/\n/g, '').trim(); + } + } catch (e) { + // Ignore. } } -logger.info(`Found refs for branches:\n ${Object.entries(branchRefs).forEach(([key, value]) => { - return `${key} => ${value}`; +logger.info(`Found refs for branches:\n ${Object.keys(branchRefs).map(key => { + return `${key} => ${JSON.stringify(branchRefs[key])}`; }).join('\n ')}`); @@ -55,33 +59,33 @@ const output = execSync('git log --format="%H %s" --no-merges', { encoding: 'utf if (output.length === 0) { logger.warn('There are zero new commits between this HEAD and master'); - return; + process.exit(0); } -const commitByLines = []; +const commitsByLine = []; let branch = null; // Finding the closest branch marker. -for (const line of output.split(/n/)) { - const [hash, ...messageArray] = line.split(/ /); +for (const line of output.split(/\n/)) { + const [hash, ...messageArray] = line.split(' '); const message = messageArray.join(' '); - const maybeBranch = Object.keys(branchRefs).find(branchName => branchRefs[branchName] == hash); + const maybeBranch = Object.keys(branchRefs).find(branchName => branchRefs[branchName] === hash); if (maybeBranch) { branch = maybeBranch; break; } - commitByLines.push(message); + commitsByLine.push(message); } if (!branch) { logger.fatal('Something wrong happened.'); - return; + process.exit(1); } logger.info(`Examining ${commitsByLine.length} commit(s) between HEAD and ${branch}`); -const someCommitsInvalid = !commitsByLine.every(validateCommitMessage); +const someCommitsInvalid = !commitsByLine.every(message => validateCommitMessage(message, branch)); if (someCommitsInvalid) { logger.error('Please fix the failing commit messages before continuing...'); diff --git a/scripts/validate-commit-message/validate-commit-message.js b/scripts/validate-commit-message/validate-commit-message.js index fcc8e631966f..0a341431dd84 100644 --- a/scripts/validate-commit-message/validate-commit-message.js +++ b/scripts/validate-commit-message/validate-commit-message.js @@ -39,11 +39,11 @@ module.exports = function(commitSubject, branch) { const type = match[2]; const types = Object.keys(config['types']); - if (!(type in types)) { - error(`${type} is not an allowed type.\n => TYPES: ${types.join(', ')}`, commitSubject); + if (types.indexOf(type) === -1) { + error(`"${type}" is not an allowed type.\n => TYPES: "${types.join('", "')}"`, commitSubject); return false; } - if (types[type] !== "" && types[type] !== branch) { + if (config['types'][type] !== '' && config['types'][type] !== branch) { error(`${type} is not allowed to be on branch ${branch}.`, commitSubject); return false; }