loopctl-mcp-server 3.32.0 → 3.33.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/README.md +12 -13
- package/index.js +345 -475
- package/lib/checkpoints.js +138 -0
- package/lib/escalation-playbook.js +0 -1
- package/lib/holder-key-file.js +1 -1
- package/lib/holder.js +4 -4
- package/lib/{claim-session.js → lock-session.js} +28 -32
- package/lib/messages.js +12 -12
- package/lib/work-item-move.js +61 -0
- package/lib/work-item-pull.js +286 -0
- package/lib/work-items.js +1 -1
- package/lib/worktree-diff.js +120 -0
- package/package.json +1 -1
- package/lib/handoff.js +0 -427
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `work_item_pull` (loopctl US-52.46): pull a work item into a git worktree here — its
|
|
3
|
+
* newest unfenced checkpoint's branch at the checkpoint's `head_sha`, the checkpoint's diff
|
|
4
|
+
* applied on top — and only then claim it (`POST /api/v1/stories/:id/claim`).
|
|
5
|
+
*
|
|
6
|
+
* The worktree is built from loopctl and the git remote alone; nothing is read from the
|
|
7
|
+
* machine that wrote the checkpoint. The claim is LAST, so a slow fetch or a failed apply
|
|
8
|
+
* holds no claim, and a refused claim removes the worktree this call added.
|
|
9
|
+
*
|
|
10
|
+
* Refusals, decided before any git write unless named:
|
|
11
|
+
*
|
|
12
|
+
* - `path_not_empty` — the target path exists and is not empty (before any git call at all
|
|
13
|
+
* when the caller names the path);
|
|
14
|
+
* - `no_checkpoint` — the item has no unfenced checkpoint to pull;
|
|
15
|
+
* - `head_not_found` — the checkpoint's head_sha is not on the remote;
|
|
16
|
+
* - `local_branch_differs` — a local branch of that name points elsewhere; it is never moved;
|
|
17
|
+
* - `diff_does_not_apply` — `git apply --check --binary` refused the diff; names the rejected
|
|
18
|
+
* files; the added worktree (and the branch this call created) is removed and nothing is
|
|
19
|
+
* claimed;
|
|
20
|
+
* - `diff_apply_failed` — the check passed but writing the diff failed (a full disk, a locked
|
|
21
|
+
* file); the worktree (and the branch this call created) is removed and nothing is claimed;
|
|
22
|
+
* - `worktree_add_failed` — `git worktree add` itself failed; a branch it left behind that
|
|
23
|
+
* this call was creating is deleted;
|
|
24
|
+
* - the claim's own refusal (`reserved_for_move`, `held_by_other_holder`, ...), with the
|
|
25
|
+
* worktree removed.
|
|
26
|
+
*
|
|
27
|
+
* KEY: the session's own key (`agentKey()` in index.js, its holder credential): the claim is
|
|
28
|
+
* the session's, and the server refuses another holder's item and an item a move reserved for
|
|
29
|
+
* another target.
|
|
30
|
+
*
|
|
31
|
+
* git runs with execFile (never a shell), so no value reaches a shell.
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
import { execFile } from "node:child_process";
|
|
35
|
+
import { existsSync, mkdtempSync, readdirSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
36
|
+
import os from "node:os";
|
|
37
|
+
import nodePath from "node:path";
|
|
38
|
+
|
|
39
|
+
import { checkpointsPath } from "./checkpoints.js";
|
|
40
|
+
import { uuid } from "./delivery-loop.js";
|
|
41
|
+
|
|
42
|
+
const MAX_BUFFER = 64 * 1024 * 1024;
|
|
43
|
+
const GIT_TIMEOUT_MS = 120_000;
|
|
44
|
+
|
|
45
|
+
function refuse(code, message, extra = {}) {
|
|
46
|
+
return { error: true, status: 0, body: { error: { code, message, ...extra } } };
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** git through execFile; rejects with an Error carrying `stderr`. */
|
|
50
|
+
export function runGit(cwd, args) {
|
|
51
|
+
return new Promise((resolve, reject) => {
|
|
52
|
+
execFile(
|
|
53
|
+
"git",
|
|
54
|
+
["-c", "core.quotepath=off", ...args],
|
|
55
|
+
{ cwd, encoding: "utf8", maxBuffer: MAX_BUFFER, timeout: GIT_TIMEOUT_MS },
|
|
56
|
+
(err, stdout, stderr) => {
|
|
57
|
+
if (err) {
|
|
58
|
+
err.stderr = stderr;
|
|
59
|
+
reject(err);
|
|
60
|
+
} else {
|
|
61
|
+
resolve(stdout);
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
);
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
async function tryGit(git, cwd, args) {
|
|
69
|
+
try {
|
|
70
|
+
return (await git(cwd, args)).trim() || null;
|
|
71
|
+
} catch {
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** `.claude/worktrees/<branch with / replaced by ->` under `top`. */
|
|
77
|
+
export function defaultPullPath(top, branch) {
|
|
78
|
+
return nodePath.join(top, ".claude", "worktrees", branch.replace(/\//g, "-"));
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function notEmpty(path) {
|
|
82
|
+
if (!existsSync(path)) return false;
|
|
83
|
+
if (!statSync(path).isDirectory()) return true;
|
|
84
|
+
return readdirSync(path).length > 0;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function pathNotEmpty(path) {
|
|
88
|
+
return refuse("path_not_empty", `${path} exists and is not empty; name another path.`, { path });
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** The files `git apply --check` names in its refusal. */
|
|
92
|
+
export function rejectedFiles(stderr) {
|
|
93
|
+
const files = new Set();
|
|
94
|
+
for (const line of String(stderr || "").split("\n")) {
|
|
95
|
+
const failed = line.match(/^error: patch failed: (.+?):\d+$/);
|
|
96
|
+
if (failed) files.add(failed[1]);
|
|
97
|
+
const other = line.match(/^error: (.+?): (?:already exists in working directory|does not exist in index|No such file or directory|patch does not apply|does not match index)$/);
|
|
98
|
+
if (other) files.add(other[1]);
|
|
99
|
+
}
|
|
100
|
+
return [...files];
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** The files a diff touches, from `git apply --numstat` (which writes nothing). */
|
|
104
|
+
async function diffFiles(git, cwd, file) {
|
|
105
|
+
const out = await git(cwd, ["apply", "--numstat", "--binary", file]);
|
|
106
|
+
return out
|
|
107
|
+
.split("\n")
|
|
108
|
+
.filter(Boolean)
|
|
109
|
+
.map((line) => line.split("\t").slice(2).join("\t"));
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
async function readJson(request) {
|
|
113
|
+
const res = await request;
|
|
114
|
+
return res?.error ? { error: res } : { value: res };
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
async function removeWorktree(git, top, path, createdBranch, branch) {
|
|
118
|
+
await tryGit(git, top, ["worktree", "remove", "--force", path]);
|
|
119
|
+
rmSync(path, { recursive: true, force: true });
|
|
120
|
+
await tryGit(git, top, ["worktree", "prune"]);
|
|
121
|
+
if (createdBranch) await tryGit(git, top, ["branch", "-D", branch]);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// Fetches the item's branch (and its wip/ branch), then the sha itself when neither carried
|
|
125
|
+
// it; true when head_sha is a commit here afterwards.
|
|
126
|
+
async function ensureHead(git, top, branch, wipBranch, headSha) {
|
|
127
|
+
const present = async () =>
|
|
128
|
+
Boolean(await tryGit(git, top, ["rev-parse", "-q", "--verify", `${headSha}^{commit}`]));
|
|
129
|
+
|
|
130
|
+
for (const ref of [branch, wipBranch].filter(Boolean)) {
|
|
131
|
+
await tryGit(git, top, ["fetch", "--quiet", "origin", ref]);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
if (await present()) return true;
|
|
135
|
+
await tryGit(git, top, ["fetch", "--quiet", "origin", headSha]);
|
|
136
|
+
return present();
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// Checks and applies a checkpoint's base64 diff in the worktree at `path`: `{ files }`, or
|
|
140
|
+
// `{ refusal }` when `git apply --check` refuses it. Any other failure (writing the temp
|
|
141
|
+
// file, `--numstat`, the apply itself) throws; the caller removes the worktree either way.
|
|
142
|
+
async function applyCheckpoint(git, path, encoded, headSha, checkpointId) {
|
|
143
|
+
const diff = encoded ? Buffer.from(encoded, "base64") : null;
|
|
144
|
+
if (!diff || diff.length === 0) return { files: [] };
|
|
145
|
+
|
|
146
|
+
const dir = mkdtempSync(nodePath.join(os.tmpdir(), "loopctl-pull-"));
|
|
147
|
+
const file = nodePath.join(dir, "checkpoint.diff");
|
|
148
|
+
|
|
149
|
+
try {
|
|
150
|
+
writeFileSync(file, diff);
|
|
151
|
+
|
|
152
|
+
try {
|
|
153
|
+
await git(path, ["apply", "--check", "--binary", file]);
|
|
154
|
+
} catch (err) {
|
|
155
|
+
return {
|
|
156
|
+
refusal: refuse("diff_does_not_apply", `The checkpoint's diff does not apply at ${headSha}.`, {
|
|
157
|
+
files: rejectedFiles(err.stderr),
|
|
158
|
+
checkpoint_id: checkpointId,
|
|
159
|
+
}),
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const files = await diffFiles(git, path, file);
|
|
164
|
+
await git(path, ["apply", "--binary", file]);
|
|
165
|
+
return { files };
|
|
166
|
+
} finally {
|
|
167
|
+
rmSync(dir, { recursive: true, force: true });
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* `{ story_id, path }` → `{ path, branch, head_sha, applied_files, fenced_checkpoint_ids,
|
|
173
|
+
* claim }`, or a refusal (see the module doc).
|
|
174
|
+
*/
|
|
175
|
+
export async function pullWorkItem(
|
|
176
|
+
{ story_id, path: wanted } = {},
|
|
177
|
+
{ apiCall, git = runGit, cwd = process.cwd() } = {},
|
|
178
|
+
) {
|
|
179
|
+
const bad = uuid(story_id, "story_id");
|
|
180
|
+
if (bad) return bad;
|
|
181
|
+
|
|
182
|
+
if (wanted !== undefined && wanted !== null && (typeof wanted !== "string" || wanted.trim() === "")) {
|
|
183
|
+
return refuse("invalid_path", "`path` is a directory path, or omitted for the default.");
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// A path the caller names is judged before any git call.
|
|
187
|
+
const named = wanted ? nodePath.resolve(cwd, wanted) : null;
|
|
188
|
+
if (named && notEmpty(named)) return pathNotEmpty(named);
|
|
189
|
+
|
|
190
|
+
const top = await tryGit(git, cwd, ["rev-parse", "--show-toplevel"]);
|
|
191
|
+
if (!top) return refuse("not_a_repository", `${cwd} is not inside a git repository.`);
|
|
192
|
+
|
|
193
|
+
const list = await readJson(apiCall("GET", `${checkpointsPath(story_id)}`, null));
|
|
194
|
+
if (list.error) return list.error;
|
|
195
|
+
const newest = (list.value?.data || []).find((c) => !c.fenced);
|
|
196
|
+
if (!newest) return refuse("no_checkpoint", "The item has no unfenced checkpoint to pull.");
|
|
197
|
+
|
|
198
|
+
const fencedList = await readJson(
|
|
199
|
+
apiCall("GET", `${checkpointsPath(story_id)}?fenced=unresolved`, null),
|
|
200
|
+
);
|
|
201
|
+
if (fencedList.error) return fencedList.error;
|
|
202
|
+
const fencedIds = (fencedList.value?.data || []).map((c) => c.id);
|
|
203
|
+
|
|
204
|
+
const one = await readJson(
|
|
205
|
+
apiCall("GET", `${checkpointsPath(story_id)}/${encodeURIComponent(newest.id)}`, null),
|
|
206
|
+
);
|
|
207
|
+
if (one.error) return one.error;
|
|
208
|
+
const checkpoint = one.value?.checkpoint || {};
|
|
209
|
+
const { branch, head_sha: headSha, wip_branch: wipBranch } = { ...newest, ...checkpoint };
|
|
210
|
+
|
|
211
|
+
const path = named || defaultPullPath(top, branch);
|
|
212
|
+
if (!named && notEmpty(path)) return pathNotEmpty(path);
|
|
213
|
+
|
|
214
|
+
if (!(await ensureHead(git, top, branch, wipBranch, headSha))) {
|
|
215
|
+
return refuse("head_not_found", `${headSha} is not on origin; the item's branch was never pushed there.`, {
|
|
216
|
+
head_sha: headSha,
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
const local = await tryGit(git, top, ["rev-parse", "-q", "--verify", `refs/heads/${branch}`]);
|
|
221
|
+
if (local && local !== headSha) {
|
|
222
|
+
return refuse(
|
|
223
|
+
"local_branch_differs",
|
|
224
|
+
`The local branch ${branch} is at ${local}, not the checkpoint's ${headSha}; it is never moved here.`,
|
|
225
|
+
{ branch, local_sha: local, head_sha: headSha },
|
|
226
|
+
);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
const createdBranch = !local;
|
|
230
|
+
const addArgs = createdBranch
|
|
231
|
+
? ["worktree", "add", "--quiet", "-b", branch, path, headSha]
|
|
232
|
+
: ["worktree", "add", "--quiet", path, branch];
|
|
233
|
+
|
|
234
|
+
try {
|
|
235
|
+
await git(top, addArgs);
|
|
236
|
+
} catch (err) {
|
|
237
|
+
// A failed `worktree add -b` can leave the branch it was creating; this call made it.
|
|
238
|
+
await tryGit(git, top, ["worktree", "prune"]);
|
|
239
|
+
if (createdBranch && (await tryGit(git, top, ["rev-parse", "-q", "--verify", `refs/heads/${branch}`]))) {
|
|
240
|
+
await tryGit(git, top, ["branch", "-D", branch]);
|
|
241
|
+
}
|
|
242
|
+
return refuse("worktree_add_failed", String(err.stderr || err.message).trim(), { path });
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// From here every way out but success removes the worktree, and the branch when this call
|
|
246
|
+
// created it, so a retry finds the path empty and the branch as it was.
|
|
247
|
+
let applied;
|
|
248
|
+
try {
|
|
249
|
+
applied = await applyCheckpoint(git, path, checkpoint.diff, headSha, newest.id);
|
|
250
|
+
} catch (err) {
|
|
251
|
+
await removeWorktree(git, top, path, createdBranch, branch);
|
|
252
|
+
return refuse(
|
|
253
|
+
"diff_apply_failed",
|
|
254
|
+
`Applying the checkpoint's diff failed after its check passed: ${String(err.stderr || err.message).trim()}`,
|
|
255
|
+
{ checkpoint_id: newest.id, path },
|
|
256
|
+
);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
if (applied.refusal) {
|
|
260
|
+
await removeWorktree(git, top, path, createdBranch, branch);
|
|
261
|
+
return applied.refusal;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
let claim;
|
|
265
|
+
try {
|
|
266
|
+
claim = await apiCall("POST", `/api/v1/stories/${encodeURIComponent(story_id)}/claim`, null);
|
|
267
|
+
} catch (err) {
|
|
268
|
+
await removeWorktree(git, top, path, createdBranch, branch);
|
|
269
|
+
throw err;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
if (claim?.error) {
|
|
273
|
+
await removeWorktree(git, top, path, createdBranch, branch);
|
|
274
|
+
return claim;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
return {
|
|
278
|
+
path,
|
|
279
|
+
branch,
|
|
280
|
+
head_sha: headSha,
|
|
281
|
+
checkpoint_id: newest.id,
|
|
282
|
+
applied_files: applied.files,
|
|
283
|
+
fenced_checkpoint_ids: fencedIds,
|
|
284
|
+
claim,
|
|
285
|
+
};
|
|
286
|
+
}
|
package/lib/work-items.js
CHANGED
|
@@ -83,7 +83,7 @@ export async function listWorkItemNotes({ story_id, section, limit, after } = {}
|
|
|
83
83
|
|
|
84
84
|
/**
|
|
85
85
|
* `work_item_timeline` (loopctl US-52.29): `GET /api/v1/stories/:id/timeline`, the item now
|
|
86
|
-
* (stage and its age, holder, lease, issue, PR, runner, newest CI runs, thread) and one page of
|
|
86
|
+
* (stage and its age, holder, lease, issue, PR, runner, newest CI runs and runner dispatches, thread) and one page of
|
|
87
87
|
* its event log and audit log merged oldest first. READ-ONLY on any authenticated key; it never
|
|
88
88
|
* moves a thread cursor. Every event reason and message body is session or reporter text and
|
|
89
89
|
* arrives flagged `untrusted: true`.
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A worktree's checkpoint state (loopctl US-52.41, shared with US-52.46's pull): the branch,
|
|
3
|
+
* the commit its work applies to, and `git diff --binary` of the working tree on top of it,
|
|
4
|
+
* untracked non-ignored files included. The same shape US-52.40 measured with
|
|
5
|
+
* scripts/factory/measure-worktree-diffs.sh, so the bound it set fits what this produces.
|
|
6
|
+
*
|
|
7
|
+
* `head_sha` is the newest commit of the branch's upstream that HEAD contains (the pushed
|
|
8
|
+
* head), else the merge-base with origin's default branch (a branch never pushed), else
|
|
9
|
+
* HEAD. A commit made but not pushed is therefore part of the diff: a puller has only what
|
|
10
|
+
* the remote has, plus this diff.
|
|
11
|
+
*
|
|
12
|
+
* NOTHING IN THE WORKTREE IS WRITTEN. Untracked files are added intent-to-add to a COPY of
|
|
13
|
+
* the index (GIT_INDEX_FILE in a temporary directory), so the worktree's own index, files and
|
|
14
|
+
* refs are untouched.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { execFile } from "node:child_process";
|
|
18
|
+
import { copyFileSync, existsSync, mkdtempSync, rmSync } from "node:fs";
|
|
19
|
+
import os from "node:os";
|
|
20
|
+
import path from "node:path";
|
|
21
|
+
|
|
22
|
+
// Larger than any checkpoint the server accepts, so an over-bound diff is measured and
|
|
23
|
+
// refused by the server with its own message rather than cut short here.
|
|
24
|
+
const MAX_BUFFER = 64 * 1024 * 1024;
|
|
25
|
+
const GIT_TIMEOUT_MS = 60_000;
|
|
26
|
+
|
|
27
|
+
function git(cwd, args, { env, encoding = "utf8" } = {}) {
|
|
28
|
+
return new Promise((resolve, reject) => {
|
|
29
|
+
execFile(
|
|
30
|
+
"git",
|
|
31
|
+
["-c", "core.quotepath=off", ...args],
|
|
32
|
+
{ cwd, env: { ...process.env, ...env }, encoding, maxBuffer: MAX_BUFFER, timeout: GIT_TIMEOUT_MS },
|
|
33
|
+
(err, stdout) => (err ? reject(err) : resolve(stdout)),
|
|
34
|
+
);
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async function tryGit(cwd, args) {
|
|
39
|
+
try {
|
|
40
|
+
return (await git(cwd, args)).trim() || null;
|
|
41
|
+
} catch {
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async function defaultBranch(cwd) {
|
|
47
|
+
const ref = await tryGit(cwd, ["symbolic-ref", "-q", "refs/remotes/origin/HEAD"]);
|
|
48
|
+
if (ref) return ref.replace(/^refs\/remotes\//, "");
|
|
49
|
+
|
|
50
|
+
for (const candidate of ["origin/main", "origin/master"]) {
|
|
51
|
+
if (await tryGit(cwd, ["rev-parse", "-q", "--verify", `${candidate}^{commit}`])) return candidate;
|
|
52
|
+
}
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** The commit the worktree's work applies to, and which rule chose it. */
|
|
57
|
+
export async function baseCommit(cwd) {
|
|
58
|
+
const upstream = await tryGit(cwd, ["rev-parse", "-q", "--verify", "@{upstream}^{commit}"]);
|
|
59
|
+
if (upstream) {
|
|
60
|
+
const pushed = await tryGit(cwd, ["merge-base", "HEAD", upstream]);
|
|
61
|
+
if (pushed) return { sha: pushed, from: "upstream" };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const fallback = await defaultBranch(cwd);
|
|
65
|
+
if (fallback) {
|
|
66
|
+
const base = await tryGit(cwd, ["merge-base", "HEAD", fallback]);
|
|
67
|
+
if (base) return { sha: base, from: "merge_base" };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const head = await tryGit(cwd, ["rev-parse", "-q", "--verify", "HEAD^{commit}"]);
|
|
71
|
+
return head ? { sha: head, from: "head" } : null;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* `{ branch, head_sha, base_from, diff }` for the worktree at `worktree`; `diff` is a Buffer.
|
|
76
|
+
* Throws an Error naming the problem when `worktree` is not a git worktree with a commit.
|
|
77
|
+
*/
|
|
78
|
+
export async function worktreeDiff(worktree) {
|
|
79
|
+
const top = await tryGit(worktree, ["rev-parse", "--show-toplevel"]);
|
|
80
|
+
if (!top) throw new Error(`${worktree} is not a git worktree.`);
|
|
81
|
+
|
|
82
|
+
const base = await baseCommit(top);
|
|
83
|
+
if (!base) throw new Error(`${top} has no commit to checkpoint against.`);
|
|
84
|
+
|
|
85
|
+
const branch = (await tryGit(top, ["symbolic-ref", "-q", "--short", "HEAD"])) || "HEAD";
|
|
86
|
+
const indexPath = (await git(top, ["rev-parse", "--path-format=absolute", "--git-path", "index"])).trim();
|
|
87
|
+
|
|
88
|
+
const dir = mkdtempSync(path.join(os.tmpdir(), "loopctl-checkpoint-"));
|
|
89
|
+
const index = path.join(dir, "index");
|
|
90
|
+
|
|
91
|
+
try {
|
|
92
|
+
if (existsSync(indexPath)) copyFileSync(indexPath, index);
|
|
93
|
+
const env = { GIT_INDEX_FILE: index };
|
|
94
|
+
|
|
95
|
+
const untracked = (await git(top, ["ls-files", "-z", "--others", "--exclude-standard"]))
|
|
96
|
+
.split("\0")
|
|
97
|
+
.filter(Boolean);
|
|
98
|
+
|
|
99
|
+
for (let i = 0; i < untracked.length; i += 500) {
|
|
100
|
+
await git(top, ["add", "--intent-to-add", "--", ...untracked.slice(i, i + 500)], { env });
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const diff = await git(top, ["diff", "--binary", "--no-ext-diff", "--no-textconv", base.sha], {
|
|
104
|
+
env,
|
|
105
|
+
encoding: "buffer",
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
return { branch, head_sha: base.sha, base_from: base.from, diff };
|
|
109
|
+
} finally {
|
|
110
|
+
rmSync(dir, { recursive: true, force: true });
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** The worktree's HEAD commit: what a pushed `wip/` branch points at. */
|
|
115
|
+
export async function headCommit(worktree) {
|
|
116
|
+
const head = await tryGit(worktree, ["rev-parse", "-q", "--verify", "HEAD^{commit}"]);
|
|
117
|
+
if (!head) throw new Error(`${worktree} is not a git worktree with a commit.`);
|
|
118
|
+
const branch = (await tryGit(worktree, ["symbolic-ref", "-q", "--short", "HEAD"])) || "HEAD";
|
|
119
|
+
return { head_sha: head, branch };
|
|
120
|
+
}
|