Skip to content

Commit 6e7c9c3

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

File tree

3 files changed

+60
-10
lines changed

3 files changed

+60
-10
lines changed

@commitlint/cli/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"lodash.isfunction": "^3.0.9",
5757
"resolve-from": "5.0.0",
5858
"resolve-global": "1.0.0",
59+
"simple-git": "^3.22.0",
5960
"yargs": "^17.0.0"
6061
},
6162
"gitHead": "70f7f4688b51774e7ac5e40e896cdaa3f132b2bc"

@commitlint/cli/src/cli.ts

+38-10
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import execa, {ExecaError} from 'execa';
22
import load from '@commitlint/load';
33
import lint from '@commitlint/lint';
44
import read from '@commitlint/read';
5+
import {simpleGit} from 'simple-git';
56
import isFunction from 'lodash.isfunction';
67
import resolveFrom from 'resolve-from';
78
import resolveGlobal from 'resolve-global';
@@ -204,7 +205,7 @@ async function main(args: MainArgs): Promise<void> {
204205
typeof flags.last !== 'undefined' &&
205206
(typeof flags.from !== 'undefined' ||
206207
typeof flags.to !== 'undefined' ||
207-
typeof flags.edit !== 'undefined')
208+
flags.edit)
208209
) {
209210
const err = new CliError(
210211
'Please use the --last flag alone. The --last flag should not be used with --to or --from or --edit.',
@@ -215,16 +216,43 @@ async function main(args: MainArgs): Promise<void> {
215216
throw err;
216217
}
217218

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

235+
if (typeof flags.last !== 'undefined') {
236+
const log = await simpleGit({baseDir: flags.cwd}).log({
237+
maxCount: 1,
238+
});
239+
input = '';
240+
if (log.latest) {
241+
input = log.latest.message;
242+
} else {
243+
throw new CliError('No commits found in the repository.', pkg.name);
244+
}
245+
} else {
246+
input = await (fromStdin
247+
? stdin()
248+
: read({
249+
to: flags.to,
250+
from: flags.from,
251+
edit: flags.edit,
252+
cwd: flags.cwd,
253+
gitLogArgs: flags['git-log-args'],
254+
}));
255+
}
228256
const messages = (Array.isArray(input) ? input : [input])
229257
.filter((message) => typeof message === 'string')
230258
.filter((message) => message.trim() !== '')

yarn.lock

+21
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,18 @@
628628
"@jridgewell/resolve-uri" "^3.1.0"
629629
"@jridgewell/sourcemap-codec" "^1.4.14"
630630

631+
"@kwsites/file-exists@^1.1.1":
632+
version "1.1.1"
633+
resolved "https://registry.npmjs.org/@kwsites/file-exists/-/file-exists-1.1.1.tgz#ad1efcac13e1987d8dbaf235ef3be5b0d96faa99"
634+
integrity sha512-m9/5YGR18lIwxSFDwfE3oA7bWuq9kdau6ugN4H2rJeyhFQZcG9AgSHkQtSD15a8WvTgfz9aikZMrKPHvbpqFiw==
635+
dependencies:
636+
debug "^4.1.1"
637+
638+
"@kwsites/promise-deferred@^1.1.1":
639+
version "1.1.1"
640+
resolved "https://registry.npmjs.org/@kwsites/promise-deferred/-/promise-deferred-1.1.1.tgz#8ace5259254426ccef57f3175bc64ed7095ed919"
641+
integrity sha512-GaHYm+c0O9MjZRu0ongGBRbinu8gVAMd2UZjji6jVmqKtZluZnptXGWhz1E8j8D2HJ3f/yMxKAUC0b+57wncIw==
642+
631643
632644
version "6.4.1"
633645
resolved "https://registry.npmjs.org/@lerna/add/-/add-6.4.1.tgz#fa20fe9ff875dc5758141262c8cde0d9a6481ec4"
@@ -8007,6 +8019,15 @@ signal-exit@^4.1.0:
80078019
resolved "https://registry.npmjs.org/signal-exit/-/signal-exit-4.1.0.tgz#952188c1cbd546070e2dd20d0f41c0ae0530cb04"
80088020
integrity sha512-bzyZ1e88w9O1iNJbKnOlvYTrWPDl46O1bG0D3XInv+9tkPrxrN8jUUTiFlDkkmKWgn1M6CfIA13SuGqOa9Korw==
80098021

8022+
simple-git@^3.22.0:
8023+
version "3.22.0"
8024+
resolved "https://registry.npmjs.org/simple-git/-/simple-git-3.22.0.tgz#616d41c661e30f9c65778956317d422b1729a242"
8025+
integrity sha512-6JujwSs0ac82jkGjMHiCnTifvf1crOiY/+tfs/Pqih6iow7VrpNKRRNdWm6RtaXpvvv/JGNYhlUtLhGFqHF+Yw==
8026+
dependencies:
8027+
"@kwsites/file-exists" "^1.1.1"
8028+
"@kwsites/promise-deferred" "^1.1.1"
8029+
debug "^4.3.4"
8030+
80108031
sisteransi@^1.0.5:
80118032
version "1.0.5"
80128033
resolved "https://registry.npmjs.org/sisteransi/-/sisteransi-1.0.5.tgz#134d681297756437cc05ca01370d3a7a571075ed"

0 commit comments

Comments
 (0)