@phren/cli 0.2.14 → 0.2.16
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 +2 -2
- package/dist/agent-instructions.d.ts +12 -0
- package/dist/agent-instructions.js +52 -0
- package/dist/bridge/agent-hook-stores.d.ts +32 -0
- package/dist/bridge/agent-hook-stores.js +67 -0
- package/dist/bridge/agent-hooks.d.ts +193 -2
- package/dist/bridge/agent-hooks.js +1045 -38
- package/dist/bridge/canary.d.ts +27 -0
- package/dist/bridge/canary.js +189 -0
- package/dist/bridge/changes.d.ts +57 -0
- package/dist/bridge/changes.js +403 -0
- package/dist/bridge/client.d.ts +3 -0
- package/dist/bridge/client.js +36 -0
- package/dist/bridge/code-note.d.ts +22 -0
- package/dist/bridge/code-note.js +42 -0
- package/dist/bridge/code-routes.d.ts +133 -0
- package/dist/bridge/code-routes.js +319 -0
- package/dist/bridge/codex-threads.d.ts +27 -0
- package/dist/bridge/codex-threads.js +442 -0
- package/dist/bridge/command.d.ts +7 -0
- package/dist/bridge/command.js +50 -5
- package/dist/bridge/computer-names.d.ts +4 -0
- package/dist/bridge/computer-names.js +16 -0
- package/dist/bridge/computers.d.ts +9 -0
- package/dist/bridge/computers.js +111 -0
- package/dist/bridge/context.d.ts +14 -0
- package/dist/bridge/context.js +149 -0
- package/dist/bridge/dispatch-command.d.ts +3 -0
- package/dist/bridge/dispatch-command.js +93 -0
- package/dist/bridge/dispatch-hosts.d.ts +23 -0
- package/dist/bridge/dispatch-hosts.js +25 -0
- package/dist/bridge/dispatch-returns.d.ts +123 -0
- package/dist/bridge/dispatch-returns.js +279 -0
- package/dist/bridge/dispatch-tree.d.ts +54 -0
- package/dist/bridge/dispatch-tree.js +198 -0
- package/dist/bridge/dispatch.d.ts +207 -0
- package/dist/bridge/dispatch.js +261 -0
- package/dist/bridge/enroll.d.ts +31 -0
- package/dist/bridge/enroll.js +236 -0
- package/dist/bridge/fanout-messages.d.ts +74 -0
- package/dist/bridge/fanout-messages.js +230 -0
- package/dist/bridge/fanouts.d.ts +160 -0
- package/dist/bridge/fanouts.js +702 -0
- package/dist/bridge/file-range.d.ts +16 -0
- package/dist/bridge/file-range.js +90 -0
- package/dist/bridge/files.d.ts +20 -0
- package/dist/bridge/files.js +54 -0
- package/dist/bridge/git-publish.d.ts +23 -0
- package/dist/bridge/git-publish.js +145 -0
- package/dist/bridge/git-worktrees.d.ts +21 -0
- package/dist/bridge/git-worktrees.js +150 -0
- package/dist/bridge/git.d.ts +54 -0
- package/dist/bridge/git.js +552 -0
- package/dist/bridge/grants.d.ts +31 -0
- package/dist/bridge/grants.js +181 -0
- package/dist/bridge/hand-off.d.ts +79 -0
- package/dist/bridge/hand-off.js +151 -0
- package/dist/bridge/health.d.ts +128 -0
- package/dist/bridge/health.js +260 -0
- package/dist/bridge/herdr.d.ts +79 -6
- package/dist/bridge/herdr.js +292 -27
- package/dist/bridge/hook-main.js +2 -0
- package/dist/bridge/install.d.ts +32 -1
- package/dist/bridge/install.js +225 -38
- package/dist/bridge/launch-worktree.d.ts +21 -0
- package/dist/bridge/launch-worktree.js +70 -0
- package/dist/bridge/limits.d.ts +20 -0
- package/dist/bridge/limits.js +65 -0
- package/dist/bridge/locate.d.ts +16 -0
- package/dist/bridge/locate.js +75 -0
- package/dist/bridge/metrics.d.ts +29 -0
- package/dist/bridge/metrics.js +71 -0
- package/dist/bridge/model-switch.d.ts +35 -0
- package/dist/bridge/model-switch.js +250 -0
- package/dist/bridge/models.d.ts +46 -0
- package/dist/bridge/models.js +257 -0
- package/dist/bridge/opencode-approvals.d.ts +6 -0
- package/dist/bridge/opencode-approvals.js +60 -0
- package/dist/bridge/pane-text.d.ts +15 -0
- package/dist/bridge/pane-text.js +23 -0
- package/dist/bridge/peers.d.ts +27 -0
- package/dist/bridge/peers.js +130 -0
- package/dist/bridge/projects.d.ts +12 -1
- package/dist/bridge/projects.js +268 -20
- package/dist/bridge/protocol.d.ts +35 -2
- package/dist/bridge/protocol.js +42 -7
- package/dist/bridge/push.d.ts +83 -0
- package/dist/bridge/push.js +199 -0
- package/dist/bridge/questions.d.ts +38 -0
- package/dist/bridge/questions.js +205 -0
- package/dist/bridge/schedule-format.d.ts +103 -0
- package/dist/bridge/schedule-format.js +266 -0
- package/dist/bridge/schedule-launch.d.ts +11 -0
- package/dist/bridge/schedule-launch.js +169 -0
- package/dist/bridge/schedule-watch.d.ts +52 -0
- package/dist/bridge/schedule-watch.js +242 -0
- package/dist/bridge/schedules.d.ts +48 -0
- package/dist/bridge/schedules.js +205 -0
- package/dist/bridge/server-launch.d.ts +14 -0
- package/dist/bridge/server-launch.js +218 -0
- package/dist/bridge/server-overview.d.ts +45 -0
- package/dist/bridge/server-overview.js +102 -0
- package/dist/bridge/server-pane-routes.d.ts +29 -0
- package/dist/bridge/server-pane-routes.js +400 -0
- package/dist/bridge/server-routes.d.ts +106 -0
- package/dist/bridge/server-routes.js +538 -0
- package/dist/bridge/server-stream.d.ts +46 -0
- package/dist/bridge/server-stream.js +253 -0
- package/dist/bridge/server.d.ts +3 -15
- package/dist/bridge/server.js +162 -264
- package/dist/bridge/session-servers.d.ts +24 -0
- package/dist/bridge/session-servers.js +136 -0
- package/dist/bridge/side-questions.d.ts +74 -0
- package/dist/bridge/side-questions.js +252 -0
- package/dist/bridge/simulators.d.ts +28 -0
- package/dist/bridge/simulators.js +198 -0
- package/dist/bridge/speech-transcribe.d.ts +24 -0
- package/dist/bridge/speech-transcribe.js +124 -0
- package/dist/bridge/speech.d.ts +35 -0
- package/dist/bridge/speech.js +98 -0
- package/dist/bridge/steps.d.ts +13 -0
- package/dist/bridge/steps.js +222 -0
- package/dist/bridge/tab-activity.d.ts +18 -0
- package/dist/bridge/tab-activity.js +99 -0
- package/dist/bridge/terminal-choice.d.ts +64 -0
- package/dist/bridge/terminal-choice.js +281 -0
- package/dist/bridge/transcript-claude.d.ts +22 -0
- package/dist/bridge/transcript-claude.js +353 -0
- package/dist/bridge/transcript-codex.d.ts +30 -0
- package/dist/bridge/transcript-codex.js +282 -0
- package/dist/bridge/transcript-copilot.d.ts +4 -0
- package/dist/bridge/transcript-copilot.js +14 -0
- package/dist/bridge/transcript-opencode.d.ts +5 -0
- package/dist/bridge/transcript-opencode.js +26 -0
- package/dist/bridge/transcript-preview.d.ts +94 -0
- package/dist/bridge/transcript-preview.js +476 -0
- package/dist/bridge/transcripts.d.ts +81 -4
- package/dist/bridge/transcripts.js +332 -67
- package/dist/bridge/transport.d.ts +3 -0
- package/dist/bridge/transport.js +96 -15
- package/dist/bridge/uploads.d.ts +15 -0
- package/dist/bridge/uploads.js +44 -2
- package/dist/bridge/usage.d.ts +124 -0
- package/dist/bridge/usage.js +859 -0
- package/dist/bridge-hook.mjs +41246 -21385
- package/dist/cli/actions.js +14 -1
- package/dist/cli/config-proactivity.d.ts +3 -1
- package/dist/cli/config-proactivity.js +35 -3
- package/dist/cli/config.js +4 -1
- package/dist/cli/extract.js +3 -6
- package/dist/cli/govern.js +5 -2
- package/dist/cli/hooks-globs.js +1 -1
- package/dist/cli/hooks.d.ts +1 -1
- package/dist/cli/hooks.js +9 -7
- package/dist/cli/namespaces-projects.js +12 -6
- package/dist/cli/namespaces-store.js +37 -6
- package/dist/cli/namespaces-tasks.js +1 -1
- package/dist/cli/schedules.d.ts +1 -0
- package/dist/cli/schedules.js +164 -0
- package/dist/cli/session-background.js +1 -1
- package/dist/cli/session-git.d.ts +8 -5
- package/dist/cli/session-git.js +45 -120
- package/dist/cli/session-start.js +3 -1
- package/dist/cli/session-stop.js +19 -8
- package/dist/cli/team.js +13 -10
- package/dist/cli-handlers.d.ts +1 -1
- package/dist/cli-handlers.js +8 -4
- package/dist/cli-help.d.ts +5 -4
- package/dist/cli-help.js +15 -15
- package/dist/cli-registry.d.ts +4 -1
- package/dist/cli-registry.js +119 -6
- package/dist/content/archive.js +2 -2
- package/dist/content/citation.d.ts +11 -0
- package/dist/content/citation.js +18 -0
- package/dist/content/learning.js +2 -0
- package/dist/content/summarize.d.ts +14 -15
- package/dist/content/summarize.js +60 -5
- package/dist/content/validate.d.ts +3 -1
- package/dist/content/validate.js +26 -8
- package/dist/core/project-registry.d.ts +26 -0
- package/dist/core/project-registry.js +573 -0
- package/dist/core/project.js +2 -3
- package/dist/data/access.d.ts +7 -1
- package/dist/data/access.js +13 -3
- package/dist/data/tasks.d.ts +18 -3
- package/dist/data/tasks.js +8 -7
- package/dist/entrypoint.js +44 -10
- package/dist/fanout/adapters/claude.d.ts +2 -0
- package/dist/fanout/adapters/claude.js +7 -0
- package/dist/fanout/adapters/codex.d.ts +2 -0
- package/dist/fanout/adapters/codex.js +9 -0
- package/dist/fanout/adapters/opencode.d.ts +4 -0
- package/dist/fanout/adapters/opencode.js +9 -0
- package/dist/fanout/adapters/types.d.ts +21 -0
- package/dist/fanout/adapters/types.js +3 -0
- package/dist/fanout/command.d.ts +2 -0
- package/dist/fanout/command.js +78 -0
- package/dist/fanout/launcher.d.ts +27 -0
- package/dist/fanout/launcher.js +156 -0
- package/dist/fanout/opencode-serve.d.ts +24 -0
- package/dist/fanout/opencode-serve.js +191 -0
- package/dist/fanout/picker.d.ts +91 -0
- package/dist/fanout/picker.js +51 -0
- package/dist/fanout/usage.d.ts +5 -0
- package/dist/fanout/usage.js +51 -0
- package/dist/fanout/watchdog.d.ts +10 -0
- package/dist/fanout/watchdog.js +27 -0
- package/dist/filenames.d.ts +9 -0
- package/dist/filenames.js +11 -0
- package/dist/finding/context.js +4 -22
- package/dist/generated/memory-ui-graph.browser.js +279 -283
- package/dist/governance/policy-constants.d.ts +11 -0
- package/dist/governance/policy-constants.js +9 -0
- package/dist/governance/policy.d.ts +5 -8
- package/dist/governance/policy.js +7 -5
- package/dist/graph-core/camera.d.ts +24 -0
- package/dist/graph-core/camera.js +18 -0
- package/dist/graph-core/index.d.ts +2 -0
- package/dist/graph-core/index.js +2 -0
- package/dist/graph-core/labels.d.ts +77 -0
- package/dist/graph-core/labels.js +124 -0
- package/dist/graph-core/model.d.ts +13 -0
- package/dist/graph-core/model.js +33 -0
- package/dist/graph-core/types.d.ts +2 -0
- package/dist/home-paths.d.ts +7 -0
- package/dist/home-paths.js +21 -0
- package/dist/hooks.d.ts +3 -0
- package/dist/hooks.js +13 -0
- package/dist/index.js +24 -2
- package/dist/init/guard-globals.d.ts +1 -1
- package/dist/init/guard-globals.js +4 -4
- package/dist/init/init-configure.js +3 -3
- package/dist/init/init-uninstall.js +2 -2
- package/dist/init/init-walkthrough.d.ts +2 -0
- package/dist/init/init-walkthrough.js +17 -6
- package/dist/init/init.js +26 -6
- package/dist/init/management-preset.d.ts +1 -1
- package/dist/init/management-preset.js +2 -2
- package/dist/init/preferences.js +1 -1
- package/dist/init/self-wiring.js +1 -1
- package/dist/init/setup.d.ts +3 -26
- package/dist/init/setup.js +49 -588
- package/dist/init/shared.js +2 -0
- package/dist/init/store-gitignore.d.ts +1 -1
- package/dist/init/store-gitignore.js +2 -0
- package/dist/init/teardown.d.ts +1 -1
- package/dist/init/teardown.js +2 -2
- package/dist/link/context.js +1 -1
- package/dist/link/doctor.d.ts +22 -1
- package/dist/link/doctor.js +100 -54
- package/dist/link/link.js +29 -61
- package/dist/link/refresh.js +2 -2
- package/dist/link/skills.js +26 -5
- package/dist/machine-identity.d.ts +3 -0
- package/dist/machine-identity.js +6 -0
- package/dist/mcp/profile.d.ts +2 -15
- package/dist/mcp/profile.js +64 -9
- package/dist/memory-ui-graph.runtime.js +279 -283
- package/dist/modules/code-package.d.ts +16 -0
- package/dist/modules/code-package.js +152 -0
- package/dist/modules/command.d.ts +2 -0
- package/dist/modules/command.js +67 -0
- package/dist/modules/config.d.ts +4 -0
- package/dist/modules/config.js +70 -0
- package/dist/modules/manifest.d.ts +41 -0
- package/dist/modules/manifest.js +1 -0
- package/dist/modules/provision.d.ts +3 -0
- package/dist/modules/provision.js +84 -0
- package/dist/modules/registry.d.ts +16 -0
- package/dist/modules/registry.js +250 -0
- package/dist/modules/runtime.d.ts +15 -0
- package/dist/modules/runtime.js +59 -0
- package/dist/package-metadata.js +10 -2
- package/dist/phren-dotenv.js +4 -2
- package/dist/phren-paths.d.ts +3 -2
- package/dist/phren-paths.js +4 -5
- package/dist/profile-store.js +4 -5
- package/dist/project-config.d.ts +19 -1
- package/dist/project-config.js +29 -3
- package/dist/project-topics.js +4 -4
- package/dist/provider-adapters.js +1 -4
- package/dist/query-correlation.js +6 -6
- package/dist/session/artifacts.js +5 -5
- package/dist/session/checkpoints.js +5 -0
- package/dist/session/utils.d.ts +2 -2
- package/dist/session/utils.js +6 -6
- package/dist/shared/content.d.ts +1 -1
- package/dist/shared/content.js +1 -1
- package/dist/shared/data-utils.js +1 -1
- package/dist/shared/fragment-graph.d.ts +1 -1
- package/dist/shared/fragment-graph.js +5 -5
- package/dist/shared/index.js +48 -7
- package/dist/shared/retrieval.d.ts +18 -1
- package/dist/shared/retrieval.js +100 -11
- package/dist/shared/search-fallback.js +4 -6
- package/dist/shared/sqljs.d.ts +2 -0
- package/dist/shared/sqljs.js +4 -2
- package/dist/shared.d.ts +0 -1
- package/dist/shared.js +0 -1
- package/dist/shell/input.d.ts +1 -5
- package/dist/shell/input.js +16 -10
- package/dist/shell/palette.d.ts +2 -2
- package/dist/shell/palette.js +3 -7
- package/dist/shell/render.d.ts +3 -1
- package/dist/shell/render.js +4 -4
- package/dist/shell/shell.js +4 -1
- package/dist/shell/types.d.ts +1 -0
- package/dist/shell/types.js +4 -0
- package/dist/shell/view.d.ts +0 -4
- package/dist/shell/view.js +19 -27
- package/dist/skill/registry.js +25 -17
- package/dist/skill/state.d.ts +3 -0
- package/dist/skill/state.js +13 -7
- package/dist/status.js +38 -17
- package/dist/store-registry.d.ts +44 -18
- package/dist/store-registry.js +169 -60
- package/dist/store-weight.d.ts +1 -6
- package/dist/store-weight.js +4 -2
- package/dist/sync/auth-doctor.d.ts +8 -0
- package/dist/sync/auth-doctor.js +27 -0
- package/dist/sync/auth.d.ts +27 -0
- package/dist/sync/auth.js +114 -0
- package/dist/sync/conflict-resolve.d.ts +9 -0
- package/dist/sync/conflict-resolve.js +146 -0
- package/dist/sync/git-state.d.ts +4 -0
- package/dist/sync/git-state.js +49 -0
- package/dist/sync/outcome.d.ts +34 -0
- package/dist/sync/outcome.js +57 -0
- package/dist/sync/pull.d.ts +15 -7
- package/dist/sync/pull.js +84 -35
- package/dist/sync/store-merge.d.ts +29 -0
- package/dist/sync/store-merge.js +137 -0
- package/dist/sync/task-merge.d.ts +22 -0
- package/dist/sync/task-merge.js +158 -0
- package/dist/task/github.js +1 -1
- package/dist/task/lifecycle.d.ts +5 -0
- package/dist/task/lifecycle.js +67 -23
- package/dist/terminal-text.d.ts +5 -0
- package/dist/terminal-text.js +11 -0
- package/dist/test-global-setup.js +7 -0
- package/dist/tool-registry.d.ts +1 -1
- package/dist/tool-registry.js +3 -1
- package/dist/tools/code.d.ts +3 -0
- package/dist/tools/code.js +178 -0
- package/dist/tools/config.js +5 -0
- package/dist/tools/data.js +14 -7
- package/dist/tools/dispatch.d.ts +2 -0
- package/dist/tools/dispatch.js +66 -0
- package/dist/tools/finding.js +43 -41
- package/dist/tools/ops.js +5 -2
- package/dist/tools/search.js +21 -8
- package/dist/tools/session.js +21 -22
- package/dist/tools/tasks.js +8 -2
- package/dist/tools/types.d.ts +2 -2
- package/dist/tools/types.js +2 -2
- package/dist/ui/assets.js +1 -1
- package/dist/ui/data.js +14 -15
- package/dist/ui/deep-void.d.ts +1 -1
- package/dist/ui/deep-void.js +0 -1
- package/dist/ui/graph-chrome.d.ts +1 -1
- package/dist/ui/graph-chrome.js +51 -0
- package/dist/ui/page.js +5 -5
- package/dist/ui/scripts.js +67 -30
- package/dist/ui/server.js +34 -6
- package/dist/update.js +1 -1
- package/dist/utils-fts.js +1 -0
- package/dist/utils-helpers.d.ts +7 -0
- package/dist/utils-helpers.js +17 -0
- package/dist/utils.d.ts +1 -1
- package/dist/utils.js +1 -1
- package/package.json +45 -3
- package/plugins/opencode/phren-transcript.js +340 -0
- package/starter/README.md +4 -4
- package/starter/global/{CLAUDE.md → AGENTS.md} +2 -2
- package/starter/global/skills/conductor/SKILL.md +106 -0
- package/starter/global/skills/fanout/SKILL.md +20 -0
- package/starter/global/skills/phren-discover/SKILL.md +6 -6
- package/starter/global/skills/phren-init/SKILL.md +4 -4
- package/starter/global/skills/phren-sync/SKILL.md +3 -3
- package/starter/templates/frontend/reference/README.md +1 -1
- package/starter/templates/library/reference/README.md +1 -1
- package/starter/templates/monorepo/reference/README.md +1 -1
- package/starter/templates/python-project/reference/README.md +1 -1
- /package/starter/templates/frontend/{CLAUDE.md → AGENTS.md} +0 -0
- /package/starter/templates/library/{CLAUDE.md → AGENTS.md} +0 -0
- /package/starter/templates/monorepo/{CLAUDE.md → AGENTS.md} +0 -0
- /package/starter/templates/python-project/{CLAUDE.md → AGENTS.md} +0 -0
|
@@ -0,0 +1,552 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { execFile } from "node:child_process";
|
|
3
|
+
import { lstat, readdir, readFile, realpath, stat } from "node:fs/promises";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { promisify } from "node:util";
|
|
6
|
+
import { BridgeError } from "./protocol.js";
|
|
7
|
+
import { git, gitRoot } from "./projects.js";
|
|
8
|
+
const exec = promisify(execFile);
|
|
9
|
+
/** A status code from `--name-status`: the letter alone, with `T` (type change)
|
|
10
|
+
* shown as an edit and anything unrecognized treated as one. */
|
|
11
|
+
function statusLetter(code) {
|
|
12
|
+
const letter = code[0];
|
|
13
|
+
return ["M", "A", "D", "R", "C", "U"].includes(letter) ? letter : "M";
|
|
14
|
+
}
|
|
15
|
+
/** `--name-status -z` records as path-to-status; a rename carries old then new. */
|
|
16
|
+
function parseNameStatus(out) {
|
|
17
|
+
const map = new Map();
|
|
18
|
+
const tokens = out.split("\0");
|
|
19
|
+
for (let index = 0; index < tokens.length; index++) {
|
|
20
|
+
const code = tokens[index];
|
|
21
|
+
if (!code)
|
|
22
|
+
continue;
|
|
23
|
+
if (code[0] === "R" || code[0] === "C") {
|
|
24
|
+
index++;
|
|
25
|
+
const destination = tokens[++index];
|
|
26
|
+
if (destination)
|
|
27
|
+
map.set(destination, code[0]);
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
const file = tokens[++index];
|
|
31
|
+
if (file)
|
|
32
|
+
map.set(file, code[0]);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return map;
|
|
36
|
+
}
|
|
37
|
+
/** `--numstat -z` per-path line counts; a rename leaves the path empty and the
|
|
38
|
+
* two names follow as their own tokens. */
|
|
39
|
+
function parseNumstat(out) {
|
|
40
|
+
const map = new Map();
|
|
41
|
+
const tokens = out.split("\0");
|
|
42
|
+
for (let index = 0; index < tokens.length; index++) {
|
|
43
|
+
const record = tokens[index];
|
|
44
|
+
if (!record)
|
|
45
|
+
continue;
|
|
46
|
+
const parts = record.split("\t");
|
|
47
|
+
if (parts.length < 2)
|
|
48
|
+
continue;
|
|
49
|
+
const additions = parts[0] === "-" ? 0 : Number(parts[0]) || 0;
|
|
50
|
+
const deletions = parts[1] === "-" ? 0 : Number(parts[1]) || 0;
|
|
51
|
+
let file = parts.slice(2).join("\t");
|
|
52
|
+
if (!file) {
|
|
53
|
+
index++;
|
|
54
|
+
const destination = tokens[++index];
|
|
55
|
+
file = destination ?? "";
|
|
56
|
+
}
|
|
57
|
+
if (file)
|
|
58
|
+
map.set(file, { additions, deletions });
|
|
59
|
+
}
|
|
60
|
+
return map;
|
|
61
|
+
}
|
|
62
|
+
/** A count of a text file's lines, for an untracked file whose diff has no base. */
|
|
63
|
+
async function countLines(root, rel) {
|
|
64
|
+
try {
|
|
65
|
+
const abs = path.join(root, await repositoryPath(root, rel));
|
|
66
|
+
if ((await stat(abs)).size > 5_000_000)
|
|
67
|
+
return 0;
|
|
68
|
+
const bytes = await readFile(abs);
|
|
69
|
+
if (!bytes.length || bytes.includes(0))
|
|
70
|
+
return 0;
|
|
71
|
+
let lines = 0;
|
|
72
|
+
for (const byte of bytes)
|
|
73
|
+
if (byte === 10)
|
|
74
|
+
lines++;
|
|
75
|
+
return bytes[bytes.length - 1] === 10 ? lines : lines + 1;
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
return 0;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
/** Everything `git status` reports, kept apart by section so `gitLog` and
|
|
82
|
+
* `gitTree` can reuse it. */
|
|
83
|
+
async function collect(root) {
|
|
84
|
+
const branch = (await git(root, "branch", "--show-current")).trim();
|
|
85
|
+
let upstream;
|
|
86
|
+
let ahead = 0, behind = 0;
|
|
87
|
+
try {
|
|
88
|
+
upstream = (await git(root, "rev-parse", "--abbrev-ref", "--symbolic-full-name", "@{upstream}")).trim() || undefined;
|
|
89
|
+
}
|
|
90
|
+
catch {
|
|
91
|
+
upstream = undefined;
|
|
92
|
+
}
|
|
93
|
+
if (upstream) {
|
|
94
|
+
try {
|
|
95
|
+
const counts = (await git(root, "rev-list", "--left-right", "--count", `${upstream}...HEAD`)).trim().split(/\s+/);
|
|
96
|
+
behind = Number(counts[0]) || 0;
|
|
97
|
+
ahead = Number(counts[1]) || 0;
|
|
98
|
+
}
|
|
99
|
+
catch {
|
|
100
|
+
ahead = 0;
|
|
101
|
+
behind = 0;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
const [stagedRaw, unstagedRaw, stagedCounts, unstagedCounts, untrackedRaw] = await Promise.all([
|
|
105
|
+
git(root, "diff", "--cached", "--name-status", "-z"),
|
|
106
|
+
git(root, "diff", "--name-status", "-z"),
|
|
107
|
+
git(root, "diff", "--cached", "--numstat", "-z"),
|
|
108
|
+
git(root, "diff", "--numstat", "-z"),
|
|
109
|
+
git(root, "ls-files", "--others", "--exclude-standard", "-z"),
|
|
110
|
+
]);
|
|
111
|
+
const staged = parseNameStatus(stagedRaw), unstaged = parseNameStatus(unstagedRaw);
|
|
112
|
+
const stagedStats = parseNumstat(stagedCounts), unstagedStats = parseNumstat(unstagedCounts);
|
|
113
|
+
const untracked = untrackedRaw.split("\0").filter(Boolean);
|
|
114
|
+
return { branch, upstream, ahead, behind, staged, unstaged, stagedStats, unstagedStats, untracked };
|
|
115
|
+
}
|
|
116
|
+
/** The remote's default branch as its `HEAD` records it. With no recorded
|
|
117
|
+
* `HEAD`, `main` and `master` are treated as default so the guard errs on the
|
|
118
|
+
* side of asking. */
|
|
119
|
+
export async function defaultBranch(root, remote = "origin") {
|
|
120
|
+
const head = (await git(root, "symbolic-ref", "--quiet", "--short", `refs/remotes/${remote}/HEAD`).catch(() => "")).trim();
|
|
121
|
+
if (head.startsWith(remote + "/"))
|
|
122
|
+
return head.slice(remote.length + 1);
|
|
123
|
+
const branches = new Set((await git(root, "for-each-ref", "--format=%(refname:short)", "refs/heads").catch(() => "")).split("\n").filter(Boolean));
|
|
124
|
+
return branches.has("main") ? "main" : branches.has("master") ? "master" : null;
|
|
125
|
+
}
|
|
126
|
+
async function repository(cwd) {
|
|
127
|
+
const root = await gitRoot(cwd);
|
|
128
|
+
if (!root)
|
|
129
|
+
throw new BridgeError(409, "This pane is not in a Git repository.");
|
|
130
|
+
return root;
|
|
131
|
+
}
|
|
132
|
+
/** Working-tree status, one record per section: a file edited in both the index
|
|
133
|
+
* and the working tree appears once with `staged: true` and once with `false`. */
|
|
134
|
+
export async function gitStatus(cwd) {
|
|
135
|
+
const root = await repository(cwd);
|
|
136
|
+
treeCache.delete(root);
|
|
137
|
+
const data = await collect(root);
|
|
138
|
+
const fallback = await defaultBranch(root, data.upstream?.split("/")[0] || "origin");
|
|
139
|
+
const files = [];
|
|
140
|
+
for (const [file, status] of data.staged) {
|
|
141
|
+
const counts = data.stagedStats.get(file) ?? { additions: 0, deletions: 0 };
|
|
142
|
+
files.push({ path: file, status: statusLetter(status), staged: true, ...counts });
|
|
143
|
+
}
|
|
144
|
+
for (const [file, status] of data.unstaged) {
|
|
145
|
+
const counts = data.unstagedStats.get(file) ?? { additions: 0, deletions: 0 };
|
|
146
|
+
files.push({ path: file, status: statusLetter(status), staged: false, ...counts });
|
|
147
|
+
}
|
|
148
|
+
for (const file of data.untracked)
|
|
149
|
+
files.push({ path: file, status: "?", staged: false, additions: await countLines(root, file), deletions: 0 });
|
|
150
|
+
const additions = files.reduce((total, file) => total + file.additions, 0);
|
|
151
|
+
const deletions = files.reduce((total, file) => total + file.deletions, 0);
|
|
152
|
+
return { branch: data.branch, upstream: data.upstream ?? null, ahead: data.ahead, behind: data.behind,
|
|
153
|
+
staged: data.staged.size, unstaged: data.unstaged.size, untracked: data.untracked.length, additions, deletions, files,
|
|
154
|
+
defaultBranch: fallback };
|
|
155
|
+
}
|
|
156
|
+
/** The repository's remotes, so a branch ref can be told from a remote-tracking
|
|
157
|
+
* one even when a local branch name contains a slash. */
|
|
158
|
+
async function remotes(root) {
|
|
159
|
+
return (await git(root, "remote").catch(() => "")).split("\n").map(line => line.trim()).filter(Boolean);
|
|
160
|
+
}
|
|
161
|
+
function refEntries(spec, remotes) {
|
|
162
|
+
const refs = [];
|
|
163
|
+
for (const raw of spec.split(",").map(part => part.trim()).filter(Boolean)) {
|
|
164
|
+
if (raw === "HEAD") {
|
|
165
|
+
refs.push({ name: "HEAD", kind: "head" });
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
if (raw.startsWith("HEAD -> ")) {
|
|
169
|
+
refs.push({ name: "HEAD", kind: "head" });
|
|
170
|
+
const name = raw.slice(8);
|
|
171
|
+
refs.push({ name, kind: remotes.some(remote => name.startsWith(remote + "/")) ? "remote" : "local" });
|
|
172
|
+
}
|
|
173
|
+
else if (raw.startsWith("tag: "))
|
|
174
|
+
refs.push({ name: raw.slice(5), kind: "tag" });
|
|
175
|
+
else
|
|
176
|
+
refs.push({ name: raw, kind: remotes.some(remote => raw.startsWith(remote + "/")) ? "remote" : "local" });
|
|
177
|
+
}
|
|
178
|
+
return refs;
|
|
179
|
+
}
|
|
180
|
+
/** Commits newest first, plus a one-line summary of what is not committed yet. */
|
|
181
|
+
export async function gitLog(cwd, limit = 60, ref) {
|
|
182
|
+
const root = await repository(cwd);
|
|
183
|
+
const count = Math.min(200, Math.max(1, Math.trunc(limit) || 60));
|
|
184
|
+
let revision;
|
|
185
|
+
if (ref !== undefined) {
|
|
186
|
+
if (!ref || ref.length > 512 || ref.startsWith("-") || /[\x00-\x1f\x7f]/.test(ref))
|
|
187
|
+
throw new BridgeError(400, "Invalid branch ref.");
|
|
188
|
+
// Resolve to a commit first so Git never interprets a ref as an option or path.
|
|
189
|
+
try {
|
|
190
|
+
revision = (await git(root, "rev-parse", "--verify", "--end-of-options", `${ref}^{commit}`)).trim();
|
|
191
|
+
}
|
|
192
|
+
catch {
|
|
193
|
+
throw new BridgeError(400, "That branch ref does not name a commit.");
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
else {
|
|
197
|
+
revision = (await git(root, "rev-parse", "--verify", "HEAD").catch(() => "")).trim();
|
|
198
|
+
}
|
|
199
|
+
const known = await remotes(root);
|
|
200
|
+
const output = revision ? await git(root, "log", "--decorate=short", "--date=iso-strict", "--format=%H%x00%h%x00%s%x00%an%x00%cI%x00%D%x00%P", "-n", String(count), revision, "--") : "";
|
|
201
|
+
const commits = [];
|
|
202
|
+
for (const line of output.split("\n")) {
|
|
203
|
+
if (!line)
|
|
204
|
+
continue;
|
|
205
|
+
const [sha, short, subject, author, date, spec, parents] = line.split("\0");
|
|
206
|
+
commits.push({ sha, short, subject, author, date, refs: refEntries(spec ?? "", known), parents: (parents ?? "").split(" ").filter(Boolean) });
|
|
207
|
+
}
|
|
208
|
+
const status = await gitStatus(root);
|
|
209
|
+
const uncommitted = { files: new Set(status.files.map(file => file.path)).size, additions: status.additions, deletions: status.deletions };
|
|
210
|
+
return { commits, uncommitted };
|
|
211
|
+
}
|
|
212
|
+
/** Local branches with their upstream and ahead/behind counts, and remote refs. */
|
|
213
|
+
export async function gitBranches(cwd) {
|
|
214
|
+
const root = await repository(cwd);
|
|
215
|
+
const format = "%(refname)%00%(refname:short)%00%(upstream:short)%00%(upstream:track)%00%(committerdate:iso-strict)%00%(HEAD)";
|
|
216
|
+
const output = await git(root, "for-each-ref", `--format=${format}`, "refs/heads", "refs/remotes");
|
|
217
|
+
const local = [], remote = [];
|
|
218
|
+
let current = null;
|
|
219
|
+
for (const line of output.split("\n")) {
|
|
220
|
+
if (!line)
|
|
221
|
+
continue;
|
|
222
|
+
const [refname, short, upstream, track, date, head] = line.split("\0");
|
|
223
|
+
if (refname.startsWith("refs/remotes/")) {
|
|
224
|
+
if (short.endsWith("/HEAD"))
|
|
225
|
+
continue;
|
|
226
|
+
remote.push({ name: short, date });
|
|
227
|
+
}
|
|
228
|
+
else if (refname.startsWith("refs/heads/")) {
|
|
229
|
+
const gone = (track ?? "").includes("gone");
|
|
230
|
+
const ahead = Number(/ahead (\d+)/.exec(track ?? "")?.[1]) || 0;
|
|
231
|
+
const behind = Number(/behind (\d+)/.exec(track ?? "")?.[1]) || 0;
|
|
232
|
+
if (head === "*")
|
|
233
|
+
current = short;
|
|
234
|
+
local.push({ name: short, ...(upstream && !gone ? { upstream } : {}), ahead, behind, date });
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
return { current, local, remote };
|
|
238
|
+
}
|
|
239
|
+
const ghEnv = () => ({ ...process.env, GIT_CONFIG_NOSYSTEM: "1", GH_PROMPT_DISABLED: "1", GH_NO_UPDATE_NOTIFIER: "1" });
|
|
240
|
+
/** A check rollup as one word: any failure fails, anything unfinished is
|
|
241
|
+
* pending, and only finished successes (or skips) pass. No checks is null. */
|
|
242
|
+
export function checkRollup(items) {
|
|
243
|
+
if (!Array.isArray(items) || !items.length)
|
|
244
|
+
return null;
|
|
245
|
+
let pending = false;
|
|
246
|
+
for (const raw of items) {
|
|
247
|
+
const item = raw && typeof raw === "object" ? raw : {};
|
|
248
|
+
const conclusion = String(item.conclusion ?? "").toUpperCase(), state = String(item.state ?? "").toUpperCase();
|
|
249
|
+
const status = String(item.status ?? "").toUpperCase();
|
|
250
|
+
if (["FAILURE", "TIMED_OUT", "CANCELLED", "ACTION_REQUIRED", "STARTUP_FAILURE"].includes(conclusion) || ["FAILURE", "ERROR"].includes(state))
|
|
251
|
+
return "failing";
|
|
252
|
+
// A commit status has a state; a check run has a status and, once
|
|
253
|
+
// completed, a conclusion.
|
|
254
|
+
if (state && !status) {
|
|
255
|
+
if (state !== "SUCCESS")
|
|
256
|
+
pending = true;
|
|
257
|
+
}
|
|
258
|
+
else if (status !== "COMPLETED")
|
|
259
|
+
pending = true;
|
|
260
|
+
}
|
|
261
|
+
return pending ? "pending" : "passing";
|
|
262
|
+
}
|
|
263
|
+
/** The checked-out branch's pull request in any state, with its checks, or
|
|
264
|
+
* null when the branch has none (or gh cannot say). */
|
|
265
|
+
async function currentPull(root, branch) {
|
|
266
|
+
try {
|
|
267
|
+
const { stdout } = await exec("gh", ["pr", "view", "--json", "number,title,url,state,isDraft,headRefName,baseRefName,statusCheckRollup"], {
|
|
268
|
+
cwd: root, timeout: 15_000, maxBuffer: 4_194_304, env: ghEnv(),
|
|
269
|
+
});
|
|
270
|
+
const pull = JSON.parse(stdout || "null");
|
|
271
|
+
if (!pull || typeof pull !== "object" || typeof pull.number !== "number" || pull.headRefName !== branch)
|
|
272
|
+
return null;
|
|
273
|
+
return { number: pull.number, title: String(pull.title ?? ""), url: String(pull.url ?? ""), head: branch,
|
|
274
|
+
base: String(pull.baseRefName ?? ""), draft: pull.isDraft === true, state: String(pull.state ?? ""), checks: checkRollup(pull.statusCheckRollup) };
|
|
275
|
+
}
|
|
276
|
+
catch {
|
|
277
|
+
return null;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
/** Open pull requests through `gh`, or `{ available: false }` when the tool is
|
|
281
|
+
* missing or not signed in. A failure is a normal answer, never an error.
|
|
282
|
+
* `current` is the checked-out branch's own pull request in any state (open,
|
|
283
|
+
* draft, merged or closed) with its checks, which the session card shows. */
|
|
284
|
+
export async function gitPulls(cwd) {
|
|
285
|
+
const root = await repository(cwd);
|
|
286
|
+
const branch = (await git(root, "branch", "--show-current").catch(() => "")).trim();
|
|
287
|
+
try {
|
|
288
|
+
const [{ stdout }, current] = await Promise.all([
|
|
289
|
+
exec("gh", ["pr", "list", "--json", "number,title,headRefName,baseRefName,author,url,isDraft,state,updatedAt", "--limit", "50"], {
|
|
290
|
+
cwd: root, timeout: 15_000, maxBuffer: 4_194_304, env: ghEnv(),
|
|
291
|
+
}),
|
|
292
|
+
branch ? currentPull(root, branch) : Promise.resolve(null),
|
|
293
|
+
]);
|
|
294
|
+
const parsed = JSON.parse(stdout || "[]");
|
|
295
|
+
const pulls = (Array.isArray(parsed) ? parsed : []).map((pull) => {
|
|
296
|
+
const author = pull.author && typeof pull.author === "object" ? pull.author : {};
|
|
297
|
+
return { number: pull.number, title: pull.title, head: pull.headRefName, base: pull.baseRefName,
|
|
298
|
+
author: String(author.login ?? author.name ?? ""), url: pull.url, draft: pull.isDraft === true, state: pull.state, updated: pull.updatedAt };
|
|
299
|
+
});
|
|
300
|
+
return { available: true, pulls, branch: branch || null, current };
|
|
301
|
+
}
|
|
302
|
+
catch {
|
|
303
|
+
return { available: false, pulls: [], branch: branch || null, current: null };
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
/** A repo-relative tree path, or the repository root for `""`. */
|
|
307
|
+
async function repositoryPath(root, raw, allowRoot = false) {
|
|
308
|
+
if (typeof raw !== "string" || raw.length > 4096 || raw.includes("\0") || path.isAbsolute(raw) || path.win32.isAbsolute(raw) || raw.startsWith("\\"))
|
|
309
|
+
throw new BridgeError(400, "Invalid path.");
|
|
310
|
+
const segments = raw.split(/[\\/]/);
|
|
311
|
+
if (segments.includes(".."))
|
|
312
|
+
throw new BridgeError(400, "Invalid path.");
|
|
313
|
+
const clean = segments.filter(part => part && part !== ".").join("/");
|
|
314
|
+
if ((!clean && !allowRoot) || clean.startsWith("-"))
|
|
315
|
+
throw new BridgeError(400, "Invalid path.");
|
|
316
|
+
const abs = path.resolve(root, clean);
|
|
317
|
+
if (abs !== root && !abs.startsWith(root + path.sep))
|
|
318
|
+
throw new BridgeError(403, "This path is outside the repository.");
|
|
319
|
+
// Deleted files still need stage/restore. Check their nearest existing
|
|
320
|
+
// ancestor, but never treat a dangling symlink as a missing ordinary path.
|
|
321
|
+
let existing = abs;
|
|
322
|
+
while (true) {
|
|
323
|
+
try {
|
|
324
|
+
await lstat(existing);
|
|
325
|
+
break;
|
|
326
|
+
}
|
|
327
|
+
catch (error) {
|
|
328
|
+
if (error.code !== "ENOENT" || existing === root)
|
|
329
|
+
throw new BridgeError(400, "Invalid path.");
|
|
330
|
+
existing = path.dirname(existing);
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
let resolved;
|
|
334
|
+
try {
|
|
335
|
+
resolved = await realpath(existing);
|
|
336
|
+
}
|
|
337
|
+
catch {
|
|
338
|
+
throw new BridgeError(403, "This path cannot be resolved inside the repository.");
|
|
339
|
+
}
|
|
340
|
+
if (resolved !== root && !resolved.startsWith(root + path.sep))
|
|
341
|
+
throw new BridgeError(403, "This path is outside the repository.");
|
|
342
|
+
return clean;
|
|
343
|
+
}
|
|
344
|
+
const treeCache = new Map();
|
|
345
|
+
/** Drop the cached tree after a write (commit, push) outside this module. */
|
|
346
|
+
export function invalidateTree(root) { treeCache.delete(root); }
|
|
347
|
+
const TREE_TTL_MS = 2_000;
|
|
348
|
+
/** Build directory children once, without diff hunks, line counts or upstream walks. */
|
|
349
|
+
async function treeSnapshot(root, head) {
|
|
350
|
+
const [tracked, porcelain] = await Promise.all([
|
|
351
|
+
git(root, "ls-files", "-z"),
|
|
352
|
+
git(root, "status", "--porcelain=v1", "-z", "--untracked-files=all"),
|
|
353
|
+
]);
|
|
354
|
+
const changed = new Map();
|
|
355
|
+
const files = new Set(tracked.split("\0").filter(Boolean));
|
|
356
|
+
const tokens = porcelain.split("\0");
|
|
357
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
358
|
+
const row = tokens[i];
|
|
359
|
+
if (!row)
|
|
360
|
+
continue;
|
|
361
|
+
const xy = row.slice(0, 2), file = row.slice(3);
|
|
362
|
+
const letter = xy === "??" ? "?" : statusLetter(xy[1] !== " " ? xy[1] : xy[0]);
|
|
363
|
+
changed.set(file, letter);
|
|
364
|
+
files.add(file);
|
|
365
|
+
// Porcelain -z places the destination before the source of a rename.
|
|
366
|
+
if (/[RC]/.test(xy))
|
|
367
|
+
i++;
|
|
368
|
+
}
|
|
369
|
+
const levels = new Map();
|
|
370
|
+
levels.set("", new Map());
|
|
371
|
+
for (const file of files) {
|
|
372
|
+
const parts = file.split("/");
|
|
373
|
+
if (parts.includes("node_modules") || parts.includes(".git"))
|
|
374
|
+
continue;
|
|
375
|
+
let parent = "";
|
|
376
|
+
for (let i = 0; i < parts.length; i++) {
|
|
377
|
+
const name = parts[i], full = parent ? `${parent}/${name}` : name;
|
|
378
|
+
const directory = i < parts.length - 1;
|
|
379
|
+
let siblings = levels.get(parent);
|
|
380
|
+
if (!siblings) {
|
|
381
|
+
siblings = new Map();
|
|
382
|
+
levels.set(parent, siblings);
|
|
383
|
+
}
|
|
384
|
+
let entry = siblings.get(name);
|
|
385
|
+
if (!entry || (directory && entry.kind === "file")) {
|
|
386
|
+
// A deleted tracked file can coexist with untracked descendants at
|
|
387
|
+
// the same path. Descendants make this a navigable directory.
|
|
388
|
+
entry = { name, path: full, kind: directory ? "dir" : "file", ...(directory ? { fileCount: 0 } : {}) };
|
|
389
|
+
siblings.set(name, entry);
|
|
390
|
+
}
|
|
391
|
+
if (directory) {
|
|
392
|
+
entry.fileCount = (entry.fileCount ?? 0) + 1;
|
|
393
|
+
if (changed.has(file))
|
|
394
|
+
entry.status = "changed";
|
|
395
|
+
}
|
|
396
|
+
else if (changed.has(file))
|
|
397
|
+
entry.status = entry.kind === "dir" ? "changed" : changed.get(file);
|
|
398
|
+
parent = full;
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
const version = createHash("sha256").update(head).update(tracked).update(porcelain).digest("hex");
|
|
402
|
+
return { version, levels: new Map([...levels].map(([directory, entries]) => [directory,
|
|
403
|
+
[...entries.values()].sort((a, b) => a.kind === b.kind ? a.name.localeCompare(b.name) : a.kind === "dir" ? -1 : 1)])) };
|
|
404
|
+
}
|
|
405
|
+
const MAX_IGNORED_ENTRIES = 1_000;
|
|
406
|
+
const ignoredCache = new Map();
|
|
407
|
+
/** Git-ignored paths as `git ls-files --others --ignored --exclude-standard
|
|
408
|
+
* --directory` reports them: a wholly ignored folder once, with a trailing
|
|
409
|
+
* slash, instead of every file inside it. */
|
|
410
|
+
async function ignoredListing(root) {
|
|
411
|
+
let cached = ignoredCache.get(root);
|
|
412
|
+
if (!cached || cached.expires <= Date.now()) {
|
|
413
|
+
if (ignoredCache.size >= 32)
|
|
414
|
+
ignoredCache.delete(ignoredCache.keys().next().value);
|
|
415
|
+
cached = { expires: Date.now() + TREE_TTL_MS,
|
|
416
|
+
listing: git(root, "ls-files", "--others", "--ignored", "--exclude-standard", "--directory", "-z").then(out => out.split("\0").filter(Boolean)) };
|
|
417
|
+
ignoredCache.set(root, cached);
|
|
418
|
+
}
|
|
419
|
+
try {
|
|
420
|
+
return await cached.listing;
|
|
421
|
+
}
|
|
422
|
+
catch (error) {
|
|
423
|
+
ignoredCache.delete(root);
|
|
424
|
+
throw error;
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
/** The ignored entries directly under `prefix`, marked `ignored`. Inside a
|
|
428
|
+
* wholly ignored folder Git lists nothing further, so that level is read from
|
|
429
|
+
* the disk itself (no `.git`, symlinks shown as files and never followed). */
|
|
430
|
+
async function ignoredEntries(root, prefix, shown) {
|
|
431
|
+
const listing = await ignoredListing(root);
|
|
432
|
+
const taken = new Set(shown.map(entry => entry.name));
|
|
433
|
+
const entries = new Map();
|
|
434
|
+
const inside = prefix && listing.some(item => item.endsWith("/") && (prefix + "/").startsWith(item));
|
|
435
|
+
if (inside) {
|
|
436
|
+
const dirents = await readdir(path.join(root, prefix), { withFileTypes: true }).catch(() => []);
|
|
437
|
+
for (const dirent of dirents) {
|
|
438
|
+
if (dirent.name === ".git" || taken.has(dirent.name) || entries.size >= MAX_IGNORED_ENTRIES)
|
|
439
|
+
continue;
|
|
440
|
+
entries.set(dirent.name, { name: dirent.name, path: `${prefix}/${dirent.name}`, kind: dirent.isDirectory() ? "dir" : "file", ignored: true });
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
else {
|
|
444
|
+
const base = prefix ? prefix + "/" : "";
|
|
445
|
+
for (const item of listing) {
|
|
446
|
+
if (!item.startsWith(base) || item.length <= base.length)
|
|
447
|
+
continue;
|
|
448
|
+
const rest = item.slice(base.length), slash = rest.indexOf("/");
|
|
449
|
+
const name = slash < 0 ? rest : rest.slice(0, slash);
|
|
450
|
+
if (!name || name === ".git" || taken.has(name) || entries.has(name))
|
|
451
|
+
continue;
|
|
452
|
+
if (entries.size >= MAX_IGNORED_ENTRIES)
|
|
453
|
+
break;
|
|
454
|
+
entries.set(name, { name, path: base + name, kind: slash < 0 ? "file" : "dir", ignored: true });
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
return [...entries.values()];
|
|
458
|
+
}
|
|
459
|
+
/** One lazy directory response from a bounded repo/HEAD/status-hash snapshot.
|
|
460
|
+
* Every request still validates the pane's repo and the requested path. A HEAD
|
|
461
|
+
* move invalidates immediately; external working-tree edits age out after 2s.
|
|
462
|
+
* Status refresh and phone mutations invalidate immediately as well. With
|
|
463
|
+
* `ignored`, git-ignored folders and files at that level are added, marked. */
|
|
464
|
+
export async function gitTree(cwd, relPath = "", ignored = false) {
|
|
465
|
+
const root = await repository(cwd);
|
|
466
|
+
const prefix = await repositoryPath(root, relPath, true);
|
|
467
|
+
const head = await git(root, "rev-parse", "HEAD").catch(() => "unborn");
|
|
468
|
+
let cached = treeCache.get(root);
|
|
469
|
+
if (!cached || cached.head !== head || cached.expires <= Date.now()) {
|
|
470
|
+
if (treeCache.size >= 32)
|
|
471
|
+
treeCache.delete(treeCache.keys().next().value);
|
|
472
|
+
cached = { head, expires: Date.now() + TREE_TTL_MS, snapshot: treeSnapshot(root, head) };
|
|
473
|
+
treeCache.set(root, cached);
|
|
474
|
+
}
|
|
475
|
+
try {
|
|
476
|
+
const snapshot = await cached.snapshot;
|
|
477
|
+
const entries = snapshot.levels.get(prefix) ?? [];
|
|
478
|
+
if (!ignored)
|
|
479
|
+
return { path: prefix, version: snapshot.version, entries };
|
|
480
|
+
const extra = await ignoredEntries(root, prefix, entries);
|
|
481
|
+
const all = [...entries, ...extra].sort((a, b) => a.kind === b.kind ? a.name.localeCompare(b.name) : a.kind === "dir" ? -1 : 1);
|
|
482
|
+
return { path: prefix, version: snapshot.version, entries: all };
|
|
483
|
+
}
|
|
484
|
+
catch (error) {
|
|
485
|
+
if (treeCache.get(root) === cached)
|
|
486
|
+
treeCache.delete(root);
|
|
487
|
+
throw error;
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
/** The phone's paths are repo-relative, at most 64, and can never climb out. */
|
|
491
|
+
async function repositoryPaths(root, raw) {
|
|
492
|
+
const list = Array.isArray(raw) ? raw : [];
|
|
493
|
+
if (!list.length || list.length > 64)
|
|
494
|
+
throw new BridgeError(400, "Choose between 1 and 64 paths.");
|
|
495
|
+
const paths = [];
|
|
496
|
+
for (const entry of list) {
|
|
497
|
+
const clean = await repositoryPath(root, entry);
|
|
498
|
+
if (clean && !paths.includes(clean))
|
|
499
|
+
paths.push(clean);
|
|
500
|
+
}
|
|
501
|
+
if (!paths.length)
|
|
502
|
+
throw new BridgeError(400, "Choose between 1 and 64 paths.");
|
|
503
|
+
return paths;
|
|
504
|
+
}
|
|
505
|
+
export async function gitStage(cwd, paths) {
|
|
506
|
+
const root = await repository(cwd);
|
|
507
|
+
treeCache.delete(root);
|
|
508
|
+
try {
|
|
509
|
+
await git(root, "add", "--", ...(await repositoryPaths(root, paths)).map(file => `:(literal)${file}`));
|
|
510
|
+
return { ok: true };
|
|
511
|
+
}
|
|
512
|
+
finally {
|
|
513
|
+
treeCache.delete(root);
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
export async function gitUnstage(cwd, paths) {
|
|
517
|
+
const root = await repository(cwd);
|
|
518
|
+
treeCache.delete(root);
|
|
519
|
+
try {
|
|
520
|
+
const files = (await repositoryPaths(root, paths)).map(file => `:(literal)${file}`);
|
|
521
|
+
const hasHead = await git(root, "rev-parse", "--verify", "HEAD").then(() => true, () => false);
|
|
522
|
+
if (hasHead)
|
|
523
|
+
await git(root, "restore", "--staged", "--", ...files);
|
|
524
|
+
else
|
|
525
|
+
await git(root, "rm", "--force", "--cached", "--", ...files);
|
|
526
|
+
return { ok: true };
|
|
527
|
+
}
|
|
528
|
+
finally {
|
|
529
|
+
treeCache.delete(root);
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
/** Destructive: tracked files go back to the index, untracked files are
|
|
533
|
+
* removed. The phone confirms first; no `-d` (directories) and no `-x`. */
|
|
534
|
+
export async function gitDiscard(cwd, paths) {
|
|
535
|
+
const root = await repository(cwd);
|
|
536
|
+
treeCache.delete(root);
|
|
537
|
+
try {
|
|
538
|
+
const files = await repositoryPaths(root, paths);
|
|
539
|
+
const untracked = new Set((await git(root, "ls-files", "--others", "--exclude-standard", "-z")).split("\0").filter(Boolean));
|
|
540
|
+
for (const file of files) {
|
|
541
|
+
const literal = `:(literal)${file}`;
|
|
542
|
+
if (untracked.has(file))
|
|
543
|
+
await git(root, "clean", "-f", "--", literal);
|
|
544
|
+
else
|
|
545
|
+
await git(root, "checkout", "--", literal);
|
|
546
|
+
}
|
|
547
|
+
return { ok: true };
|
|
548
|
+
}
|
|
549
|
+
finally {
|
|
550
|
+
treeCache.delete(root);
|
|
551
|
+
}
|
|
552
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
declare const grantAction: z.ZodEnum<{
|
|
3
|
+
dispatch: "dispatch";
|
|
4
|
+
hand_off: "hand_off";
|
|
5
|
+
}>;
|
|
6
|
+
export declare const grantSchema: z.ZodObject<{
|
|
7
|
+
scope: z.ZodUnion<readonly [z.ZodLiteral<"global">, z.ZodString]>;
|
|
8
|
+
actions: z.ZodArray<z.ZodEnum<{
|
|
9
|
+
dispatch: "dispatch";
|
|
10
|
+
hand_off: "hand_off";
|
|
11
|
+
}>>;
|
|
12
|
+
computers: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
13
|
+
until: z.ZodOptional<z.ZodString>;
|
|
14
|
+
}, z.core.$strict>;
|
|
15
|
+
export type Grant = z.infer<typeof grantSchema>;
|
|
16
|
+
export declare function listGrants(root?: string): Promise<Grant[]>;
|
|
17
|
+
export interface GrantQuery {
|
|
18
|
+
action: z.infer<typeof grantAction>;
|
|
19
|
+
project?: string;
|
|
20
|
+
computer?: string;
|
|
21
|
+
}
|
|
22
|
+
/** Most specific matching grant wins. A computers-restricted grant never
|
|
23
|
+
* covers an unresolved destination: `anywhere` may pick a peer outside the
|
|
24
|
+
* list, and a local hand-off never goes to one of them. */
|
|
25
|
+
export declare function matchGrant(grants: Grant[], query: GrantQuery, now?: number): Grant | undefined;
|
|
26
|
+
export declare function grantLabel(grant: Grant): string;
|
|
27
|
+
export declare function addGrant(input: unknown, root?: string): Promise<Grant>;
|
|
28
|
+
/** Write a grant from an approval card answer: already listed is success. */
|
|
29
|
+
export declare function ensureGrant(input: unknown, root?: string): Promise<Grant>;
|
|
30
|
+
export declare function removeGrant(input: unknown, root?: string): Promise<Grant>;
|
|
31
|
+
export {};
|