@henryqw/pi-herdr-done 0.2.2 → 0.4.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 CHANGED
@@ -12,17 +12,20 @@ pi install npm:@henryqw/pi-herdr-done
12
12
 
13
13
  | Surface | Type | Purpose |
14
14
  | --- | --- | --- |
15
- | `/done` | command | Remove the current worktree checkout and close only this session's tab. |
16
- | `/done --force` | command | Same, even when the worktree is dirty. |
15
+ | `/done` | command | Remove the current worktree checkout, fast-forward the parent workspace, and close only this session's tab. |
16
+ | `/done --force` | command | Same, even when the worktree is dirty or used by other Herdr tabs. |
17
17
 
18
18
  Both forms wait for Pi to become idle. `/done` asks for confirmation first; `/done --force` skips it because the flag already states intent. Normal removal runs:
19
19
 
20
20
  ```bash
21
21
  git worktree remove .
22
+ git -C <parent> pull --ff-only
22
23
  herdr tab close "$HERDR_TAB_ID"
23
24
  ```
24
25
 
25
- Only the current session's tab closes, so sibling tabs in the same workspace survive. Command requires Pi running inside Herdr with `HERDR_ENV=1` and `HERDR_TAB_ID` set.
26
+ The parent pull runs only when this session ran in a linked worktree with a non-bare primary; it is skipped when the parent has diverged (`--ff-only` fails safely) or when another Herdr tab is working in the parent. Concurrent completions serialize on a lock around the parent checkout.
27
+
28
+ Only the current session's tab closes, so sibling tabs in the same workspace survive. When another Herdr tab still uses the checkout or its parent, `/done` refuses and lists the blocking tabs by name; use `/done --force` to remove the checkout regardless of other tabs. Command requires Pi running inside Herdr with `HERDR_ENV=1` and `HERDR_TAB_ID` set.
26
29
 
27
30
  Dirty worktrees make `/done` fail. Commit or discard changes, or use `/done --force` to explicitly delete them.
28
31
 
@@ -6,6 +6,7 @@ import { lock } from "proper-lockfile";
6
6
  type ExecResult = { stdout: string; stderr: string; code: number; killed?: boolean };
7
7
 
8
8
  type SnapshotPane = { tab_id?: unknown; cwd?: unknown };
9
+ type TabEntry = { tab_id?: unknown; label?: unknown };
9
10
 
