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 SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost";
9import * as TestSourceControlHost from "@t3tools/source-control-testing/TestSourceControlHost";
10
11import * as GitHubAppAccess from "./GitHubAppAccess.ts";
12import { cascadeRebaseStack } from "./githubStackRebase.ts";
13
14const layer = Layer.mergeAll(
15 Layer.mock(GitHubAppAccess.GitHubAppAccess)({
16 credentialFor: () => Effect.succeed({ token: Redacted.make("token"), fingerprint: "fp" }),
17 }),
18 TestSourceControlHost.layerWithRealGit(),
19).pipe(Layer.provideMerge(NodeServices.layer));
20
21const setup = Effect.gen(function* () {
22 const fs = yield* FileSystem.FileSystem;
23 const { process } = yield* SourceControlHost.SourceControlHost;
24 const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-cascade-test-" });
25 const remote = `${root}/remote.git`;
26 const work = `${root}/work`;
27 const git = (cwd: string, ...args: string[]) =>
28 process
29 .run({
30 operation: "test",
31 command: "git",
32 args,
33 cwd,
34 env: {
35 GIT_AUTHOR_NAME: "t",
36 GIT_AUTHOR_EMAIL: "t@t",
37 GIT_COMMITTER_NAME: "t",
38 GIT_COMMITTER_EMAIL: "t@t",
39 },
40 })
41 .pipe(Effect.map((out) => out.stdout.trim()));
42 const write = (path: string, text: string) => fs.writeFileString(`${work}/${path}`, text);
43 yield* git(root, "init", "--quiet", "--bare", "-b", "main", remote);
44 yield* git(root, "clone", "--quiet", remote, work);
45 yield* write("index.ts", "export const answer = 41;\n");
46 yield* git(work, "add", "-A");
47 yield* git(work, "commit", "--quiet", "-m", "Initial");
48 yield* git(work, "switch", "--quiet", "-c", "feat/answer-42");
49 yield* write("index.ts", "export const answer = 42;\n");
50 yield* git(work, "commit", "--quiet", "-am", "A: set the answer to 42");
51 yield* git(work, "switch", "--quiet", "-c", "feat/answer-doc");
52 yield* write("index.ts", "/** The answer. */\nexport const answer = 42;\n");
53 yield* git(work, "commit", "--quiet", "-am", "B: document the answer");
54 yield* git(work, "switch", "--quiet", "main");
55 yield* write("README.md", "main moved ahead\n");
56 yield* git(work, "add", "-A");
57 yield* git(work, "commit", "--quiet", "-m", "Docs");
58 yield* git(work, "push", "--quiet", "origin", "main", "feat/answer-42", "feat/answer-doc");
59 const head = (branch: string) => git(remote, "--git-dir=.", "rev-parse", `refs/heads/${branch}`);
60 return {
61 remote,
62 head,
63 git: (...args: string[]) => git(remote, "--git-dir=.", ...args),
64 layers: [
65 { number: 1, headBranch: "feat/answer-42", headSha: yield* head("feat/answer-42") },
66 { number: 2, headBranch: "feat/answer-doc", headSha: yield* head("feat/answer-doc") },
67 ],
68 };
69});
70
71it.layer(layer)("cascadeRebaseStack", (it) => {
72 it.effect("moves each layer's own commits onto the rebased layer below it", () =>
73 Effect.gen(function* () {
74 const repo = yield* setup;
75 const completed = yield* cascadeRebaseStack({
76 cwd: "/repo",
77 host: "github.com",
78 repository: "acme/web",
79 base: "main",
80 layers: repo.layers,
81 remote: repo.remote,
82 });
83 assert.strictEqual(completed, 2);
84
85 const main = yield* repo.head("main");
86 const bottom = yield* repo.head("feat/answer-42");
87 const top = yield* repo.head("feat/answer-doc");
88 expect(yield* repo.git("rev-parse", `${bottom}~1`)).toBe(main);
89 expect(yield* repo.git("rev-parse", `${top}~1`)).toBe(bottom);
90 expect(yield* repo.git("log", "--format=%s", `${bottom}..${top}`)).toBe(
91 "B: document the answer",
92 );
93 }).pipe(Effect.scoped),
94 );
95
96 it.effect("drops a squash-merged lower layer's commits from the layer above it", () =>
97 Effect.gen(function* () {
98 const repo = yield* setup;
99 const fs = yield* FileSystem.FileSystem;
100 const { process } = yield* SourceControlHost.SourceControlHost;
101 const scratch = yield* fs.makeTempDirectoryScoped({ prefix: "t3-cascade-squash-" });
102 const run = (...args: string[]) =>
103 process
104 .run({
105 operation: "test",
106 command: "git",
107 args,
108 cwd: scratch,
109 env: {
110 GIT_AUTHOR_NAME: "t",
111 GIT_AUTHOR_EMAIL: "t@t",
112 GIT_COMMITTER_NAME: "t",
113 GIT_COMMITTER_EMAIL: "t@t",
114 },
115 })
116 .pipe(Effect.map((out) => out.stdout.trim()));
117 const write = (text: string) => fs.writeFileString(`${scratch}/index.ts`, text);
118 yield* run("clone", "--quiet", repo.remote, ".");
119 yield* run("switch", "--quiet", "-c", "feat/two-steps");
120 yield* write("export const answer = 40;\n");
121 yield* run("commit", "--quiet", "-am", "A1");
122 yield* write("export const answer = 42;\n");
123 yield* run("commit", "--quiet", "-am", "A2");
124 const mergedHeadSha = yield* run("rev-parse", "HEAD");
125 yield* run("switch", "--quiet", "-c", "feat/on-top");
126 yield* write("/** The answer. */\nexport const answer = 42;\n");
127 yield* run("commit", "--quiet", "-am", "B");
128 const headSha = yield* run("rev-parse", "HEAD");
129 yield* run("switch", "--quiet", "main");
130 yield* run("merge", "--quiet", "--squash", "feat/two-steps");
131 yield* run("commit", "--quiet", "-m", "A (#1)");
132 yield* run("push", "--quiet", "origin", "main", "feat/on-top");
133
134 const completed = yield* cascadeRebaseStack({
135 cwd: "/repo",
136 host: "github.com",
137 repository: "acme/web",
138 base: "main",
139 layers: [{ number: 2, headBranch: "feat/on-top", headSha }],
140 mergedHeadSha,
141 remote: repo.remote,
142 });
143 assert.strictEqual(completed, 1);
144 const top = yield* repo.head("feat/on-top");
145 expect(yield* repo.git("rev-parse", `${top}~1`)).toBe(yield* repo.head("main"));
146 expect(yield* repo.git("log", "--format=%s", `main..${top}`)).toBe("B");
147 }).pipe(Effect.scoped),
148 );
149
150 it.effect("refuses to overwrite a layer pushed after it was reviewed", () =>
151 Effect.gen(function* () {
152 const repo = yield* setup;
153 const stale = [repo.layers[0]!, { ...repo.layers[1]!, headSha: repo.layers[0]!.headSha }];
154 const error = yield* Effect.flip(
155 cascadeRebaseStack({
156 cwd: "/repo",
157 host: "github.com",
158 repository: "acme/web",
159 base: "main",
160 layers: stale,
161 remote: repo.remote,
162 }),
163 );
164 expect(error).toMatchObject({ _tag: "GitHubStackRebaseGitError", number: 2, completed: 1 });
165 expect(yield* repo.head("feat/answer-doc")).toBe(repo.layers[1]!.headSha);
166 }).pipe(Effect.scoped),
167 );
168
169 it.effect("stops at a conflicting layer and leaves it untouched", () =>
170 Effect.gen(function* () {
171 const repo = yield* setup;
172 const fs = yield* FileSystem.FileSystem;
173 const { process } = yield* SourceControlHost.SourceControlHost;
174 const scratch = yield* fs.makeTempDirectoryScoped({ prefix: "t3-cascade-conflict-" });
175 const run = (...args: string[]) =>
176 process.run({
177 operation: "test",
178 command: "git",
179 args,
180 cwd: scratch,
181 env: {
182 GIT_AUTHOR_NAME: "t",
183 GIT_AUTHOR_EMAIL: "t@t",
184 GIT_COMMITTER_NAME: "t",
185 GIT_COMMITTER_EMAIL: "t@t",
186 },
187 });
188 yield* run("clone", "--quiet", repo.remote, ".");
189 yield* fs.writeFileString(`${scratch}/index.ts`, "export const answer = 43;\n");
190 yield* run("commit", "--quiet", "-am", "Conflict");
191 yield* run("push", "--quiet", "origin", "main");
192
193 const error = yield* Effect.flip(
194 cascadeRebaseStack({
195 cwd: "/repo",
196 host: "github.com",
197 repository: "acme/web",
198 base: "main",
199 layers: repo.layers,
200 remote: repo.remote,
201 }),
202 );
203 expect(error).toMatchObject({
204 _tag: "GitHubStackRebaseConflictError",
205 number: 1,
206 completed: 0,
207 });
208 expect(yield* repo.head("feat/answer-42")).toBe(repo.layers[0]!.headSha);
209 }).pipe(Effect.scoped),
210 );
211});