Skip to content

Commit bf8516d

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 bf8516d

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-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

+24-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,19 @@ 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' || typeof flags.to !== 'undefined')
206+
) {
207+
const err = new CliError(
208+
'Please use the --last flag alone. The --last flag should not be used with --to or --from.',
209+
pkg.name
210+
);
211+
yargs.showHelp('log');
212+
console.log(err.message);
213+
throw err;
214+
}
215+
198216
const input = await (fromStdin
199217
? stdin()
200218
: read({
@@ -212,7 +230,7 @@ async function main(args: MainArgs): Promise<void> {
212230

213231
if (messages.length === 0 && !checkFromRepository(flags)) {
214232
const err = new CliError(
215-
'[input] is required: supply via stdin, or --env or --edit or --from and --to',
233+
'[input] is required: supply via stdin, or --env or --edit or --last or --from and --to',
216234
pkg.name
217235
);
218236
yargs.showHelp('log');
@@ -355,7 +373,11 @@ function checkFromEdit(flags: CliFlags): boolean {
355373
}
356374

357375
function checkFromHistory(flags: CliFlags): boolean {
358-
return typeof flags.from === 'string' || typeof flags.to === 'string';
376+
return (
377+
typeof flags.from === 'string' ||
378+
typeof flags.to === 'string' ||
379+
typeof flags.last === 'boolean'
380+
);
359381
}
360382

361383
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)