8000 feat(rebase): record what a rebase conflict hit by dominikwozniak · Pull Request #40 · dominikwozniak/dw-mc · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions src/adapters/git.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> | undefined
}) =>
layerFake((command) => {
if (command._tag !== "StandardCommand") {
Expand Down Expand Up @@ -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") }))
Expand Down Expand Up 8000 @@ -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<string> = []
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", () => {
Expand Down
35 changes: 29 additions & 6 deletions src/adapters/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> }
| { 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. */
Expand All @@ -214,7 +214,24 @@ 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 !== "")
})

/** 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<string> }

/**
* 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
Expand All @@ -223,19 +240,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 { _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 false
return { _tag: "conflicted", paths } satisfies Replayed
})

/**
Expand Down Expand Up @@ -265,8 +287,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 replayed = yield* replayOnto(worktree.directory, base)
if (replayed._tag === "conflicted") {
return replayed satisfies Rebased
}

const after = yield* git(["-C", worktree.directory, "rev-parse", "HEAD"])
Expand Down
28 changes: 26 additions & 2 deletions src/cli/rebase.test.ts
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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<string> | undefined
/** Who opened the pull request, where it was not me. */
readonly author?: string | undefined
/** Whether the head branch lives in a fork. */
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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<string> = []
const printed: Array<string> = []
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<string> = []
const printed: Array<string> = []
Expand Down
15 changes: 10 additions & 5 deletions src/cli/rebase.ts