Skip to content

Commit

Permalink
fix(cli): remove hard coded comment char with linting `COMMIT_EDIT_MS…
Browse files Browse the repository at this point in the history
…G` (#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
  • Loading branch information
AdeAttwood committed May 27, 2021
1 parent bbf246b commit 5badf6d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
10 changes: 10 additions & 0 deletions @commitlint/cli/fixtures/comment-char/commitlint.config.js
@@ -0,0 +1,10 @@
module.exports = {
rules: {
'subject-empty': [2, 'never']
},
parserPreset: {
parserOpts: {
commentChar: '$'
}
},
};
10 changes: 10 additions & 0 deletions @commitlint/cli/src/cli.test.ts
Expand Up @@ -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');
Expand Down
6 changes: 4 additions & 2 deletions @commitlint/cli/src/cli.ts
Expand Up @@ -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 = '#';
}

Expand Down

0 comments on commit 5badf6d

Please sign in to comment.