Skip to content

Commit b7a6b7a

Browse files
committed
fix(cli): remove hard coded comment char with linting COMMIT_EDIT_MSG
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: conventional-changelog#2351
1 parent 73b2ed1 commit b7a6b7a

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = {
2+
rules: {
3+
'subject-empty': [2, 'never']
4+
},
5+
parserPreset: {
6+
parserOpts: {
7+
commentChar: '$'
8+
}
9+
},
10+
};

@commitlint/cli/src/cli.test.ts

+10
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,16 @@ test('should handle --amend with signoff', async () => {
329329
expect(commit).toBeTruthy();
330330
}, 10000);
331331

332+
test('should fail with an empty message and a commentChar is set', async () => {
333+
const cwd = await gitBootstrap('fixtures/comment-char');
334+
await execa('git', ['config', '--local', 'core.commentChar', '$'], {cwd});
335+
await fs.writeFile(path.join(cwd, '.git', 'COMMIT_EDITMSG'), '#1234');
336+
337+
const actual = await cli(['--edit', '.git/COMMIT_EDITMSG'], {cwd})();
338+
expect(actual.stdout).toContain('[subject-empty]');
339+
expect(actual.exitCode).toBe(1);
340+
});
341+
332342
test('should handle linting with issue prefixes', async () => {
333343
const cwd = await gitBootstrap('fixtures/issue-prefixes');
334344
const actual = await cli([], {cwd})('foobar REF-1');

@commitlint/cli/src/cli.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,10 @@ async function main(args: MainArgs) {
220220
}
221221
const format = loadFormatter(loaded, flags);
222222

223-
// Strip comments if reading from `.git/COMMIT_EDIT_MSG`
224-
if (flags.edit) {
223+
// Strip comments if reading from `.git/COMMIT_EDIT_MSG` using the
224+
// commentChar from the parser preset falling back to a `#` if that is not
225+
// set
226+
if (flags.edit && typeof opts.parserOpts.commentChar !== 'string') {
225227
opts.parserOpts.commentChar = '#';
226228
}
227229

0 commit comments

Comments
 (0)