The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import * as Effect from "effect/Effect"; |
| 2 | import * as FileSystem from "effect/FileSystem"; |
| 3 | import * as Redacted from "effect/Redacted"; |
| 4 | import * as Schema from "effect/Schema"; |
| 5 | import * as Base64 from "effect/encoding/Base64"; |
| 6 | |
| 7 | import * as GitHubAppAccess from "./GitHubAppAccess.ts"; |
| 8 | import * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost"; |
| 9 | |
| 10 | export interface CascadeLayer { |
| 11 | readonly number: number; |
| 12 | readonly headBranch: string; |
| 13 | readonly headSha: string; |
| 14 | } |
| 15 | |
| 16 | export class GitHubStackRebaseConflictError extends Schema.TaggedError<GitHubStackRebaseConflictError>()( |
| 17 | "GitHubStackRebaseConflictError", |
| 18 | { number: Schema.Int, completed: Schema.Int }, |
| 19 | ) { |
| 20 | override get message(): string { |
| 21 | return `PR #${this.number} conflicts with the layer below it. ${this.completed} layers were rebased; resolve #${this.number} with \`gh stack rebase\` and push.`; |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | export class GitHubStackRebaseGitError extends Schema.TaggedError<GitHubStackRebaseGitError>()( |
| 26 | "GitHubStackRebaseGitError", |
| 27 | { step: Schema.String, number: Schema.Int, completed: Schema.Int, cause: Schema.Defect() }, |
| 28 | ) { |
| 29 | override get message(): string { |
| 30 | return `Stack rebase stopped at PR #${this.number} while ${this.step}. ${this.completed} layers were rebased.`; |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | function remoteUrl(host: string, repository: string): string { |
| 35 | return `https://${host.trim().toLowerCase()}/${repository}.git`; |
| 36 | } |
| 37 | |
| 38 | export const cascadeRebaseStack = Effect.fn("cascadeRebaseStack")(function* (input: { |
| 39 | readonly host: string; |
| 40 | readonly repository: string; |
| 41 | readonly base: string; |
| 42 | readonly layers: ReadonlyArray<CascadeLayer>; |
| 43 | /** Last head of the merged layer directly below the bottom open layer. */ |
| 44 | readonly mergedHeadSha?: string; |
| 45 | readonly remote?: string; |
| 46 | }) { |
| 47 | const appAccess = yield* GitHubAppAccess.GitHubAppAccess; |
| 48 | const { process } = yield* SourceControlHost.SourceControlHost; |
| 49 | const fileSystem = yield* FileSystem.FileSystem; |
| 50 | // A push takes one credential up front, so it must be one that reaches the repository. |
| 51 | const { token } = yield* appAccess.credentialFor(input.host, input.repository); |
| 52 | const first = input.layers[0]; |
| 53 | if (first === undefined) return 0; |
| 54 | const directory = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-stack-rebase-" }).pipe( |
| 55 | Effect.mapError( |
| 56 | (cause) => |
| 57 | new GitHubStackRebaseGitError({ |
| 58 | step: "preparing", |
| 59 | number: first.number, |
| 60 | completed: 0, |
| 61 | cause, |
| 62 | }), |
| 63 | ), |
| 64 | ); |
| 65 | // Keep the token in this process's header, outside URLs, argv and git config. |
| 66 | const remote = input.remote ?? remoteUrl(input.host, input.repository); |
| 67 | const authorization = `AUTHORIZATION: basic ${Base64.encode(`x-access-token:${Redacted.value(token)}`)}`; |
| 68 | const env = { |
| 69 | GIT_TERMINAL_PROMPT: "0", |
| 70 | GIT_CONFIG_COUNT: "3", |
| 71 | GIT_CONFIG_KEY_0: `http.${remote}.extraheader`, |
| 72 | GIT_CONFIG_VALUE_0: authorization, |
| 73 | GIT_CONFIG_KEY_1: "user.name", |
| 74 | GIT_CONFIG_VALUE_1: "T3 Code", |
| 75 | GIT_CONFIG_KEY_2: "user.email", |
| 76 | GIT_CONFIG_VALUE_2: "noreply@t3.codes", |
| 77 | }; |
| 78 | const git = (args: ReadonlyArray<string>, allowNonZeroExit = false) => |
| 79 | process.run({ |
| 80 | operation: "cascadeRebaseStack", |
| 81 | command: "git", |
| 82 | args, |
| 83 | cwd: directory, |
| 84 | env, |
| 85 | timeoutMs: 120_000, |
| 86 | ...(allowNonZeroExit ? { allowNonZeroExit: true } : {}), |
| 87 | }); |
| 88 | const failed = (step: string, number: number, completed: number) => (cause: unknown) => |
| 89 | new GitHubStackRebaseGitError({ step, number, completed, cause }); |
| 90 | |
| 91 | yield* git(["init", "--quiet"]).pipe(Effect.mapError(failed("preparing", first.number, 0))); |
| 92 | yield* git(["remote", "add", "origin", remote]).pipe( |
| 93 | Effect.mapError(failed("preparing", first.number, 0)), |
| 94 | ); |
| 95 | yield* git([ |
| 96 | "fetch", |
| 97 | "--quiet", |
| 98 | "--no-tags", |
| 99 | "origin", |
| 100 | `+refs/heads/${input.base}:refs/remotes/origin/${input.base}`, |
| 101 | ...input.layers.map( |
| 102 | (layer) => `+refs/heads/${layer.headBranch}:refs/remotes/origin/${layer.headBranch}`, |
| 103 | ), |
| 104 | ]).pipe(Effect.mapError(failed("fetching", first.number, 0))); |
| 105 | |
| 106 | let parentOld = `origin/${input.base}`; |
| 107 | let parentNew = `origin/${input.base}`; |
| 108 | // A squash or rebase merge leaves the merged layer's commits outside the base's history. |
| 109 | const mergedParent = |
| 110 | input.mergedHeadSha !== undefined && |
| 111 | (yield* git(["merge-base", "--is-ancestor", input.mergedHeadSha, first.headSha], true).pipe( |
| 112 | Effect.mapError(failed("reading the fork point", first.number, 0)), |
| 113 | )).exitCode === 0 |
| 114 | ? input.mergedHeadSha |
| 115 | : undefined; |
| 116 | for (const [index, layer] of input.layers.entries()) { |
| 117 | // Exclude old parents: the merged layer or fork point for the bottom, the reviewed head above. |
| 118 | const upstream = |
| 119 | index === 0 |
| 120 | ? (mergedParent ?? |
| 121 | (yield* git(["merge-base", parentOld, layer.headSha]).pipe( |
| 122 | Effect.mapError(failed("reading the fork point", layer.number, index)), |
| 123 | )).stdout.trim()) |
| 124 | : parentOld; |
| 125 | yield* git(["checkout", "--quiet", "--detach", layer.headSha]).pipe( |
| 126 | Effect.mapError(failed("checking out", layer.number, index)), |
| 127 | ); |
| 128 | const rebase = yield* git(["rebase", "--quiet", "--onto", parentNew, upstream], true).pipe( |
| 129 | Effect.mapError(failed("rebasing", layer.number, index)), |
| 130 | ); |
| 131 | if (rebase.exitCode !== 0) { |
| 132 | yield* git(["rebase", "--abort"], true).pipe(Effect.ignore); |
| 133 | return yield* new GitHubStackRebaseConflictError({ number: layer.number, completed: index }); |
| 134 | } |
| 135 | const rebased = (yield* git(["rev-parse", "HEAD"]).pipe( |
| 136 | Effect.mapError(failed("rebasing", layer.number, index)), |
| 137 | )).stdout.trim(); |
| 138 | if (rebased !== layer.headSha) { |
| 139 | yield* git([ |
| 140 | "push", |
| 141 | "--quiet", |
| 142 | `--force-with-lease=refs/heads/${layer.headBranch}:${layer.headSha}`, |
| 143 | "origin", |
| 144 | `${rebased}:refs/heads/${layer.headBranch}`, |
| 145 | ]).pipe(Effect.mapError(failed("pushing", layer.number, index))); |
| 146 | } |
| 147 | parentOld = layer.headSha; |
| 148 | parentNew = rebased; |
| 149 | } |
| 150 | return input.layers.length; |
| 151 | }, Effect.scoped); |