From 784a4b36a0b5afdd32759754ec031994bbf0fdc5 Mon Sep 17 00:00:00 2001 From: morriswinkler <4302527+morriswinkler@users.noreply.github.com> Date: Wed, 24 Aug 2022 09:11:00 +0200 Subject: [PATCH] feat(commitlint): add additional git log args --- @commitlint/cli/src/cli.test.ts | 2 ++ @commitlint/cli/src/cli.ts | 6 ++++++ @commitlint/cli/src/types.ts | 1 + @commitlint/read/package.json | 4 +++- @commitlint/read/src/get-history-commits.ts | 2 +- @commitlint/read/src/read.test.ts | 20 ++++++++++++++++++++ @commitlint/read/src/read.ts | 16 ++++++++++++++-- yarn.lock | 2 +- 8 files changed, 48 insertions(+), 5 deletions(-) diff --git a/@commitlint/cli/src/cli.test.ts b/@commitlint/cli/src/cli.test.ts index 9f628f4d58..6430383d96 100644 --- a/@commitlint/cli/src/cli.test.ts +++ b/@commitlint/cli/src/cli.test.ts @@ -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] diff --git a/@commitlint/cli/src/cli.ts b/@commitlint/cli/src/cli.ts index 4f9fd5bd21..3c66282614 100644 --- a/@commitlint/cli/src/cli.ts +++ b/@commitlint/cli/src/cli.ts @@ -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', @@ -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]) diff --git a/@commitlint/cli/src/types.ts b/@commitlint/cli/src/types.ts index 90b72af90d..236bd225d5 100644 --- a/@commitlint/cli/src/types.ts +++ b/@commitlint/cli/src/types.ts @@ -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; diff --git a/@commitlint/read/package.json b/@commitlint/read/package.json index e56c521cd5..193f9d4d54 100644 --- a/@commitlint/read/package.json +++ b/@commitlint/read/package.json @@ -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" } diff --git a/@commitlint/read/src/get-history-commits.ts b/@commitlint/read/src/get-history-commits.ts index caf2fae197..21616b466f 100644 --- a/@commitlint/read/src/get-history-commits.ts +++ b/@commitlint/read/src/get-history-commits.ts @@ -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 { return streamToPromise(gitRawCommits(options, {cwd: opts.cwd})); diff --git a/@commitlint/read/src/read.test.ts b/@commitlint/read/src/read.test.ts index a4c1a4c48e..404375fd9c 100644 --- a/@commitlint/read/src/read.test.ts +++ b/@commitlint/read/src/read.test.ts @@ -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); +}); diff --git a/@commitlint/read/src/read.ts b/@commitlint/read/src/read.ts index 6074d9d9b4..5985bf4463 100644 --- a/@commitlint/read/src/read.ts +++ b/@commitlint/read/src/read.ts @@ -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'; @@ -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 { - 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}); } diff --git a/yarn.lock b/yarn.lock index 2e98927435..f132fe4ac2 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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==