Skip to content

Instantly share code, notes, and snippets.

@fatso83
Created November 15, 2017 11:04
Show Gist options
  • Save fatso83/4111c713ba83375c82edff7d1829bf67 to your computer and use it in GitHub Desktop.
Save fatso83/4111c713ba83375c82edff7d1829bf67 to your computer and use it in GitHub Desktop.
/* eslint-disable no-console */
const fs = require('fs');
const process = require('process');
const chalk = require('chalk');
const filename = process.env['GIT_PARAMS'].split(' ')[0];
const commit_msg = fs.readFileSync(filename).toString();
const printWarning = msg => console.warn(chalk.red(msg));
const errors = [];
if(!commit_msg.match(/^GH-\d{2,4} /)) {
errors.push('Commit messages should be prefixed with the github issue number, i.e. "GH-637 Update design of ListView"');
}
const firstLine = commit_msg.split('\n')[0];
if(firstLine.length > 60) {
errors.push("Too long subject line in git commit message! Max 60 characters.");
}
if(errors.length){
printWarning('Commit message policy violation(s)!\n');
printWarning(' - ' + errors.join('\n - '));
// https://chris.beams.io/posts/git-commit/#seven-rules
console.log(`\nThe 7 rules of a good commit message
1. Separate subject from body with a blank line
2. Limit the subject line to 50 characters
3. Capitalize the subject line
4. Do not end the subject line with a period
5. Use the imperative mood in the subject line
6. Wrap the body at 72 characters
7. Use the body to explain what and why vs. how
`);
console.log(chalk.green('\nYou can skip the checks using the flag `--no-verify`.'));
process.exit(1);
}
@fatso83
Copy link
Author

fatso83 commented Nov 15, 2017

Use with the NPM package husky and add an npm script called commitmsg with the line node scripts/check-commit-msg.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment