@aisac/cli 0.4.0-beta.21 → 0.4.0-beta.23

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.
@@ -0,0 +1,106 @@
1
+ // Which room a launch belongs to (spec aisac-engineering-manager D3): the
2
+ // repository by default, so that every worktree of one repository shares a
3
+ // room, or the name given with `--room`. A manager never sees a worker of
4
+ // another room.
5
+
6
+ import { execFile } from 'node:child_process';
7
+ import { realpath } from 'node:fs/promises';
8
+ import { basename, dirname, resolve } from 'node:path';
9
+ import { BusError, NAME_PATTERN, roomIdFor, roomLabel } from './bus.mjs';
10
+
11
+ const GIT_TIMEOUT_MS = 3_000;
12
+ const GIT_OUTPUT_BYTES_MAX = 256 * 1024;
13
+ const GIT_DIRECTORY_NAME = '.git';
14
+ const WORKTREE_LINE = /^worktree (.+)$/u;
15
+
16
+ /** @typedef {(args: string[], cwd: string) => Promise<string | null>} GitRunner */
17
+ /**
18
+ * @typedef {object} ResolvedRoom
19
+ * @property {string} id
20
+ * @property {'repository' | 'named'} kind
21
+ * @property {string} label what a person calls the room: the repository's directory name, or the given name, without control characters (`roomLabel`)
22
+ */
23
+
24
+ /**
25
+ * Run git without a shell. Null when git is missing, fails or times out.
26
+ * @type {GitRunner}
27
+ */
28
+ export function runGit(args, cwd) {
29
+ return new Promise((resolvePromise) => {
30
+ execFile(
31
+ 'git',
32
+ ['-C', cwd, ...args],
33
+ {
34
+ timeout: GIT_TIMEOUT_MS,
35
+ maxBuffer: GIT_OUTPUT_BYTES_MAX,
36
+ env: { ...process.env, GIT_OPTIONAL_LOCKS: '0' },
37
+ windowsHide: true,
38
+ },
39
+ (error, stdout) => resolvePromise(error ? null : stdout),
40
+ );
41
+ });
42
+ }
43
+
44
+ /** @param {string} path */
45
+ async function realPathOrNull(path) {
46
+ return realpath(path).catch(() => null);
47
+ }
48
+
49
+ /**
50
+ * The real path of the git common directory, which every worktree of one
51
+ * repository shares; null outside a repository.
52
+ * @param {string} cwd
53
+ * @param {GitRunner} git
54
+ */
55
+ async function commonDirectory(cwd, git) {
56
+ const output = await git(['rev-parse', '--path-format=absolute', '--git-common-dir'], cwd);
57
+ const path = output?.trim() ?? '';
58
+ return path.startsWith('/') ? realPathOrNull(path) : null;
59
+ }
60
+
61
+ /**
62
+ * @param {{ cwd: string, roomName?: string | null, git?: GitRunner }} input
63
+ * @returns {Promise<ResolvedRoom>}
64
+ */
65
+ export async function resolveRoom({ cwd, roomName = null, git = runGit }) {
66
+ if (roomName !== null) {
67
+ if (!NAME_PATTERN.test(roomName)) throw new BusError('room_name_invalid');
68
+ return { id: roomIdFor(`named:${roomName}`), kind: 'named', label: roomName };
69
+ }
70
+ const common = await commonDirectory(cwd, git);
71
+ if (common !== null) {
72
+ const repository = basename(common) === GIT_DIRECTORY_NAME ? dirname(common) : common;
73
+ return {
74
+ id: roomIdFor(`repository:${common}`),
75
+ kind: 'repository',
76
+ label: roomLabel(basename(repository)),
77
+ };
78
+ }
79
+ const directory = (await realPathOrNull(cwd)) ?? resolve(cwd);
80
+ return {
81
+ id: roomIdFor(`repository:${directory}`),
82
+ kind: 'repository',
83
+ label: roomLabel(basename(directory)),
84
+ };
85
+ }
86
+
87
+ /**
88
+ * The real paths of the repository's worktrees, the working directory alone
89
+ * outside a repository. A delegated file change must stay inside one of them
90
+ * (FR13, class `files`).
91
+ * @param {string} cwd
92
+ * @param {GitRunner} [git]
93
+ * @returns {Promise<string[]>}
94
+ */
95
+ export async function repositoryRoots(cwd, git = runGit) {
96
+ const output = await git(['worktree', 'list', '--porcelain'], cwd);
97
+ /** @type {string[]} */
98
+ const roots = [];
99
+ for (const line of (output ?? '').split('\n')) {
100
+ const match = WORKTREE_LINE.exec(line);
101
+ const path = match?.[1] === undefined ? null : await realPathOrNull(match[1]);
102
+ if (path !== null && !roots.includes(path)) roots.push(path);
103
+ }
104
+ if (roots.length > 0) return roots;
105
+ return [(await realPathOrNull(cwd)) ?? resolve(cwd)];
106
+ }