Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli): remove hard coded comment char with linting COMMIT_EDIT_MSG #2618

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions @commitlint/cli/fixtures/comment-char/commitlint.config.js
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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