Skip to content

Commit

Permalink
Fix git bisect when repo isn't clean
Browse files Browse the repository at this point in the history
  • Loading branch information
jakebailey committed Oct 7, 2023
1 parent 9c55fc1 commit 409dc01
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/loud-impalas-whisper.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"every-ts": patch
---

Fix git bisect when repo isn't clean
42 changes: 32 additions & 10 deletions src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,25 @@ export class Bisect extends BaseCommand {

await ensureRepo();

if (await isBisecting() && actionsWithSideEffects.has(this.subcommand)) {
let shouldReset = false;
const bisectInfo = await getBisectInfo();

if (this.subcommand === `start` && revs.length >= 2) {
shouldReset = true;
} else if (bisectInfo?.terms.size === 2) {
shouldReset = true;
} else {
if (
bisectInfo
&& actionsWithSideEffects.has(this.subcommand)
&& !bisectInfo.terms.has(this.subcommand)
&& bisectInfo.terms.size === 1
) {
shouldReset = true;
}
}

if (shouldReset) {
await resetTypeScript(`node_modules`, `built`);
}

Expand All @@ -72,17 +90,21 @@ export class Bisect extends BaseCommand {
}
}

async function isBisecting() {
async function getBisectInfo() {
try {
const { stdout } = await execa(`git`, [`bisect`, `log`], { cwd: tsDir });
const lines = stdout.split(/\r?\n/);
if (lines.some((v) => v.startsWith(`# first `))) {
return false;
}
const actions = lines.filter((v) => !v.startsWith(`#`));
return actions.length >= 3;
const done = lines.some((v) => v.startsWith(`# first `));
// Info lines look like "# bad: ...", "# good: ...", "# skip: ...", "# new: ...", "# old: ...", "# status: ..."
const terms = new Set(
lines
.filter((v) => v.startsWith(`# `))
.map((v) => v.split(` `)[1].slice(0, -1))
.filter((v) => v !== `status` && v !== `skip`),
);
return { done, terms };
} catch {
return false;
return undefined;
}
}

Expand All @@ -98,14 +120,14 @@ export class BisectRun extends BaseCommand {
override async execute(): Promise<number | void> {
await ensureRepo();

if (!await isBisecting()) {
if (!await getBisectInfo()) {
throw new ExitError(`Not bisecting`);
}

const { stdout: termGood } = await execa(`git`, [`bisect`, `terms`, `--term-good`], { cwd: tsDir });
const { stdout: termBad } = await execa(`git`, [`bisect`, `terms`, `--term-bad`], { cwd: tsDir });

while (await isBisecting()) {
while (!(await getBisectInfo())?.done) {
await resetTypeScript(`node_modules`, `built`);
await ensureBuilt();

Expand Down

0 comments on commit 409dc01

Please sign in to comment.