Skip to content

Commit 784a4b3

Browse files
committed
feat(commitlint): add additional git log args
1 parent c376ce6 commit 784a4b3

File tree

8 files changed

+48
-5
lines changed

8 files changed

+48
-5
lines changed

@commitlint/cli/src/cli.test.ts

+2
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,8 @@ test('should print help', async () => {
506506
-H, --help-url help url in error message [string]
507507
-f, --from lower end of the commit range to lint; applies if
508508
edit=false [string]
509+
--git-log-args addditional git log arguments as space separated string,
510+
example \'--first-parent --cherry-pick\' [string]
509511
-o, --format output format of the results [string]
510512
-p, --parser-preset configuration preset to use for
511513
conventional-commits-parser [string]

@commitlint/cli/src/cli.ts

+6
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ const cli = yargs
7777
'lower end of the commit range to lint; applies if edit=false',
7878
type: 'string',
7979
},
80+
'git-log-args': {
81+
description:
82+
"addditional git log arguments as space separated string, example '--first-parent --cherry-pick'",
83+
type: 'string',
84+
},
8085
format: {
8186
alias: 'o',
8287
description: 'output format of the results',
@@ -182,6 +187,7 @@ async function main(args: MainArgs) {
182187
from: flags.from,
183188
edit: flags.edit,
184189
cwd: flags.cwd,
190+
gitLogArgs: flags['git-log-args'],
185191
}));
186192

187193
const messages = (Array.isArray(input) ? input : [input])

@commitlint/cli/src/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export interface CliFlags {
88
help?: boolean;
99
'help-url'?: string;
1010
from?: string;
11+
'git-log-args'?: string;
1112
format?: string;
1213
'parser-preset'?: string;
1314
quiet: boolean;

@commitlint/read/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,15 @@
3939
"@commitlint/utils": "^17.0.0",
4040
"@types/fs-extra": "^9.0.1",
4141
"@types/git-raw-commits": "^2.0.0",
42+
"@types/minimist": "^1.2.2",
4243
"execa": "^5.0.0"
4344
},
4445
"dependencies": {
4546
"@commitlint/top-level": "^17.0.0",
4647
"@commitlint/types": "^17.0.0",
4748
"fs-extra": "^10.0.0",
48-
"git-raw-commits": "^2.0.0"
49+
"git-raw-commits": "^2.0.0",
50+
"minimist": "^1.2.6"
4951
},
5052
"gitHead": "70f7f4688b51774e7ac5e40e896cdaa3f132b2bc"
5153
}

@commitlint/read/src/get-history-commits.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {streamToPromise} from './stream-to-promise';
33

44
// Get commit messages from history
55
export async function getHistoryCommits(
6-
options: {from?: string; to?: string},
6+
options: gitRawCommits.GitOptions,
77
opts: {cwd?: string} = {}
88
): Promise<string[]> {
99
return streamToPromise(gitRawCommits(options, {cwd: opts.cwd}));

@commitlint/read/src/read.test.ts

+20
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,23 @@ test('get edit commit message from git subdirectory', async () => {
5151
const actual = await read({edit: true, cwd});
5252
expect(actual).toEqual(expected);
5353
});
54+
55+
test('get edit commit message while skipping first commit', async () => {
56+
const cwd: string = await git.bootstrap();
57+
await fs.mkdir(path.join(cwd, 'beta'));
58+
await fs.writeFile(path.join(cwd, 'beta/beta.txt'), 'beta');
59+
60+
await fs.writeFile(path.join(cwd, 'alpha.txt'), 'alpha');
61+
await execa('git', ['add', 'alpha.txt'], {cwd});
62+
await execa('git', ['commit', '-m', 'alpha'], {cwd});
63+
await fs.writeFile(path.join(cwd, 'beta.txt'), 'beta');
64+
await execa('git', ['add', 'beta.txt'], {cwd});
65+
await execa('git', ['commit', '-m', 'beta'], {cwd});
66+
await fs.writeFile(path.join(cwd, 'gamma.txt'), 'gamma');
67+
await execa('git', ['add', 'gamma.txt'], {cwd});
68+
await execa('git', ['commit', '-m', 'gamma'], {cwd});
69+
70+
const expected = ['beta\n\n'];
71+
const actual = await read({from: 'HEAD~2', cwd, gitLogArgs: '--skip 1'});
72+
expect(actual).toEqual(expected);
73+
});

@commitlint/read/src/read.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import minimist from 'minimist';
2+
import type {GitOptions} from 'git-raw-commits';
13
import {getHistoryCommits} from './get-history-commits';
24
import {getEditCommit} from './get-edit-commit';
35

@@ -6,17 +8,27 @@ interface GetCommitMessageOptions {
68
from?: string;
79
to?: string;
810
edit?: boolean | string;
11+
gitLogArgs?: string;
912
}
1013

1114
// Get commit messages
1215
export default async function getCommitMessages(
1316
settings: GetCommitMessageOptions
1417
): Promise<string[]> {
15-
const {cwd, from, to, edit} = settings;
18+
const {cwd, from, to, edit, gitLogArgs} = settings;
1619

1720
if (edit) {
1821
return getEditCommit(cwd, edit);
1922
}
2023

21-
return getHistoryCommits({from, to}, {cwd});
24+
let gitOptions: GitOptions = {from, to};
25+
if (gitLogArgs) {
26+
gitOptions = {
27+
...minimist(gitLogArgs.split(' ')),
28+
from,
29+
to,
30+
};
31+
}
32+
33+
return getHistoryCommits(gitOptions, {cwd});
2234
}

yarn.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -2265,7 +2265,7 @@
22652265
resolved "https://registry.npmjs.org/@types/minimatch/-/minimatch-3.0.5.tgz#1001cc5e6a3704b83c236027e77f2f58ea010f40"
22662266
integrity sha512-Klz949h02Gz2uZCMGwDUSDS1YBlTdDDgbWHi+81l29tQALUtvz4rAYi5uoVhE5Lagoq6DeqAUlbrHvW/mXDgdQ==
22672267

2268-
"@types/minimist@^1.2.0":
2268+
"@types/minimist@^1.2.0", "@types/minimist@^1.2.2":
22692269
version "1.2.2"
22702270
resolved "https://registry.npmjs.org/@types/minimist/-/minimist-1.2.2.tgz#ee771e2ba4b3dc5b372935d549fd9617bf345b8c"
22712271
integrity sha512-jhuKLIRrhvCPLqwPcx6INqmKeiA5EWrsCOPhrlFSrbrmU4ZMPjj5Ul/oLCMDO98XRUIwVm78xICz4EPCektzeQ==

0 commit comments

Comments
 (0)