Skip to content

Commit

Permalink
feat: print commit message when the message is invalid
Browse files Browse the repository at this point in the history
When using the CLI, if the message is invalid we lose all its content (using a custom message in VIM mode), which might be a little frustrating when we a have a long commit body.

This feature prints the full message when there are linting errors.

Closes #222
  • Loading branch information
Duarte Amorim authored and marionebl committed Mar 19, 2018
1 parent 8b00d72 commit 86c34f1
Show file tree
Hide file tree
Showing 5 changed files with 408 additions and 32 deletions.
8 changes: 5 additions & 3 deletions @commitlint/cli/src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,13 @@ async function main(options) {
messages.map(async message => {
const report = await lint(message, loaded.rules, opts);
const formatted = format(report, {color: flags.color});
const input =
report.errors.length > 0
? `\n${report.input}\n`
: message.split('\n')[0];

if (!flags.quiet) {
console.log(
`${fmt.grey('⧗')} input: ${fmt.bold(message.split('\n')[0])}`
);
console.log(`${fmt.grey('⧗')} input: ${fmt.bold(input)}`);
console.log(formatted.join('\n'));
}

Expand Down
19 changes: 19 additions & 0 deletions @commitlint/cli/src/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,25 @@ test('should handle linting with issue prefixes', async t => {
t.is(actual.code, 0);
});

test('should print full commit message when input from stdin fails', async t => {
const cwd = await git.bootstrap('fixtures/simple');
const input = 'foo: bar\n\nFoo bar bizz buzz.\n\nCloses #123.';
const actual = await cli([], {cwd})(input);

t.true(actual.stdout.includes(input));
t.is(actual.code, 1);
});

test('should not print full commit message when input succeeds', async t => {
const cwd = await git.bootstrap('fixtures/empty');
const message = 'foo: bar\n\nFoo bar bizz buzz.\n\nCloses #123.';
const actual = await cli([], {cwd})(message);

t.false(actual.stdout.includes(message));
t.true(actual.stdout.includes(message.split('\n')[0]));
t.is(actual.code, 0);
});

async function writePkg(payload, options) {
const pkgPath = path.join(options.cwd, 'package.json');
const pkg = JSON.parse(await sander.readFile(pkgPath));
Expand Down
15 changes: 13 additions & 2 deletions @commitlint/lint/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,23 @@ import parse from '@commitlint/parse';
import implementations from '@commitlint/rules';
import entries from 'lodash.topairs';

const buildCommitMesage = ({header, body, footer}) => {
let message = header;

message = body ? `${message}\n\n${body}` : message;
message = footer ? `${message}\n\n${footer}` : message;

return message;
};

export default async (message, rules = {}, opts = {}) => {
// Found a wildcard match, skip
if (isIgnored(message)) {
return {
valid: true,
errors: [],
warnings: []
warnings: [],
input: message
};
}

Expand Down Expand Up @@ -133,6 +143,7 @@ export default async (message, rules = {}, opts = {}) => {
return {
valid,
errors,
warnings
warnings,
input: buildCommitMesage(parsed)
};
};
40 changes: 40 additions & 0 deletions @commitlint/lint/src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ test('positive on ignored message and broken rule', async t => {
'type-empty': [2, 'never']
});
t.true(actual.valid);
t.is(actual.input, 'Revert "some bogus commit"');
});

test('positive on stub message and opts', async t => {
Expand Down Expand Up @@ -182,3 +183,42 @@ test('fails for custom issue prefix', async t => {

t.false(report.valid);
});

test('returns original message only with commit header', async t => {
const message = 'foo: bar';
const report = await lint(message);

t.is(report.input, message);
});

test('returns original message with commit header and body', async t => {
const message = 'foo: bar/n/nFoo bar bizz buzz.';
const report = await lint(message);

t.is(report.input, message);
});

test('returns original message with commit header, body and footer', async t => {
const message = 'foo: bar/n/nFoo bar bizz buzz./n/nCloses #1';
const report = await lint(message);

t.is(report.input, message);
});

test('returns original message with commit header, body and footer, parsing comments', async t => {
const expected = 'foo: bar/n/nFoo bar bizz buzz./n/nCloses #1';
const message = `${expected}\n\n# Some comment to ignore`;
const report = await lint(
message,
{
'references-empty': [2, 'never']
},
{
parserOpts: {
commentChar: '#'
}
}
);

t.is(report.input, expected);
});
Loading

0 comments on commit 86c34f1

Please sign in to comment.