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 = '#'; }