TypeScript
fix(github): Rebase stack no longer conflicts after a lower layer is squash-merged (#29919)
Alex Southwell30724e2e9ae1
diff --git a/packages/source-control-github/src/server/githubStackActions.test.ts b/packages/source-control-github/src/server/githubStackActions.test.tsindex 3d95638bc48..e6559d05195 100644--- a/packages/source-control-github/src/server/githubStackActions.test.ts+++ b/packages/source-control-github/src/server/githubStackActions.test.ts@@ -341,7 +341,7 @@ it.effect("rebases the open layers bottom to top in a scratch clone, each onto texpect(api.calls.some(isMutation)).toBe(false);const rebases = gitCalls.filter((args) => args[0] === "rebase");expect(rebases).toEqual([- ["rebase", "--quiet", "--onto", "origin/main", "new-sha"],+ ["rebase", "--quiet", "--onto", "origin/main", "aaa"],["rebase", "--quiet", "--onto", "new-sha", "bbb"],]);expect(gitCalls.filter((args) => args[0] === "push")).toEqual([diff --git a/packages/source-control-github/src/server/githubStackActions.ts b/packages/source-control-github/src/server/githubStackActions.tsindex f3997a45642..fb524ae0518 100644--- a/packages/source-control-github/src/server/githubStackActions.ts+++ b/packages/source-control-github/src/server/githubStackActions.ts@@ -201,6 +201,7 @@ export const runGitHubStackAction = Effect.fn("runGitHubStackAction")(function*}))return yield* new GitHubStackPermissionError({ ...identity });+ const mergedHeadSha = stack.layers[stack.layers.indexOf(open[0]!) - 1]?.headSha;// GitHub has no stack-rebase API; per-PR updates replay lower layers' old commits.yield* cascadeRebaseStack({host: input.host,@@ -211,6 +212,7 @@ export const runGitHubStackAction = Effect.fn("runGitHubStackAction")(function*headBranch: layer.headBranch,headSha: layer.headSha!,})),+ ...(mergedHeadSha === undefined ? {} : { mergedHeadSha }),}).pipe(Effect.mapError((cause) =>cause._tag === "GitHubStackRebaseConflictError" ||diff --git a/packages/source-control-github/src/server/githubStackRebase.test.ts b/packages/source-control-github/src/server/githubStackRebase.test.tsindex aa04c617447..8bf4662a49e 100644--- a/packages/source-control-github/src/server/githubStackRebase.test.ts+++ b/packages/source-control-github/src/server/githubStackRebase.test.ts@@ -92,6 +92,59 @@ it.layer(layer)("cascadeRebaseStack", (it) => {}).pipe(Effect.scoped),);+ it.effect("drops a squash-merged lower layer's commits from the layer above it", () =>+ Effect.gen(function* () {+ const repo = yield* setup;+ const fs = yield* FileSystem.FileSystem;+ const { process } = yield* SourceControlHost.SourceControlHost;+ const scratch = yield* fs.makeTempDirectoryScoped({ prefix: "t3-cascade-squash-" });+ const run = (...args: string[]) =>+ process+ .run({+ operation: "test",+ command: "git",+ args,+ cwd: scratch,+ env: {+ GIT_AUTHOR_NAME: "t",+ GIT_AUTHOR_EMAIL: "t@t",+ GIT_COMMITTER_NAME: "t",+ GIT_COMMITTER_EMAIL: "t@t",+ },+ })+ .pipe(Effect.map((out) => out.stdout.trim()));+ const write = (text: string) => fs.writeFileString(`${scratch}/index.ts`, text);+ yield* run("clone", "--quiet", repo.remote, ".");+ yield* run("switch", "--quiet", "-c", "feat/two-steps");+ yield* write("export const answer = 40;\n");+ yield* run("commit", "--quiet", "-am", "A1");+ yield* write("export const answer = 42;\n");+ yield* run("commit", "--quiet", "-am", "A2");+ const mergedHeadSha = yield* run("rev-parse", "HEAD");+ yield* run("switch", "--quiet", "-c", "feat/on-top");+ yield* write("/** The answer. */\nexport const answer = 42;\n");+ yield* run("commit", "--quiet", "-am", "B");+ const headSha = yield* run("rev-parse", "HEAD");+ yield* run("switch", "--quiet", "main");+ yield* run("merge", "--quiet", "--squash", "feat/two-steps");+ yield* run("commit", "--quiet", "-m", "A (#1)");+ yield* run("push", "--quiet", "origin", "main", "feat/on-top");++ const completed = yield* cascadeRebaseStack({+ host: "github.com",+ repository: "acme/web",+ base: "main",+ layers: [{ number: 2, headBranch: "feat/on-top", headSha }],+ mergedHeadSha,+ remote: repo.remote,+ });+ assert.strictEqual(completed, 1);+ const top = yield* repo.head("feat/on-top");+ expect(yield* repo.git("rev-parse", `${top}~1`)).toBe(yield* repo.head("main"));+ expect(yield* repo.git("log", "--format=%s", `main..${top}`)).toBe("B");+ }).pipe(Effect.scoped),+ );+it.effect("refuses to overwrite a layer pushed after it was reviewed", () =>Effect.gen(function* () {const repo = yield* setup;diff --git a/packages/source-control-github/src/server/githubStackRebase.ts b/packages/source-control-github/src/server/githubStackRebase.tsindex 27595e7e770..e75b5e880b0 100644--- a/packages/source-control-github/src/server/githubStackRebase.ts+++ b/packages/source-control-github/src/server/githubStackRebase.ts@@ -40,6 +40,8 @@ export const cascadeRebaseStack = Effect.fn("cascadeRebaseStack")(function* (inpreadonly repository: string;readonly base: string;readonly layers: ReadonlyArray<CascadeLayer>;+ /** Last head of the merged layer directly below the bottom open layer. */+ readonly mergedHeadSha?: string;readonly remote?: string;}) {const appAccess = yield* GitHubAppAccess.GitHubAppAccess;@@ -103,13 +105,22 @@ export const cascadeRebaseStack = Effect.fn("cascadeRebaseStack")(function* (inplet parentOld = `origin/${input.base}`;let parentNew = `origin/${input.base}`;+ // A squash or rebase merge leaves the merged layer's commits outside the base's history.+ const mergedParent =+ input.mergedHeadSha !== undefined &&+ (yield* git(["merge-base", "--is-ancestor", input.mergedHeadSha, first.headSha], true).pipe(+ Effect.mapError(failed("reading the fork point", first.number, 0)),+ )).exitCode === 0+ ? input.mergedHeadSha+ : undefined;for (const [index, layer] of input.layers.entries()) {- // Exclude old parents: the base fork point for the bottom, the reviewed lower head above it.+ // Exclude old parents: the merged layer or fork point for the bottom, the reviewed head above.const upstream =index === 0- ? (yield* git(["merge-base", parentOld, layer.headSha]).pipe(+ ? (mergedParent ??+ (yield* git(["merge-base", parentOld, layer.headSha]).pipe(Effect.mapError(failed("reading the fork point", layer.number, index)),- )).stdout.trim()+ )).stdout.trim()): parentOld;yield* git(["checkout", "--quiet", "--detach", layer.headSha]).pipe(Effect.mapError(failed("checking out", layer.number, index)),