Skip to content

Commit 7e737bb

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 f43433d commit 7e737bb

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
@@ -205,8 +205,10 @@ async function main(options: CliFlags) {
205205
}
206206
const format = loadFormatter(loaded, flags);
207207

208-
// Strip comments if reading from `.git/COMMIT_EDIT_MSG`
209-
if (flags.edit) {
208+
// Strip comments if reading from `.git/COMMIT_EDIT_MSG` using the
209+
// commentChar from the parser preset falling back to a `#` if that is not
210+
// set
211+
if (flags.edit && typeof opts.parserOpts.commentChar !== 'string') {
210212
opts.parserOpts.commentChar = '#';
211213
}
212214

0 commit comments

Comments
 (0)