Skip to content

Commit 0b9980f

Browse files
committed
feat(cli): implement new --last CLI flag
By default, the getCommitMessages function retrieves the HEAD commit message when the `to` and `from` parameters are not specified. Therefore, there's no need to implement or edit this function to get the last commit.
1 parent 665d57a commit 0b9980f

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

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

+2
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,8 @@ test('should print help', async () => {
527527
edit=false [string]
528528
--git-log-args additional git log arguments as space separated string,
529529
example '--first-parent --cherry-pick' [string]
530+
-l, --last just analyze the last commit; applies if edit=false
531+
[boolean]
530532
-o, --format output format of the results [string]
531533
-p, --parser-preset configuration preset to use for
532534
conventional-commits-parser [string]

@commitlint/cli/src/cli.ts

+26-2
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ const cli = yargs
8282
"additional git log arguments as space separated string, example '--first-parent --cherry-pick'",
8383
type: 'string',
8484
},
85+
last: {
86+
alias: 'l',
87+
description: 'just analyze the last commit; applies if edit=false',
88+
type: 'boolean',
89+
},
8590
format: {
8691
alias: 'o',
8792
description: 'output format of the results',
@@ -195,6 +200,21 @@ async function main(args: MainArgs): Promise<void> {
195200

196201
const fromStdin = checkFromStdin(raw, flags);
197202

203+
if (
204+
typeof flags.last !== 'undefined' &&
205+
(typeof flags.from !== 'undefined' ||
206+
typeof flags.to !== 'undefined' ||
207+
typeof flags.edit !== 'undefined')
208+
) {
209+
const err = new CliError(
210+
'Please use the --last flag alone. The --last flag should not be used with --to or --from or --edit.',
211+
pkg.name
212+
);
213+
yargs.showHelp('log');
214+
console.log(err.message);
215+
throw err;
216+
}
217+
198218
const input = await (fromStdin
199219
? stdin()
200220
: read({
@@ -212,7 +232,7 @@ async function main(args: MainArgs): Promise<void> {
212232

213233
if (messages.length === 0 && !checkFromRepository(flags)) {
214234
const err = new CliError(
215-
'[input] is required: supply via stdin, or --env or --edit or --from and --to',
235+
'[input] is required: supply via stdin, or --env or --edit or --last or --from and --to',
216236
pkg.name
217237
);
218238
yargs.showHelp('log');
@@ -355,7 +375,11 @@ function checkFromEdit(flags: CliFlags): boolean {
355375
}
356376

357377
function checkFromHistory(flags: CliFlags): boolean {
358-
return typeof flags.from === 'string' || typeof flags.to === 'string';
378+
return (
379+
typeof flags.from === 'string' ||
380+
typeof flags.to === 'string' ||
381+
typeof flags.last === 'boolean'
382+
);
359383
}
360384

361385
function normalizeFlags(flags: CliFlags): CliFlags {

@commitlint/cli/src/types.ts

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

0 commit comments

Comments
 (0)