From 5badf6dc08116ed3557e6c780e55764b4f07ca67 Mon Sep 17 00:00:00 2001 From: Ade Attwood Date: Thu, 27 May 2021 06:17:16 +0100 Subject: [PATCH] fix(cli): remove hard coded comment char with linting `COMMIT_EDIT_MSG` (#2618) When running the cli and passing the `--edit` flag the comment char was hard coded to a `#`. Even if there is a `commentChar` set in the `parserOpts` the cli will override it with a `#`. Now the cli will only set it to a `#` if its not already set in the `parserOpts` Fixes Issue: #2351 --- .../cli/fixtures/comment-char/commitlint.config.js | 10 ++++++++++ @commitlint/cli/src/cli.test.ts | 10 ++++++++++ @commitlint/cli/src/cli.ts | 6 ++++-- 3 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 @commitlint/cli/fixtures/comment-char/commitlint.config.js diff --git a/@commitlint/cli/fixtures/comment-char/commitlint.config.js b/@commitlint/cli/fixtures/comment-char/commitlint.config.js new file mode 100644 index 0000000000..2292580640 --- /dev/null +++ b/@commitlint/cli/fixtures/comment-char/commitlint.config.js @@ -0,0 +1,10 @@ +module.exports = { + rules: { + 'subject-empty': [2, 'never'] + }, + parserPreset: { + parserOpts: { + commentChar: '$' + } + }, +}; diff --git a/@commitlint/cli/src/cli.test.ts b/@commitlint/cli/src/cli.test.ts index ea67ee2db1..324c55e409 100644 --- a/@commitlint/cli/src/cli.test.ts +++ b/@commitlint/cli/src/cli.test.ts @@ -329,6 +329,16 @@ test('should handle --amend with signoff', async () => { expect(commit).toBeTruthy(); }, 10000); +test('should fail with an empty message and a commentChar is set', async () => { + const cwd = await gitBootstrap('fixtures/comment-char'); + await execa('git', ['config', '--local', 'core.commentChar', '$'], {cwd}); + await fs.writeFile(path.join(cwd, '.git', 'COMMIT_EDITMSG'), '#1234'); + + const actual = await cli(['--edit', '.git/COMMIT_EDITMSG'], {cwd})(); + expect(actual.stdout).toContain('[subject-empty]'); + expect(actual.exitCode).toBe(1); +}); + test('should handle linting with issue prefixes', async () => { const cwd = await gitBootstrap('fixtures/issue-prefixes'); const actual = await cli([], {cwd})('foobar REF-1'); diff --git a/@commitlint/cli/src/cli.ts b/@commitlint/cli/src/cli.ts index bae03d6500..974974be8d 100644 --- a/@commitlint/cli/src/cli.ts +++ b/@commitlint/cli/src/cli.ts @@ -220,8 +220,10 @@ async function main(args: MainArgs) { } const format = loadFormatter(loaded, flags); - // Strip comments if reading from `.git/COMMIT_EDIT_MSG` - if (flags.edit) { + // Strip comments if reading from `.git/COMMIT_EDIT_MSG` using the + // commentChar from the parser preset falling back to a `#` if that is not + // set + if (flags.edit && typeof opts.parserOpts.commentChar !== 'string') { opts.parserOpts.commentChar = '#'; }