Skip to content

Commit 11afc0b

Browse files
committed
feat(cli): commit hash validation
Fixes conventional-changelog#3376
1 parent 8082487 commit 11afc0b

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

@commitlint/cli/src/cli.ts

+15
Original file line numberDiff line numberDiff line change
@@ -215,11 +215,26 @@ async function main(args: MainArgs): Promise<void> {
215215
throw err;
216216
}
217217

218+
if (
219+
typeof flags.from !== 'undefined' &&
220+
typeof flags.to !== 'undefined' &&
221+
flags.from === flags.to
222+
) {
223+
const err = new CliError(
224+
'Please use a different commit hash for --from and --to, not the same. (Or use the --last flag for analyzing just the last commit.)',
225+
pkg.name
226+
);
227+
yargs.showHelp('log');
228+
console.log(err.message);
229+
throw err;
230+
}
231+
218232
const input = await (fromStdin
219233
? stdin()
220234
: read({
221235
to: flags.to,
222236
from: flags.from,
237+
last: flags.last,
223238
edit: flags.edit,
224239
cwd: flags.cwd,
225240
gitLogArgs: flags['git-log-args'],

@commitlint/read/src/read.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ interface GetCommitMessageOptions {
77
cwd?: string;
88
from?: string;
99
to?: string;
10+
last?: boolean;
1011
edit?: boolean | string;
1112
gitLogArgs?: string;
1213
}
@@ -15,13 +16,16 @@ interface GetCommitMessageOptions {
1516
export default async function getCommitMessages(
1617
settings: GetCommitMessageOptions
1718
): Promise<string[]> {
18-
const {cwd, from, to, edit, gitLogArgs} = settings;
19+
const {cwd, from, to, last, edit, gitLogArgs} = settings;
1920

2021
if (edit) {
2122
return getEditCommit(cwd, edit);
2223
}
2324

2425
let gitOptions: GitOptions = {from, to};
26+
if (last) {
27+
gitOptions = {from: 'HEAD~1', to: 'HEAD'};
28+
}
2529
if (gitLogArgs) {
2630
gitOptions = {
2731
...minimist(gitLogArgs.split(' ')),

0 commit comments

Comments
 (0)