|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const fs = require('fs'); |
| 4 | +const path = require('path'); |
| 5 | + |
| 6 | +const { TYPES, Err } = require('./commit'); |
| 7 | + |
| 8 | +/** |
| 9 | + * @param string header |
| 10 | + * @returns object - object with the following keys: |
| 11 | + * - isBreaking: true if header implies a breaking change with an exclamation point |
| 12 | + * - scope: matched value for the optional <scope> portion of the header |
| 13 | + * - summary: matched value for the <summary> portion of the header |
| 14 | + * - type: matched value for the <type> portion of the header |
| 15 | + */ |
| 16 | +function parseHeader (header) { |
| 17 | + // See https://regex101.com/r/FRRac2/10 |
| 18 | + let pattern = /^(?<type>\w+)(\((?<scope>.*)\))?(?<isBreaking>!)?: (?<summary>.+)$/; |
| 19 | + |
| 20 | + // check header for matches against the regex pattern |
| 21 | + let matches = pattern.exec(header); |
| 22 | + |
| 23 | + // default capture group values if there are no matches |
| 24 | + let groups = { |
| 25 | + isBreaking: null, |
| 26 | + scope: null, |
| 27 | + summary: null, |
| 28 | + type: null, |
| 29 | + }; |
| 30 | + |
| 31 | + // replace default capture group values with the real values |
| 32 | + if (matches && matches.groups) { |
| 33 | + groups = matches.groups; |
| 34 | + } |
| 35 | + |
| 36 | + return { |
| 37 | + ...groups, |
| 38 | + isBreaking: !!groups.isBreaking, |
| 39 | + matches, |
| 40 | + }; |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * Validate format of the message header |
| 45 | + * |
| 46 | + * @param string header |
| 47 | + * @returns boolean |
| 48 | + */ |
| 49 | +function isValidHeader (header) { |
| 50 | + let { matches, type, scope } = parsed = parseHeader(header); |
| 51 | + |
| 52 | + if (!matches) { |
| 53 | + Err.incorrectFormat(header); |
| 54 | + return false; |
| 55 | + } |
| 56 | + |
| 57 | + if (!TYPES.hasOwnProperty(type)) { |
| 58 | + Err.unknownType(header, type); |
| 59 | + return false; |
| 60 | + } |
| 61 | + |
| 62 | + if (typeof scope === 'string' && scope.trim() === '') { |
| 63 | + Err.emptyScope(header); |
| 64 | + return false; |
| 65 | + } |
| 66 | + |
| 67 | + return true; |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * Split raw commit message into an array of message lines (excluding comments) |
| 72 | + * |
| 73 | + * @param string message - commit message contents |
| 74 | + * @returns array<string> |
| 75 | + */ |
| 76 | +function getLinesWithoutComments (message) { |
| 77 | + let allLines = message.split('\n'); |
| 78 | + |
| 79 | + // filter out comments |
| 80 | + let nonCommentLines = allLines.filter( line => !line.match(/^#/) ); |
| 81 | + |
| 82 | + return nonCommentLines; |
| 83 | +} |
| 84 | + |
| 85 | +/** |
| 86 | + * git uses the first non-empty, non-comment line as the header |
| 87 | + * for the commit message, so we need to do the same |
| 88 | + * |
| 89 | + * @param string message - full git commit message (may be multi-line) |
| 90 | + * @returns string - first non-empty, non-comment line of text |
| 91 | + */ |
| 92 | +function getFirstMessageLine (message) { |
| 93 | + let nonCommentLines = getLinesWithoutComments(message); |
| 94 | + |
| 95 | + // exclude empty/blank lines |
| 96 | + let linesWithContent = nonCommentLines.filter( line => line.trim().length ); |
| 97 | + |
| 98 | + // if no usable content, print error and exit 1 to fail hook |
| 99 | + if (linesWithContent.length === 0) { |
| 100 | + Err.unusableContent(message); |
| 101 | + process.exit(1); |
| 102 | + } |
| 103 | + |
| 104 | + // return first non-empty, non-comment line of text |
| 105 | + return linesWithContent[0]; |
| 106 | +} |
| 107 | + |
| 108 | +(function () { |
| 109 | + console.log('[commit-msg] checking git commit message format'); |
| 110 | + |
| 111 | + let msgBuffer = fs.readFileSync(path.resolve(__dirname, '..', process.argv[2])); |
| 112 | + let msgString = msgBuffer.toString(); |
| 113 | + let strHeader = getFirstMessageLine(msgString); |
| 114 | + let isValid = isValidHeader(strHeader); |
| 115 | + |
| 116 | + if (!isValid) { |
| 117 | + // exit with non-zero status code to fail hook |
| 118 | + process.exit(1); |
| 119 | + } else { |
| 120 | + // exit with zero status code to pass hook |
| 121 | + process.exit(0); |
| 122 | + } |
| 123 | +})() |
0 commit comments