From 7dd3c791f5b2d8e6a5537e8e5c0996640dec03a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Wo=C5=BAniak?= Date: Thu, 17 Sep 2026 09:42:51 +0200 Subject: [PATCH 1/2] feat(rebase): record what a rebase conflict hit A conflict said only which head it happened at, which cannot tell a stale lockfile from half the pull request. git names the unmerged files itself, read before the abort because the abort takes them with it, so the record carries them beside the head and the command prints them. The paths are an optional key, so a record an older version wrote still puts the pull request in Needs me. Closes #37 Co-Authored-By: Claude Opus 5 (1M context) --- src/adapters/git.test.ts | 32 ++++++++++++++++++++++++++++++-- src/adapters/git.ts | 32 ++++++++++++++++++++++++++------ src/cli/rebase.test.ts | 28 ++++++++++++++++++++++++++-- src/cli/rebase.ts | 12 +++++++++--- src/domain/rebase.test.ts | 36 +++++++++++++++++++++++++++++++++++- src/domain/rebase.ts | 28 ++++++++++++++++++++-------- 6 files changed, 146 insertions(+), 22 deletions(-) diff --git a/src/adapters/git.test.ts b/src/adapters/git.test.ts index 9ace1e5..6a70f60 100644 --- a/src/adapters/git.test.ts +++ b/src/adapters/git.test.ts @@ -37,6 +37,8 @@ const git = (options: { readonly ahead?: number | undefined /** How many commits the base has that the pull request's head does not. */ readonly behind?: number | undefined + /** The files a conflicted replay left unmerged in the worktree. */ + readonly unmerged?: ReadonlyArray | undefined }) => layerFake((command) => { if (command._tag !== "StandardCommand") { @@ -70,6 +72,9 @@ const git = (options: { if (argv === `-C ${worktree} rev-parse HEAD`) { return Effect.succeed(fakeHandle({ stdout: `${rebased}\n` })) } + if (argv === `-C ${worktree} diff --name-only --diff-filter=U`) { + return Effect.succeed(fakeHandle({ stdout: (options.unmerged ?? []).map((path) => `${path}\n`).join("") })) + } if (argv === `-C ${clone} worktree list --porcelain`) { const listed = (options.worktrees ?? []).map((directory) => `worktree ${directory}\nbare\n`) return Effect.succeed(fakeHandle({ stdout: [`worktree ${clone}\nbare\n`, ...listed].join("\n") })) @@ -312,11 +317,34 @@ describe("rebasing a branch onto its base", () => { return Effect.gen(function* () { const done = yield* rebaseOnto("dominikwozniak/dw-mc", 28, "main", "feat/28-a-branch") - assert.deepStrictEqual(done, { _tag: "conflicted" }) + assert.deepStrictEqual(done, { _tag: "conflicted", paths: ["src/cli/rebase.ts", "pnpm-lock.yaml"] }) assert.include(spawned, `git -C ${worktree} rebase --abort`) assert.isFalse(spawned.some((vector) => vector.includes(" push "))) assert.strictEqual(spawned.at(-1), `git -C ${clone} worktree remove --force ${worktree}`) - }).pipe(Effect.provide(machine(git({ spawned, cloned: true, behind: 3, refuses })))) + }).pipe( + Effect.provide( + machine(git({ spawned, cloned: true, behind: 3, refuses, unmerged: ["src/cli/rebase.ts", "pnpm-lock.yaml"] })) + ) + ) + }) + + it.effect("reads the unmerged files while the conflict is still there, before the abort", () => { + const spawned: Array = [] + const refuses = { argv: `-C ${worktree} rebase refs/heads/main`, detail: said.rebaseConflict } + + return Effect.gen(function* () { + yield* rebaseOnto("dominikwozniak/dw-mc", 28, "main", "feat/28-a-branch") + + // An abort puts the branch back, and with it every unmerged path: read + // after it, the conflict record would carry nothing. + assert.include(spawned, `git -C ${worktree} diff --name-only --diff-filter=U`) + assert.isBelow( + spawned.indexOf(`git -C ${worktree} diff --name-only --diff-filter=U`), + spawned.indexOf(`git -C ${worktree} rebase --abort`) + ) + }).pipe( + Effect.provide(machine(git({ spawned, cloned: true, behind: 3, refuses, unmerged: ["src/cli/rebase.ts"] }))) + ) }) it.effect("reports a rebase that never started as what it is, and not as a conflict", () => { diff --git a/src/adapters/git.ts b/src/adapters/git.ts index 4e4ff21..370b1dc 100644 --- a/src/adapters/git.ts +++ b/src/adapters/git.ts @@ -204,7 +204,7 @@ export const fixWorktree = Effect.fn("git.fixWorktree")(function* (repo: string, /** What a rebase of a pull request's branch onto its base came to. */ export type Rebased = | { readonly _tag: "up-to-date" } - | { readonly _tag: "conflicted" } + | { readonly _tag: "conflicted"; readonly paths: ReadonlyArray } | { readonly _tag: "pushed"; readonly before: string; readonly after: string; readonly behind: number } /** How many commits the base has that the worktree's head does not. */ @@ -214,7 +214,21 @@ const behindBy = Effect.fn("git.behindBy")(function* (directory: string, base: s }) /** - * Rebases onto `base` in `directory`, or says the rebase conflicted. + * The files the stopped replay left unmerged, which is what the conflict is + * about. + * + * `git` names them itself rather than being read out of its prose, and a + * listing that refuses is no reason to leave a rebase standing: the paths are + * worth less than the abort, so the conflict is recorded with none of them. + */ +const unmergedIn = Effect.fn("git.unmergedIn")(function* (directory: string) { + const listed = yield* Effect.orElseSucceed(git(["-C", directory, "diff", "--name-only", "--diff-filter=U"]), () => "") + return listed.split("\n").filter((line) => line !== "") +}) + +/** + * Rebases onto `base` in `directory`, or says which files the rebase conflicted + * on. * * A conflict is told apart from every other way `git rebase` refuses by asking * `git` to abort: an abort succeeds only where a rebase is in progress, which @@ -223,19 +237,24 @@ const behindBy = Effect.fn("git.behindBy")(function* (directory: string, base: s * started, and is worth its own words rather than being reported as a conflict * that never happened. * + * The unmerged files are read before the abort, which is the only moment they + * exist: the abort puts the branch back and takes the stopped replay's index + * with it. + * * Either way nothing half-finished is left behind: the worktree is thrown away * with the run, and the abort puts the branch back where it was first. */ const replayOnto = Effect.fn("git.replayOnto")(function* (directory: string, base: string) { const rebased = yield* Effect.result(git(["-C", directory, "rebase", `refs/heads/${base}`])) if (Result.isSuccess(rebased)) { - return true + return null } + const paths = yield* unmergedIn(directory) const aborted = yield* Effect.result(git(["-C", directory, "rebase", "--abort"])) if (Result.isFailure(aborted)) { return yield* rebased.failure } - return false + return paths }) /** @@ -265,8 +284,9 @@ export const rebaseOnto = Effect.fn("git.rebaseOnto")(function* ( if (behind === 0) { return { _tag: "up-to-date" } satisfies Rebased } - if (!(yield* replayOnto(worktree.directory, base))) { - return { _tag: "conflicted" } satisfies Rebased + const conflicted = yield* replayOnto(worktree.directory, base) + if (conflicted !== null) { + return { _tag: "conflicted", paths: conflicted } satisfies Rebased } const after = yield* git(["-C", worktree.directory, "rev-parse", "HEAD"]) diff --git a/src/cli/rebase.test.ts b/src/cli/rebase.test.ts index 7dee258..c8418c4 100644 --- a/src/cli/rebase.test.ts +++ b/src/cli/rebase.test.ts @@ -1,5 +1,5 @@ import { assert, describe, it } from "@effect/vitest" -import { ConfigProvider, Console, Effect, FileSystem, Layer, Path, Stdio } from "effect" +import { ConfigProvider, Console, Effect, FileSystem, Layer, Option, Path, Stdio } from "effect" import { Command } from "effect/unstable/cli" import type { ConfigFile } from "#adapters/config.ts" @@ -8,7 +8,7 @@ import { layerScripted } from "#adapters/picker.ts" import { fakeHandle, layerFake } from "#adapters/spawner.ts" import * as Store from "#adapters/store.ts" import { dwMc, version } from "#cli/cli.ts" -import { conflictedAt } from "#domain/rebase.ts" +import { Conflict, conflictedAt } from "#domain/rebase.ts" const repo = "dominikwozniak/dw-mc" const branch = "feat/28-a-branch" @@ -39,6 +39,8 @@ const machine = (options: { readonly open?: ReadonlyArray<{ number: number; headRefName: string; baseRefName: string }> | undefined /** Whether replaying the commits onto the base conflicts. */ readonly conflicts?: boolean | undefined + /** The files a conflicted replay leaves unmerged in the worktree. */ + readonly unmerged?: ReadonlyArray | undefined /** Who opened the pull request, where it was not me. */ readonly author?: string | undefined /** Whether the head branch lives in a fork. */ @@ -68,6 +70,9 @@ const machine = (options: { if (argv === `-C ${worktree} rev-parse HEAD`) { return Effect.succeed(fakeHandle({ stdout: `${rebased}\n` })) } + if (argv === `-C ${worktree} diff --name-only --diff-filter=U`) { + return Effect.succeed(fakeHandle({ stdout: (options.unmerged ?? []).map((path) => `${path}\n`).join("") })) + } return Effect.succeed(fakeHandle({})) } if (/^pr view 28 --repo \S+ --json \S+$/.test(argv)) { @@ -195,6 +200,25 @@ describe("dw-mc rebase", () => { }).pipe(Effect.provide(machine({ spawned, behind: 3, conflicts: true })), recording(printed)) }) + it.effect("prints the files the rebase conflicted on and writes them down beside the head", () => { + const spawned: Array = [] + const printed: Array = [] + const unmerged = ["src/cli/rebase.ts", "pnpm-lock.yaml"] + + return Effect.gen(function* () { + yield* enabled + + yield* run("rebase", "28") + + assert.include(printed.join("\n"), "2 files") + assert.include(printed.join("\n"), "src/cli/rebase.ts") + assert.include(printed.join("\n"), "pnpm-lock.yaml") + + const store = yield* Store.storeFor("rebases", Conflict) + assert.deepStrictEqual(yield* store.get(Store.prKey(repo, 28)), Option.some({ head, paths: unmerged })) + }).pipe(Effect.provide(machine({ spawned, behind: 3, conflicts: true, unmerged })), recording(printed)) + }) + it.effect("is off until the repository turns it on", () => { const spawned: Array = [] const printed: Array = [] diff --git a/src/cli/rebase.ts b/src/cli/rebase.ts index fd64fcb..26385f2 100644 --- a/src/cli/rebase.ts +++ b/src/cli/rebase.ts @@ -34,8 +34,10 @@ const allowed = (situation: Situation) => { * whether CI is running, and a rebase decided on that would cancel the run I am * waiting on. * - * A conflict is written down against the head it conflicted at, which puts the - * pull request in Needs me until the branch moves. Nothing half-finished is + * A conflict is written down against the head it conflicted at, with the files + * it stopped on, which puts the pull request in Needs me until the branch + * moves. The files are what makes it something to open: a conflict with no + * paths says only that one happened. Nothing half-finished is * left behind either way: the rebase aborts and the worktree it ran in goes * with the run. * @@ -75,11 +77,15 @@ export const rebase = Command.make( return } if (done._tag === "conflicted") { - yield* recordConflict(repo, number, view.headRefOid) + yield* recordConflict(repo, number, view.headRefOid, done.paths) yield* Console.log( `${where} ${short(view.headRefOid)} the rebase onto ${view.baseRefName} conflicted, ` + `so it was aborted and nothing was pushed.` ) + if (done.paths.length > 0) { + yield* Console.log(`It stopped on ${count(done.paths.length, "file")}:`) + yield* Effect.forEach(done.paths, (path) => Console.log(` ${path}`)) + } yield* Console.log("The next sweep puts it in Needs me, and it stays there until the branch moves.") return } diff --git a/src/domain/rebase.test.ts b/src/domain/rebase.test.ts index fa3d613..06b3eb5 100644 --- a/src/domain/rebase.test.ts +++ b/src/domain/rebase.test.ts @@ -1,7 +1,9 @@ import { assert, describe, it } from "@effect/vitest" +import { Effect, Option, Schema } from "effect" +import { layerTest, prKey, storeFor } from "#adapters/store.ts" import type { Branches, Situation } from "#domain/rebase.ts" -import { decide, stackOf } from "#domain/rebase.ts" +import { Conflict, conflictedAt, decide, recordConflict, stackOf } from "#domain/rebase.ts" const repo = "dominikwozniak/dw-mc" @@ -126,3 +128,35 @@ describe("decide", () => { assert.include(said, "off") }) }) + +describe("the record a conflicted rebase leaves", () => { + const head = "284d599022a55d4dcae74b31b9a49a0f50061014" + + it.effect("carries the files the rebase conflicted on beside the head", () => + Effect.gen(function* () { + yield* recordConflict(repo, 28, head, ["src/cli/rebase.ts", "pnpm-lock.yaml"]) + + const store = yield* storeFor("rebases", Conflict) + + assert.deepStrictEqual( + yield* store.get(prKey(repo, 28)), + Option.some({ head, paths: ["src/cli/rebase.ts", "pnpm-lock.yaml"] }) + ) + assert.strictEqual(yield* conflictedAt(repo, 28), head) + }).pipe(Effect.provide(layerTest)) + ) + + it.effect("reads a record an older version wrote, which has no paths at all", () => + Effect.gen(function* () { + // The record as an older version of the tool wrote it: a head and nothing + // else. + const older = yield* storeFor("rebases", Schema.Struct({ head: Schema.String })) + yield* older.set(prKey(repo, 28), { head }) + + // The head is what puts the pull request in Needs me, so a record without + // paths is worth less than one with them and still worth everything the + // bucket asks of it. + assert.strictEqual(yield* conflictedAt(repo, 28), head) + }).pipe(Effect.provide(layerTest)) + ) +}) diff --git a/src/domain/rebase.ts b/src/domain/rebase.ts index c59a9fc..769fac2 100644 --- a/src/domain/rebase.ts +++ b/src/domain/rebase.ts @@ -147,13 +147,24 @@ export const decide = (situation: Situation): string | null => { } /** - * A rebase that conflicted, and the head it conflicted at. + * A rebase that conflicted: the head it conflicted at and the files it stopped + * on. * - * The head is the whole record, as it is for a withdrawn stamp: a conflict is - * about the code the branch is at, so it lasts exactly as long as that code is - * what the pull request is. + * The head is what the record is scoped to, as it is for a withdrawn stamp: a + * conflict is about the code the branch is at, so it lasts exactly as long as + * that code is what the pull request is. A branch that moved is a branch + * nothing here has tried to rebase yet. + * + * The paths are what makes the conflict something to open: `a rebase + * conflicted` cannot tell a stale lockfile from half the pull request. They are + * an optional key rather than a required one so a record an older version wrote + * still reads, and a conflict with no paths still puts the pull request in + * Needs me. */ -export const Conflict = Schema.Struct({ head: Schema.String }) +export const Conflict = Schema.Struct({ + head: Schema.String, + paths: Schema.optionalKey(Schema.Array(Schema.String)) +}) export type Conflict = typeof Conflict.Type /** @@ -170,12 +181,13 @@ export const conflictedAt = Effect.fn("rebase.conflictedAt")(function* (repo: st return Option.match(conflict, { onNone: () => null, onSome: (it) => it.head }) }) -/** Writes down that a rebase of `head` conflicted, which is the only head it holds for. */ +/** Writes down that a rebase of `head` conflicted on `paths`, which is the only head it holds for. */ export const recordConflict = Effect.fn("rebase.recordConflict")(function* ( repo: string, number: number, - head: string + head: string, + paths: ReadonlyArray ) { const store = yield* storeFor("rebases", Conflict) - yield* store.set(prKey(repo, number), { head }) + yield* store.set(prKey(repo, number), { head, paths }) }) From 608b390c4a65d8f745a84a8f7fb3b22091e26cbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Wo=C5=BAniak?= Date: Thu, 17 Sep 2026 09:44:57 +0200 Subject: [PATCH 2/2] refactor(rebase): say a replay's outcome with a tag rather than a null The replay returned null where it went through and an array of paths where it stopped, so the call site read backwards: the array was the failure and null the success. It now returns the same tagged shape the rebase itself reports, which the caller hands straight on. Co-Authored-By: Claude Opus 5 (1M context) --- src/adapters/git.ts | 13 ++++++++----- src/cli/rebase.ts | 5 ++--- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/adapters/git.ts b/src/adapters/git.ts index 370b1dc..ea46942 100644 --- a/src/adapters/git.ts +++ b/src/adapters/git.ts @@ -226,6 +226,9 @@ const unmergedIn = Effect.fn("git.unmergedIn")(function* (directory: string) { return listed.split("\n").filter((line) => line !== "") }) +/** What replaying a branch's commits onto its base came to, inside the worktree. */ +type Replayed = { readonly _tag: "replayed" } | { readonly _tag: "conflicted"; readonly paths: ReadonlyArray } + /** * Rebases onto `base` in `directory`, or says which files the rebase conflicted * on. @@ -247,14 +250,14 @@ const unmergedIn = Effect.fn("git.unmergedIn")(function* (directory: string) { const replayOnto = Effect.fn("git.replayOnto")(function* (directory: string, base: string) { const rebased = yield* Effect.result(git(["-C", directory, "rebase", `refs/heads/${base}`])) if (Result.isSuccess(rebased)) { - return null + return { _tag: "replayed" } satisfies Replayed } const paths = yield* unmergedIn(directory) const aborted = yield* Effect.result(git(["-C", directory, "rebase", "--abort"])) if (Result.isFailure(aborted)) { return yield* rebased.failure } - return paths + return { _tag: "conflicted", paths } satisfies Replayed }) /** @@ -284,9 +287,9 @@ export const rebaseOnto = Effect.fn("git.rebaseOnto")(function* ( if (behind === 0) { return { _tag: "up-to-date" } satisfies Rebased } - const conflicted = yield* replayOnto(worktree.directory, base) - if (conflicted !== null) { - return { _tag: "conflicted", paths: conflicted } satisfies Rebased + const replayed = yield* replayOnto(worktree.directory, base) + if (replayed._tag === "conflicted") { + return replayed satisfies Rebased } const after = yield* git(["-C", worktree.directory, "rev-parse", "HEAD"]) diff --git a/src/cli/rebase.ts b/src/cli/rebase.ts index 26385f2..d69611b 100644 --- a/src/cli/rebase.ts +++ b/src/cli/rebase.ts @@ -37,9 +37,8 @@ const allowed = (situation: Situation) => { * A conflict is written down against the head it conflicted at, with the files * it stopped on, which puts the pull request in Needs me until the branch * moves. The files are what makes it something to open: a conflict with no - * paths says only that one happened. Nothing half-finished is - * left behind either way: the rebase aborts and the worktree it ran in goes - * with the run. + * paths says only that one happened. Nothing half-finished is left behind + * either way: the rebase aborts and the worktree it ran in goes with the run. * * A stack is recognised and never driven. The tool does not understand stacks, * so what it has to say about one is where the pull request sits in it.