diff --git a/.all-contributorsrc b/.all-contributorsrc index 1ebd8f7..9c9c22c 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -267,6 +267,15 @@ "contributions": [ "doc" ] + }, + { + "login": "maxcnunes", + "name": "Max Claus Nunes", + "avatar_url": "https://avatars1.githubusercontent.com/u/680356?v=3", + "profile": "http://blog.maxcnunes.net/", + "contributions": [ + "code" + ] } ] } diff --git a/README.md b/README.md index f972908..d26ba4f 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ [![downloads][downloads-badge]][npm-stat] [![MIT License][license-badge]][LICENSE] -[![All Contributors](https://img.shields.io/badge/all_contributors-26-orange.svg?style=flat-square)](#contributors) +[![All Contributors](https://img.shields.io/badge/all_contributors-27-orange.svg?style=flat-square)](#contributors) [![PRs Welcome][prs-badge]][prs] [![Donate][donate-badge]][donate] [![Code of Conduct][coc-badge]][coc] @@ -192,7 +192,7 @@ Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds | :---: | :---: | :---: | :---: | :---: | :---: | :---: | | [
Shawn Erquhart](http://www.professant.com)
[πŸ’»](https://github.com/kentcdodds/validate-commit-msg/commits?author=erquhart) [πŸ“–](https://github.com/kentcdodds/validate-commit-msg/commits?author=erquhart) [⚠️](https://github.com/kentcdodds/validate-commit-msg/commits?author=erquhart) | [
Tushar Mathur](http://tusharm.com)
[πŸ’»](https://github.com/kentcdodds/validate-commit-msg/commits?author=tusharmath) [⚠️](https://github.com/kentcdodds/validate-commit-msg/commits?author=tusharmath) | [
Jason Dreyzehner](https://twitter.com/bitjson)
[πŸ’»](https://github.com/kentcdodds/validate-commit-msg/commits?author=bitjson) [πŸ“–](https://github.com/kentcdodds/validate-commit-msg/commits?author=bitjson) [⚠️](https://github.com/kentcdodds/validate-commit-msg/commits?author=bitjson) | [
Abimbola Idowu](http://twitter.com/hisabimbola)
[πŸ’»](https://github.com/kentcdodds/validate-commit-msg/commits?author=hisabimbola) | [
Gleb Bahmutov](https://glebbahmutov.com/)
[πŸ’»](https://github.com/kentcdodds/validate-commit-msg/commits?author=bahmutov) [⚠️](https://github.com/kentcdodds/validate-commit-msg/commits?author=bahmutov) | [
Dennis](http://dennis.io)
[πŸ’»](https://github.com/kentcdodds/validate-commit-msg/commits?author=ds82) | [
Matt Lewis](https://mattlewis.me/)
[πŸ’»](https://github.com/kentcdodds/validate-commit-msg/commits?author=mattlewis92) | | [
Tom Vincent](https://tlvince.com)
[πŸ’»](https://github.com/kentcdodds/validate-commit-msg/commits?author=tlvince) | [
Anders D. Johnson](https://andrz.me/)
[πŸ’»](https://github.com/kentcdodds/validate-commit-msg/commits?author=adjohnson916) [πŸ“–](https://github.com/kentcdodds/validate-commit-msg/commits?author=adjohnson916) [⚠️](https://github.com/kentcdodds/validate-commit-msg/commits?author=adjohnson916) | [
James Zetlen](http://jameszetlen.com)
[πŸ’»](https://github.com/kentcdodds/validate-commit-msg/commits?author=zetlen) [⚠️](https://github.com/kentcdodds/validate-commit-msg/commits?author=zetlen) | [
Paul Bienkowski](http://opatut.de)
[πŸ’»](https://github.com/kentcdodds/validate-commit-msg/commits?author=opatut) [⚠️](https://github.com/kentcdodds/validate-commit-msg/commits?author=opatut) | [
Barney Scott](https://github.com/bmds)
[πŸ’»](https://github.com/kentcdodds/validate-commit-msg/commits?author=bmds) [⚠️](https://github.com/kentcdodds/validate-commit-msg/commits?author=bmds) | [
Emmanuel Murillo SΓ‘nchez](https://github.com/Emmurillo)
[πŸ’»](https://github.com/kentcdodds/validate-commit-msg/commits?author=Emmurillo) [⚠️](https://github.com/kentcdodds/validate-commit-msg/commits?author=Emmurillo) | [
Hans Kristian Flaatten](https://starefossen.github.io)
[πŸ’»](https://github.com/kentcdodds/validate-commit-msg/commits?author=Starefossen) [⚠️](https://github.com/kentcdodds/validate-commit-msg/commits?author=Starefossen) | -| [
Bo Lingen](https://github.com/citycide)
[πŸ“–](https://github.com/kentcdodds/validate-commit-msg/commits?author=citycide) | [
Spyros Ioakeimidis](http://www.spyros.io)
[πŸ’»](https://github.com/kentcdodds/validate-commit-msg/commits?author=spirosikmd) [πŸ“–](https://github.com/kentcdodds/validate-commit-msg/commits?author=spirosikmd) [⚠️](https://github.com/kentcdodds/validate-commit-msg/commits?author=spirosikmd) | [
Matt Travi](https://matt.travi.org)
[πŸ›](https://github.com/kentcdodds/validate-commit-msg/issues?q=author%3Atravi) | [
Jonathan Garbee](http://jonathan.garbee.me)
[πŸ’»](https://github.com/kentcdodds/validate-commit-msg/commits?author=Garbee) [πŸ“–](https://github.com/kentcdodds/validate-commit-msg/commits?author=Garbee) [⚠️](https://github.com/kentcdodds/validate-commit-msg/commits?author=Garbee) | [
Tobias Lins](https://lins.in)
[πŸ“–](https://github.com/kentcdodds/validate-commit-msg/commits?author=tobiaslins) | +| [
Bo Lingen](https://github.com/citycide)
[πŸ“–](https://github.com/kentcdodds/validate-commit-msg/commits?author=citycide) | [
Spyros Ioakeimidis](http://www.spyros.io)
[πŸ’»](https://github.com/kentcdodds/validate-commit-msg/commits?author=spirosikmd) [πŸ“–](https://github.com/kentcdodds/validate-commit-msg/commits?author=spirosikmd) [⚠️](https://github.com/kentcdodds/validate-commit-msg/commits?author=spirosikmd) | [
Matt Travi](https://matt.travi.org)
[πŸ›](https://github.com/kentcdodds/validate-commit-msg/issues?q=author%3Atravi) | [
Jonathan Garbee](http://jonathan.garbee.me)
[πŸ’»](https://github.com/kentcdodds/validate-commit-msg/commits?author=Garbee) [πŸ“–](https://github.com/kentcdodds/validate-commit-msg/commits?author=Garbee) [⚠️](https://github.com/kentcdodds/validate-commit-msg/commits?author=Garbee) | [
Tobias Lins](https://lins.in)
[πŸ“–](https://github.com/kentcdodds/validate-commit-msg/commits?author=tobiaslins) | [
Max Claus Nunes](http://blog.maxcnunes.net/)
[πŸ’»](https://github.com/kentcdodds/validate-commit-msg/commits?author=maxcnunes) | This project follows the [all-contributors](https://github.com/kentcdodds/all-contributors) specification. Contributions of any kind welcome! diff --git a/lib/cli.js b/lib/cli.js index 434afa8..63f5c05 100755 --- a/lib/cli.js +++ b/lib/cli.js @@ -18,27 +18,44 @@ var getGitFolder = require('./getGitFolder'); // hacky start if not run by mocha :-D // istanbul ignore next if (process.argv.join('').indexOf('mocha') === -1) { + var commitMsgFileOrText = process.argv[2] || getGitFolder() + '/COMMIT_EDITMSG'; - var commitMsgFile = process.argv[2] || getGitFolder() + '/COMMIT_EDITMSG'; - var incorrectLogFile = commitMsgFile.replace('COMMIT_EDITMSG', 'logs/incorrect-commit-msgs'); + var bufferToString = function (buffer) { + return hasToString(buffer) && buffer.toString(); + }; - var hasToString = function hasToString(x) { + var hasToString = function (x) { return x && typeof x.toString === 'function'; }; - fs.readFile(commitMsgFile, function(err, buffer) { - var msg = getCommitMessage(buffer); - + var validate = function (msg, isFile) { if (!validateMessage(msg)) { - fs.appendFile(incorrectLogFile, msg + '\n', function() { + var incorrectLogFile = ( + isFile + ? commitMsgFileOrText.replace('COMMIT_EDITMSG', 'logs/incorrect-commit-msgs') + : (getGitFolder() + '/logs/incorrect-commit-msgs') + ); + + fs.appendFile(incorrectLogFile, msg + '\n', function appendFile() { process.exit(1); }); } else { process.exit(0); } + }; - function getCommitMessage(buffer) { - return hasToString(buffer) && buffer.toString(); + fs.readFile(commitMsgFileOrText, function readFile(err, buffer) { + if(err && err.code !== 'ENOENT') { + throw err; } + + var isFile = !err; + var msg = ( + isFile + ? bufferToString(buffer) + : commitMsgFileOrText + ); + + validate(msg, isFile); }); }