longtable 0.1.34 → 0.1.35
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/package.json +1 -1
- package/src/repo.mjs +45 -16
package/package.json
CHANGED
package/src/repo.mjs
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
import { execFileSync } from "node:child_process";
|
|
4
4
|
import { cpSync, existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
5
5
|
import { join, resolve } from "node:path";
|
|
6
|
+
import { fileURLToPath } from "node:url";
|
|
6
7
|
import { home, log } from "./store.mjs";
|
|
7
8
|
|
|
8
9
|
// Git never stops to ask for a name or password here: if this computer's saved sign-in cannot reach a repository,
|
|
@@ -314,10 +315,27 @@ export function realRepo(seat) {
|
|
|
314
315
|
try { return existsSync(join(folder, ".git")) ? resolve(folder) : null; } catch { return null; }
|
|
315
316
|
}
|
|
316
317
|
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
318
|
+
// Where the team's code lives is the table's source (what the locked mode reads too), never whatever the person's own
|
|
319
|
+
// repository happens to call origin: that can be somebody else's repository (a client's), which work must never reach.
|
|
320
|
+
// When the source is this very repository, work stays in it as a local branch and nothing is pushed anywhere.
|
|
321
|
+
function sameRepo(source, repo) {
|
|
322
|
+
if (!source) return true;
|
|
323
|
+
let p = String(source);
|
|
324
|
+
if (/^file:/i.test(p)) { try { p = fileURLToPath(p); } catch { return false; } }
|
|
325
|
+
else if (!/^([A-Za-z]:[\\/]|[\\/])/.test(p)) return false;
|
|
326
|
+
const norm = (x) => resolve(x).replace(/[\\/]+$/, "").toLowerCase();
|
|
327
|
+
return norm(p) === norm(repo) || norm(p) === norm(join(repo, ".git"));
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
// The tip of a branch (or of the source's default branch) at the source, fetched under a private name of the
|
|
331
|
+
// connection's and read back as a commit: nothing of the person's own refs or remotes changes.
|
|
332
|
+
function sourceTip(repo, url, ref) {
|
|
333
|
+
const tmp = `refs/longtable-src/tip-${process.pid}-${Date.now()}`;
|
|
334
|
+
try {
|
|
335
|
+
tgit(repo, ["fetch", "--quiet", "--no-tags", url, `+${ref}:${tmp}`]);
|
|
336
|
+
return tgit(repo, ["rev-parse", tmp]);
|
|
337
|
+
} catch { return null; }
|
|
338
|
+
finally { try { tgit(repo, ["update-ref", "-d", tmp]); } catch {} }
|
|
321
339
|
}
|
|
322
340
|
|
|
323
341
|
// Only files git ignores are copied, as Claude Code does: something tracked is already in the worktree, and something
|
|
@@ -347,26 +365,35 @@ export function openTree(seat, branch, { doing = false, job = "read" } = {}) {
|
|
|
347
365
|
const repo = realRepo(seat) || join(home, "copies", table);
|
|
348
366
|
const dir = doing ? join(home, "trees", table, `work-${branch.slice("longtable/".length)}`)
|
|
349
367
|
: join(home, "trees", table, `read-${String(job).replace(/[^A-Za-z0-9-]/g, "").slice(0, 36) || "job"}`);
|
|
350
|
-
|
|
368
|
+
// The table's code: this repository itself, or the address the table reads from (never "origin" by name).
|
|
369
|
+
const local = sameRepo(seat.source, repo);
|
|
370
|
+
const url = local ? null : seat.source;
|
|
351
371
|
let onRemote = false;
|
|
352
|
-
|
|
353
|
-
|
|
372
|
+
let base = null;
|
|
373
|
+
if (local) {
|
|
374
|
+
if (branch) { try { base = tgit(repo, ["rev-parse", "--verify", "--quiet", `refs/heads/${branch}`]); onRemote = true; } catch {} }
|
|
375
|
+
base ??= tgit(repo, ["rev-parse", "HEAD"]);
|
|
376
|
+
} else {
|
|
377
|
+
if (branch) { base = sourceTip(repo, url, `refs/heads/${branch}`); onRemote = !!base; }
|
|
378
|
+
base ??= sourceTip(repo, url, "HEAD");
|
|
379
|
+
if (!base) throw new Error("the project's code could not be fetched from where the table reads it");
|
|
380
|
+
}
|
|
354
381
|
const fresh = !existsSync(join(dir, ".git"));
|
|
355
382
|
if (fresh) {
|
|
356
383
|
mkdirSync(join(home, "trees", table), { recursive: true });
|
|
357
384
|
// A worktree of ours whose folder is gone is let go by name; the person's own worktrees are never touched.
|
|
358
385
|
try { tgit(repo, ["worktree", "remove", "--force", dir]); } catch {}
|
|
359
|
-
if (doing) {
|
|
386
|
+
if (doing && !local) {
|
|
360
387
|
// Their own local branch of the same name is never reset over work that is not on the server.
|
|
361
|
-
let
|
|
362
|
-
try {
|
|
363
|
-
if (
|
|
364
|
-
try { tgit(repo, ["merge-base", "--is-ancestor",
|
|
388
|
+
let mine = null;
|
|
389
|
+
try { mine = tgit(repo, ["rev-parse", "--verify", "--quiet", `refs/heads/${branch}`]); } catch {}
|
|
390
|
+
if (mine) {
|
|
391
|
+
try { tgit(repo, ["merge-base", "--is-ancestor", mine, base]); }
|
|
365
392
|
catch { throw new Error(`a branch named ${branch} on this computer has work that is not on the server, so it was left as it is`); }
|
|
366
393
|
}
|
|
367
394
|
}
|
|
368
395
|
tgit(repo, ["worktree", "add", "--quiet", ...(doing ? ["-B", branch, dir, base] : ["--detach", dir, base])]);
|
|
369
|
-
} else if (doing && onRemote) {
|
|
396
|
+
} else if (doing && onRemote && !local) {
|
|
370
397
|
// Work picked up again starts from what is on the server (another AI's part may have landed there since), unless
|
|
371
398
|
// this computer holds commits the server does not have yet.
|
|
372
399
|
try { tgit(dir, ["merge-base", "--is-ancestor", "HEAD", base]); tgit(dir, ["reset", "--quiet", "--hard", base]); } catch {}
|
|
@@ -380,7 +407,7 @@ export function openTree(seat, branch, { doing = false, job = "read" } = {}) {
|
|
|
380
407
|
const NL = String.fromCharCode(10);
|
|
381
408
|
if (!now.split(/\r?\n/).includes(".longtable-yours/")) writeFileSync(exclude, `${now}${now && !now.endsWith(NL) ? NL : ""}.longtable-yours/${NL}`);
|
|
382
409
|
if (fresh && realRepo(seat)) copyIncluded(repo, dir);
|
|
383
|
-
return { dir, repo, base: tgit(dir, ["rev-parse", "HEAD"]), continued: onRemote, tree: true };
|
|
410
|
+
return { dir, repo, url, base: tgit(dir, ["rev-parse", "HEAD"]), continued: onRemote, tree: true };
|
|
384
411
|
}
|
|
385
412
|
|
|
386
413
|
// What changed in the worktree since it was opened, committed or not.
|
|
@@ -410,13 +437,15 @@ export function landTree(tree, branch, message, who) {
|
|
|
410
437
|
const commit = land(["rev-parse", "--short", "HEAD"]);
|
|
411
438
|
let pushed = false;
|
|
412
439
|
let landed = branch;
|
|
413
|
-
|
|
440
|
+
// Work on the table's own repository is already on its branch there: nothing is pushed anywhere.
|
|
441
|
+
if (!tree.url) return { files, commit, branch, pushed: true, apart: false };
|
|
442
|
+
const push = (to) => land(["push", "--quiet", "--no-verify", tree.url, `HEAD:refs/heads/${to}`]);
|
|
414
443
|
try { push(branch); pushed = true; } catch (e) {
|
|
415
444
|
log(`push of ${branch} failed: ${String(e.message).split(/\r?\n/)[0]}`);
|
|
416
445
|
// Another part landed on the branch first (two people's work on one task): put the two together and try again;
|
|
417
446
|
// when they touch the same lines, this part goes on a branch of its own beside it, as the other work does.
|
|
418
447
|
try {
|
|
419
|
-
land(["fetch", "--quiet", "
|
|
448
|
+
land(["fetch", "--quiet", "--no-tags", tree.url, `refs/heads/${branch}`]);
|
|
420
449
|
land(["merge", "--quiet", "--no-edit", "--no-verify", "FETCH_HEAD"]);
|
|
421
450
|
push(branch);
|
|
422
451
|
pushed = true;
|