10
11
  export default function herdrDoneExtension(pi: ExtensionAPI): void {
11
12
  const herdr = createHerdrClient<{ cwd: string }>((command, args, options) =>
@@ -20,7 +21,7 @@ export default function herdrDoneExtension(pi: ExtensionAPI): void {
20
21
  };
21
22
 
22
23
  pi.registerCommand("done", {
23
- description: "Close and remove the current Herdr worktree",
24
+ description: "Remove the current Herdr worktree, then fast-forward its parent workspace",
24
25
  handler: async (args, ctx) => {
25
26
  const option = args.trim();
26
27
  if (option && option !== "--force") throw new Error("Usage: /done [--force]");
@@ -37,20 +38,41 @@ export default function herdrDoneExtension(pi: ExtensionAPI): void {
37
38
 
38
39
  await ctx.waitForIdle();
39
40
  const checkout = (await execOrThrow("git", ["rev-parse", "--show-toplevel"], ctx.cwd)).trim();
41
+ // First record of the NUL-delimited list is always the main worktree;
42
+ // -z keeps paths containing newlines parseable.
43
+ const worktreeFields = (await execOrThrow("git", ["worktree", "list", "--porcelain", "-z"], checkout)).split("\0");
44
+ const mainCheckout = worktreeFields[0]?.startsWith("worktree ") ? worktreeFields[0].slice("worktree ".length) : undefined;
45
+ if (!mainCheckout) throw new Error("git worktree list returned no main worktree.");
46
+ // A bare primary has no working tree to pull into.
47
+ const parentIsBare = mainCheckout !== checkout &&
48
+ worktreeFields.slice(1, worktreeFields.indexOf("")).includes("bare");
40
49
 
41
50
  const release = await lock(checkout);
42
51
  try {
43
- const snapshot = await herdr.json(["api", "snapshot"], { cwd: ctx.cwd });
44
- const panes = (snapshot.result as { snapshot?: { panes?: SnapshotPane[] } } | undefined)?.snapshot?.panes;
45
- if (!Array.isArray(panes)) throw new Error("herdr api snapshot returned no panes.");
46
- const dependents = [...new Set(panes
47
- .filter((pane): pane is SnapshotPane & { tab_id: string; cwd: string } =>
48
- typeof pane.tab_id === "string" && pane.tab_id !== tabId &&
49
- typeof pane.cwd === "string" &&
50
- (pane.cwd === checkout || pane.cwd.startsWith(`${checkout}/`)))
51
- .map((pane) => pane.tab_id))];
52
- if (dependents.length > 0) {
53
- throw new Error(`Worktree still used by Herdr tabs ${dependents.join(", ")}; close them first.`);
52
+ // With --force, skip the dependents check entirely and let git remove the checkout.
53
+ if (option !== "--force") {
54
+ const snapshot = await herdr.json(["api", "snapshot"], { cwd: ctx.cwd });
55
+ const panes = (snapshot.result as { snapshot?: { panes?: SnapshotPane[] } } | undefined)?.snapshot?.panes;
56
+ if (!Array.isArray(panes)) throw new Error("herdr api snapshot returned no panes.");
57
+ const guarded = mainCheckout === checkout ? [checkout] : [checkout, mainCheckout];
58
+ const isGuarded = (cwd: string) =>
59
+ guarded.some((root) => cwd === root || cwd.startsWith(`${root}/`));
60
+ const dependentIds = [...new Set(panes
61
+ .filter((pane): pane is SnapshotPane & { tab_id: string; cwd: string } =>
62
+ typeof pane.tab_id === "string" && pane.tab_id !== tabId &&
63
+ typeof pane.cwd === "string" && isGuarded(pane.cwd))
64
+ .map((pane) => pane.tab_id))];
65
+ if (dependentIds.length > 0) {
66
+ const listing = await herdr.json(["tab", "list"], { cwd: ctx.cwd });
67
+ const tabs = (listing.result as { tabs?: TabEntry[] } | undefined)?.tabs;
68
+ const labels = new Map(
69
+ (Array.isArray(tabs) ? tabs : [])
70
+ .filter((tab): tab is TabEntry & { tab_id: string; label: string } =>
71
+ typeof tab.tab_id === "string" && typeof tab.label === "string")
72
+ .map((tab) => [tab.tab_id, tab.label]));
73
+ const names = [...new Set(dependentIds.map((id) => labels.get(id) ?? id))];
74
+ throw new Error(`Worktree still used by Herdr tabs ${names.join(", ")}; close them first.`);
75
+ }
54
76
  }
55
77
 
56
78
  await execOrThrow("git", [
@@ -59,6 +81,16 @@ export default function herdrDoneExtension(pi: ExtensionAPI): void {
59
81
  } finally {
60
82
  await release();
61
83
  }
84
+ if (!parentIsBare && mainCheckout !== checkout) {
85
+ // Serialize concurrent completions pulling the same parent checkout.
86
+ // Run outside the removed checkout because its directory no longer exists.
87
+ const releasePull = await lock(mainCheckout);
88
+ try {
89
+ await execOrThrow("git", ["pull", "--ff-only"], mainCheckout);
90
+ } finally {
91
+ await releasePull();
92
+ }
93
+ }
62
94
  // Close only this session's tab so unrelated tabs survive.
63
95
  // Run outside the removed checkout because its directory no longer exists.
64
96
  await herdr.run(["tab", "close", tabId], { cwd: tmpdir() });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@henryqw/pi-herdr-done",
3
- "version": "0.2.2",
3
+ "version": "0.4.0",
4
4
  "description": "Close and remove the current Herdr worktree from Pi.",
5
5
  "keywords": [
6
6
  "pi-package",