Skip to content

Commit

Permalink
feat(commitlint): add additional git log args (#3334)
Browse files Browse the repository at this point in the history
  • Loading branch information
morriswinkler authored Aug 25, 2022
1 parent d8498b7 commit 229c65b
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 5 deletions.
2 changes: 2 additions & 0 deletions @commitlint/cli/src/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,8 @@ test('should print help', async () => {
-H, --help-url help url in error message [string]
-f, --from lower end of the commit range to lint; applies if
edit=false [string]
--git-log-args addditional git log arguments as space separated string,
example \'--first-parent --cherry-pick\' [string]
-o, --format output format of the results [string]
-p, --parser-preset configuration preset to use for
conventional-commits-parser [string]
Expand Down
6 changes: 6 additions & 0 deletions @commitlint/cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ const cli = yargs
'lower end of the commit range to lint; applies if edit=false',
type: 'string',
},
'git-log-args': {
description:
"addditional git log arguments as space separated string, example '--first-parent --cherry-pick'",
type: 'string',
},
format: {
alias: 'o',
description: 'output format of the results',
Expand Down Expand Up @@ -182,6 +187,7 @@ async function main(args: MainArgs) {
from: flags.from,
edit: flags.edit,
cwd: flags.cwd,
gitLogArgs: flags['git-log-args'],
}));

const messages = (Array.isArray(input) ? input : [input])
Expand Down
1 change: 1 addition & 0 deletions @commitlint/cli/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface CliFlags {
help?: boolean;
'help-url'?: string;
from?: string;
'git-log-args'?: string;
format?: string;
'parser-preset'?: string;
quiet: boolean;
Expand Down
4 changes: 3 additions & 1 deletion @commitlint/read/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@
"@commitlint/utils": "^17.0.0",
"@types/fs-extra": "^9.0.1",
"@types/git-raw-commits": "^2.0.0",
"@types/minimist": "^1.2.2",
"execa": "^5.0.0"
},
"dependencies": {
"@commitlint/top-level": "^17.0.0",
"@commitlint/types": "^17.0.0",
"fs-extra": "^10.0.0",
"git-raw-commits": "^2.0.0"
"git-raw-commits": "^2.0.0",
"minimist": "^1.2.6"
},
"gitHead": "70f7f4688b51774e7ac5e40e896cdaa3f132b2bc"
}
2 changes: 1 addition & 1 deletion @commitlint/read/src/get-history-commits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {streamToPromise} from './stream-to-promise';

// Get commit messages from history
export async function getHistoryCommits(
options: {from?: string; to?: string},
options: gitRawCommits.GitOptions,
opts: {cwd?: string} = {}
): Promise<string[]> {
return streamToPromise(gitRawCommits(options, {cwd: opts.cwd}));
Expand Down
20 changes: 20 additions & 0 deletions @commitlint/read/src/read.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,23 @@ test('get edit commit message from git subdirectory', async () => {
const actual = await read({edit: true, cwd});
expect(actual).toEqual(expected);
});

test('get edit commit message while skipping first commit', async () => {
const cwd: string = await git.bootstrap();
await fs.mkdir(path.join(cwd, 'beta'));
await fs.writeFile(path.join(cwd, 'beta/beta.txt'), 'beta');

await fs.writeFile(path.join(cwd, 'alpha.txt'), 'alpha');
await execa('git', ['add', 'alpha.txt'], {cwd});
await execa('git', ['commit', '-m', 'alpha'], {cwd});
await fs.writeFile(path.join(cwd, 'beta.txt'), 'beta');
await execa('git', ['add', 'beta.txt'], {cwd});
await execa('git', ['commit', '-m', 'beta'], {cwd});
await fs.writeFile(path.join(cwd, 'gamma.txt'), 'gamma');
await execa('git', ['add', 'gamma.txt'], {cwd});
await execa('git', ['commit', '-m', 'gamma'], {cwd});

const expected = ['beta\n\n'];
const actual = await read({from: 'HEAD~2', cwd, gitLogArgs: '--skip 1'});
expect(actual).toEqual(expected);
});
16 changes: 14 additions & 2 deletions @commitlint/read/src/read.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import minimist from 'minimist';
import type {GitOptions} from 'git-raw-commits';
import {getHistoryCommits} from './get-history-commits';
import {getEditCommit} from './get-edit-commit';

Expand All @@ -6,17 +8,27 @@ interface GetCommitMessageOptions {
from?: string;
to?: string;
edit?: boolean | string;
gitLogArgs?: string;
}

// Get commit messages
export default async function getCommitMessages(
settings: GetCommitMessageOptions
): Promise<string[]> {
const {cwd, from, to, edit} = settings;
const {cwd, from, to, edit, gitLogArgs} = settings;

if (edit) {
return getEditCommit(cwd, edit);
}

return getHistoryCommits({from, to}, {cwd});
let gitOptions: GitOptions = {from, to};
if (gitLogArgs) {
gitOptions = {
...minimist(gitLogArgs.split(' ')),
from,
to,
};
}

return getHistoryCommits(gitOptions, {cwd});
}
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2265,7 +2265,7 @@
resolved "https://registry.npmjs.org/@types/minimatch/-/minimatch-3.0.5.tgz#1001cc5e6a3704b83c236027e77f2f58ea010f40"
integrity sha512-Klz949h02Gz2uZCMGwDUSDS1YBlTdDDgbWHi+81l29tQALUtvz4rAYi5uoVhE5Lagoq6DeqAUlbrHvW/mXDgdQ==

"@types/minimist@^1.2.0":
"@types/minimist@^1.2.0", "@types/minimist@^1.2.2":
version "1.2.2"
resolved "https://registry.npmjs.org/@types/minimist/-/minimist-1.2.2.tgz#ee771e2ba4b3dc5b372935d549fd9617bf345b8c"
integrity sha512-jhuKLIRrhvCPLqwPcx6INqmKeiA5EWrsCOPhrlFSrbrmU4ZMPjj5Ul/oLCMDO98XRUIwVm78xICz4EPCektzeQ==
Expand Down

0 comments on commit 229c65b

Please sign in to comment.