Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, it } from "@effect/vitest";
3import * as Effect from "effect/Effect";
4import * as FileSystem from "effect/FileSystem";
5import * as Layer from "effect/Layer";
6import * as Path from "effect/Path";
7import * as Sink from "effect/Sink";
8import * as Stream from "effect/Stream";
9import { ChildProcess, ChildProcessSpawner } from "effect/process";
10
11import * as ProcessRunner from "../processRunner.ts";
12import { makeIsWorktreeContainer } from "./worktreeContainer.ts";
13
14const git = Effect.fn("worktreeContainerTest.git")(function* (
15 cwd: string,
16 ...args: ReadonlyArray<string>
17) {
18 const processRunner = yield* ProcessRunner.ProcessRunner;
19 return yield* processRunner.run({
20 command: "git",
21 args: ["-C", cwd, "-c", "user.email=t@t", "-c", "user.name=t", ...args],
22 });
23});
24
25it.layer(ProcessRunner.layer.pipe(Layer.provideMerge(NodeServices.layer)))(
26 "isWorktreeContainer",
27 (it) => {
28 it.effect("is true only for a folder of worktrees around metadata that is no checkout", () =>
29 Effect.gen(function* () {
30 const fileSystem = yield* FileSystem.FileSystem;
31 const path = yield* Path.Path;
32 const isWorktreeContainer = yield* makeIsWorktreeContainer;
33 const fixture = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-container-test-" });
34
35 const source = path.join(fixture, "source");
36 yield* fileSystem.makeDirectory(source);
37 yield* git(source, "init", "-b", "develop");
38 yield* fileSystem.writeFileString(path.join(source, "README.md"), "readme\n");
39 yield* git(source, "add", "README.md");
40 yield* git(source, "commit", "-m", "init");
41
42 const makeContainer = Effect.fn(function* (
43 name: string,
44 coreBare: boolean,
45 metadata = ".bare",
46 ) {
47 const root = path.join(fixture, name);
48 yield* fileSystem.makeDirectory(root);
49 yield* git(fixture, "clone", "--bare", source, path.join(root, metadata));
50 yield* fileSystem.writeFileString(path.join(root, ".git"), `gitdir: ./${metadata}\n`);
51 yield* git(root, "worktree", "add", "develop", "develop");
52 yield* git(root, "config", "core.bare", String(coreBare));
53 return root;
54 });
55 const makeSeparateGitDirCheckout = Effect.fn(function* (name: string) {
56 const root = path.join(fixture, name);
57 yield* fileSystem.makeDirectory(root);
58 yield* git(root, "init", "-b", "develop", "--separate-git-dir", ".metadata", ".");
59 return root;
60 });
61
62 const container = yield* makeContainer("container", false, ".bare\u00a0");
63 assert.isTrue(
64 yield* isWorktreeContainer(container),
65 "container with whitespace in metadata path",
66 );
67 assert.isFalse(yield* isWorktreeContainer(path.join(container, "develop")), "its worktree");
68
69 const linked = path.join(fixture, "link");
70 yield* fileSystem.symlink(container, linked);
71 assert.isTrue(yield* isWorktreeContainer(linked), "container through a symlink");
72
73 // The issue's steps leave core.bare set; Initialize Git would clear it later.
74 const bare = yield* makeContainer("bare", true);
75 assert.isTrue(yield* isWorktreeContainer(bare), "core.bare container");
76
77 // Git allows an orphan worktree before the first commit: no index, yet a valid checkout.
78 const fresh = yield* makeSeparateGitDirCheckout("fresh");
79 yield* git(fresh, "worktree", "add", "--orphan", "-b", "feature", "../fresh-feature");
80 assert.isFalse(yield* isWorktreeContainer(fresh), "fresh checkout with an orphan worktree");
81
82 const checkout = yield* makeSeparateGitDirCheckout("checkout");
83 yield* fileSystem.writeFileString(path.join(checkout, "README.md"), "readme\n");
84 yield* git(checkout, "add", "README.md");
85 yield* git(checkout, "commit", "-m", "init");
86 yield* git(checkout, "worktree", "add", "worktrees/feature", "-b", "feature");
87 assert.isFalse(yield* isWorktreeContainer(checkout), "checkout with a nested worktree");
88
89 const outside = path.join(fixture, "outside");
90 yield* git(
91 fixture,
92 "clone",
93 "--no-checkout",
94 "--separate-git-dir",
95 path.join(outside, ".metadata"),
96 source,
97 outside,
98 );
99 yield* git(outside, "worktree", "add", "../outside-feature", "-b", "feature");
100 assert.isFalse(yield* isWorktreeContainer(outside), "worktrees outside the folder");
101 }),
102 );
103 it.effect("finds a container with Git that rejects worktree list -z", () =>
104 Effect.gen(function* () {
105 const fileSystem = yield* FileSystem.FileSystem;
106 const path = yield* Path.Path;
107 const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;
108 // Git before 2.36 has no `worktree list -z` and exits with a usage error.
109 const oldGitSpawner = ChildProcessSpawner.make((command) =>
110 Effect.gen(function* () {
111 if (!ChildProcess.isStandardCommand(command)) {
112 return yield* Effect.die("expected a standard Git command");
113 }
114 if (command.args.includes("worktree") && command.args.includes("-z")) {
115 return ChildProcessSpawner.makeHandle({
116 pid: ChildProcessSpawner.ProcessId(1),
117 exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(129)),
118 isRunning: Effect.succeed(false),
119 kill: () => Effect.void,
120 unref: Effect.succeed(Effect.void),
121 stdin: Sink.drain,
122 stdout: Stream.empty,
123 stderr: Stream.encodeText(Stream.make("error: unknown switch `z'")),
124 all: Stream.empty,
125 getInputFd: () => Sink.drain,
126 getOutputFd: () => Stream.empty,
127 });
128 }
129 return yield* delegate.spawn(command);
130 }),
131 );
132 const isWorktreeContainer = yield* makeIsWorktreeContainer.pipe(
133 Effect.provideServiceEffect(
134 ProcessRunner.ProcessRunner,
135 ProcessRunner.make().pipe(
136 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, oldGitSpawner),
137 ),
138 ),
139 );
140 const fixture = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-container-test-" });
141 const source = path.join(fixture, "source");
142 yield* fileSystem.makeDirectory(source);
143 yield* git(source, "init", "-b", "develop");
144 yield* fileSystem.writeFileString(path.join(source, "README.md"), "readme\n");
145 yield* git(source, "add", "README.md");
146 yield* git(source, "commit", "-m", "init");
147 const root = path.join(fixture, "container");
148 yield* fileSystem.makeDirectory(root);
149 yield* git(fixture, "clone", "--bare", source, path.join(root, ".bare"));
150 yield* fileSystem.writeFileString(path.join(root, ".git"), "gitdir: ./.bare\n");
151 yield* git(root, "worktree", "add", "develop", "develop");
152
153 assert.isTrue(yield* isWorktreeContainer(root));
154 }),
155 );
156 },
157);