|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * Name: validate-commit-message. |
| 5 | + * Description: Validate commit message before git commit |
| 6 | + * Authors: Angular Team (Note: this script was originally written by Vojta for AngularJS) |
| 7 | + * URL: https://github.com/angular/angular/blob/master/tools/validate-commit-message/validate-commit-message.js |
| 8 | + * LICENSE: MIT License |
| 9 | + * |
| 10 | + * Copyright Google Inc. All Rights Reserved. |
| 11 | + * |
| 12 | + * Use of this source code is governed by an MIT-style license that can be |
| 13 | + * found in the LICENSE file at https://angular.io/license |
| 14 | + */ |
| 15 | + |
| 16 | +'use strict'; |
| 17 | + |
| 18 | +let fs = require('fs'); |
| 19 | +let util = require('util'); |
| 20 | + |
| 21 | + |
| 22 | +let MAX_LENGTH = 100; |
| 23 | +let IGNORED_TEXT = '[maven-release-plugin]'; |
| 24 | +let PATTERN = /^(?:fixup!\s*)?(\w*)(\(([\w\$\.\:\*/-]*)\))?\: (.*)$/; |
| 25 | +let IGNORED = /^WIP\:/; |
| 26 | +let TYPES = { |
| 27 | + feat: true, |
| 28 | + fix: true, |
| 29 | + docs: true, |
| 30 | + style: true, |
| 31 | + refactor: true, |
| 32 | + perf: true, |
| 33 | + test: true, |
| 34 | + chore: true, |
| 35 | + cleanup: true, |
| 36 | + revert: true, |
| 37 | + tracking: true |
| 38 | +}; |
| 39 | + |
| 40 | + |
| 41 | +let error = function() { |
| 42 | + // gitx does not display it |
| 43 | + // http://gitx.lighthouseapp.com/projects/17830/tickets/294-feature-display-hook-error-message-when-hook-fails |
| 44 | + // https://groups.google.com/group/gitx/browse_thread/thread/a03bcab60844b812 |
| 45 | + console.error('INVALID COMMIT MSG: ' + util.format.apply(null, arguments)); |
| 46 | +}; |
| 47 | + |
| 48 | + |
| 49 | +let validateMessage = function(message) { |
| 50 | + let isValid = true; |
| 51 | + |
| 52 | + if (IGNORED.test(message) || message.includes(IGNORED_TEXT)) { |
| 53 | + console.log('Commit message validation ignored.'); |
| 54 | + return true; |
| 55 | + } |
| 56 | + |
| 57 | + let match = PATTERN.exec(message), |
| 58 | + isATagCommit = /^\d+.\d+.\d+$/.exec(message), |
| 59 | + isMergeCommit = /^Merge remote-tracking branch.*/.exec(message); |
| 60 | + |
| 61 | + if (!isMergeCommit && message.length > MAX_LENGTH) { |
| 62 | + error('is longer than %d characters by %d character!', MAX_LENGTH, message.length - MAX_LENGTH); |
| 63 | + isValid = false; |
| 64 | + } |
| 65 | + |
| 66 | + let isCommitMessageBypassed = isATagCommit || isMergeCommit; |
| 67 | + // Allow version commit without scope restriction and allow merge commits too |
| 68 | + match = match || isCommitMessageBypassed; |
| 69 | + if (!match) { |
| 70 | + error('does not match "<type>(<scope>): <subject>" ! was: ' + message); |
| 71 | + return false; |
| 72 | + } |
| 73 | + |
| 74 | + let type = match[1]; |
| 75 | + let scope = match[3]; |
| 76 | + let subject = match[4]; |
| 77 | + |
| 78 | + if (!isCommitMessageBypassed && !TYPES.hasOwnProperty(type)) { |
| 79 | + error('"%s" is not allowed type !', type); |
| 80 | + return false; |
| 81 | + } |
| 82 | + |
| 83 | + // Some more ideas, do want anything like this ? |
| 84 | + // - allow only specific scopes (eg. fix(docs) should not be allowed ? |
| 85 | + // - auto correct the type to lower case ? |
| 86 | + // - auto correct first letter of the subject to lower case ? |
| 87 | + // - auto add empty line after subject ? |
| 88 | + // - auto remove empty () ? |
| 89 | + // - auto correct typos in type ? |
| 90 | + // - store incorrect messages, so that we can learn |
| 91 | + |
| 92 | + return isValid; |
| 93 | +}; |
| 94 | + |
| 95 | + |
| 96 | +let firstLineFromBuffer = function(buffer) { |
| 97 | + return buffer.toString().split('\n').shift(); |
| 98 | +}; |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | +// publish for testing |
| 103 | +exports.validateMessage = validateMessage; |
| 104 | + |
| 105 | +// hacky start if not run by jasmine :-D |
| 106 | +if (process.argv.join('').indexOf('jasmine-node') === -1) { |
| 107 | + let commitMsgFile = process.argv[2]; |
| 108 | + let incorrectLogFile = commitMsgFile.replace('COMMIT_EDITMSG', 'logs/incorrect-commit-msgs'); |
| 109 | + |
| 110 | + fs.readFile(commitMsgFile, function(err, buffer) { |
| 111 | + let msg = firstLineFromBuffer(buffer); |
| 112 | + |
| 113 | + if (!validateMessage(msg)) { |
| 114 | + fs.appendFile(incorrectLogFile, msg + '\n', function() { |
| 115 | + process.exit(1); |
| 116 | + }); |
| 117 | + } else { |
| 118 | + process.exit(0); |
| 119 | + } |
| 120 | + }); |
| 121 | +} |
0 commit comments