@niwa-labs/git 0.0.0-stage → 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +2 -2
- package/package.json +17 -4
- package/src/actions.ts +124 -0
- package/src/git.ts +247 -0
- package/src/index.ts +753 -0
- package/src/record-types.ts +141 -0
- package/src/records.ts +5 -0
- package/src/registration.ts +61 -0
- package/src/workflows/worktree-cleanup.ts +54 -0
- package/test/fixtures/git-worktree-host.ts +43 -0
- package/test/git-repo.ts +45 -0
- package/test/git-worktree-replay.test.ts +126 -0
- package/test/registration.test.ts +56 -0
- package/test/replay.ts +89 -0
- package/test/served.ts +46 -0
- package/test/worktree-create.test.ts +74 -0
- package/test/worktree-matched-removal.test.ts +101 -0
- package/test/worktree-observe.test.ts +49 -0
- package/test/worktree-removal.test.ts +156 -0
- package/test/worktree-replay-steps.test.ts +181 -0
- package/tsconfig.json +6 -0
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
// What a create copies in and what it says of the remote: an ignored file never replaces what the base tracks, a
|
|
2
|
+
// push goes to the remote git would push to, and a remote that is not plainly one project is not shown.
|
|
3
|
+
|
|
4
|
+
import { afterEach, expect, test } from "bun:test";
|
|
5
|
+
import { readFileSync, writeFileSync } from "node:fs";
|
|
6
|
+
import { join } from "node:path";
|
|
7
|
+
|
|
8
|
+
import { remoteProject, verifiedRemote } from "../src/git.ts";
|
|
9
|
+
import { gitWorktreeCreate, gitWorktreeGet } from "../src/index.ts";
|
|
10
|
+
import { gitIn, makeRepo } from "./git-repo.ts";
|
|
11
|
+
import { closeAll, opened, served } from "./served.ts";
|
|
12
|
+
|
|
13
|
+
const held = opened();
|
|
14
|
+
afterEach(() => closeAll(held));
|
|
15
|
+
|
|
16
|
+
test("an ignored file does not replace the version of it that the base tracks", async () => {
|
|
17
|
+
const repo = await makeRepo();
|
|
18
|
+
// The base tracks local.cfg; the checkout the worktree is made from ignores it and holds its own version.
|
|
19
|
+
writeFileSync(join(repo, "local.cfg"), "tracked\n");
|
|
20
|
+
gitIn(repo, "add", "local.cfg");
|
|
21
|
+
gitIn(repo, "commit", "-q", "-m", "track the config");
|
|
22
|
+
const base = gitIn(repo, "rev-parse", "HEAD");
|
|
23
|
+
gitIn(repo, "rm", "-q", "--cached", "local.cfg");
|
|
24
|
+
writeFileSync(join(repo, ".gitignore"), ".env\nsecret.local\nnode_modules/\nlocal.cfg\n");
|
|
25
|
+
writeFileSync(join(repo, ".worktreeinclude"), ".env\nnode_modules/\nplain.txt\nlocal.cfg\n");
|
|
26
|
+
gitIn(repo, "add", ".gitignore", ".worktreeinclude");
|
|
27
|
+
gitIn(repo, "commit", "-q", "-m", "ignore the config");
|
|
28
|
+
writeFileSync(join(repo, "local.cfg"), "mine\n");
|
|
29
|
+
const { host } = await served(held);
|
|
30
|
+
|
|
31
|
+
const made = await host.call(gitWorktreeCreate, { repo, base, description: "from the commit that tracks it" });
|
|
32
|
+
|
|
33
|
+
expect(readFileSync(join(made.path, "local.cfg"), "utf8")).toBe("tracked\n");
|
|
34
|
+
expect(readFileSync(join(made.path, ".env"), "utf8")).toBe("TOKEN=1\n");
|
|
35
|
+
expect(await host.call(gitWorktreeGet, { id: made.id })).toMatchObject({ dirty: false });
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test("a remote URL with a path that is not valid percent-encoding names no project", () => {
|
|
39
|
+
expect(remoteProject("https://example.org/group/100%.git")).toBeUndefined();
|
|
40
|
+
expect(remoteProject("https://example.org/group/name%20x.git")).toBeUndefined();
|
|
41
|
+
expect(remoteProject("https://Example.org/group/name.git")).toEqual({ host: "example.org", path: "group/name" });
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
test("the remote shown is the one a push of the base goes to, and every push URL must name that project", async () => {
|
|
45
|
+
const repo = await makeRepo();
|
|
46
|
+
gitIn(repo, "remote", "add", "upstream", "https://forge.example/acme/app.git");
|
|
47
|
+
gitIn(repo, "remote", "add", "fork", "git@forge.example:me/app.git");
|
|
48
|
+
gitIn(repo, "config", "branch.main.remote", "upstream");
|
|
49
|
+
|
|
50
|
+
expect(await verifiedRemote(repo, "main")).toEqual({ name: "upstream", host: "forge.example", path: "acme/app" });
|
|
51
|
+
// A fork workflow: the branch tracks upstream, and pushes go to the fork.
|
|
52
|
+
gitIn(repo, "config", "branch.main.pushRemote", "fork");
|
|
53
|
+
expect(await verifiedRemote(repo, "main")).toEqual({ name: "fork", host: "forge.example", path: "me/app" });
|
|
54
|
+
gitIn(repo, "config", "--unset", "branch.main.pushRemote");
|
|
55
|
+
gitIn(repo, "config", "remote.pushDefault", "fork");
|
|
56
|
+
expect(await verifiedRemote(repo, "main")).toEqual({ name: "fork", host: "forge.example", path: "me/app" });
|
|
57
|
+
|
|
58
|
+
// A second push URL on another project is not one project.
|
|
59
|
+
gitIn(repo, "remote", "set-url", "--add", "--push", "fork", "git@forge.example:me/app.git");
|
|
60
|
+
gitIn(repo, "remote", "set-url", "--add", "--push", "fork", "git@forge.example:other/app.git");
|
|
61
|
+
expect(await verifiedRemote(repo, "main")).toBeUndefined();
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
test("an unconfigured branch pushes to the only remote even when it is not origin", async () => {
|
|
65
|
+
const repo = await makeRepo();
|
|
66
|
+
gitIn(repo, "remote", "add", "fork", "git@forge.example:me/app.git");
|
|
67
|
+
expect(await verifiedRemote(repo, "main")).toEqual({ name: "fork", host: "forge.example", path: "me/app" });
|
|
68
|
+
|
|
69
|
+
gitIn(repo, "remote", "add", "upstream", "https://forge.example/acme/app.git");
|
|
70
|
+
expect(await verifiedRemote(repo, "main")).toBeUndefined();
|
|
71
|
+
|
|
72
|
+
gitIn(repo, "remote", "add", "origin", "https://forge.example/acme/app.git");
|
|
73
|
+
expect(await verifiedRemote(repo, "main")).toEqual({ name: "origin", host: "forge.example", path: "acme/app" });
|
|
74
|
+
});
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { afterEach, expect, test } from "bun:test";
|
|
2
|
+
import { existsSync, readFileSync, renameSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
|
|
5
|
+
import { record } from "@niwa-labs/core";
|
|
6
|
+
import { createNiwa, plugin, tempHome } from "@niwa-labs/testing";
|
|
7
|
+
import { z } from "zod";
|
|
8
|
+
|
|
9
|
+
import { git, gitWorktreeCreate, gitWorktreeGet, gitWorktreeRemove } from "../src/index.ts";
|
|
10
|
+
import { gitIn, makeRepo } from "./git-repo.ts";
|
|
11
|
+
import { closeAll, opened, refusalOf } from "./served.ts";
|
|
12
|
+
|
|
13
|
+
const held = opened();
|
|
14
|
+
afterEach(() => closeAll(held));
|
|
15
|
+
|
|
16
|
+
const Started = record("session.started", z.object({ session: z.string(), id: z.string(), cwd: z.string() }));
|
|
17
|
+
const project = { host: "example.invalid", path: "group/repository" };
|
|
18
|
+
|
|
19
|
+
async function fixture() {
|
|
20
|
+
const repo = await makeRepo();
|
|
21
|
+
gitIn(repo, "rm", "-q", ".worktreeinclude");
|
|
22
|
+
gitIn(repo, "commit", "-q", "-m", "use a worktree without copied ignored files");
|
|
23
|
+
gitIn(repo, "remote", "add", "origin", `https://${project.host}/${project.path}.git`);
|
|
24
|
+
const home = await tempHome();
|
|
25
|
+
held.homes.push(home);
|
|
26
|
+
const host = createNiwa({ home: home.path });
|
|
27
|
+
held.hosts.push(host);
|
|
28
|
+
host.use(git({ cleanup: false }), plugin("test-sessions", (api) => api.declare(Started)));
|
|
29
|
+
await host.serve();
|
|
30
|
+
const tree = await host.call(gitWorktreeCreate, { repo, description: "selected worktree" });
|
|
31
|
+
const match = {
|
|
32
|
+
id: tree.id,
|
|
33
|
+
onlyIf: { repo: tree.repo, path: tree.path, branch: tree.branch, head: tree.baseSha, project },
|
|
34
|
+
};
|
|
35
|
+
return { repo, host, tree, match };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
test("remove only the selected checkout at the expected HEAD and retain its commits", async () => {
|
|
39
|
+
const { repo, host, tree, match } = await fixture();
|
|
40
|
+
writeFileSync(join(tree.path, "work.txt"), "a commit ahead of the local base\n");
|
|
41
|
+
gitIn(tree.path, "add", "work.txt");
|
|
42
|
+
gitIn(tree.path, "commit", "-q", "-m", "keep these commits independently of the checkout");
|
|
43
|
+
const head = gitIn(tree.path, "rev-parse", "HEAD");
|
|
44
|
+
const other = await host.call(gitWorktreeCreate, { repo, base: head, description: "another checkout at exactly the same HEAD" });
|
|
45
|
+
expect(gitIn(other.path, "rev-parse", "HEAD")).toBe(head);
|
|
46
|
+
const args = { ...match, onlyIf: { ...match.onlyIf, head } };
|
|
47
|
+
await expect(host.call(gitWorktreeRemove, { ...args, force: true })).rejects.toThrow("onlyIf cannot be combined with force");
|
|
48
|
+
expect(await host.call(gitWorktreeRemove, args)).toEqual({ id: tree.id, removed: true });
|
|
49
|
+
expect(await host.call(gitWorktreeRemove, args)).toEqual({ id: tree.id, removed: false });
|
|
50
|
+
expect(existsSync(tree.path)).toBe(false);
|
|
51
|
+
expect(gitIn(other.path, "rev-parse", "HEAD")).toBe(head);
|
|
52
|
+
expect(gitIn(repo, "rev-parse", tree.branch)).toBe(head);
|
|
53
|
+
expect(await host.records.read()).toEqual(expect.arrayContaining([
|
|
54
|
+
expect.objectContaining({ type: "git.worktree.removal.claimed", data: { id: tree.id, onlyIf: args.onlyIf } }),
|
|
55
|
+
expect.objectContaining({ type: "git.worktree.removed", data: { id: tree.id, forced: false, branchKept: true } }),
|
|
56
|
+
]));
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
test("keep a checkout locked or changed after inspecting it", async () => {
|
|
60
|
+
const { repo, host, tree, match } = await fixture();
|
|
61
|
+
expect((await host.call(gitWorktreeGet, { id: tree.id })).dirty).toBe(false);
|
|
62
|
+
gitIn(repo, "worktree", "lock", tree.path);
|
|
63
|
+
expect(await refusalOf(host.call(gitWorktreeRemove, match))).toBe("changed");
|
|
64
|
+
expect(existsSync(tree.path)).toBe(true);
|
|
65
|
+
gitIn(repo, "worktree", "unlock", tree.path);
|
|
66
|
+
writeFileSync(join(tree.path, "README.md"), "uncommitted\n");
|
|
67
|
+
expect(await refusalOf(host.call(gitWorktreeRemove, match))).toBe("dirty");
|
|
68
|
+
expect(readFileSync(join(tree.path, "README.md"), "utf8")).toBe("uncommitted\n");
|
|
69
|
+
gitIn(tree.path, "add", "README.md");
|
|
70
|
+
gitIn(tree.path, "commit", "-q", "-m", "new HEAD after the PR receipt");
|
|
71
|
+
expect(await refusalOf(host.call(gitWorktreeRemove, match))).toBe("changed");
|
|
72
|
+
expect(existsSync(tree.path)).toBe(true);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
test("keep local-only files and reject changing the verified forge project", async () => {
|
|
76
|
+
const { repo, host, tree, match } = await fixture();
|
|
77
|
+
writeFileSync(join(tree.path, ".env"), "local only\n");
|
|
78
|
+
expect(await refusalOf(host.call(gitWorktreeRemove, match))).toBe("dirty");
|
|
79
|
+
expect(readFileSync(join(tree.path, ".env"), "utf8")).toBe("local only\n");
|
|
80
|
+
gitIn(repo, "remote", "set-url", "origin", "https://example.invalid/another/repository.git");
|
|
81
|
+
expect(await refusalOf(host.call(gitWorktreeRemove, match))).toBe("changed");
|
|
82
|
+
expect(existsSync(tree.path)).toBe(true);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test("keep a worktree when a session started inside it after inspection", async () => {
|
|
86
|
+
const { host, tree, match } = await fixture();
|
|
87
|
+
expect((await host.call(gitWorktreeGet, { id: tree.id })).sessions).toEqual([]);
|
|
88
|
+
await host.emit(Started, { session: "fixture native", id: "s-fixture", cwd: join(tree.path, "src") });
|
|
89
|
+
expect(await refusalOf(host.call(gitWorktreeRemove, match))).toBe("live-session");
|
|
90
|
+
expect(existsSync(tree.path)).toBe(true);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
test("keep a different checkout placed at the same path, branch and HEAD", async () => {
|
|
94
|
+
const { repo, host, tree, match } = await fixture();
|
|
95
|
+
renameSync(tree.path, join(tree.path, "..", "retained-original"));
|
|
96
|
+
gitIn(repo, "worktree", "prune", "--expire=now");
|
|
97
|
+
gitIn(repo, "worktree", "add", "-q", tree.path, tree.branch);
|
|
98
|
+
expect(gitIn(tree.path, "rev-parse", "HEAD")).toBe(tree.baseSha);
|
|
99
|
+
expect(await refusalOf(host.call(gitWorktreeRemove, match))).toBe("changed");
|
|
100
|
+
expect(existsSync(tree.path)).toBe(true);
|
|
101
|
+
});
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// What looking at the heads of worktrees records: a change once, even when two looks overlap, and the remote a
|
|
2
|
+
// worktree's work goes to when it changes.
|
|
3
|
+
|
|
4
|
+
import { afterEach, expect, test } from "bun:test";
|
|
5
|
+
import { writeFileSync } from "node:fs";
|
|
6
|
+
import { join } from "node:path";
|
|
7
|
+
|
|
8
|
+
import { gitHeadObserve, gitWorktreeCreate } from "../src/index.ts";
|
|
9
|
+
import { gitIn, makeRepo } from "./git-repo.ts";
|
|
10
|
+
import { closeAll, opened, served } from "./served.ts";
|
|
11
|
+
|
|
12
|
+
const held = opened();
|
|
13
|
+
afterEach(() => closeAll(held));
|
|
14
|
+
|
|
15
|
+
test("two looks at a worktree that overlap record its moved head once", async () => {
|
|
16
|
+
const repo = await makeRepo();
|
|
17
|
+
const { host } = await served(held);
|
|
18
|
+
const made = await host.call(gitWorktreeCreate, { repo, description: "two observers" });
|
|
19
|
+
writeFileSync(join(made.path, "work.txt"), "work\n");
|
|
20
|
+
gitIn(made.path, "add", "work.txt");
|
|
21
|
+
gitIn(made.path, "commit", "-q", "-m", "work");
|
|
22
|
+
|
|
23
|
+
const [first, second] = await Promise.all([host.call(gitHeadObserve, {}), host.call(gitHeadObserve, {})]);
|
|
24
|
+
|
|
25
|
+
const head = gitIn(made.path, "rev-parse", "HEAD");
|
|
26
|
+
expect([...first.moved, ...second.moved]).toEqual([{ worktree: made.id, head }]);
|
|
27
|
+
expect((await host.records.read()).filter((row) => row.type === "git.head.moved")).toHaveLength(1);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
test("a change of the remote a worktree's work goes to is recorded when it is seen, and its removal too", async () => {
|
|
31
|
+
const repo = await makeRepo();
|
|
32
|
+
gitIn(repo, "remote", "add", "origin", "https://forge.example/acme/app.git");
|
|
33
|
+
const { host } = await served(held);
|
|
34
|
+
const made = await host.call(gitWorktreeCreate, { repo, description: "a worktree with a remote" });
|
|
35
|
+
const remotes = async () =>
|
|
36
|
+
(await host.records.read()).filter((row) => row.type === "git.remote.updated").map((row) => row.data as { worktree: string; remote?: { host: string; path: string } });
|
|
37
|
+
|
|
38
|
+
await host.call(gitHeadObserve, {});
|
|
39
|
+
await host.call(gitHeadObserve, {});
|
|
40
|
+
expect(await remotes()).toEqual([{ worktree: made.id, repo, remote: { name: "origin", host: "forge.example", path: "acme/app" } }]);
|
|
41
|
+
|
|
42
|
+
gitIn(repo, "remote", "set-url", "origin", "https://forge.example/acme/renamed.git");
|
|
43
|
+
await host.call(gitHeadObserve, {});
|
|
44
|
+
gitIn(repo, "remote", "remove", "origin");
|
|
45
|
+
await host.call(gitHeadObserve, {});
|
|
46
|
+
await host.call(gitHeadObserve, {});
|
|
47
|
+
|
|
48
|
+
expect((await remotes()).map((row) => row.remote?.path)).toEqual(["acme/app", "acme/renamed", undefined]);
|
|
49
|
+
});
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
// What a remove of a worktree keeps: a directory git will not remove, work committed on a detached head, work in a
|
|
2
|
+
// repository whose checkout the worktree was made from, and a directory that took the place of a removed worktree.
|
|
3
|
+
|
|
4
|
+
import { afterEach, expect, test } from "bun:test";
|
|
5
|
+
import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
6
|
+
import { join } from "node:path";
|
|
7
|
+
|
|
8
|
+
import { gitWorktreeCreate, gitWorktreeGet, gitWorktreeList, gitWorktreeRemove } from "../src/index.ts";
|
|
9
|
+
import { gitIn, makeRepo } from "./git-repo.ts";
|
|
10
|
+
import { closeAll, opened, refusalOf, served } from "./served.ts";
|
|
11
|
+
|
|
12
|
+
const held = opened();
|
|
13
|
+
afterEach(() => closeAll(held));
|
|
14
|
+
|
|
15
|
+
test("a remove leaves a worktree git will not remove, and removes it with force", async () => {
|
|
16
|
+
const repo = await makeRepo();
|
|
17
|
+
const { host } = await served(held);
|
|
18
|
+
const made = await host.call(gitWorktreeCreate, { repo, description: "locked by its owner" });
|
|
19
|
+
gitIn(repo, "worktree", "lock", made.path);
|
|
20
|
+
|
|
21
|
+
expect(await refusalOf(host.call(gitWorktreeRemove, { id: made.id }))).toBe("changed");
|
|
22
|
+
expect(existsSync(made.path)).toBe(true);
|
|
23
|
+
expect(gitIn(repo, "branch", "--list", made.id)).not.toBe("");
|
|
24
|
+
expect((await host.records.read()).filter((row) => row.type === "git.worktree.removed")).toEqual([]);
|
|
25
|
+
|
|
26
|
+
expect(await host.call(gitWorktreeRemove, { id: made.id, force: true })).toEqual({ id: made.id, removed: true });
|
|
27
|
+
expect(existsSync(made.path)).toBe(false);
|
|
28
|
+
expect(gitIn(repo, "branch", "--list", made.id)).toBe("");
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test("commits made after the head was detached hold the worktree, though its branch is at the base", async () => {
|
|
32
|
+
const repo = await makeRepo();
|
|
33
|
+
const { host } = await served(held);
|
|
34
|
+
const made = await host.call(gitWorktreeCreate, { repo, description: "detached work" });
|
|
35
|
+
gitIn(made.path, "switch", "-q", "--detach");
|
|
36
|
+
writeFileSync(join(made.path, "kept.txt"), "work\n");
|
|
37
|
+
gitIn(made.path, "add", "kept.txt");
|
|
38
|
+
gitIn(made.path, "commit", "-q", "-m", "work on a detached head");
|
|
39
|
+
|
|
40
|
+
expect(await host.call(gitWorktreeGet, { id: made.id })).toMatchObject({ dirty: false, aheadOfBase: 1 });
|
|
41
|
+
expect(await refusalOf(host.call(gitWorktreeRemove, { id: made.id }))).toBe("ahead");
|
|
42
|
+
expect(existsSync(made.path)).toBe(true);
|
|
43
|
+
|
|
44
|
+
expect(await host.call(gitWorktreeRemove, { id: made.id, force: true })).toEqual({ id: made.id, removed: true });
|
|
45
|
+
expect(existsSync(made.path)).toBe(false);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
test("a worktree made from a linked worktree belongs to the repository that owns it, and keeps its commits when the source goes", async () => {
|
|
49
|
+
const repo = await makeRepo();
|
|
50
|
+
const source = join(repo, "..", `linked-${String(Date.now())}`);
|
|
51
|
+
gitIn(repo, "worktree", "add", "-q", "-b", "source", source);
|
|
52
|
+
writeFileSync(join(source, "source.txt"), "source work\n");
|
|
53
|
+
writeFileSync(join(source, ".env"), "TOKEN=source\n");
|
|
54
|
+
gitIn(source, "add", "source.txt");
|
|
55
|
+
gitIn(source, "commit", "-q", "-m", "source work");
|
|
56
|
+
const base = gitIn(source, "rev-parse", "HEAD");
|
|
57
|
+
const { host } = await served(held);
|
|
58
|
+
|
|
59
|
+
const made = await host.call(gitWorktreeCreate, { repo: source, description: "from a linked checkout" });
|
|
60
|
+
expect(made).toMatchObject({ repo, base: "source", baseSha: base });
|
|
61
|
+
expect(readFileSync(join(made.path, ".env"), "utf8")).toBe("TOKEN=source\n");
|
|
62
|
+
writeFileSync(join(made.path, "child.txt"), "work\n");
|
|
63
|
+
gitIn(made.path, "add", "child.txt");
|
|
64
|
+
gitIn(made.path, "commit", "-q", "-m", "child work");
|
|
65
|
+
gitIn(repo, "worktree", "remove", "--force", source);
|
|
66
|
+
|
|
67
|
+
expect(await host.call(gitWorktreeGet, { id: made.id })).toMatchObject({ aheadOfBase: 1 });
|
|
68
|
+
expect(await refusalOf(host.call(gitWorktreeRemove, { id: made.id }))).toBe("ahead");
|
|
69
|
+
expect(existsSync(made.path)).toBe(true);
|
|
70
|
+
expect(gitIn(repo, "branch", "--list", made.id)).not.toBe("");
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
test("a worktree whose repository is gone is not known to be clean, and is removed with force", async () => {
|
|
74
|
+
const repo = await makeRepo();
|
|
75
|
+
const { host } = await served(held);
|
|
76
|
+
const made = await host.call(gitWorktreeCreate, { repo, description: "its repository goes" });
|
|
77
|
+
rmSync(repo, { recursive: true, force: true });
|
|
78
|
+
|
|
79
|
+
expect(await refusalOf(host.call(gitWorktreeRemove, { id: made.id }))).toBe("unverifiable");
|
|
80
|
+
expect(existsSync(made.path)).toBe(true);
|
|
81
|
+
expect(await host.call(gitWorktreeRemove, { id: made.id, force: true })).toEqual({ id: made.id, removed: true });
|
|
82
|
+
expect(existsSync(made.path)).toBe(false);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test("a directory that took the place of a removed worktree is an orphan that a remove can remove with force", async () => {
|
|
86
|
+
const repo = await makeRepo();
|
|
87
|
+
const { host } = await served(held);
|
|
88
|
+
const made = await host.call(gitWorktreeCreate, { repo, description: "removed, then replaced" });
|
|
89
|
+
expect(await host.call(gitWorktreeRemove, { id: made.id })).toEqual({ id: made.id, removed: true });
|
|
90
|
+
expect(await host.call(gitWorktreeRemove, { id: made.id })).toEqual({ id: made.id, removed: false });
|
|
91
|
+
|
|
92
|
+
mkdirSync(made.path, { recursive: true });
|
|
93
|
+
expect((await host.call(gitWorktreeList, {})).items).toMatchObject([{ id: made.id, state: "orphan" }]);
|
|
94
|
+
expect(await refusalOf(host.call(gitWorktreeRemove, { id: made.id }))).toBe("orphan");
|
|
95
|
+
expect(await host.call(gitWorktreeRemove, { id: made.id, force: true })).toEqual({ id: made.id, removed: true });
|
|
96
|
+
expect(existsSync(made.path)).toBe(false);
|
|
97
|
+
expect(await host.call(gitWorktreeRemove, { id: made.id })).toEqual({ id: made.id, removed: false });
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
test("force keeps an unrelated checkout that replaced a recorded worktree", async () => {
|
|
101
|
+
const repo = await makeRepo();
|
|
102
|
+
const { host } = await served(held);
|
|
103
|
+
const made = await host.call(gitWorktreeCreate, { repo, description: "replaced checkout" });
|
|
104
|
+
gitIn(repo, "worktree", "remove", made.path);
|
|
105
|
+
mkdirSync(made.path);
|
|
106
|
+
gitIn(made.path, "init", "-q");
|
|
107
|
+
writeFileSync(join(made.path, "owner.txt"), "keep me\n");
|
|
108
|
+
|
|
109
|
+
await expect(refusalOf(host.call(gitWorktreeRemove, { id: made.id, force: true }))).resolves.toBe("changed");
|
|
110
|
+
expect(readFileSync(join(made.path, "owner.txt"), "utf8")).toBe("keep me\n");
|
|
111
|
+
expect(gitIn(repo, "branch", "--list", made.id)).not.toBe("");
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
test("overlapping removals report one outcome and write one removed record", async () => {
|
|
115
|
+
const repo = await makeRepo();
|
|
116
|
+
const { host } = await served(held);
|
|
117
|
+
const made = await host.call(gitWorktreeCreate, { repo, description: "two removals" });
|
|
118
|
+
|
|
119
|
+
const results = await Promise.all([
|
|
120
|
+
host.call(gitWorktreeRemove, { id: made.id }),
|
|
121
|
+
host.call(gitWorktreeRemove, { id: made.id }),
|
|
122
|
+
]);
|
|
123
|
+
|
|
124
|
+
expect(results.map(({ removed }) => removed).sort()).toEqual([false, true]);
|
|
125
|
+
expect((await host.records.read()).filter((row) => row.type === "git.worktree.removed")).toHaveLength(1);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
test("a branch checked out elsewhere keeps its worktree and ref", async () => {
|
|
129
|
+
const repo = await makeRepo();
|
|
130
|
+
const { host } = await served(held);
|
|
131
|
+
const made = await host.call(gitWorktreeCreate, { repo, description: "branch in use elsewhere" });
|
|
132
|
+
gitIn(made.path, "switch", "-q", "--detach");
|
|
133
|
+
const other = join(repo, "other");
|
|
134
|
+
gitIn(repo, "worktree", "add", "-q", other, made.branch);
|
|
135
|
+
|
|
136
|
+
expect(await refusalOf(host.call(gitWorktreeRemove, { id: made.id }))).toBe("changed");
|
|
137
|
+
expect(existsSync(made.path)).toBe(true);
|
|
138
|
+
expect(gitIn(other, "rev-parse", "HEAD")).toBe(made.baseSha);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
test("a locked worktree remains registered when its directory is missing", async () => {
|
|
142
|
+
const repo = await makeRepo();
|
|
143
|
+
const { host } = await served(held);
|
|
144
|
+
const made = await host.call(gitWorktreeCreate, { repo, description: "locked removable checkout" });
|
|
145
|
+
gitIn(made.path, "switch", "-q", "--detach");
|
|
146
|
+
gitIn(repo, "worktree", "lock", made.path);
|
|
147
|
+
rmSync(made.path, { recursive: true, force: true });
|
|
148
|
+
|
|
149
|
+
expect(await refusalOf(host.call(gitWorktreeRemove, { id: made.id }))).toBe("changed");
|
|
150
|
+
expect(gitIn(repo, "worktree", "list", "--porcelain")).toContain(made.path);
|
|
151
|
+
expect(gitIn(repo, "branch", "--list", made.branch)).not.toBe("");
|
|
152
|
+
|
|
153
|
+
expect(await host.call(gitWorktreeRemove, { id: made.id, force: true })).toEqual({ id: made.id, removed: true });
|
|
154
|
+
expect(gitIn(repo, "worktree", "list", "--porcelain")).not.toContain(made.path);
|
|
155
|
+
expect(gitIn(repo, "branch", "--list", made.branch)).toBe("");
|
|
156
|
+
});
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
// A create or a look that was cut off is made again under the same operation and ends as if it had not been cut:
|
|
2
|
+
// the base is the one reserved, not the one the base branch has moved to, and the records a look wrote are written
|
|
3
|
+
// again in their places before it goes on.
|
|
4
|
+
|
|
5
|
+
import { afterEach, expect, test } from "bun:test";
|
|
6
|
+
import { existsSync, mkdirSync, readFileSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
|
|
7
|
+
import { join } from "node:path";
|
|
8
|
+
|
|
9
|
+
import { tempHome } from "@niwa-labs/testing";
|
|
10
|
+
|
|
11
|
+
import type { Worktree } from "../src/index.ts";
|
|
12
|
+
import { gitIn, makeRepo } from "./git-repo.ts";
|
|
13
|
+
import { CutOff, MemoryJournal, module, run } from "./replay.ts";
|
|
14
|
+
|
|
15
|
+
const homes: Array<{ cleanup(): Promise<void> }> = [];
|
|
16
|
+
afterEach(async () => {
|
|
17
|
+
for (const home of homes.splice(0)) await home.cleanup();
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
async function setup() {
|
|
21
|
+
const home = await tempHome();
|
|
22
|
+
homes.push(home);
|
|
23
|
+
const journal = new MemoryJournal();
|
|
24
|
+
return { journal, actions: module(journal, home.path), repo: await makeRepo() };
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const NOTE = { description: "a worktree" };
|
|
28
|
+
|
|
29
|
+
function commitIn(dir: string, file: string): string {
|
|
30
|
+
writeFileSync(join(dir, file), `${file}\n`);
|
|
31
|
+
gitIn(dir, "add", file);
|
|
32
|
+
gitIn(dir, "commit", "-q", "-m", file);
|
|
33
|
+
return gitIn(dir, "rev-parse", "HEAD");
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
test("a create cut off after its checkout, made again after the base moved, records the commit it reserved", async () => {
|
|
37
|
+
const { journal, actions, repo } = await setup();
|
|
38
|
+
const reservedAt = gitIn(repo, "rev-parse", "HEAD");
|
|
39
|
+
|
|
40
|
+
// The checkout is made; the host stops before the worktree is recorded (the record after the reservation).
|
|
41
|
+
await expect(run(actions, journal, "git.worktree.create", { repo, ...NOTE }, "create-1", 2)).rejects.toBeInstanceOf(CutOff);
|
|
42
|
+
expect(journal.of("git.worktree.created")).toEqual([]);
|
|
43
|
+
commitIn(repo, "moved-on.txt");
|
|
44
|
+
|
|
45
|
+
const made = await run<Worktree>(actions, journal, "git.worktree.create", { repo, ...NOTE }, "create-1");
|
|
46
|
+
|
|
47
|
+
expect(made.baseSha).toBe(reservedAt);
|
|
48
|
+
expect(gitIn(made.path, "rev-parse", "HEAD")).toBe(reservedAt);
|
|
49
|
+
expect(existsSync(join(made.path, "moved-on.txt"))).toBe(false);
|
|
50
|
+
expect(journal.of("git.worktree.reserved")).toHaveLength(1);
|
|
51
|
+
expect(journal.of("git.worktree.created").map((row) => row.data)).toEqual([made]);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
test("a create made again does not take a branch of that name that is somewhere else", async () => {
|
|
55
|
+
const { journal, actions, repo } = await setup();
|
|
56
|
+
await expect(run(actions, journal, "git.worktree.create", { repo, ...NOTE }, "create-2", 2)).rejects.toBeInstanceOf(CutOff);
|
|
57
|
+
const [reserved] = journal.of("git.worktree.reserved");
|
|
58
|
+
const { id, path } = reserved?.data as Worktree;
|
|
59
|
+
gitIn(repo, "worktree", "remove", "--force", path);
|
|
60
|
+
const elsewhere = commitIn(repo, "elsewhere.txt");
|
|
61
|
+
gitIn(repo, "branch", "-f", id, elsewhere);
|
|
62
|
+
|
|
63
|
+
await expect(run(actions, journal, "git.worktree.create", { repo, ...NOTE }, "create-2")).rejects.toMatchObject({ code: "occupied" });
|
|
64
|
+
expect(journal.of("git.worktree.created")).toEqual([]);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test("a create made again does not take a checkout of another repository at its directory", async () => {
|
|
68
|
+
const { journal, actions, repo } = await setup();
|
|
69
|
+
await expect(run(actions, journal, "git.worktree.create", { repo, ...NOTE }, "create-3", 2)).rejects.toBeInstanceOf(CutOff);
|
|
70
|
+
const [reserved] = journal.of("git.worktree.reserved");
|
|
71
|
+
const { id, path } = reserved?.data as Worktree;
|
|
72
|
+
gitIn(repo, "worktree", "remove", "--force", path);
|
|
73
|
+
// Another repository has a checkout there, on a branch of the same name.
|
|
74
|
+
const other = await makeRepo();
|
|
75
|
+
gitIn(other, "worktree", "add", "-q", "-b", id, path);
|
|
76
|
+
|
|
77
|
+
await expect(run(actions, journal, "git.worktree.create", { repo, ...NOTE }, "create-3")).rejects.toMatchObject({ code: "occupied" });
|
|
78
|
+
expect(journal.of("git.worktree.created")).toEqual([]);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
test("a create made again keeps the files its first attempt copied in, as they are now", async () => {
|
|
82
|
+
const { journal, actions, repo } = await setup();
|
|
83
|
+
await expect(run(actions, journal, "git.worktree.create", { repo, ...NOTE }, "create-4", 2)).rejects.toBeInstanceOf(CutOff);
|
|
84
|
+
const [reserved] = journal.of("git.worktree.reserved");
|
|
85
|
+
const { path } = reserved?.data as Worktree;
|
|
86
|
+
expect(readFileSync(join(path, ".env"), "utf8")).toBe("TOKEN=1\n");
|
|
87
|
+
writeFileSync(join(path, ".env"), "TOKEN=2\n");
|
|
88
|
+
|
|
89
|
+
await run(actions, journal, "git.worktree.create", { repo, ...NOTE }, "create-4");
|
|
90
|
+
|
|
91
|
+
expect(readFileSync(join(path, ".env"), "utf8")).toBe("TOKEN=2\n");
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
test("a checkout with a replaced destination directory does not write outside the worktree", async () => {
|
|
95
|
+
const { journal, actions, repo } = await setup();
|
|
96
|
+
await expect(run(actions, journal, "git.worktree.create", { repo, ...NOTE }, "create-linked-copy", 2)).rejects.toBeInstanceOf(CutOff);
|
|
97
|
+
const [reserved] = journal.of("git.worktree.reserved");
|
|
98
|
+
const { path } = reserved?.data as Worktree;
|
|
99
|
+
const outside = join(repo, "outside");
|
|
100
|
+
mkdirSync(outside);
|
|
101
|
+
rmSync(join(path, "node_modules"), { recursive: true });
|
|
102
|
+
symlinkSync(outside, join(path, "node_modules"), "dir");
|
|
103
|
+
|
|
104
|
+
await run(actions, journal, "git.worktree.create", { repo, ...NOTE }, "create-linked-copy");
|
|
105
|
+
|
|
106
|
+
expect(existsSync(join(outside, "pkg"))).toBe(false);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
test("a worktree created before reservations can still be named and its create replayed", async () => {
|
|
110
|
+
const { journal, actions, repo } = await setup();
|
|
111
|
+
const operation = "old-create";
|
|
112
|
+
const made = await run<Worktree>(actions, journal, "git.worktree.create", { repo, ...NOTE }, operation);
|
|
113
|
+
const [birth] = journal.of("git.worktree.reserved");
|
|
114
|
+
const [created] = journal.of("git.worktree.created");
|
|
115
|
+
if (birth === undefined || created === undefined) throw new Error("the create must write both records");
|
|
116
|
+
journal.records.splice(0, journal.records.length, { ...created, id: birth.id, seq: birth.seq });
|
|
117
|
+
|
|
118
|
+
await expect(run(actions, journal, "git.worktree.get", { id: made.id }, "get")).resolves.toMatchObject({ id: made.id });
|
|
119
|
+
await expect(run(actions, journal, "git.worktree.create", { repo, ...NOTE }, operation)).resolves.toEqual(made);
|
|
120
|
+
expect(journal.records).toHaveLength(1);
|
|
121
|
+
await expect(run(actions, journal, "git.worktree.remove", { id: made.id }, "remove")).resolves.toEqual({ id: made.id, removed: true });
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
test("removal can find the repository when a recorded linked source has gone", async () => {
|
|
125
|
+
const { journal, actions, repo } = await setup();
|
|
126
|
+
const source = join(repo, "linked");
|
|
127
|
+
gitIn(repo, "worktree", "add", "-q", "-b", "source", source);
|
|
128
|
+
const made = await run<Worktree>(actions, journal, "git.worktree.create", { repo: source, ...NOTE }, "old-source");
|
|
129
|
+
const [birth] = journal.of("git.worktree.reserved");
|
|
130
|
+
const [created] = journal.of("git.worktree.created");
|
|
131
|
+
if (birth === undefined || created === undefined) throw new Error("the create must write both records");
|
|
132
|
+
journal.records.splice(0, journal.records.length, { ...created, id: birth.id, seq: birth.seq, data: { ...made, repo: source } });
|
|
133
|
+
gitIn(repo, "worktree", "remove", source);
|
|
134
|
+
commitIn(made.path, "child.txt");
|
|
135
|
+
|
|
136
|
+
await expect(run(actions, journal, "git.worktree.get", { id: made.id }, "get")).resolves.toMatchObject({ aheadOfBase: 1 });
|
|
137
|
+
await expect(run(actions, journal, "git.worktree.remove", { id: made.id }, "remove")).rejects.toMatchObject({ code: "ahead" });
|
|
138
|
+
|
|
139
|
+
gitIn(repo, "merge", "--ff-only", made.branch);
|
|
140
|
+
gitIn(repo, "branch", "-f", "source", made.branch);
|
|
141
|
+
await expect(run(actions, journal, "git.worktree.remove", { id: made.id }, "remove")).resolves.toEqual({ id: made.id, removed: true });
|
|
142
|
+
expect(gitIn(repo, "branch", "--list", made.branch)).toBe("");
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
test("a create does not take a pre-existing branch at the intended base", async () => {
|
|
146
|
+
const { journal, actions, repo } = await setup();
|
|
147
|
+
await expect(run(actions, journal, "git.worktree.create", { repo, ...NOTE }, "create-collision", 2)).rejects.toBeInstanceOf(CutOff);
|
|
148
|
+
const [reserved] = journal.of("git.worktree.reserved");
|
|
149
|
+
const { path } = reserved?.data as Worktree;
|
|
150
|
+
gitIn(repo, "worktree", "remove", "--force", path);
|
|
151
|
+
|
|
152
|
+
await expect(run(actions, journal, "git.worktree.create", { repo, ...NOTE }, "create-collision")).rejects.toMatchObject({ code: "occupied" });
|
|
153
|
+
expect(journal.of("git.worktree.created")).toEqual([]);
|
|
154
|
+
expect(existsSync(path)).toBe(false);
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
test("a look cut off after the first worktree it recorded, made again, records the second in its place and answers both", async () => {
|
|
158
|
+
const { journal, actions, repo } = await setup();
|
|
159
|
+
const first = await run<Worktree>(actions, journal, "git.worktree.create", { repo, description: "first" }, "make-1");
|
|
160
|
+
const second = await run<Worktree>(actions, journal, "git.worktree.create", { repo, description: "second" }, "make-2");
|
|
161
|
+
const heads = [commitIn(first.path, "a.txt"), commitIn(second.path, "b.txt")];
|
|
162
|
+
|
|
163
|
+
await expect(run(actions, journal, "git.head.observe", {}, "look-1", 2)).rejects.toBeInstanceOf(CutOff);
|
|
164
|
+
expect(journal.of("git.head.moved")).toHaveLength(1);
|
|
165
|
+
|
|
166
|
+
const seen = await run<{ moved: Array<{ worktree: string; head: string }> }>(actions, journal, "git.head.observe", {}, "look-1");
|
|
167
|
+
|
|
168
|
+
expect(seen.moved).toEqual([
|
|
169
|
+
{ worktree: first.id, head: heads[0] },
|
|
170
|
+
{ worktree: second.id, head: heads[1] },
|
|
171
|
+
]);
|
|
172
|
+
expect(journal.of("git.head.moved").map((row) => [row.id, (row.data as { worktree: string }).worktree])).toEqual([
|
|
173
|
+
["look-1/emit/1", first.id],
|
|
174
|
+
["look-1/emit/2", second.id],
|
|
175
|
+
]);
|
|
176
|
+
|
|
177
|
+
// Made once more after it ended: the same answer, and nothing new.
|
|
178
|
+
const again = await run<typeof seen>(actions, journal, "git.head.observe", {}, "look-1");
|
|
179
|
+
expect(again).toEqual(seen);
|
|
180
|
+
expect(journal.of("git.head.moved")).toHaveLength(2);
|
|
181
|
+
});
|