Skip to content

Commit

Permalink
feat(cli): commit hash validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Mersho committed Feb 13, 2024
1 parent 1646c6f commit 67332d7
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 9 deletions.
1 change: 1 addition & 0 deletions @commitlint/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"lodash.isfunction": "^3.0.9",
"resolve-from": "5.0.0",
"resolve-global": "1.0.0",
"simple-git": "^3.22.0",
"yargs": "^17.0.0"
},
"gitHead": "70f7f4688b51774e7ac5e40e896cdaa3f132b2bc"
Expand Down
46 changes: 37 additions & 9 deletions @commitlint/cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import execa, {ExecaError} from 'execa';
import load from '@commitlint/load';
import lint from '@commitlint/lint';
import read from '@commitlint/read';
import {simpleGit} from 'simple-git';
import isFunction from 'lodash.isfunction';
import resolveFrom from 'resolve-from';
import resolveGlobal from 'resolve-global';
Expand Down Expand Up @@ -213,16 +214,43 @@ async function main(args: MainArgs): Promise<void> {
throw err;
}

const input = await (fromStdin
? stdin()
: read({
to: flags.to,
from: flags.from,
edit: flags.edit,
cwd: flags.cwd,
gitLogArgs: flags['git-log-args'],
}));
if (
Object.hasOwn(flags, 'from') &&
Object.hasOwn(flags, 'to') &&
flags.from === flags.to
) {
const err = new CliError(
'Please use a different commit hash for --from and --to, not the same. (Or use the --last flag for analyzing just the last commit.)',
pkg.name
);
yargs.showHelp('log');
console.log(err.message);
throw err;
}

let input;

if (Object.hasOwn(flags, 'last')) {
const log = await simpleGit({baseDir: flags.cwd}).log({
maxCount: 1,
});
input = '';
if (log.latest) {
input = log.latest.message;
} else {
throw new CliError('No commits found in the repository.', pkg.name);
}
} else {
input = await (fromStdin
? stdin()
: read({
to: flags.to,
from: flags.from,
edit: flags.edit,
cwd: flags.cwd,
gitLogArgs: flags['git-log-args'],
}));
}
const messages = (Array.isArray(input) ? input : [input])
.filter((message) => typeof message === 'string')
.filter((message) => message.trim() !== '')
Expand Down
21 changes: 21 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,18 @@
"@jridgewell/resolve-uri" "^3.1.0"
"@jridgewell/sourcemap-codec" "^1.4.14"

"@kwsites/file-exists@^1.1.1":
version "1.1.1"
resolved "https://registry.npmjs.org/@kwsites/file-exists/-/file-exists-1.1.1.tgz#ad1efcac13e1987d8dbaf235ef3be5b0d96faa99"
integrity sha512-m9/5YGR18lIwxSFDwfE3oA7bWuq9kdau6ugN4H2rJeyhFQZcG9AgSHkQtSD15a8WvTgfz9aikZMrKPHvbpqFiw==
dependencies:
debug "^4.1.1"

"@kwsites/promise-deferred@^1.1.1":
version "1.1.1"
resolved "https://registry.npmjs.org/@kwsites/promise-deferred/-/promise-deferred-1.1.1.tgz#8ace5259254426ccef57f3175bc64ed7095ed919"
integrity sha512-GaHYm+c0O9MjZRu0ongGBRbinu8gVAMd2UZjji6jVmqKtZluZnptXGWhz1E8j8D2HJ3f/yMxKAUC0b+57wncIw==

"@lerna/[email protected]":
version "6.4.1"
resolved "https://registry.npmjs.org/@lerna/add/-/add-6.4.1.tgz#fa20fe9ff875dc5758141262c8cde0d9a6481ec4"
Expand Down Expand Up @@ -8007,6 +8019,15 @@ signal-exit@^4.1.0:
resolved "https://registry.npmjs.org/signal-exit/-/signal-exit-4.1.0.tgz#952188c1cbd546070e2dd20d0f41c0ae0530cb04"
integrity sha512-bzyZ1e88w9O1iNJbKnOlvYTrWPDl46O1bG0D3XInv+9tkPrxrN8jUUTiFlDkkmKWgn1M6CfIA13SuGqOa9Korw==

simple-git@^3.22.0:
version "3.22.0"
resolved "https://registry.npmjs.org/simple-git/-/simple-git-3.22.0.tgz#616d41c661e30f9c65778956317d422b1729a242"
integrity sha512-6JujwSs0ac82jkGjMHiCnTifvf1crOiY/+tfs/Pqih6iow7VrpNKRRNdWm6RtaXpvvv/JGNYhlUtLhGFqHF+Yw==
dependencies:
"@kwsites/file-exists" "^1.1.1"
"@kwsites/promise-deferred" "^1.1.1"
debug "^4.3.4"

sisteransi@^1.0.5:
version "1.0.5"
resolved "https://registry.npmjs.org/sisteransi/-/sisteransi-1.0.5.tgz#134d681297756437cc05ca01370d3a7a571075ed"
Expand Down

0 comments on commit 67332d7

Please sign in to comment.