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

Use core.commentChar from git config with --edit flag #3191

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion @commitlint/cli/fixtures/comment-char/commitlint.config.js
@@ -1,6 +1,6 @@
module.exports = {
rules: {
'subject-empty': [2, 'never']
'body-empty': [2, 'never']
},
parserPreset: {
parserOpts: {
Expand Down
2 changes: 1 addition & 1 deletion @commitlint/cli/package.json
Expand Up @@ -41,7 +41,6 @@
"@commitlint/utils": "^17.0.0",
"@types/node": "12.20.52",
"@types/yargs": "^17.0.0",
"execa": "^5.0.0",
"fs-extra": "^10.0.0"
},
"dependencies": {
Expand All @@ -50,6 +49,7 @@
"@commitlint/load": "^17.0.0",
"@commitlint/read": "^17.0.0",
"@commitlint/types": "^17.0.0",
"execa": "^5.0.0",
"lodash": "^4.17.19",
"resolve-from": "5.0.0",
"resolve-global": "1.0.0",
Expand Down
46 changes: 42 additions & 4 deletions @commitlint/cli/src/cli.test.ts
Expand Up @@ -329,13 +329,51 @@ test('should handle --amend with signoff', async () => {
expect(commit).toBeTruthy();
}, 10000);

test('should fail with an empty message and a commentChar is set', async () => {
test('it uses parserOpts.commentChar when not using edit mode', 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 input = 'header: foo\n$body\n';

const actual = await cli([], {cwd})(input);
expect(actual.stdout).toContain('[body-empty]');
expect(actual.exitCode).toBe(1);
});

test("it doesn't use parserOpts.commentChar when using edit mode", async () => {
const cwd = await gitBootstrap('fixtures/comment-char');
await fs.writeFile(
path.join(cwd, '.git', 'COMMIT_EDITMSG'),
'header: foo\n\n$body\n'
);

const actual = await cli(['--edit', '.git/COMMIT_EDITMSG'], {cwd})();
expect(actual.stdout).not.toContain('[body-empty]');
expect(actual.exitCode).toBe(0);
});

test('it uses core.commentChar git config when using edit mode', async () => {
const cwd = await gitBootstrap('fixtures/comment-char');
await execa('git', ['config', 'core.commentChar', '$'], {cwd});
await fs.writeFile(
path.join(cwd, '.git', 'COMMIT_EDITMSG'),
'header: foo\n\n$body\n'
);

const actual = await cli(['--edit', '.git/COMMIT_EDITMSG'], {cwd})();
expect(actual.stdout).toContain('[body-empty]');
expect(actual.exitCode).toBe(1);
});

test('it falls back to # for core.commentChar when using edit mode', async () => {
const cwd = await gitBootstrap('fixtures/comment-char');
await execa('git', ['config', '--unset', 'core.commentChar'], {cwd});
await fs.writeFile(
path.join(cwd, '.git', 'COMMIT_EDITMSG'),
'header: foo\n\n#body\n'
);

const actual = await cli(['--edit', '.git/COMMIT_EDITMSG'], {cwd})();
expect(actual.stdout).toContain('[subject-empty]');
expect(actual.stdout).toContain('[body-empty]');
expect(actual.stderr).toEqual('');
expect(actual.exitCode).toBe(1);
});

Expand Down
24 changes: 19 additions & 5 deletions @commitlint/cli/src/cli.ts
@@ -1,3 +1,4 @@
import execa, {ExecaError} from 'execa';
import load from '@commitlint/load';
import lint from '@commitlint/lint';
import read from '@commitlint/read';
Expand All @@ -21,6 +22,8 @@ import {CliError} from './cli-error';

const pkg = require('../package');

const gitDefaultCommentChar = '#';

const cli = yargs
.options({
color: {
Expand Down Expand Up @@ -221,11 +224,22 @@ async function main(args: MainArgs) {
}
const format = loadFormatter(loaded, flags);

// 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 = '#';
// If reading from `.git/COMMIT_EDIT_MSG`, strip comments using
// core.commentChar from git configuration, falling back to '#'.
if (flags.edit) {
try {
const {stdout} = await execa('git', ['config', 'core.commentChar']);
opts.parserOpts.commentChar = stdout.trim() || gitDefaultCommentChar;
} catch (e) {
const execaError = e as ExecaError;
if (!execaError.failed || execaError.exitCode !== 1) {
console.warn(
'Could not determine core.commentChar git configuration',
e
);
}
opts.parserOpts.commentChar = gitDefaultCommentChar;
}
}

const results = await Promise.all(
Expand Down
1 change: 1 addition & 0 deletions @packages/test/src/git.ts
Expand Up @@ -35,6 +35,7 @@ async function setup(cwd: string, gitCommand = 'git') {
cwd,
});
await execa(gitCommand, ['config', 'commit.gpgsign', 'false'], {cwd});
await execa(gitCommand, ['config', 'core.commentChar', '#'], {cwd});
jscheid marked this conversation as resolved.
Show resolved Hide resolved
} catch (err: any) {
if (typeof err === 'object' && typeof err.message === 'object') {
console.warn(`git config in ${cwd} failed`, err.message);
Expand Down