Skip to content

Commit af05a3a

Browse files
silverwindclaude
andcommitted
simplify
- throw early-validation errors instead of return end(new Error()) - consolidate base version stripV/isSemver in the resolver IIFE - parallelize pre-commit write-tree and tag-verify probes - reuse single HEAD rev-parse for --force-with-lease - tighten replacements type to RegExp only Co-Authored-By: Claude (Opus 4.7) <noreply@anthropic.com>
1 parent a734578 commit af05a3a

1 file changed

Lines changed: 17 additions & 22 deletions

File tree

index.ts

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ export type GetFileChangesOpts = {
108108
file: string,
109109
baseVersion: string,
110110
newVersion: string,
111-
replacements?: Array<{re: RegExp | string, replacement: string}>,
111+
replacements?: Array<{re: RegExp, replacement: string}>,
112112
date?: string,
113113
};
114114
@@ -371,24 +371,25 @@ async function main(): Promise<void> {
371371
files = files.map(file => relative(pwd, file));
372372

373373
if (level === "prerelease" && !args.preid) {
374-
return end(new Error("prerelease requires --preid option"));
374+
throw new Error("prerelease requires --preid option");
375375
}
376376
if (args.gitless && args.release) {
377-
return end(new Error("--gitless and --release are mutually exclusive"));
377+
throw new Error("--gitless and --release are mutually exclusive");
378378
}
379379
if (args["no-push"] && args.release) {
380-
return end(new Error("--no-push and --release are mutually exclusive"));
380+
throw new Error("--no-push and --release are mutually exclusive");
381381
}
382382

383383
const baseVersionPromise = (async (): Promise<{baseVersion: string, baseSource: string, describeTag: string}> => {
384384
let baseVersion = "";
385385
let baseSource = "";
386386
let describeTag = "";
387387
if (args.base) {
388-
return {baseVersion: String(args.base), baseSource: "--base", describeTag};
388+
const raw = String(args.base);
389+
if (!isSemver(raw)) throw new Error(`Invalid base version: ${raw}`);
390+
return {baseVersion: stripV(raw), baseSource: "--base", describeTag};
389391
}
390392
if (!args.gitless) {
391-
// git describe is O(depth); tag list is O(n log n)
392393
try {
393394
const result = await exec("git", ["describe", "--tags", "--abbrev=0"]);
394395
describeTag = result.stdout.trim();
@@ -432,20 +433,15 @@ async function main(): Promise<void> {
432433
return branchOut.trim();
433434
})() : Promise.resolve("");
434435

435-
let {baseVersion, baseSource, describeTag} = await baseVersionPromise;
436+
const {baseVersion, baseSource, describeTag} = await baseVersionPromise;
436437
if (args.gitless && !baseVersion) {
437-
return end(new Error(`--gitless requires --base to be set or a version in package.json or pyproject.toml`));
438+
throw new Error(`--gitless requires --base to be set or a version in package.json or pyproject.toml`);
438439
}
439440
logVerbose(`base version ${baseVersion} from ${baseSource}`);
440441

441-
baseVersion = stripV(baseVersion);
442-
if (!isSemver(baseVersion)) {
443-
throw new Error(`Invalid base version: ${baseVersion}`);
444-
}
445-
446442
const pushBranch = await pushBranchPromise;
447443
if (pushBranch === "HEAD") {
448-
return end(new Error("Cannot push from detached HEAD. Pass --branch <name> or --no-push."));
444+
throw new Error("Cannot push from detached HEAD. Pass --branch <name> or --no-push.");
449445
}
450446

451447
const newVersion = incrementSemver(baseVersion, level, typeof args.preid === "string" ? args.preid : undefined);
@@ -458,7 +454,7 @@ async function main(): Promise<void> {
458454
let [, re, replacement, flags] = (reReplaceString.exec(replaceStr) || []);
459455

460456
if (!re || !replacement) {
461-
end(new Error(`Invalid replace string: ${replaceStr}`));
457+
throw new Error(`Invalid replace string: ${replaceStr}`);
462458
}
463459

464460
replacement = replaceTokens(replacement, newVersion);
@@ -546,7 +542,10 @@ async function main(): Promise<void> {
546542
const changelog = (await changelogPromise) ?? undefined;
547543

548544
// preserve user's staged hunks on rollback (--soft would leave our changes staged)
549-
const preIndexTreeOid = await exec("git", ["write-tree"]).then(r => r.stdout.trim()).catch(() => null);
545+
const [preIndexTreeOid, priorLocalTagOid] = await Promise.all([
546+
exec("git", ["write-tree"]).then(r => r.stdout.trim()).catch(() => null),
547+
exec("git", ["rev-parse", "--verify", tagRef]).then(r => r.stdout.trim()).catch(() => null),
548+
]);
550549

551550
const commitMsg = joinStrings([tagName, ...msgs, changelog], "\n\n");
552551
let commitArgs: string[];
@@ -566,10 +565,6 @@ async function main(): Promise<void> {
566565
if (preIndexTreeOid) await exec("git", ["read-tree", preIndexTreeOid]);
567566
});
568567

569-
// capture the prior local tag (if any) since `git tag -f` overwrites it
570-
const priorLocalTagOid = await exec("git", ["rev-parse", "--verify", tagRef])
571-
.then(r => r.stdout.trim()).catch(() => null);
572-
573568
const tagMsg = joinStrings([...msgs, changelog], "\n\n");
574569
// adding explicit -a here seems to make git no longer sign the tag
575570
writeResult(await exec("git", ["tag", "-f", "-F", "-", tagName], {stdin: {string: tagMsg}}));
@@ -582,15 +577,15 @@ async function main(): Promise<void> {
582577

583578
if (!args["no-push"]) {
584579
const probe = await remoteProbePromise!;
580+
const headOid = (await exec("git", ["rev-parse", "HEAD"])).stdout.trim();
585581

586582
writeResult(await exec("git", ["push", pushRemote, pushBranch, tagName]));
587-
const pushedHeadOid = (await exec("git", ["rev-parse", "HEAD"])).stdout.trim();
588583

589584
if (probe.ok) {
590585
// --force-with-lease guards against concurrent pushes overwriting work
591586
rollbacks.push(async () => {
592587
if (probe.branch) {
593-
await exec("git", ["push", `--force-with-lease=${branchRef}:${pushedHeadOid}`, pushRemote, `${probe.branch}:${branchRef}`]);
588+
await exec("git", ["push", `--force-with-lease=${branchRef}:${headOid}`, pushRemote, `${probe.branch}:${branchRef}`]);
594589
} else {
595590
await exec("git", ["push", pushRemote, `:${branchRef}`]);
596591
}

0 commit comments

Comments
 (0)