@carrieros/cli-staging 0.0.0-stage → 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 +6 -2
- package/dist/commands/attach.js +45 -0
- package/dist/commands/auth.js +60 -0
- package/dist/commands/btw.js +78 -0
- package/dist/commands/changes.js +167 -0
- package/dist/commands/chat.js +182 -0
- package/dist/commands/context.js +200 -0
- package/dist/commands/doctor.js +119 -0
- package/dist/commands/files.js +94 -0
- package/dist/commands/help.js +37 -0
- package/dist/commands/history.js +112 -0
- package/dist/commands/login.js +62 -0
- package/dist/commands/logout.js +25 -0
- package/dist/commands/memory.js +162 -0
- package/dist/commands/mode-model.js +64 -0
- package/dist/commands/models.js +28 -0
- package/dist/commands/modes.js +30 -0
- package/dist/commands/permissions.js +77 -0
- package/dist/commands/project.js +106 -0
- package/dist/commands/repo.js +146 -0
- package/dist/commands/repos.js +61 -0
- package/dist/commands/review.js +92 -0
- package/dist/commands/scope.js +155 -0
- package/dist/commands/search.js +92 -0
- package/dist/commands/sessions.js +282 -0
- package/dist/commands/settings.js +100 -0
- package/dist/commands/update.js +229 -0
- package/dist/commands/upload.js +223 -0
- package/dist/commands/usage.js +227 -0
- package/dist/commands/version.js +26 -0
- package/dist/commands/whoami.js +44 -0
- package/dist/ide/editor-context.js +275 -0
- package/dist/ide/ide-session.js +360 -0
- package/dist/ide/tool-host.js +109 -0
- package/dist/main.js +126 -0
- package/dist/services/api.js +439 -0
- package/dist/services/auth-origin.js +20 -0
- package/dist/services/chat-stream.js +168 -0
- package/dist/services/checkpoints.js +278 -0
- package/dist/services/client-tools.js +356 -0
- package/dist/services/code-intel.js +243 -0
- package/dist/services/code-symbols.js +94 -0
- package/dist/services/coding-workflow.js +554 -0
- package/dist/services/command-coordinator.js +273 -0
- package/dist/services/command-policy.js +434 -0
- package/dist/services/config.js +383 -0
- package/dist/services/container-runner.js +254 -0
- package/dist/services/container-supervisor.js +38 -0
- package/dist/services/context.js +371 -0
- package/dist/services/device-auth.js +286 -0
- package/dist/services/diff.js +196 -0
- package/dist/services/env-policy.js +108 -0
- package/dist/services/execution-platform.js +30 -0
- package/dist/services/file-detect.js +104 -0
- package/dist/services/file-resolver.js +199 -0
- package/dist/services/file-upload.js +126 -0
- package/dist/services/folder-key.js +22 -0
- package/dist/services/git-tools.js +429 -0
- package/dist/services/glob-match.js +164 -0
- package/dist/services/helper-bin.js +85 -0
- package/dist/services/ignore-rules.js +189 -0
- package/dist/services/instruction-resolver.js +54 -0
- package/dist/services/job-helper.js +328 -0
- package/dist/services/local-files.js +639 -0
- package/dist/services/local-tools.js +937 -0
- package/dist/services/login-flow.js +156 -0
- package/dist/services/meta.js +15 -0
- package/dist/services/mutation-coordinator.js +327 -0
- package/dist/services/mutation-groups.js +309 -0
- package/dist/services/mutation-recovery.js +159 -0
- package/dist/services/mutations.js +679 -0
- package/dist/services/open-url.js +68 -0
- package/dist/services/output.js +62 -0
- package/dist/services/packaged-update.js +403 -0
- package/dist/services/packaged.js +39 -0
- package/dist/services/permission-mode.js +78 -0
- package/dist/services/process-runner.js +412 -0
- package/dist/services/project-context.js +298 -0
- package/dist/services/redaction.js +201 -0
- package/dist/services/result-reconciliation.js +49 -0
- package/dist/services/secure-store.js +217 -0
- package/dist/services/session-resolver.js +113 -0
- package/dist/services/sia.js +51 -0
- package/dist/services/spinner.js +40 -0
- package/dist/services/supported-runtime.js +59 -0
- package/dist/services/task-context.js +212 -0
- package/dist/services/task-snapshot.js +38 -0
- package/dist/services/tool-result-outbox.js +195 -0
- package/dist/services/update.js +311 -0
- package/dist/services/workspace-registry.js +310 -0
- package/dist/services/workspace.js +580 -0
- package/dist/shell/shell-chat.js +1619 -0
- package/dist/shell/shell-commands.js +411 -0
- package/dist/shell/shell-prompt.js +110 -0
- package/dist/shell/shell-renderer.js +51 -0
- package/dist/shell/shell-router.js +37 -0
- package/dist/shell/shell.js +389 -0
- package/dist/shell/task-context-view.js +40 -0
- package/dist/staging.js +5 -0
- package/dist/types/index.js +6 -0
- package/dist/ui/agent-cards.js +111 -0
- package/dist/ui/agent-presentation.js +131 -0
- package/dist/ui/approval.js +111 -0
- package/dist/ui/banner.js +56 -0
- package/dist/ui/btw-overlay.js +230 -0
- package/dist/ui/change-review.js +334 -0
- package/dist/ui/chat-renderer.js +463 -0
- package/dist/ui/code-block.js +52 -0
- package/dist/ui/color-picker.js +100 -0
- package/dist/ui/colors.js +55 -0
- package/dist/ui/command-details.js +11 -0
- package/dist/ui/command-filter.js +92 -0
- package/dist/ui/command-item.js +6 -0
- package/dist/ui/command-palette.js +383 -0
- package/dist/ui/command-registry.js +128 -0
- package/dist/ui/command-renderer.js +55 -0
- package/dist/ui/command-review.js +187 -0
- package/dist/ui/composer-layout.js +74 -0
- package/dist/ui/conversation.js +450 -0
- package/dist/ui/interrupt.js +35 -0
- package/dist/ui/live.js +141 -0
- package/dist/ui/logo.js +58 -0
- package/dist/ui/markdown.js +205 -0
- package/dist/ui/message.js +21 -0
- package/dist/ui/screen-prompt.js +181 -0
- package/dist/ui/screen.js +297 -0
- package/dist/ui/select.js +284 -0
- package/dist/ui/sia-qa.js +213 -0
- package/dist/ui/spinner.js +23 -0
- package/dist/ui/sticky-stream.js +64 -0
- package/dist/ui/task-context-card.js +99 -0
- package/dist/ui/text-prompt.js +74 -0
- package/dist/ui/theme.js +80 -0
- package/dist/ui/trust.js +88 -0
- package/dist/ui/ui.js +101 -0
- package/package.json +33 -4
|
@@ -0,0 +1,580 @@
|
|
|
1
|
+
import { execFileSync } from "node:child_process";
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
/**
|
|
5
|
+
* What the CLI knows about the folder it was started in.
|
|
6
|
+
*
|
|
7
|
+
* One module owns this so cwd resolution does not scatter: before this, every
|
|
8
|
+
* caller that needed a path reached for `process.cwd()` on its own, and the
|
|
9
|
+
* shell had no idea what project it was sitting in. Everything here is derived
|
|
10
|
+
* at runtime and cached for the life of the process — nothing is persisted,
|
|
11
|
+
* because a branch name or a dirty count written to config would be stale the
|
|
12
|
+
* moment the user committed something, and a confidently wrong answer is worse
|
|
13
|
+
* than asking again.
|
|
14
|
+
*
|
|
15
|
+
* The design constraint that shapes the rest of this file: it runs on every
|
|
16
|
+
* startup, so it must be fast, and it must never be able to stop the shell from
|
|
17
|
+
* opening. Every Git call is bounded, every failure has a defined answer, and
|
|
18
|
+
* the whole thing is wrapped so that a surprise still yields a usable
|
|
19
|
+
* descriptor rather than an exception.
|
|
20
|
+
*/
|
|
21
|
+
/**
|
|
22
|
+
* Budgets for Git. The metadata calls read a file or two and return in single
|
|
23
|
+
* -digit milliseconds; the generous ceiling is only there for a cold disk.
|
|
24
|
+
*
|
|
25
|
+
* `status` is the one that walks the working tree, so it gets its own budget
|
|
26
|
+
* and its own failure mode: on a large monorepo it can take seconds, and
|
|
27
|
+
* holding the banner hostage for that is not worth a "3 modified" chip. When it
|
|
28
|
+
* runs out we report the branch and say nothing about cleanliness.
|
|
29
|
+
*/
|
|
30
|
+
const GIT_TIMEOUT_MS = 2000;
|
|
31
|
+
const STATUS_TIMEOUT_MS = 1500;
|
|
32
|
+
/** package.json only needs its top matter; refuse anything absurd. */
|
|
33
|
+
const MAX_MANIFEST_BYTES = 1024 * 1024;
|
|
34
|
+
/**
|
|
35
|
+
* Global options every Git call carries.
|
|
36
|
+
*
|
|
37
|
+
* A repository's own config is attacker-controlled input: cloning or unzipping
|
|
38
|
+
* a project is enough to plant `.git/config`. Measured against a hostile
|
|
39
|
+
* fixture, a plain `git status` ran BOTH a `core.fsmonitor` hook and a
|
|
40
|
+
* `filter.<name>.clean` command. `-c` beats every config file, and Git exports
|
|
41
|
+
* these overrides to any Git it starts itself.
|
|
42
|
+
*
|
|
43
|
+
* `--no-ext-diff` / `--no-textconv` are per-command diff options, not global
|
|
44
|
+
* ones — `status` rejects them — so the diff-driver keys are neutralized as
|
|
45
|
+
* config overrides instead (see neutralizingOverrides).
|
|
46
|
+
*/
|
|
47
|
+
/**
|
|
48
|
+
* A hooks directory that cannot hold hooks. `NUL` is a device only on Windows;
|
|
49
|
+
* elsewhere it is an ordinary relative folder a repository can ship hooks in.
|
|
50
|
+
* Commands on macOS/Linux run git there or in the Linux container, so both use
|
|
51
|
+
* /dev/null.
|
|
52
|
+
*/
|
|
53
|
+
export const GIT_NO_HOOKS_PATH = process.platform === "win32" ? "NUL" : "/dev/null";
|
|
54
|
+
export const HARDENED_GIT_OPTIONS = [
|
|
55
|
+
"--no-pager",
|
|
56
|
+
"-c",
|
|
57
|
+
"core.fsmonitor=false",
|
|
58
|
+
"-c",
|
|
59
|
+
`core.hooksPath=${GIT_NO_HOOKS_PATH}`,
|
|
60
|
+
"-c",
|
|
61
|
+
"diff.external="
|
|
62
|
+
];
|
|
63
|
+
/** Git never prompts, and never takes an optional lock on the user's index. */
|
|
64
|
+
export function hardenedGitEnv(base = process.env) {
|
|
65
|
+
return { ...base, GIT_OPTIONAL_LOCKS: "0", GIT_TERMINAL_PROMPT: "0" };
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Run one Git command.
|
|
69
|
+
*
|
|
70
|
+
* `execFileSync` with an argument array, never a composed shell string: the
|
|
71
|
+
* working directory is attacker-adjacent input (a folder can be named anything,
|
|
72
|
+
* including `& del /q`), and it reaches Git through the `cwd` OPTION rather
|
|
73
|
+
* than through the command text, so there is no shell to quote for. This is
|
|
74
|
+
* also why there is no `shell: true` here and must never be.
|
|
75
|
+
*/
|
|
76
|
+
function git(args, cwd, timeoutMs = GIT_TIMEOUT_MS, extra = []) {
|
|
77
|
+
try {
|
|
78
|
+
const out = execFileSync("git", [...HARDENED_GIT_OPTIONS, ...extra, ...args], {
|
|
79
|
+
cwd,
|
|
80
|
+
timeout: timeoutMs,
|
|
81
|
+
windowsHide: true,
|
|
82
|
+
encoding: "utf8",
|
|
83
|
+
env: hardenedGitEnv(),
|
|
84
|
+
// stdin closed so a Git that decides to prompt (credentials, a pager)
|
|
85
|
+
// fails immediately instead of hanging the shell before it opens.
|
|
86
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
87
|
+
maxBuffer: 8 * 1024 * 1024
|
|
88
|
+
});
|
|
89
|
+
return { ok: true, out: String(out).trim() };
|
|
90
|
+
}
|
|
91
|
+
catch (err) {
|
|
92
|
+
const e = err;
|
|
93
|
+
return { ok: false, out: "", code: e.status, reason: gitFailureReason(e) };
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
/** Config keys whose VALUE is a program Git may run while reading the tree. */
|
|
97
|
+
const PROGRAM_KEYS = [
|
|
98
|
+
/^filter\..+\.(clean|smudge|process)$/i,
|
|
99
|
+
/^diff\..+\.(textconv|command)$/i,
|
|
100
|
+
/^merge\..+\.driver$/i
|
|
101
|
+
];
|
|
102
|
+
/**
|
|
103
|
+
* `-c key=` for every configured program-valued key, so it is empty for this
|
|
104
|
+
* call. Filter and diff-driver NAMES are chosen by the repository, which is why
|
|
105
|
+
* they cannot be listed in advance. Listing config runs nothing.
|
|
106
|
+
*
|
|
107
|
+
* Returns null when the config cannot be read: the caller then skips the calls
|
|
108
|
+
* that would need these overrides instead of running them unprotected.
|
|
109
|
+
*/
|
|
110
|
+
export function neutralizingOverrides(root, run = git) {
|
|
111
|
+
const res = run(["config", "--null", "--list"], root);
|
|
112
|
+
if (!res.ok)
|
|
113
|
+
return null;
|
|
114
|
+
const overrides = [];
|
|
115
|
+
const seen = new Set();
|
|
116
|
+
for (const entry of res.out.split("\0")) {
|
|
117
|
+
const key = entry.split(/\r?\n/, 1)[0];
|
|
118
|
+
if (!key || seen.has(key) || !PROGRAM_KEYS.some((re) => re.test(key)))
|
|
119
|
+
continue;
|
|
120
|
+
seen.add(key);
|
|
121
|
+
overrides.push("-c", `${key}=`);
|
|
122
|
+
}
|
|
123
|
+
return overrides;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* The folder trust is decided for, found WITHOUT running Git.
|
|
127
|
+
*
|
|
128
|
+
* Trust has to be settled before any Git process starts in a folder, so this
|
|
129
|
+
* walks up looking for a `.git` entry (a directory, or a file for worktrees and
|
|
130
|
+
* submodules) instead of asking `git rev-parse`.
|
|
131
|
+
*/
|
|
132
|
+
export function discoverRootWithoutGit(cwd = getCurrentDirectory()) {
|
|
133
|
+
let dir = path.resolve(cwd);
|
|
134
|
+
for (;;) {
|
|
135
|
+
try {
|
|
136
|
+
if (fs.existsSync(path.join(dir, ".git")))
|
|
137
|
+
return dir;
|
|
138
|
+
}
|
|
139
|
+
catch {
|
|
140
|
+
/* unreadable level — keep walking */
|
|
141
|
+
}
|
|
142
|
+
const parent = path.dirname(dir);
|
|
143
|
+
if (parent === dir)
|
|
144
|
+
return path.resolve(cwd);
|
|
145
|
+
dir = parent;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
function gitFailureReason(e) {
|
|
149
|
+
if (e.code === "ENOENT")
|
|
150
|
+
return "git-not-installed";
|
|
151
|
+
if (e.code === "ETIMEDOUT")
|
|
152
|
+
return "timed-out";
|
|
153
|
+
if (e.code === "EACCES" || e.code === "EPERM")
|
|
154
|
+
return "permission-denied";
|
|
155
|
+
const stderr = e.stderr ? String(e.stderr).trim() : "";
|
|
156
|
+
return stderr || e.message || "unknown error";
|
|
157
|
+
}
|
|
158
|
+
/** Only the first line of a Git error is worth showing a person. */
|
|
159
|
+
function firstLine(text) {
|
|
160
|
+
return (text.split(/\r?\n/).find((l) => l.trim()) ?? text).replace(/^fatal:\s*/i, "").trim();
|
|
161
|
+
}
|
|
162
|
+
// ── Repository ───────────────────────────────────────────────────────────────
|
|
163
|
+
/**
|
|
164
|
+
* Read the repository around `cwd`.
|
|
165
|
+
*
|
|
166
|
+
* Spawn count is the whole design here. On Windows every `git` invocation costs
|
|
167
|
+
* roughly 75ms of process startup no matter how trivial the command, so the
|
|
168
|
+
* difference between five calls and two is the difference between a startup you
|
|
169
|
+
* notice and one you do not. `status --porcelain=v2 --branch` is the lever: it
|
|
170
|
+
* costs the same as a plain status but also carries the branch, the HEAD oid,
|
|
171
|
+
* whether we are detached, and whether any commit exists — four things that
|
|
172
|
+
* would otherwise be two extra spawns.
|
|
173
|
+
*
|
|
174
|
+
* The remote is skipped unless asked for, because nothing on the startup path
|
|
175
|
+
* displays one.
|
|
176
|
+
*/
|
|
177
|
+
function readRepository(cwd, opts = {}) {
|
|
178
|
+
// --show-toplevel walks up for us, so this works identically from the root
|
|
179
|
+
// and from any nested directory.
|
|
180
|
+
const top = git(["rev-parse", "--show-toplevel"], cwd);
|
|
181
|
+
if (!top.ok) {
|
|
182
|
+
if (top.reason === "git-not-installed") {
|
|
183
|
+
return { state: "none", gitAvailable: false };
|
|
184
|
+
}
|
|
185
|
+
const detail = top.reason ?? "";
|
|
186
|
+
// "not a git repository" is the ordinary answer for an ordinary folder, not
|
|
187
|
+
// a malfunction. Anything else that fails here — dubious ownership on a
|
|
188
|
+
// shared drive, an unreadable .git, a timeout — is a repository we could
|
|
189
|
+
// not read, and saying "not detected" would misdescribe it.
|
|
190
|
+
if (/not a git repository/i.test(detail)) {
|
|
191
|
+
return { state: "none", gitAvailable: true };
|
|
192
|
+
}
|
|
193
|
+
return {
|
|
194
|
+
state: "unavailable",
|
|
195
|
+
gitAvailable: true,
|
|
196
|
+
error: detail ? firstLine(detail) : "Git could not read this directory."
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
// Git prints POSIX separators even on Windows; resolve() puts it back into
|
|
200
|
+
// platform form so it matches what the user sees everywhere else.
|
|
201
|
+
const root = path.resolve(top.out);
|
|
202
|
+
const info = {
|
|
203
|
+
state: "detected",
|
|
204
|
+
gitAvailable: true,
|
|
205
|
+
root,
|
|
206
|
+
vcs: "git"
|
|
207
|
+
};
|
|
208
|
+
readBranchAndStatus(info, root);
|
|
209
|
+
if (opts.includeRemote)
|
|
210
|
+
readRemote(info, root);
|
|
211
|
+
return info;
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Branch, HEAD state and working-tree counts — one spawn on the happy path.
|
|
215
|
+
*
|
|
216
|
+
* Falls back to two cheap metadata calls when the combined command fails, which
|
|
217
|
+
* happens on a Git older than 2.11 (no porcelain v2) and, more importantly, when
|
|
218
|
+
* the status walk exceeds its budget on a very large repository. Losing the
|
|
219
|
+
* counts there is acceptable; losing the branch as well would not be, and the
|
|
220
|
+
* fallback calls never touch the working tree.
|
|
221
|
+
*/
|
|
222
|
+
function readBranchAndStatus(info, root) {
|
|
223
|
+
// status is the call that reads file content, so it is the one a planted
|
|
224
|
+
// filter or fsmonitor could hijack. Without the overrides it does not run.
|
|
225
|
+
// Submodules are skipped: each has its own config, with filter names these
|
|
226
|
+
// overrides never saw.
|
|
227
|
+
const overrides = neutralizingOverrides(root);
|
|
228
|
+
const res = overrides
|
|
229
|
+
? git(["status", "--porcelain=v2", "--branch", "--ignore-submodules=all"], root, STATUS_TIMEOUT_MS, overrides)
|
|
230
|
+
: { ok: false, out: "" };
|
|
231
|
+
if (res.ok) {
|
|
232
|
+
let modified = 0;
|
|
233
|
+
let untracked = 0;
|
|
234
|
+
let oid;
|
|
235
|
+
let head;
|
|
236
|
+
for (const raw of res.out.split(/\r?\n/)) {
|
|
237
|
+
if (!raw)
|
|
238
|
+
continue;
|
|
239
|
+
const header = raw.match(/^# branch\.(oid|head) (.+)$/);
|
|
240
|
+
if (header) {
|
|
241
|
+
if (header[1] === "oid")
|
|
242
|
+
oid = header[2].trim();
|
|
243
|
+
else
|
|
244
|
+
head = header[2].trim();
|
|
245
|
+
continue;
|
|
246
|
+
}
|
|
247
|
+
if (raw.startsWith("#"))
|
|
248
|
+
continue;
|
|
249
|
+
// v2 entry kinds: 1 = changed, 2 = renamed/copied, u = unmerged,
|
|
250
|
+
// ? = untracked, ! = ignored (not emitted at this verbosity).
|
|
251
|
+
if (raw.startsWith("? "))
|
|
252
|
+
untracked += 1;
|
|
253
|
+
else if (raw.startsWith("1 ") || raw.startsWith("2 ") || raw.startsWith("u "))
|
|
254
|
+
modified += 1;
|
|
255
|
+
}
|
|
256
|
+
// A repository with no commits reports the literal "(initial)".
|
|
257
|
+
info.hasCommits = oid !== undefined && oid !== "(initial)";
|
|
258
|
+
if (head && head !== "(detached)") {
|
|
259
|
+
info.branch = head;
|
|
260
|
+
}
|
|
261
|
+
else if (info.hasCommits && oid) {
|
|
262
|
+
info.detached = true;
|
|
263
|
+
info.head = oid.slice(0, 7);
|
|
264
|
+
}
|
|
265
|
+
info.status = { dirty: modified + untracked > 0, modified, untracked };
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
info.status = null;
|
|
269
|
+
const symbolic = git(["symbolic-ref", "--quiet", "--short", "HEAD"], root);
|
|
270
|
+
const head = git(["rev-parse", "--short", "HEAD"], root);
|
|
271
|
+
info.hasCommits = head.ok;
|
|
272
|
+
if (symbolic.ok && symbolic.out)
|
|
273
|
+
info.branch = symbolic.out;
|
|
274
|
+
else if (head.ok) {
|
|
275
|
+
info.detached = true;
|
|
276
|
+
info.head = head.out;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
function readRemote(info, root) {
|
|
280
|
+
info.remoteChecked = true;
|
|
281
|
+
const origin = git(["remote", "get-url", "origin"], root);
|
|
282
|
+
if (origin.ok && origin.out) {
|
|
283
|
+
info.remote = "origin";
|
|
284
|
+
info.remoteUrl = origin.out;
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
// A fork or a mirror may have no `origin` but still have a remote worth
|
|
288
|
+
// naming, so look for any before reporting none.
|
|
289
|
+
const list = git(["remote"], root);
|
|
290
|
+
const first = list.ok
|
|
291
|
+
? list.out.split(/\r?\n/).map((l) => l.trim()).filter(Boolean)[0]
|
|
292
|
+
: undefined;
|
|
293
|
+
if (!first)
|
|
294
|
+
return;
|
|
295
|
+
info.remote = first;
|
|
296
|
+
const url = git(["remote", "get-url", first], root);
|
|
297
|
+
if (url.ok && url.out)
|
|
298
|
+
info.remoteUrl = url.out;
|
|
299
|
+
}
|
|
300
|
+
/** Read and parse a small JSON file. Never throws; returns null on anything odd. */
|
|
301
|
+
function readManifest(file) {
|
|
302
|
+
try {
|
|
303
|
+
const stat = fs.statSync(file);
|
|
304
|
+
if (!stat.isFile() || stat.size > MAX_MANIFEST_BYTES)
|
|
305
|
+
return null;
|
|
306
|
+
return JSON.parse(fs.readFileSync(file, "utf8"));
|
|
307
|
+
}
|
|
308
|
+
catch {
|
|
309
|
+
return null;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
function exists(root, name) {
|
|
313
|
+
try {
|
|
314
|
+
return fs.existsSync(path.join(root, name));
|
|
315
|
+
}
|
|
316
|
+
catch {
|
|
317
|
+
return false;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* Root-level markers only — this reads a handful of filenames and at most one
|
|
322
|
+
* package.json. It deliberately does not look inside the tree: the point is to
|
|
323
|
+
* name the stack in the banner, not to analyse the project.
|
|
324
|
+
*
|
|
325
|
+
* Detection stays conservative. A dependency is evidence; a package NAME is
|
|
326
|
+
* not, so nothing here branches on what the project calls itself.
|
|
327
|
+
*/
|
|
328
|
+
function detectType(root) {
|
|
329
|
+
const pkg = readManifest(path.join(root, "package.json"));
|
|
330
|
+
if (pkg) {
|
|
331
|
+
const deps = {
|
|
332
|
+
...(pkg.dependencies ?? {}),
|
|
333
|
+
...(pkg.devDependencies ?? {})
|
|
334
|
+
};
|
|
335
|
+
// Order matters: a Next.js project also depends on React, and a SvelteKit
|
|
336
|
+
// project also depends on Svelte. Most specific wins.
|
|
337
|
+
if (deps["next"])
|
|
338
|
+
return { type: "nextjs", label: "Next.js" };
|
|
339
|
+
if (deps["nuxt"])
|
|
340
|
+
return { type: "nuxt", label: "Nuxt" };
|
|
341
|
+
if (deps["@angular/core"])
|
|
342
|
+
return { type: "angular", label: "Angular" };
|
|
343
|
+
if (deps["svelte"])
|
|
344
|
+
return { type: "svelte", label: "Svelte" };
|
|
345
|
+
if (deps["vue"])
|
|
346
|
+
return { type: "vue", label: "Vue" };
|
|
347
|
+
if (deps["react"])
|
|
348
|
+
return { type: "react", label: "React" };
|
|
349
|
+
return { type: "node", label: "Node.js" };
|
|
350
|
+
}
|
|
351
|
+
if (exists(root, "pyproject.toml") || exists(root, "requirements.txt") || exists(root, "setup.py")) {
|
|
352
|
+
return { type: "python", label: "Python" };
|
|
353
|
+
}
|
|
354
|
+
if (exists(root, "Cargo.toml"))
|
|
355
|
+
return { type: "rust", label: "Rust" };
|
|
356
|
+
if (exists(root, "go.mod"))
|
|
357
|
+
return { type: "go", label: "Go" };
|
|
358
|
+
if (exists(root, "pom.xml"))
|
|
359
|
+
return { type: "maven", label: "Java (Maven)" };
|
|
360
|
+
if (exists(root, "build.gradle") || exists(root, "build.gradle.kts")) {
|
|
361
|
+
return { type: "gradle", label: "Java (Gradle)" };
|
|
362
|
+
}
|
|
363
|
+
if (exists(root, "composer.json"))
|
|
364
|
+
return { type: "php", label: "PHP" };
|
|
365
|
+
if (exists(root, "Gemfile"))
|
|
366
|
+
return { type: "ruby", label: "Ruby" };
|
|
367
|
+
return { type: "unknown", label: "Unknown" };
|
|
368
|
+
}
|
|
369
|
+
/**
|
|
370
|
+
* A readable name for the folder you are in.
|
|
371
|
+
*
|
|
372
|
+
* The repository folder comes first because that is what people call the
|
|
373
|
+
* project out loud — and a package name can be a scoped publishing artifact
|
|
374
|
+
* (`@carrieros/cli`) that nobody would recognize as "the folder I'm in".
|
|
375
|
+
*/
|
|
376
|
+
function projectName(cwd, root, manifestName) {
|
|
377
|
+
const fromRoot = root ? path.basename(root) : "";
|
|
378
|
+
if (fromRoot)
|
|
379
|
+
return fromRoot;
|
|
380
|
+
if (typeof manifestName === "string" && manifestName.trim())
|
|
381
|
+
return manifestName.trim();
|
|
382
|
+
// basename() of a drive root ("D:\") is empty — fall back to the path itself
|
|
383
|
+
// so the banner never renders a blank workspace.
|
|
384
|
+
return path.basename(cwd) || cwd;
|
|
385
|
+
}
|
|
386
|
+
function readProject(projectRoot, cwd, repoRoot) {
|
|
387
|
+
const manifest = readManifest(path.join(projectRoot, "package.json"));
|
|
388
|
+
const { type, label } = detectType(projectRoot);
|
|
389
|
+
return {
|
|
390
|
+
name: projectName(cwd, repoRoot, manifest?.name),
|
|
391
|
+
type,
|
|
392
|
+
label,
|
|
393
|
+
root: projectRoot
|
|
394
|
+
};
|
|
395
|
+
}
|
|
396
|
+
let cached = null;
|
|
397
|
+
/**
|
|
398
|
+
* The working directory, absolute and platform-normalized.
|
|
399
|
+
*
|
|
400
|
+
* `process.cwd()` throws if the directory has been deleted out from under a
|
|
401
|
+
* running process — rare, but it would take the shell down at startup, so it is
|
|
402
|
+
* guarded and falls back to the home directory.
|
|
403
|
+
*/
|
|
404
|
+
export function getCurrentDirectory() {
|
|
405
|
+
try {
|
|
406
|
+
return path.resolve(process.cwd());
|
|
407
|
+
}
|
|
408
|
+
catch {
|
|
409
|
+
return path.resolve(process.env.USERPROFILE || process.env.HOME || ".");
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
function build(opts) {
|
|
413
|
+
const cwd = getCurrentDirectory();
|
|
414
|
+
let repository;
|
|
415
|
+
try {
|
|
416
|
+
repository = readRepository(cwd, opts);
|
|
417
|
+
}
|
|
418
|
+
catch {
|
|
419
|
+
// readRepository already handles every failure it expects; this is the
|
|
420
|
+
// backstop that guarantees the shell opens no matter what Git does.
|
|
421
|
+
repository = { state: "unavailable", gitAvailable: true, error: "Detection failed." };
|
|
422
|
+
}
|
|
423
|
+
const projectRoot = repository.root ?? cwd;
|
|
424
|
+
let project;
|
|
425
|
+
try {
|
|
426
|
+
project = readProject(projectRoot, cwd, repository.root);
|
|
427
|
+
}
|
|
428
|
+
catch {
|
|
429
|
+
project = {
|
|
430
|
+
name: path.basename(cwd) || cwd,
|
|
431
|
+
type: "unknown",
|
|
432
|
+
label: "Unknown",
|
|
433
|
+
root: projectRoot
|
|
434
|
+
};
|
|
435
|
+
}
|
|
436
|
+
return { cwd, repository, project };
|
|
437
|
+
}
|
|
438
|
+
/**
|
|
439
|
+
* The cached descriptor, computing it once if this is the first ask.
|
|
440
|
+
*
|
|
441
|
+
* Callers on a hot path must NOT use this — see peekWorkspace.
|
|
442
|
+
*/
|
|
443
|
+
export function getWorkspace(opts = {}) {
|
|
444
|
+
// A cached descriptor built WITHOUT the remote cannot answer a request that
|
|
445
|
+
// wants one — its absent `remote` means "not looked up", not "none".
|
|
446
|
+
if (cached && (!opts.includeRemote || cached.repository.remoteChecked))
|
|
447
|
+
return cached;
|
|
448
|
+
return refreshWorkspace(opts);
|
|
449
|
+
}
|
|
450
|
+
/**
|
|
451
|
+
* Re-read everything from disk.
|
|
452
|
+
*
|
|
453
|
+
* Called at startup and by `/repo`. There is no watcher and no polling: Git
|
|
454
|
+
* state changes when the user does something, and the moments that matter are
|
|
455
|
+
* the ones where they have asked to see it.
|
|
456
|
+
*/
|
|
457
|
+
export function refreshWorkspace(opts = {}) {
|
|
458
|
+
cached = build(opts);
|
|
459
|
+
return cached;
|
|
460
|
+
}
|
|
461
|
+
/**
|
|
462
|
+
* Describe ONE repository root, without touching the cached descriptor.
|
|
463
|
+
*
|
|
464
|
+
* The cache answers "where am I?", which is a single answer per process. A
|
|
465
|
+
* workspace holds several repositories, and `/repos` needs each one described
|
|
466
|
+
* on its own terms — so this reads a specific root and returns what it found,
|
|
467
|
+
* leaving the ambient descriptor alone.
|
|
468
|
+
*/
|
|
469
|
+
export function describeRepository(root) {
|
|
470
|
+
let repository;
|
|
471
|
+
try {
|
|
472
|
+
repository = readRepository(root);
|
|
473
|
+
}
|
|
474
|
+
catch {
|
|
475
|
+
repository = { state: "unavailable", gitAvailable: true, error: "Detection failed." };
|
|
476
|
+
}
|
|
477
|
+
let project;
|
|
478
|
+
try {
|
|
479
|
+
project = readProject(repository.root ?? root, root, repository.root);
|
|
480
|
+
}
|
|
481
|
+
catch {
|
|
482
|
+
project = { name: path.basename(root) || root, type: "unknown", label: "Unknown", root };
|
|
483
|
+
}
|
|
484
|
+
return {
|
|
485
|
+
branch: branchLabel(repository),
|
|
486
|
+
status: repository.status ?? null,
|
|
487
|
+
detached: repository.detached === true,
|
|
488
|
+
hasCommits: repository.hasCommits ?? null,
|
|
489
|
+
projectType: project.type,
|
|
490
|
+
projectLabel: project.label,
|
|
491
|
+
...(repository.state === "unavailable" ? { error: repository.error } : {})
|
|
492
|
+
};
|
|
493
|
+
}
|
|
494
|
+
/**
|
|
495
|
+
* The descriptor if one has already been built, otherwise null. Never runs Git.
|
|
496
|
+
*
|
|
497
|
+
* This exists for the footer, which is rebuilt on every keystroke. Calling
|
|
498
|
+
* getWorkspace() there would be fine today because of the cache, but it would
|
|
499
|
+
* be one refactor away from spawning a process per character typed.
|
|
500
|
+
*/
|
|
501
|
+
export function peekWorkspace() {
|
|
502
|
+
return cached;
|
|
503
|
+
}
|
|
504
|
+
export function getRepositoryRoot() {
|
|
505
|
+
return getWorkspace().repository.root ?? null;
|
|
506
|
+
}
|
|
507
|
+
export function getRepositoryInfo() {
|
|
508
|
+
return getWorkspace().repository;
|
|
509
|
+
}
|
|
510
|
+
export function getProjectInfo() {
|
|
511
|
+
return getWorkspace().project;
|
|
512
|
+
}
|
|
513
|
+
/**
|
|
514
|
+
* The traversal ignore policy, re-exported so a future local walk reaches it
|
|
515
|
+
* through the workspace rather than importing a second copy from somewhere
|
|
516
|
+
* else. Nothing traverses yet — see services/ignore-rules.ts.
|
|
517
|
+
*/
|
|
518
|
+
export { ignorePolicy } from "./ignore-rules.js";
|
|
519
|
+
// ── Presentation helpers ─────────────────────────────────────────────────────
|
|
520
|
+
// Shared by the banner, the footer and /repo so the same state is never
|
|
521
|
+
// described two different ways in two places.
|
|
522
|
+
/** "main", "HEAD (detached at a1b2c3d)", "no commits yet", or null. */
|
|
523
|
+
export function branchLabel(repo) {
|
|
524
|
+
if (repo.state !== "detected")
|
|
525
|
+
return null;
|
|
526
|
+
if (repo.detached)
|
|
527
|
+
return `HEAD (detached at ${repo.head ?? "unknown"})`;
|
|
528
|
+
if (repo.branch && repo.hasCommits === false)
|
|
529
|
+
return `${repo.branch} (no commits yet)`;
|
|
530
|
+
if (repo.branch)
|
|
531
|
+
return repo.branch;
|
|
532
|
+
return repo.hasCommits === false ? "no commits yet" : null;
|
|
533
|
+
}
|
|
534
|
+
/** "3 modified · 1 untracked", "Clean", or null when the status is unknown. */
|
|
535
|
+
export function statusLabel(repo) {
|
|
536
|
+
const s = repo.status;
|
|
537
|
+
if (s === null || s === undefined)
|
|
538
|
+
return null;
|
|
539
|
+
if (!s.dirty)
|
|
540
|
+
return "Clean";
|
|
541
|
+
const parts = [];
|
|
542
|
+
if (s.modified)
|
|
543
|
+
parts.push(`${s.modified} modified`);
|
|
544
|
+
if (s.untracked)
|
|
545
|
+
parts.push(`${s.untracked} untracked`);
|
|
546
|
+
return parts.join(" · ");
|
|
547
|
+
}
|
|
548
|
+
/**
|
|
549
|
+
* `github.com/carrieros-ai/carrieros-cli` from either an SSH or an HTTPS remote.
|
|
550
|
+
* Returns the raw value unchanged when it matches neither, rather than guessing.
|
|
551
|
+
*/
|
|
552
|
+
export function shortenRemoteUrl(url) {
|
|
553
|
+
const ssh = url.match(/^[^@]+@([^:]+):(.+?)(?:\.git)?$/);
|
|
554
|
+
if (ssh)
|
|
555
|
+
return `${ssh[1]}/${ssh[2]}`;
|
|
556
|
+
const https = url.match(/^https?:\/\/(?:[^@/]+@)?([^/]+)\/(.+?)(?:\.git)?$/);
|
|
557
|
+
if (https)
|
|
558
|
+
return `${https[1]}/${https[2]}`;
|
|
559
|
+
return url.replace(/\.git$/, "");
|
|
560
|
+
}
|
|
561
|
+
/**
|
|
562
|
+
* Shorten a path to fit, keeping the drive and the tail — the end of a path is
|
|
563
|
+
* what identifies the project, and the middle is the part nobody reads.
|
|
564
|
+
*/
|
|
565
|
+
export function fitPath(p, max) {
|
|
566
|
+
const chars = [...p];
|
|
567
|
+
if (max < 8 || chars.length <= max)
|
|
568
|
+
return p;
|
|
569
|
+
const head = chars.slice(0, 3).join("");
|
|
570
|
+
const tail = chars.slice(-(max - head.length - 1)).join("");
|
|
571
|
+
return `${head}…${tail}`;
|
|
572
|
+
}
|
|
573
|
+
/** Where the user is standing relative to the repository root, or null at it. */
|
|
574
|
+
export function subPath(ws) {
|
|
575
|
+
const root = ws.repository.root;
|
|
576
|
+
if (!root)
|
|
577
|
+
return null;
|
|
578
|
+
const rel = path.relative(root, ws.cwd);
|
|
579
|
+
return rel && rel !== "." ? rel : null;
|
|
580
|
+
}
|