Skip to content

Commit 7279c89

Browse files
committed
feat(cli): commit hash validation
Fixes conventional-changelog#3376
1 parent bf8516d commit 7279c89

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
@@ -213,11 +213,26 @@ async function main(args: MainArgs): Promise<void> {
213213
throw err;
214214
}
215215

216+
if (
217+
typeof flags.from !== 'undefined' &&
218+
typeof flags.to !== 'undefined' &&
219+
flags.from === flags.to
220+
) {
221+
const err = new CliError(
222+
'Please use a different commit hash for --from and --to, not the same. (Or use the --last flag for analyzing just the last commit.)',
223+
pkg.name
224+
);
225+
yargs.showHelp('log');
226+
console.log(err.message);
227+
throw err;
228+
}
229+
216230
const input = await (fromStdin
217231
? stdin()
218232
: read({
219233
to: flags.to,
220234
from: flags.from,
235+
last: flags.last,
221236
edit: flags.edit,
222237
cwd: flags.cwd,
223238
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)