Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, expect, it } from "@effect/vitest";
3import * as Effect from "effect/Effect";
4import * as FileSystem from "effect/FileSystem";
5import * as Layer from "effect/Layer";
6import * as Redacted from "effect/Redacted";
7
8import * as GitHubApi from "../sourceControl/GitHubApi.ts";
9import * as VcsProcess from "../vcs/VcsProcess.ts";
10import { cascadeRebaseStack } from "./githubStackRebase.ts";
11
12const layer = Layer.mergeAll(
13 Layer.mock(GitHubApi.GitHubApi)({
14 credential: () => Effect.succeed({ token: Redacted.make("token"), fingerprint: "fp" }),
15 }),
16 VcsProcess.layer,
17).pipe(Layer.provideMerge(NodeServices.layer));
18
19/**
20 * A bare "GitHub" holding the live-run stack: `main` moved ahead after the stack was cut, and a
21 * second layer sits on the first. Returns each branch's head.
22 */
23const setup = Effect.gen(function* () {
24 const fs = yield* FileSystem.FileSystem;
25 const process = yield* VcsProcess.VcsProcess;
26 const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-cascade-test-" });
27 const remote = `${root}/remote.git`;
28 const work = `${root}/work`;
29 const git = (cwd: string, ...args: string[]) =>
30 process
31 .run({
32 operation: "test",
33 command: "git",
34 args,
35 cwd,
36 env: {
37 GIT_AUTHOR_NAME: "t",
38 GIT_AUTHOR_EMAIL: "t@t",
39 GIT_COMMITTER_NAME: "t",
40 GIT_COMMITTER_EMAIL: "t@t",
41 },
42 })
43 .pipe(Effect.map((out) => out.stdout.trim()));
44 const write = (path: string, text: string) => fs.writeFileString(`${work}/${path}`, text);
45 yield* git(root, "init", "--quiet", "--bare", "-b", "main", remote);
46 yield* git(root, "clone", "--quiet", remote, work);
47 yield* write("index.ts", "export const answer = 41;\n");
48 yield* git(work, "add", "-A");
49 yield* git(work, "commit", "--quiet", "-m", "Initial");
50 yield* git(work, "switch", "--quiet", "-c", "feat/answer-42");
51 yield* write("index.ts", "export const answer = 42;\n");
52 yield* git(work, "commit", "--quiet", "-am", "A: set the answer to 42");
53 yield* git(work, "switch", "--quiet", "-c", "feat/answer-doc");
54 yield* write("index.ts", "/** The answer. */\nexport const answer = 42;\n");
55 yield* git(work, "commit", "--quiet", "-am", "B: document the answer");
56 yield* git(work, "switch", "--quiet", "main");
57 yield* write("README.md", "main moved ahead\n");
58 yield* git(work, "add", "-A");
59 yield* git(work, "commit", "--quiet", "-m", "Docs");
60 yield* git(work, "push", "--quiet", "origin", "main", "feat/answer-42", "feat/answer-doc");
61 const head = (branch: string) => git(remote, "rev-parse", `refs/heads/${branch}`);
62 return {
63 remote,
64 head,
65 git: (...args: string[]) => git(remote, ...args),
66 layers: [
67 { number: 1, headBranch: "feat/answer-42", headSha: yield* head("feat/answer-42") },
68 { number: 2, headBranch: "feat/answer-doc", headSha: yield* head("feat/answer-doc") },
69 ],
70 };
71});
72
73it.layer(layer)("cascadeRebaseStack", (it) => {
74 it.effect("moves each layer's own commits onto the rebased layer below it", () =>
75 Effect.gen(function* () {
76 const repo = yield* setup;
77 const completed = yield* cascadeRebaseStack({
78 host: "github.com",
79 repository: "acme/web",
80 base: "main",
81 layers: repo.layers,
82 remote: repo.remote,
83 });
84 assert.strictEqual(completed, 2);
85
86 const main = yield* repo.head("main");
87 const bottom = yield* repo.head("feat/answer-42");
88 const top = yield* repo.head("feat/answer-doc");
89 // Bottom sits on the new main; top sits on the new bottom with only its own commit.
90 expect(yield* repo.git("rev-parse", `${bottom}~1`)).toBe(main);
91 expect(yield* repo.git("rev-parse", `${top}~1`)).toBe(bottom);
92 expect(yield* repo.git("log", "--format=%s", `${bottom}..${top}`)).toBe(
93 "B: document the answer",
94 );
95 }).pipe(Effect.scoped),
96 );
97
98 it.effect("refuses to overwrite a layer pushed after it was reviewed", () =>
99 Effect.gen(function* () {
100 const repo = yield* setup;
101 const stale = [
102 repo.layers[0]!,
103 // Reviewed at the bottom layer's head, but the branch is really at its own commit.
104 { ...repo.layers[1]!, headSha: repo.layers[0]!.headSha },
105 ];
106 const error = yield* Effect.flip(
107 cascadeRebaseStack({
108 host: "github.com",
109 repository: "acme/web",
110 base: "main",
111 layers: stale,
112 remote: repo.remote,
113 }),
114 );
115 expect(error).toMatchObject({ _tag: "GitHubStackRebaseGitError", number: 2, completed: 1 });
116 expect(yield* repo.head("feat/answer-doc")).toBe(repo.layers[1]!.headSha);
117 }).pipe(Effect.scoped),
118 );
119
120 it.effect("stops at a conflicting layer and leaves it untouched", () =>
121 Effect.gen(function* () {
122 const repo = yield* setup;
123 // Make main conflict with the bottom layer's change to the same line.
124 const fs = yield* FileSystem.FileSystem;
125 const process = yield* VcsProcess.VcsProcess;
126 const scratch = yield* fs.makeTempDirectoryScoped({ prefix: "t3-cascade-conflict-" });
127 const run = (...args: string[]) =>
128 process.run({
129 operation: "test",
130 command: "git",
131 args,
132 cwd: scratch,
133 env: {
134 GIT_AUTHOR_NAME: "t",
135 GIT_AUTHOR_EMAIL: "t@t",
136 GIT_COMMITTER_NAME: "t",
137 GIT_COMMITTER_EMAIL: "t@t",
138 },
139 });
140 yield* run("clone", "--quiet", repo.remote, ".");
141 yield* fs.writeFileString(`${scratch}/index.ts`, "export const answer = 43;\n");
142 yield* run("commit", "--quiet", "-am", "Conflict");
143 yield* run("push", "--quiet", "origin", "main");
144
145 const error = yield* Effect.flip(
146 cascadeRebaseStack({
147 host: "github.com",
148 repository: "acme/web",
149 base: "main",
150 layers: repo.layers,
151 remote: repo.remote,
152 }),
153 );
154 expect(error).toMatchObject({
155 _tag: "GitHubStackRebaseConflictError",
156 number: 1,
157 completed: 0,
158 });
159 expect(yield* repo.head("feat/answer-42")).toBe(repo.layers[0]!.headSha);
160 }).pipe(Effect.scoped),
161 );
162});