Skip to content

Commit

Permalink
fix: support prepared commit message (#122)
Browse files Browse the repository at this point in the history
* fix: support passed commit message

When used as a git hook, using git commit -m'my message' was not making use of 'my message'. Now it does.

* style: Array destructuring and import order

* style: remove whitespace

* test: add tests for prepared commit

Co-authored-by: Leonardo Correa <[email protected]>
  • Loading branch information
politician and leonardoanalista authored Jul 7, 2020
1 parent 017494a commit 152565f
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
24 changes: 24 additions & 0 deletions questions.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const fs = require('fs');
const _ = require('lodash');
const buildCommit = require('./buildCommit');
const log = require('./logger');
Expand All @@ -18,6 +19,27 @@ const isValidateTicketNo = (value, config) => {
return true;
};

const getPreparedCommit = context => {
let message = null;
if (fs.existsSync('./.git/COMMIT_EDITMSG')) {
let preparedCommit = fs.readFileSync('./.git/COMMIT_EDITMSG', 'utf-8');
preparedCommit = preparedCommit
.replace(/^#.*/gm, '')
.replace(/^\s*[\r\n]/gm, '')
.replace(/[\r\n]$/, '')
.split(/\r\n|\r|\n/);

if (preparedCommit.length && preparedCommit[0]) {
if (context === 'subject') [message] = preparedCommit;
else if (context === 'body' && preparedCommit.length > 1) {
preparedCommit.shift();
message = preparedCommit.join('|');
}
}
}
return message;
};

module.exports = {
getQuestions(config, cz) {
// normalize config optional options
Expand Down Expand Up @@ -110,6 +132,7 @@ module.exports = {
type: 'input',
name: 'subject',
message: messages.subject,
default: getPreparedCommit('subject'),
validate(value) {
const limit = config.subjectLimit || 100;
if (value.length > limit) {
Expand All @@ -127,6 +150,7 @@ module.exports = {
type: 'input',
name: 'body',
message: messages.body,
default: getPreparedCommit('body'),
},
{
type: 'input',
Expand Down
42 changes: 42 additions & 0 deletions spec/questionsSpec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable nada/path-case */
const fs = require('fs');
const questions = require('../questions.js');

describe('cz-customizable', () => {
Expand Down Expand Up @@ -62,6 +64,7 @@ describe('cz-customizable', () => {
// question 5 - SUBJECT
expect(getQuestion(5).name).toEqual('subject');
expect(getQuestion(5).type).toEqual('input');
expect(getQuestion(5).default).toEqual(null);
expect(getQuestion(5).message).toMatch(/IMPERATIVE tense description/);
expect(getQuestion(5).filter('Subject')).toEqual('subject');
expect(getQuestion(5).validate('bad subject that exceed limit for 6 characters')).toEqual('Exceed limit: 40');
Expand All @@ -70,6 +73,7 @@ describe('cz-customizable', () => {
// question 6 - BODY
expect(getQuestion(6).name).toEqual('body');
expect(getQuestion(6).type).toEqual('input');
expect(getQuestion(6).default).toEqual(null);

// question 7 - BREAKING CHANGE
expect(getQuestion(7).name).toEqual('breaking');
Expand Down Expand Up @@ -260,4 +264,42 @@ describe('cz-customizable', () => {
});
});
});

describe('commit already prepared', () => {
let existsSync;
let readFileSync;

beforeEach(() => {
config = {};
existsSync = spyOn(fs, 'existsSync');
readFileSync = spyOn(fs, 'readFileSync');
});

it('should ignore if there is no prepared commit file', () => {
existsSync.andReturn(false);
expect(getQuestion(5).default).toEqual(null);
expect(getQuestion(6).default).toEqual(null);
});

it('should ignore an empty prepared commit', () => {
existsSync.andReturn(true);
readFileSync.andReturn('');
expect(getQuestion(5).default).toEqual(null);
expect(getQuestion(6).default).toEqual(null);
});

it('should take a single line commit as the subject', () => {
existsSync.andReturn(true);
readFileSync.andReturn('my commit');
expect(getQuestion(5).default).toEqual('my commit');
expect(getQuestion(6).default).toEqual(null);
});

it('should split multi line commit between the subject and the body', () => {
existsSync.andReturn(true);
readFileSync.andReturn('my commit\nmessage\n\nis on several lines');
expect(getQuestion(5).default).toEqual('my commit');
expect(getQuestion(6).default).toEqual(`message|is on several lines`);
});
});
});

0 comments on commit 152565f

Please sign in to comment.