@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,141 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The records of the git module: a worktree was made, a worktree was removed, the head of a worktree moved.
|
|
3
|
+
*
|
|
4
|
+
* A worktree is a checkout of a repository on a branch of its own, in a directory niwa keeps
|
|
5
|
+
* (`<home>/worktrees/<id>`), so an agent works beside the owner's checkout and not in it. The records
|
|
6
|
+
* hold where the worktree is and what it was made from, never the content of a file.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { EntityDescription, SessionRefSchema, idSchema, record } from "@niwa-labs/core";
|
|
10
|
+
import type { Id } from "@niwa-labs/core";
|
|
11
|
+
import { z } from "zod";
|
|
12
|
+
|
|
13
|
+
/** The id of a worktree: `wt-3fa9c2`. Niwa issues it; the branch of the worktree has the same name. */
|
|
14
|
+
export const WorktreeId = idSchema("worktree");
|
|
15
|
+
export type WorktreeId = Id<"worktree">;
|
|
16
|
+
|
|
17
|
+
/** A full commit id. */
|
|
18
|
+
export const CommitId = z.string().regex(/^[0-9a-f]{40,64}$/, "a full commit id");
|
|
19
|
+
|
|
20
|
+
/** A worktree as it was made. */
|
|
21
|
+
export const Worktree = z.object({
|
|
22
|
+
/** The id, which is also the name of its branch and of its directory. */
|
|
23
|
+
id: WorktreeId,
|
|
24
|
+
/** A few words a list shows. */
|
|
25
|
+
description: EntityDescription,
|
|
26
|
+
/** The top of the repository the worktree was made from. */
|
|
27
|
+
repo: z.string().min(1),
|
|
28
|
+
/** The directory of the worktree. */
|
|
29
|
+
path: z.string().min(1),
|
|
30
|
+
/** The branch the worktree is on: the id. */
|
|
31
|
+
branch: z.string().min(1),
|
|
32
|
+
/** What the worktree was made from: a branch name, or a commit when the repository's HEAD was detached. */
|
|
33
|
+
base: z.string().min(1),
|
|
34
|
+
/** The commit `base` named when the worktree was made. */
|
|
35
|
+
baseSha: CommitId,
|
|
36
|
+
});
|
|
37
|
+
export type Worktree = z.infer<typeof Worktree>;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* What a worktree is going to be made from, written before its checkout is: a create cut off after the checkout
|
|
41
|
+
* and made again takes the checkout it made from the same repository, base commit, branch and directory, not from
|
|
42
|
+
* what the base is by then. The id of the worktree is issued from this record.
|
|
43
|
+
*/
|
|
44
|
+
export const WorktreeReserved = record(
|
|
45
|
+
"git.worktree.reserved",
|
|
46
|
+
Worktree.extend({
|
|
47
|
+
/** The checkout asked for, when it differs from the repository that owns it. */
|
|
48
|
+
source: z.string().min(1).optional(),
|
|
49
|
+
}),
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
/** A worktree was made. */
|
|
53
|
+
export const WorktreeCreated = record("git.worktree.created", Worktree);
|
|
54
|
+
|
|
55
|
+
export const WorktreeCoordinates = Worktree.pick({id: true, path: true, repo: true});
|
|
56
|
+
|
|
57
|
+
/** The git plugin attests the checkout as the host before publishing its creation. */
|
|
58
|
+
export const CheckoutVerified = record("git.worktree.verified", WorktreeCoordinates.extend({
|
|
59
|
+
creation: z.string().min(1),
|
|
60
|
+
directories: z.array(z.string()).length(3),
|
|
61
|
+
}));
|
|
62
|
+
|
|
63
|
+
/** The exact registered checkout and project expected by an automated cleanup. */
|
|
64
|
+
export const RemovalMatch = Worktree.pick({ repo: true, path: true, branch: true }).extend({
|
|
65
|
+
head: CommitId,
|
|
66
|
+
project: z.strictObject({ host: z.string().min(1), path: z.string().min(1) }),
|
|
67
|
+
}).strict();
|
|
68
|
+
export type RemovalMatch = z.infer<typeof RemovalMatch>;
|
|
69
|
+
|
|
70
|
+
/** A removal was authorized against a particular checkout before invoking Git. */
|
|
71
|
+
export const WorktreeRemovalClaimed = record(
|
|
72
|
+
"git.worktree.removal.claimed",
|
|
73
|
+
z.object({ id: WorktreeId, onlyIf: RemovalMatch }),
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
/** A managed checkout was removed. Ordinary removal deletes the branch as well. */
|
|
77
|
+
export const WorktreeRemoved = record(
|
|
78
|
+
"git.worktree.removed",
|
|
79
|
+
z.object({
|
|
80
|
+
id: WorktreeId,
|
|
81
|
+
/** Whether it was removed past a refusal (dirty, ahead of its base, a live session in it). */
|
|
82
|
+
forced: z.boolean(),
|
|
83
|
+
branchKept: z.literal(true).optional(),
|
|
84
|
+
}),
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* The head of a worktree is on another commit than the last one recorded for it (its base commit, at first): work
|
|
89
|
+
* was committed, or the checkout moved. It names the session whose turn ended when it was seen, when that is who
|
|
90
|
+
* moved it; without one, the record is of whoever wrote it (the run that looked).
|
|
91
|
+
*/
|
|
92
|
+
export const HeadMoved = record(
|
|
93
|
+
"git.head.moved",
|
|
94
|
+
z.object({
|
|
95
|
+
worktree: WorktreeId,
|
|
96
|
+
/** The top of the repository the worktree was made from. */
|
|
97
|
+
repo: z.string().min(1),
|
|
98
|
+
/** The directory of the worktree. */
|
|
99
|
+
path: z.string().min(1),
|
|
100
|
+
/** The branch the head is on; absent when it is detached. */
|
|
101
|
+
branch: z.string().min(1).optional(),
|
|
102
|
+
head: CommitId,
|
|
103
|
+
/** The commit recorded for it before. */
|
|
104
|
+
previous: CommitId,
|
|
105
|
+
/** The session, as `<harness> <native>`, whose turn ended in the worktree when the head was seen to move. */
|
|
106
|
+
session: SessionRefSchema.optional(),
|
|
107
|
+
}),
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* The remote a worktree's work goes to, as the repository's own configuration says it: the remote a push of its
|
|
112
|
+
* base goes to. It is shown only when it is verified: the remote is configured, its fetch and push URLs name the
|
|
113
|
+
* same project, and that project is on a server (`host`, lower case, no port) at a path of at least a group and a
|
|
114
|
+
* name, without `.git`. A remote on this machine, or one that pushes elsewhere, is not shown.
|
|
115
|
+
*/
|
|
116
|
+
export const WorktreeRemote = z.object({
|
|
117
|
+
/** The name of the remote in the repository. */
|
|
118
|
+
name: z.string().min(1),
|
|
119
|
+
host: z.string().min(1),
|
|
120
|
+
path: z.string().min(1),
|
|
121
|
+
});
|
|
122
|
+
export type WorktreeRemote = z.infer<typeof WorktreeRemote>;
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* The verified remote of a worktree is another than the last one recorded for it (none, at first): it was set, it
|
|
126
|
+
* changed, or it is gone or no longer verifies. The project it names is by path; which project that is on the
|
|
127
|
+
* server is for the module that talks to the server.
|
|
128
|
+
*/
|
|
129
|
+
export const RemoteUpdated = record(
|
|
130
|
+
"git.remote.updated",
|
|
131
|
+
z.object({
|
|
132
|
+
worktree: WorktreeId,
|
|
133
|
+
/** The top of the repository the worktree was made from. */
|
|
134
|
+
repo: z.string().min(1),
|
|
135
|
+
/** The remote now; absent when there is none that verifies. */
|
|
136
|
+
remote: WorktreeRemote.optional(),
|
|
137
|
+
}),
|
|
138
|
+
);
|
|
139
|
+
|
|
140
|
+
/** Every record type of the git module. */
|
|
141
|
+
export const GIT_TYPES = [WorktreeReserved, CheckoutVerified, WorktreeCreated, WorktreeRemovalClaimed, WorktreeRemoved, HeadMoved, RemoteUpdated] as const;
|
package/src/records.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { lstatSync, realpathSync } from "node:fs";
|
|
2
|
+
import { join, relative, sep } from "node:path";
|
|
3
|
+
import type { z } from "zod";
|
|
4
|
+
|
|
5
|
+
import { recordId } from "@niwa-labs/core";
|
|
6
|
+
import type { PluginApi } from "@niwa-labs/plugin";
|
|
7
|
+
import { CheckoutVerified, WorktreeCoordinates, WorktreeCreated, WorktreeId, WorktreeRemoved } from "./record-types.ts";
|
|
8
|
+
import type { Worktree } from "./record-types.ts";
|
|
9
|
+
|
|
10
|
+
type Coordinates = z.infer<typeof WorktreeCoordinates>;
|
|
11
|
+
|
|
12
|
+
const receiptId = (id: string) => recordId(`git/worktree/${id}/verified`);
|
|
13
|
+
|
|
14
|
+
function identities({id, repo, path}: Coordinates): string[] | undefined {
|
|
15
|
+
const common = join(repo, ".git");
|
|
16
|
+
try {
|
|
17
|
+
return [path, common, join(common, "worktrees", id)].map(dir => {
|
|
18
|
+
if (realpathSync(dir) !== dir) throw new Error("noncanonical directory");
|
|
19
|
+
const stat = lstatSync(dir, {bigint: true});
|
|
20
|
+
if (!stat.isDirectory()) throw new Error("missing directory");
|
|
21
|
+
return `${stat.dev}:${stat.ino}`;
|
|
22
|
+
});
|
|
23
|
+
} catch {
|
|
24
|
+
return undefined;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export async function verifyCheckout(api: PluginApi, tree: Worktree, creation: string): Promise<void> {
|
|
29
|
+
const coordinates = WorktreeCoordinates.parse(tree);
|
|
30
|
+
const directories = identities(coordinates);
|
|
31
|
+
if (directories) {
|
|
32
|
+
await api.emit(CheckoutVerified, {...coordinates, creation, directories}, {id: receiptId(tree.id)});
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Resolve only a still-live checkout in this host's own managed store. */
|
|
37
|
+
export function registeredWorktree(api: Pick<PluginApi, "home" | "journal">, cwd: string): Coordinates | undefined {
|
|
38
|
+
const journal = api.journal?.();
|
|
39
|
+
if (!journal) return undefined;
|
|
40
|
+
let store: string, dir: string;
|
|
41
|
+
try {
|
|
42
|
+
store = join(realpathSync(api.home()), "worktrees");
|
|
43
|
+
dir = realpathSync(cwd);
|
|
44
|
+
} catch {
|
|
45
|
+
return undefined;
|
|
46
|
+
}
|
|
47
|
+
const id = WorktreeId.safeParse(relative(store, dir).split(sep)[0]);
|
|
48
|
+
if (!id.success) return undefined;
|
|
49
|
+
const stamp = journal.get(receiptId(id.data));
|
|
50
|
+
if (stamp?.actor.kind !== "host" || stamp.type !== CheckoutVerified.name) return undefined;
|
|
51
|
+
const proof = CheckoutVerified.schema.safeParse(stamp.data);
|
|
52
|
+
if (!proof.success || proof.data.id !== id.data || proof.data.path !== join(store, id.data)) return undefined;
|
|
53
|
+
const created = journal.get(recordId(proof.data.creation));
|
|
54
|
+
if (created?.type !== WorktreeCreated.name || created.seq <= stamp.seq) return undefined;
|
|
55
|
+
const tree = WorktreeCreated.schema.safeParse(created.data);
|
|
56
|
+
if (!tree.success || tree.data.id !== id.data || tree.data.path !== proof.data.path || tree.data.repo !== proof.data.repo) return undefined;
|
|
57
|
+
if (journal.find({type: WorktreeRemoved.name, field: "id", equals: id.data}).length) return undefined;
|
|
58
|
+
const current = identities(proof.data);
|
|
59
|
+
if (!current?.every((identity, i) => identity === proof.data.directories[i])) return undefined;
|
|
60
|
+
return WorktreeCoordinates.parse(tree.data);
|
|
61
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
// The cleanup of old worktrees, a built-in workflow of the git module: every day, on
|
|
2
|
+
// the timer the module offers, it lists the worktrees and removes the ones older than the days of `git({ cleanup })`
|
|
3
|
+
// that are clean, have no commit their base lacks and no live session in them. A dirty or unmerged worktree is never
|
|
4
|
+
// touched; removing it is the owner's (`git.worktree.remove` with `force`).
|
|
5
|
+
//
|
|
6
|
+
// The workflow module registers and installs it when the host starts; its runs hold `uses` and nothing more.
|
|
7
|
+
|
|
8
|
+
import type { ScriptApi } from "@niwa-labs/script";
|
|
9
|
+
|
|
10
|
+
declare const niwa: ScriptApi["niwa"];
|
|
11
|
+
declare const workflow: ScriptApi["workflow"];
|
|
12
|
+
declare const z: ScriptApi["z"];
|
|
13
|
+
|
|
14
|
+
/** One worktree as `git.worktree.list` shows it. */
|
|
15
|
+
interface Listed {
|
|
16
|
+
id: string;
|
|
17
|
+
state: "ok" | "missing" | "orphan";
|
|
18
|
+
ageDays?: number;
|
|
19
|
+
dirty?: boolean;
|
|
20
|
+
aheadOfBase?: number;
|
|
21
|
+
sessions?: string[];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export default workflow(
|
|
25
|
+
{
|
|
26
|
+
on: [{ type: "timer.fired", filter: { timer: "git-worktree-cleanup" } }],
|
|
27
|
+
uses: ["git.cleanup.config", "git.worktree.list", "git.worktree.remove"],
|
|
28
|
+
output: z.array(z.string()),
|
|
29
|
+
},
|
|
30
|
+
async () => {
|
|
31
|
+
const { days } = await niwa.git.cleanup.config({}, { label: "config" });
|
|
32
|
+
const { items } = (await niwa.git.worktree.list({}, { label: "list" })) as { items: Listed[] };
|
|
33
|
+
const old = items.filter(
|
|
34
|
+
(item) =>
|
|
35
|
+
item.state === "ok" &&
|
|
36
|
+
(item.ageDays ?? 0) >= days &&
|
|
37
|
+
item.dirty === false &&
|
|
38
|
+
item.aheadOfBase === 0 &&
|
|
39
|
+
item.sessions?.length === 0,
|
|
40
|
+
);
|
|
41
|
+
const removed: string[] = [];
|
|
42
|
+
for (const item of old) {
|
|
43
|
+
// Removed without force: one that changed since the list is refused, and kept.
|
|
44
|
+
try {
|
|
45
|
+
await niwa.git.worktree.remove({ id: item.id }, { label: `remove-${item.id}` });
|
|
46
|
+
removed.push(item.id);
|
|
47
|
+
} catch (error) {
|
|
48
|
+
// A refusal keeps it: it is not clean any more, or a session started in it. Anything else is a failure.
|
|
49
|
+
if (!(error instanceof Error) || error.name !== "WorktreeRefusal") throw error;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return removed;
|
|
53
|
+
},
|
|
54
|
+
);
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// The host program of the replay of a cut-off worktree create (test/git-worktree-replay.test.ts).
|
|
2
|
+
//
|
|
3
|
+
// The plugin declares the record Start { repo }. Its handler makes a worktree of the repository as one
|
|
4
|
+
// step. The test parks the step inside `git worktree add` (a hook of the repository waits on a gate),
|
|
5
|
+
// kills the host, and starts it again: the step is made again under the same identity.
|
|
6
|
+
//
|
|
7
|
+
// The home directory comes from argv[2]. The test imports this file for the Start record type, so
|
|
8
|
+
// the host only starts when the file is run directly.
|
|
9
|
+
|
|
10
|
+
import { EntityDescription, record } from "@niwa-labs/core";
|
|
11
|
+
import { plugin } from "@niwa-labs/plugin";
|
|
12
|
+
import { createNiwa } from "@niwa-labs/testing";
|
|
13
|
+
import { git, gitWorktreeCreate, gitWorktreeRemove } from "../../src/index.ts";
|
|
14
|
+
import { z } from "zod";
|
|
15
|
+
|
|
16
|
+
export const Remove = record("git-replay.remove", gitWorktreeRemove.input, { open: true });
|
|
17
|
+
export const Start = record("git-replay.start", z.object({ repo: z.string() }), { open: true });
|
|
18
|
+
|
|
19
|
+
const replay = plugin("git-replay", (api) => {
|
|
20
|
+
api.declare(Start, Remove);
|
|
21
|
+
api.on(Remove, async (data, step) => {
|
|
22
|
+
await step(gitWorktreeRemove, data);
|
|
23
|
+
});
|
|
24
|
+
api.on(Start, async (data, step) => {
|
|
25
|
+
await step(gitWorktreeCreate, { repo: data.repo, description: EntityDescription.parse("made once") });
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
if (import.meta.main) {
|
|
30
|
+
const home = process.argv[2];
|
|
31
|
+
if (!home) {
|
|
32
|
+
console.error("usage: bun git-worktree-host.ts <home>");
|
|
33
|
+
process.exit(2);
|
|
34
|
+
}
|
|
35
|
+
const gitPath = process.argv[3];
|
|
36
|
+
if (gitPath !== undefined) process.env.PATH = `${gitPath}:${process.env.PATH}`;
|
|
37
|
+
const niwa = createNiwa({ home });
|
|
38
|
+
niwa.use(git({ cleanup: false }), replay);
|
|
39
|
+
await niwa.serve();
|
|
40
|
+
process.on("SIGTERM", () => {
|
|
41
|
+
void niwa.close().then(() => process.exit(0));
|
|
42
|
+
});
|
|
43
|
+
}
|
package/test/git-repo.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// A real git repository in a temporary directory, for the tests of the git module. Git runs with no
|
|
2
|
+
// global or system configuration, so the owner's hooks, signing and default branch do not reach it;
|
|
3
|
+
// the host processes the tests spawn inherit the same environment.
|
|
4
|
+
|
|
5
|
+
import { mkdirSync, realpathSync, writeFileSync } from "node:fs";
|
|
6
|
+
import { join } from "node:path";
|
|
7
|
+
|
|
8
|
+
import { tempDir } from "@niwa-labs/testing";
|
|
9
|
+
|
|
10
|
+
process.env.GIT_CONFIG_GLOBAL = "/dev/null";
|
|
11
|
+
process.env.GIT_CONFIG_NOSYSTEM = "1";
|
|
12
|
+
process.env.GIT_AUTHOR_NAME = "niwa test";
|
|
13
|
+
process.env.GIT_AUTHOR_EMAIL = "test@niwa.invalid";
|
|
14
|
+
process.env.GIT_COMMITTER_NAME = "niwa test";
|
|
15
|
+
process.env.GIT_COMMITTER_EMAIL = "test@niwa.invalid";
|
|
16
|
+
|
|
17
|
+
/** Run git in a directory; its output, trimmed. */
|
|
18
|
+
export function gitIn(dir: string, ...args: string[]): string {
|
|
19
|
+
const result = Bun.spawnSync(["git", "-C", dir, ...args], { stdout: "pipe", stderr: "pipe" });
|
|
20
|
+
if (result.exitCode !== 0) throw new Error(`git ${args.join(" ")} failed: ${result.stderr.toString()}`);
|
|
21
|
+
return result.stdout.toString().trim();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* A repository on `main` with one commit, a `.gitignore` and a `.worktreeinclude`, and four untracked
|
|
26
|
+
* files: `.env` and `node_modules/pkg/index.js` (ignored and named by the include), `secret.local`
|
|
27
|
+
* (ignored, not named) and `plain.txt` (named, not ignored).
|
|
28
|
+
*/
|
|
29
|
+
export async function makeRepo(): Promise<string> {
|
|
30
|
+
const repo = realpathSync(await tempDir("git-repo"));
|
|
31
|
+
gitIn(repo, "init", "-q", "-b", "main");
|
|
32
|
+
writeFileSync(join(repo, ".gitignore"), ".env\nsecret.local\nnode_modules/\n");
|
|
33
|
+
writeFileSync(join(repo, ".worktreeinclude"), ".env\nnode_modules/\nplain.txt\n");
|
|
34
|
+
mkdirSync(join(repo, "src"));
|
|
35
|
+
writeFileSync(join(repo, "src", "main.ts"), "export {};\n");
|
|
36
|
+
writeFileSync(join(repo, "README.md"), "a repository\n");
|
|
37
|
+
gitIn(repo, "add", ".");
|
|
38
|
+
gitIn(repo, "commit", "-q", "-m", "start");
|
|
39
|
+
writeFileSync(join(repo, ".env"), "TOKEN=1\n");
|
|
40
|
+
writeFileSync(join(repo, "secret.local"), "secret\n");
|
|
41
|
+
mkdirSync(join(repo, "node_modules", "pkg"), { recursive: true });
|
|
42
|
+
writeFileSync(join(repo, "node_modules", "pkg", "index.js"), "module.exports = 1;\n");
|
|
43
|
+
writeFileSync(join(repo, "plain.txt"), "plain\n");
|
|
44
|
+
return repo;
|
|
45
|
+
}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
// A worktree create cut off by a crash is made again under the same step identity, and finds what it
|
|
2
|
+
// made: the host is killed while `git worktree add` waits in a hook of the repository (the directory
|
|
3
|
+
// and the branch exist, the record does not), and the host that starts again takes that checkout as
|
|
4
|
+
// the worktree. One record, one directory, one branch.
|
|
5
|
+
|
|
6
|
+
import { expect, test } from "bun:test";
|
|
7
|
+
import { chmodSync, existsSync, mkdirSync, readdirSync, realpathSync, writeFileSync } from "node:fs";
|
|
8
|
+
import { join } from "node:path";
|
|
9
|
+
import { fileURLToPath } from "node:url";
|
|
10
|
+
|
|
11
|
+
import { eventually, gate, spawnHost, tempHome, connectAsOwner, lastState, watch } from "@niwa-labs/testing";
|
|
12
|
+
|
|
13
|
+
import { gitWorktreeCreate } from "../src/index.ts";
|
|
14
|
+
import { Start, Remove } from "./fixtures/git-worktree-host.ts";
|
|
15
|
+
import { gitIn, makeRepo } from "./git-repo.ts";
|
|
16
|
+
|
|
17
|
+
const HOST_SCRIPT = fileURLToPath(new URL("./fixtures/git-worktree-host.ts", import.meta.url));
|
|
18
|
+
|
|
19
|
+
test("a create cut off inside git is made again under the same identity and takes the checkout it made", async () => {
|
|
20
|
+
const home = await tempHome();
|
|
21
|
+
const repo = await makeRepo();
|
|
22
|
+
const inside = join(home.path, "inside-hook");
|
|
23
|
+
const opened = join(home.path, "gates", "hook");
|
|
24
|
+
// The checkout of the new worktree runs this hook: it says it is there, then waits for the gate.
|
|
25
|
+
const hook = join(repo, ".git", "hooks", "post-checkout");
|
|
26
|
+
writeFileSync(hook, `#!/bin/sh\ntouch "${inside}"\nwhile [ ! -f "${opened}" ]; do sleep 0.05; done\n`);
|
|
27
|
+
chmodSync(hook, 0o755);
|
|
28
|
+
|
|
29
|
+
let first: ReturnType<typeof spawnHost> | undefined;
|
|
30
|
+
let second: ReturnType<typeof spawnHost> | undefined;
|
|
31
|
+
let observer: Awaited<ReturnType<typeof watch>> | undefined;
|
|
32
|
+
try {
|
|
33
|
+
first = spawnHost(HOST_SCRIPT, home.path);
|
|
34
|
+
await first.ready();
|
|
35
|
+
observer = await watch(home.path);
|
|
36
|
+
const client = await connectAsOwner(home.path);
|
|
37
|
+
await client.emit(Start, { repo });
|
|
38
|
+
await eventually(() => existsSync(inside), 15_000, "the create should reach the hook");
|
|
39
|
+
|
|
40
|
+
await first.kill();
|
|
41
|
+
await client.close().catch(() => {});
|
|
42
|
+
gate(home.path, "hook").open();
|
|
43
|
+
|
|
44
|
+
second = spawnHost(HOST_SCRIPT, home.path);
|
|
45
|
+
await second.ready();
|
|
46
|
+
const seen = observer.seen;
|
|
47
|
+
await eventually(() => lastState(seen)?.state === "done", 20_000, "the handler should end as done");
|
|
48
|
+
|
|
49
|
+
const created = seen.filter((row) => row.type === "git.worktree.created").map((row) => row.data as { id: string; path: string });
|
|
50
|
+
expect(created).toHaveLength(1);
|
|
51
|
+
const [made] = created;
|
|
52
|
+
if (made === undefined) return;
|
|
53
|
+
const store = join(realpathSync(home.path), "worktrees");
|
|
54
|
+
expect(made.path).toBe(join(store, made.id));
|
|
55
|
+
expect(readdirSync(store)).toEqual([made.id]);
|
|
56
|
+
expect(gitIn(made.path, "symbolic-ref", "--short", "HEAD")).toBe(made.id);
|
|
57
|
+
expect(gitIn(repo, "branch", "--list", "wt-*").split("\n").map((line) => line.replace(/^[*+ ]+/, "").trim())).toEqual([made.id]);
|
|
58
|
+
} finally {
|
|
59
|
+
await observer?.stop();
|
|
60
|
+
await first?.kill().catch(() => {});
|
|
61
|
+
await second?.stop().catch(() => {});
|
|
62
|
+
await home.cleanup();
|
|
63
|
+
}
|
|
64
|
+
}, 60_000);
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
test("a matched removal interrupted after Git is settled once on restart", async () => {
|
|
68
|
+
const home = await tempHome();
|
|
69
|
+
const repo = await makeRepo();
|
|
70
|
+
gitIn(repo, "rm", "-q", ".worktreeinclude");
|
|
71
|
+
gitIn(repo, "commit", "-q", "-m", "remove checkout fixture copies");
|
|
72
|
+
gitIn(repo, "remote", "add", "origin", "https://example.invalid/cleanup/repo.git");
|
|
73
|
+
const bin = join(home.path, "bin");
|
|
74
|
+
const marker = join(home.path, "git-removed");
|
|
75
|
+
const barrier = join(home.path, "gates", "removed");
|
|
76
|
+
const realGit = Bun.which("git");
|
|
77
|
+
if (realGit === null) throw new Error("git is required");
|
|
78
|
+
mkdirSync(bin);
|
|
79
|
+
const wrapper = join(bin, "git");
|
|
80
|
+
writeFileSync(wrapper, [
|
|
81
|
+
"#!/bin/sh",
|
|
82
|
+
`"${realGit}" "$@"`,
|
|
83
|
+
"result=$?",
|
|
84
|
+
'if [ "$result" = 0 ] && [ "$3" = worktree ] && [ "$4" = remove ]; then',
|
|
85
|
+
` touch "${marker}"`,
|
|
86
|
+
` while [ ! -f "${barrier}" ]; do sleep 0.05; done`,
|
|
87
|
+
"fi",
|
|
88
|
+
'exit "$result"',
|
|
89
|
+
].join("\n"));
|
|
90
|
+
chmodSync(wrapper, 0o755);
|
|
91
|
+
let first: ReturnType<typeof spawnHost> | undefined;
|
|
92
|
+
let second: ReturnType<typeof spawnHost> | undefined;
|
|
93
|
+
let observer: Awaited<ReturnType<typeof watch>> | undefined;
|
|
94
|
+
let owner: Awaited<ReturnType<typeof connectAsOwner>> | undefined;
|
|
95
|
+
try {
|
|
96
|
+
first = spawnHost(HOST_SCRIPT, home.path, [bin]);
|
|
97
|
+
await first.ready();
|
|
98
|
+
observer = await watch(home.path);
|
|
99
|
+
owner = await connectAsOwner(home.path);
|
|
100
|
+
const tree = await owner.call(gitWorktreeCreate, { repo, description: "removed before the action could finish" });
|
|
101
|
+
const onlyIf = { repo: tree.repo, path: tree.path, branch: tree.branch, head: tree.baseSha, project: { host: "example.invalid", path: "cleanup/repo" } };
|
|
102
|
+
await owner.emit(Remove, { id: tree.id, onlyIf });
|
|
103
|
+
await eventually(() => existsSync(marker), 15_000, "Git should have removed the checkout");
|
|
104
|
+
expect(existsSync(tree.path)).toBe(false);
|
|
105
|
+
expect(observer.seen.some((row) => row.type === "git.worktree.removal.claimed")).toBe(true);
|
|
106
|
+
expect(observer.seen.some((row) => row.type === "git.worktree.removed")).toBe(false);
|
|
107
|
+
|
|
108
|
+
await first.kill();
|
|
109
|
+
await owner.close();
|
|
110
|
+
gate(home.path, "removed").open();
|
|
111
|
+
second = spawnHost(HOST_SCRIPT, home.path);
|
|
112
|
+
await second.ready();
|
|
113
|
+
const seen = observer.seen;
|
|
114
|
+
await eventually(() => lastState(seen)?.state === "done", 15_000, "removal should finish after restart");
|
|
115
|
+
expect(seen.filter((row) => row.type === "git.worktree.removal.claimed")).toHaveLength(1);
|
|
116
|
+
expect(seen.filter((row) => row.type === "git.worktree.removed").map((row) => row.data)).toEqual([{ id: tree.id, forced: false, branchKept: true }]);
|
|
117
|
+
expect(gitIn(repo, "rev-parse", tree.branch)).toBe(tree.baseSha);
|
|
118
|
+
} finally {
|
|
119
|
+
gate(home.path, "removed").open();
|
|
120
|
+
await owner?.close().catch(() => {});
|
|
121
|
+
await observer?.stop();
|
|
122
|
+
await first?.kill().catch(() => {});
|
|
123
|
+
await second?.stop().catch(() => {});
|
|
124
|
+
await home.cleanup();
|
|
125
|
+
}
|
|
126
|
+
}, 45_000);
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import {expect, test} from "bun:test";
|
|
2
|
+
import {mkdirSync, realpathSync, renameSync} from "node:fs";
|
|
3
|
+
import {join} from "node:path";
|
|
4
|
+
import {tempHome} from "@niwa-labs/testing";
|
|
5
|
+
import {recordId, seq, timestamp} from "@niwa-labs/core";
|
|
6
|
+
import type {JournalReader, StoredRecord} from "@niwa-labs/core";
|
|
7
|
+
import type {PluginApi} from "@niwa-labs/plugin";
|
|
8
|
+
import {registeredWorktree, verifyCheckout} from "../src/registration.ts";
|
|
9
|
+
import {CheckoutVerified, WorktreeCreated, WorktreeRemoved, WorktreeId} from "../src/record-types.ts";
|
|
10
|
+
|
|
11
|
+
test("the registration requires a host receipt, a matching creation, unchanged directories and no removal", async () => {
|
|
12
|
+
const home = await tempHome();
|
|
13
|
+
try {
|
|
14
|
+
const dir = realpathSync(home.path);
|
|
15
|
+
const id = WorktreeId.parse("wt-aaa111");
|
|
16
|
+
const path = join(dir, "worktrees", id);
|
|
17
|
+
const repo = join(dir, "repo");
|
|
18
|
+
const common = join(repo, ".git");
|
|
19
|
+
mkdirSync(join(common, "worktrees", id), {recursive: true});
|
|
20
|
+
mkdirSync(join(path, "src"), {recursive: true});
|
|
21
|
+
const rows: StoredRecord[] = [];
|
|
22
|
+
const reader = {
|
|
23
|
+
get: (record: string) => rows.find(row => row.id === record),
|
|
24
|
+
find: ({type, equals}: {type: string; equals: string}) => rows.filter(row => row.type === type && (row.data as {id: string}).id === equals),
|
|
25
|
+
} as JournalReader;
|
|
26
|
+
const api = {
|
|
27
|
+
home: () => dir, journal: () => reader,
|
|
28
|
+
emit: (type: typeof CheckoutVerified, data: unknown, opts: {id: string}) => {
|
|
29
|
+
rows.push({id: recordId(opts.id), data, type: type.name, actor: {kind: "host"}, seq: seq(1), at: timestamp(0)} as StoredRecord);
|
|
30
|
+
},
|
|
31
|
+
} as PluginApi;
|
|
32
|
+
const tree = {id, path, repo, description: "fixture", base: "main", baseSha: "a".repeat(40), branch: id};
|
|
33
|
+
const lookup = () => registeredWorktree(api, join(path, "src"));
|
|
34
|
+
expect(lookup()).toBeUndefined();
|
|
35
|
+
await verifyCheckout(api, tree, "creation");
|
|
36
|
+
expect(lookup()).toBeUndefined();
|
|
37
|
+
const [receipt] = rows;
|
|
38
|
+
if (!receipt) throw new Error("missing fixture receipt");
|
|
39
|
+
rows.push({...receipt, id: recordId("creation"), seq: seq(2), type: WorktreeCreated.name, data: tree});
|
|
40
|
+
expect(lookup()).toEqual({id, repo, path});
|
|
41
|
+
|
|
42
|
+
rows[0] = {...receipt, actor: {kind: "client", id: "fixture" as never}};
|
|
43
|
+
expect(lookup()).toBeUndefined();
|
|
44
|
+
rows[0] = {...receipt, actor: {kind: "handler", handler: "test/a" as never, invocation: "fixture" as never}};
|
|
45
|
+
expect(lookup()).toBeUndefined();
|
|
46
|
+
rows[0] = receipt;
|
|
47
|
+
renameSync(common, join(repo, "old"));
|
|
48
|
+
mkdirSync(join(common, "worktrees", id), {recursive: true});
|
|
49
|
+
expect(lookup()).toBeUndefined();
|
|
50
|
+
renameSync(common, join(repo, "other"));
|
|
51
|
+
renameSync(join(repo, "old"), common);
|
|
52
|
+
expect(lookup()).toBeDefined();
|
|
53
|
+
rows.push({...receipt, id: recordId("removal"), seq: seq(3), type: WorktreeRemoved.name, data: {id, forced: false}});
|
|
54
|
+
expect(lookup()).toBeUndefined();
|
|
55
|
+
} finally { await home.cleanup(); }
|
|
56
|
+
});
|
package/test/replay.ts
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// The git module's actions run by hand on a journal in memory, so a call can be cut off at a chosen record and made
|
|
2
|
+
// again under the same operation, as the host makes it again after a crash. The records are numbered as the host
|
|
3
|
+
// numbers them (`<operation>/emit/<n>`); a record written again must say what it said, as the journal demands.
|
|
4
|
+
|
|
5
|
+
import { stableJson } from "@niwa-labs/core";
|
|
6
|
+
import type { Action, ActionContext, JournalReader, RecordType, StoredRecord } from "@niwa-labs/core";
|
|
7
|
+
import type { PluginApi } from "@niwa-labs/plugin";
|
|
8
|
+
|
|
9
|
+
import { git } from "../src/index.ts";
|
|
10
|
+
|
|
11
|
+
/** The records written so far, as the module reads and the host keeps them. */
|
|
12
|
+
export class MemoryJournal {
|
|
13
|
+
readonly records: StoredRecord[] = [];
|
|
14
|
+
|
|
15
|
+
reader(): JournalReader {
|
|
16
|
+
const view = {
|
|
17
|
+
get: (id: string) => this.records.find((row) => row.id === id),
|
|
18
|
+
since: (_from: unknown, types?: string[]) => this.records.filter((row) => types === undefined || types.includes(row.type)),
|
|
19
|
+
last: () => this.records.length,
|
|
20
|
+
};
|
|
21
|
+
return view as unknown as JournalReader;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
of(type: string): StoredRecord[] {
|
|
25
|
+
return this.records.filter((row) => row.type === type);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** A call cut off: what an action throws when the host stops under it. */
|
|
30
|
+
export class CutOff extends Error {}
|
|
31
|
+
|
|
32
|
+
/** The actions of the git module over a journal in memory, in a home. */
|
|
33
|
+
export function module(journal: MemoryJournal, home: string): ReadonlyMap<string, Action<any, any>> {
|
|
34
|
+
const actions = new Map<string, Action<any, any>>();
|
|
35
|
+
const api = {
|
|
36
|
+
home: () => home,
|
|
37
|
+
emit: async (type: RecordType<any>, data: unknown, opts: {id: string}) => {
|
|
38
|
+
const existing = journal.reader().get(opts.id as StoredRecord["id"]);
|
|
39
|
+
if (existing) return existing;
|
|
40
|
+
const row = {id: opts.id, data, type: type.name, actor: {kind: "host"}, seq: journal.records.length + 1} as StoredRecord;
|
|
41
|
+
journal.records.push(row);
|
|
42
|
+
return row;
|
|
43
|
+
},
|
|
44
|
+
journal: () => journal.reader(),
|
|
45
|
+
action: (made: Action<any, any>) => void actions.set(made.name, made),
|
|
46
|
+
offer: () => {},
|
|
47
|
+
declare: () => {},
|
|
48
|
+
subscribers: () => {},
|
|
49
|
+
} as unknown as PluginApi;
|
|
50
|
+
git({ cleanup: false }).setup(api);
|
|
51
|
+
return actions;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Run an action under an operation. `cutAt` is the number of the record whose write the host is cut off at: the
|
|
56
|
+
* records before it are written, it and the ones after are not, and the call ends with `CutOff`.
|
|
57
|
+
*/
|
|
58
|
+
export async function run<O>(
|
|
59
|
+
actions: ReadonlyMap<string, Action<any, any>>,
|
|
60
|
+
journal: MemoryJournal,
|
|
61
|
+
name: string,
|
|
62
|
+
input: unknown,
|
|
63
|
+
operation: string,
|
|
64
|
+
cutAt?: number,
|
|
65
|
+
): Promise<O> {
|
|
66
|
+
const found = actions.get(name);
|
|
67
|
+
if (found === undefined) throw new Error(`no action ${name}`);
|
|
68
|
+
let ordinal = 0;
|
|
69
|
+
const ctx = {
|
|
70
|
+
operationId: operation,
|
|
71
|
+
caller: { kind: "owner" },
|
|
72
|
+
emit: async <T>(type: RecordType<T>, data: T): Promise<StoredRecord> => {
|
|
73
|
+
ordinal += 1;
|
|
74
|
+
if (ordinal === cutAt) throw new CutOff(`cut off before record ${String(ordinal)}`);
|
|
75
|
+
const id = `${operation}/emit/${String(ordinal)}`;
|
|
76
|
+
const earlier = journal.records.find((row) => row.id === id);
|
|
77
|
+
if (earlier !== undefined) {
|
|
78
|
+
if (earlier.type !== type.name || stableJson(earlier.data) !== stableJson(data)) {
|
|
79
|
+
throw new Error(`replay mismatch at ${id}: it was ${earlier.type}, and is ${type.name} now`);
|
|
80
|
+
}
|
|
81
|
+
return earlier;
|
|
82
|
+
}
|
|
83
|
+
const stored = { id, seq: journal.records.length + 1, type: type.name, data, at: Date.now() } as unknown as StoredRecord;
|
|
84
|
+
journal.records.push(stored);
|
|
85
|
+
return stored;
|
|
86
|
+
},
|
|
87
|
+
} as unknown as ActionContext;
|
|
88
|
+
return (await found.run(found.input.parse(input), ctx)) as O;
|
|
89
|
+
}
|
package/test/served.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// A host with the git module and nothing else, on a temporary home, for the tests of the git package.
|
|
2
|
+
|
|
3
|
+
import { realpathSync } from "node:fs";
|
|
4
|
+
|
|
5
|
+
import { createNiwa, tempHome } from "@niwa-labs/testing";
|
|
6
|
+
import type { Host } from "@niwa-labs/testing";
|
|
7
|
+
|
|
8
|
+
import { git } from "../src/index.ts";
|
|
9
|
+
import { WorktreeRefusal } from "../src/index.ts";
|
|
10
|
+
|
|
11
|
+
/** What a test holds open, closed after it. */
|
|
12
|
+
export interface Opened {
|
|
13
|
+
readonly hosts: Host[];
|
|
14
|
+
readonly homes: Array<{ cleanup(): Promise<void> }>;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function opened(): Opened {
|
|
18
|
+
return { hosts: [], homes: [] };
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export async function closeAll(held: Opened): Promise<void> {
|
|
22
|
+
for (const host of held.hosts.splice(0)) await host.close().catch(() => {});
|
|
23
|
+
for (const home of held.homes.splice(0)) await home.cleanup();
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** A served host with the git module (no cleanup timer), and its home with links resolved. */
|
|
27
|
+
export async function served(held: Opened): Promise<{ host: Host; home: string }> {
|
|
28
|
+
const home = await tempHome();
|
|
29
|
+
held.homes.push(home);
|
|
30
|
+
const host = createNiwa({ home: home.path });
|
|
31
|
+
host.use(git({ cleanup: false }));
|
|
32
|
+
await host.serve();
|
|
33
|
+
held.hosts.push(host);
|
|
34
|
+
return { host, home: realpathSync(home.path) };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** The refusal code a call ended with. */
|
|
38
|
+
export async function refusalOf(call: Promise<unknown>): Promise<string> {
|
|
39
|
+
try {
|
|
40
|
+
await call;
|
|
41
|
+
} catch (error) {
|
|
42
|
+
if (!(error instanceof WorktreeRefusal)) throw error;
|
|
43
|
+
return error.code;
|
|
44
|
+
}
|
|
45
|
+
throw new Error("the call was not refused");
|
|
46
|
+
}
|