@andorra-labs/code 0.1.16 → 0.1.18
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/dist/src/acp-agent.d.ts +6 -0
- package/dist/src/acp-agent.js +15 -4
- package/dist/src/agent-execution.js +108 -19
- package/dist/src/agent-mcp-catalog.js +1 -0
- package/dist/src/agent-mcp.d.ts +3 -0
- package/dist/src/agent-mcp.js +33 -1
- package/dist/src/andorra/entry.d.ts +10 -0
- package/dist/src/andorra/entry.js +18 -4
- package/dist/src/andorra/errors.js +3 -0
- package/dist/src/andorra/hosted.d.ts +2 -1
- package/dist/src/andorra/hosted.js +2 -1
- package/dist/src/andorra/notices.d.ts +5 -0
- package/dist/src/andorra/notices.js +7 -0
- package/dist/src/andorra/state.d.ts +3 -2
- package/dist/src/andorra/state.js +4 -4
- package/dist/src/andorra/terminal.js +7 -2
- package/dist/src/anthropic-credentials.js +2 -0
- package/dist/src/app-server/error.d.ts +5 -0
- package/dist/src/app-server/error.js +7 -0
- package/dist/src/app-server/protocol.d.ts +30 -44
- package/dist/src/app-server/protocol.js +5 -1
- package/dist/src/app-server/workspace.d.ts +108 -0
- package/dist/src/app-server/workspace.js +608 -11
- package/dist/src/app-server.d.ts +390 -33
- package/dist/src/app-server.js +3033 -565
- package/dist/src/attachments.d.ts +10 -0
- package/dist/src/attachments.js +102 -7
- package/dist/src/automation-tool.js +2 -1
- package/dist/src/bridge/cloud.d.ts +27 -0
- package/dist/src/bridge/cloud.js +187 -0
- package/dist/src/bridge/command.d.ts +30 -0
- package/dist/src/bridge/command.js +359 -0
- package/dist/src/bridge/control.d.ts +70 -0
- package/dist/src/bridge/control.js +238 -0
- package/dist/src/bridge/engines.d.ts +90 -0
- package/dist/src/bridge/engines.js +601 -0
- package/dist/src/bridge/github.d.ts +108 -0
- package/dist/src/bridge/github.js +378 -0
- package/dist/src/bridge/host-ops.d.ts +9 -0
- package/dist/src/bridge/host-ops.js +139 -0
- package/dist/src/bridge/host.d.ts +47 -0
- package/dist/src/bridge/host.js +543 -0
- package/dist/src/bridge/link.d.ts +38 -0
- package/dist/src/bridge/link.js +508 -0
- package/dist/src/bridge/login-link.d.ts +29 -0
- package/dist/src/bridge/login-link.js +54 -0
- package/dist/src/bridge/protocol.d.ts +136 -0
- package/dist/src/bridge/protocol.js +11 -0
- package/dist/src/bridge/store.d.ts +12 -0
- package/dist/src/bridge/store.js +61 -0
- package/dist/src/bridge/wording.d.ts +6 -0
- package/dist/src/bridge/wording.js +10 -0
- package/dist/src/browser-server.js +7 -1
- package/dist/src/browser-ui/tokens.css +1 -1
- package/dist/src/bug-report-attachments.d.ts +48 -0
- package/dist/src/bug-report-attachments.js +327 -0
- package/dist/src/bug-report.d.ts +23 -3
- package/dist/src/bug-report.js +72 -9
- package/dist/src/change-authors.d.ts +41 -0
- package/dist/src/change-authors.js +146 -0
- package/dist/src/cli.d.ts +1 -3
- package/dist/src/cli.js +191 -35
- package/dist/src/codex-provider.js +1 -1
- package/dist/src/coding-tools.js +24 -17
- package/dist/src/command-screens.d.ts +6 -0
- package/dist/src/command-screens.js +14 -11
- package/dist/src/commands.d.ts +15 -3
- package/dist/src/commands.js +2 -1
- package/dist/src/compaction-phrases.js +1 -1
- package/dist/src/completion.js +89 -8
- package/dist/src/config.d.ts +71 -3
- package/dist/src/config.js +205 -78
- package/dist/src/context-budget.js +15 -2
- package/dist/src/deadline.js +13 -1
- package/dist/src/decision/DecisionEngine.d.ts +2 -0
- package/dist/src/decision/DecisionManager.d.ts +11 -0
- package/dist/src/decision/DecisionManager.js +36 -0
- package/dist/src/decision/applyTaskKind.d.ts +8 -0
- package/dist/src/decision/applyTaskKind.js +53 -0
- package/dist/src/decision/checks.js +1 -1
- package/dist/src/decision/hostedPolicy.js +2 -1
- package/dist/src/decision/providers/AndorraDecisionProvider.d.ts +2 -0
- package/dist/src/decision/providers/AndorraDecisionProvider.js +12 -0
- package/dist/src/decision/requestPhrases.js +0 -1
- package/dist/src/decision/taskKind.d.ts +52 -0
- package/dist/src/decision/taskKind.js +76 -0
- package/dist/src/decision/types.d.ts +1 -1
- package/dist/src/default-graph.js +1 -1
- package/dist/src/efficiency.d.ts +5 -0
- package/dist/src/efficiency.js +5 -1
- package/dist/src/effort-routing.d.ts +27 -0
- package/dist/src/effort-routing.js +34 -0
- package/dist/src/evidence.d.ts +110 -0
- package/dist/src/evidence.js +195 -0
- package/dist/src/file-diff.d.ts +4 -0
- package/dist/src/file-diff.js +2 -1
- package/dist/src/github-app/agent-writes.d.ts +102 -0
- package/dist/src/github-app/agent-writes.js +320 -0
- package/dist/src/github-app/client.d.ts +368 -0
- package/dist/src/github-app/client.js +830 -0
- package/dist/src/github-app/event-source.d.ts +208 -0
- package/dist/src/github-app/event-source.js +684 -0
- package/dist/src/github-app/metrics.d.ts +32 -0
- package/dist/src/github-app/metrics.js +49 -0
- package/dist/src/github-app/review-loop.d.ts +274 -0
- package/dist/src/github-app/review-loop.js +1177 -0
- package/dist/src/github-app/review-node.d.ts +81 -0
- package/dist/src/github-app/review-node.js +205 -0
- package/dist/src/github-app/review-post.d.ts +73 -0
- package/dist/src/github-app/review-post.js +139 -0
- package/dist/src/github-app/review-text.d.ts +107 -0
- package/dist/src/github-app/review-text.js +1133 -0
- package/dist/src/github-app/review-threads.d.ts +47 -0
- package/dist/src/github-app/review-threads.js +107 -0
- package/dist/src/github-app/reviewer.d.ts +121 -0
- package/dist/src/github-app/reviewer.js +598 -0
- package/dist/src/github-app/store.d.ts +59 -0
- package/dist/src/github-app/store.js +148 -0
- package/dist/src/github-app/triggers.d.ts +141 -0
- package/dist/src/github-app/triggers.js +441 -0
- package/dist/src/github-names.d.ts +15 -0
- package/dist/src/github-names.js +15 -0
- package/dist/src/glob-search.d.ts +61 -0
- package/dist/src/glob-search.js +180 -0
- package/dist/src/graph-health.js +6 -1
- package/dist/src/graph-manager-chat.js +1 -1
- package/dist/src/graph-preview.d.ts +48 -0
- package/dist/src/graph-preview.js +56 -0
- package/dist/src/graph-provider-portability.js +2 -1
- package/dist/src/graph-runner.js +65 -4
- package/dist/src/graph-server.js +7 -5
- package/dist/src/graph-sync.js +3 -3
- package/dist/src/graph-ui/app.js +121 -7
- package/dist/src/graph-ui/icons.js +2 -1
- package/dist/src/graph-ui/studio.js +15 -1
- package/dist/src/graph-ui/tokens.css +1 -1
- package/dist/src/graphs.d.ts +40 -1
- package/dist/src/graphs.js +164 -15
- package/dist/src/hardened-runtime.js +32 -21
- package/dist/src/image-input.d.ts +16 -0
- package/dist/src/image-input.js +41 -0
- package/dist/src/membership.d.ts +10 -0
- package/dist/src/membership.js +19 -4
- package/dist/src/model-health-cache.js +1 -1
- package/dist/src/model-metadata.d.ts +6 -0
- package/dist/src/model-metadata.js +35 -15
- package/dist/src/model-override.d.ts +2 -1
- package/dist/src/model-override.js +9 -0
- package/dist/src/model-relay/local.d.ts +116 -0
- package/dist/src/model-relay/local.js +468 -0
- package/dist/src/model-relay/remote.d.ts +129 -0
- package/dist/src/model-relay/remote.js +252 -0
- package/dist/src/model-relay/runs.d.ts +34 -0
- package/dist/src/model-relay/runs.js +146 -0
- package/dist/src/model-relay/wire.d.ts +5 -0
- package/dist/src/model-relay/wire.js +5 -0
- package/dist/src/model-tiers.d.ts +3 -0
- package/dist/src/model-tiers.js +3 -3
- package/dist/src/project-registry.d.ts +21 -0
- package/dist/src/project-registry.js +74 -0
- package/dist/src/provider-engine.d.ts +13 -1
- package/dist/src/provider-engine.js +74 -0
- package/dist/src/provider-import.d.ts +0 -12
- package/dist/src/provider-import.js +7 -195
- package/dist/src/providers.d.ts +28 -2
- package/dist/src/providers.js +59 -12
- package/dist/src/pull-request-logs.d.ts +12 -1
- package/dist/src/pull-request-logs.js +14 -3
- package/dist/src/pull-requests.d.ts +338 -8
- package/dist/src/pull-requests.js +482 -53
- package/dist/src/react-runtime.js +99 -16
- package/dist/src/remote/engine-pool.d.ts +88 -4
- package/dist/src/remote/engine-pool.js +367 -90
- package/dist/src/remote/roots.d.ts +19 -0
- package/dist/src/remote/roots.js +45 -7
- package/dist/src/remote/types.d.ts +0 -44
- package/dist/src/run-control.d.ts +7 -0
- package/dist/src/run-control.js +23 -0
- package/dist/src/run-records.d.ts +7 -2
- package/dist/src/run-records.js +71 -6
- package/dist/src/runtime.d.ts +34 -2
- package/dist/src/runtime.js +18 -2
- package/dist/src/session-changes.d.ts +35 -0
- package/dist/src/session-changes.js +194 -6
- package/dist/src/session-monitor.js +2 -1
- package/dist/src/session-reaper.d.ts +1 -1
- package/dist/src/session-reaper.js +6 -1
- package/dist/src/session-summary.d.ts +2 -1
- package/dist/src/session-summary.js +2 -1
- package/dist/src/sessions.d.ts +35 -1
- package/dist/src/sessions.js +70 -4
- package/dist/src/skills/bundled/bug-report/SKILL.md +51 -7
- package/dist/src/skills/bundled/diagram/SKILL.md +5 -3
- package/dist/src/skills/bundled/graph-manager/SKILL.md +7 -4
- package/dist/src/skills/bundled/provenance.json +3 -3
- package/dist/src/skills/runtime-skills.js +1 -1
- package/dist/src/task-checkout.d.ts +141 -0
- package/dist/src/task-checkout.js +558 -0
- package/dist/src/telemetry/emitter.d.ts +15 -0
- package/dist/src/telemetry/emitter.js +35 -1
- package/dist/src/telemetry/schema.d.ts +25 -2
- package/dist/src/telemetry/schema.js +52 -4
- package/dist/src/telemetry/surface.d.ts +2 -1
- package/dist/src/telemetry/surface.js +4 -1
- package/dist/src/terminal-ui.d.ts +1 -0
- package/dist/src/terminal-ui.js +10 -3
- package/dist/src/test-edit-guard.d.ts +37 -0
- package/dist/src/test-edit-guard.js +216 -3
- package/dist/src/thinking-level-detect.d.ts +11 -0
- package/dist/src/thinking-level-detect.js +47 -0
- package/dist/src/thinking-levels.d.ts +30 -0
- package/dist/src/thinking-levels.js +124 -0
- package/dist/src/timers.d.ts +3 -0
- package/dist/src/tool-catalog.js +1 -1
- package/dist/src/tools.js +59 -9
- package/dist/src/transcript.js +1 -1
- package/dist/src/types.d.ts +34 -0
- package/package.json +6 -1
- package/dist/src/remote/cert.d.ts +0 -11
- package/dist/src/remote/cert.js +0 -87
- package/dist/src/remote/client.d.ts +0 -119
- package/dist/src/remote/client.js +0 -544
- package/dist/src/remote/command.d.ts +0 -4
- package/dist/src/remote/command.js +0 -194
- package/dist/src/remote/errors.d.ts +0 -11
- package/dist/src/remote/errors.js +0 -14
- package/dist/src/remote/network.d.ts +0 -22
- package/dist/src/remote/network.js +0 -102
- package/dist/src/remote/pairing.d.ts +0 -63
- package/dist/src/remote/pairing.js +0 -299
- package/dist/src/remote/pem.d.ts +0 -2
- package/dist/src/remote/pem.js +0 -7
- package/dist/src/remote/serve.d.ts +0 -68
- package/dist/src/remote/serve.js +0 -244
- package/dist/src/remote/server.d.ts +0 -21
- package/dist/src/remote/server.js +0 -406
|
@@ -1,13 +1,18 @@
|
|
|
1
1
|
import { spawn } from 'node:child_process';
|
|
2
|
+
import { createHash } from 'node:crypto';
|
|
2
3
|
import { createReadStream } from 'node:fs';
|
|
3
4
|
import { opendir, open, readFile, readdir, realpath, stat } from 'node:fs/promises';
|
|
4
5
|
import { StringDecoder } from 'node:string_decoder';
|
|
5
|
-
import { dirname, isAbsolute, join, relative, resolve, sep } from 'node:path';
|
|
6
|
+
import { dirname, extname, isAbsolute, join, posix, relative, resolve, sep } from 'node:path';
|
|
6
7
|
import { runRecordPath, runRecordsDirectory, sessionHasOpenRun } from '../run-records.js';
|
|
7
8
|
import { validSavedSession } from '../sessions.js';
|
|
8
9
|
import { listBundles } from '../extensions.js';
|
|
9
10
|
import { builtinSkillEntries } from '../skills/invocable-skills.js';
|
|
10
|
-
const SKIP_NAMES = new Set(['.git', '
|
|
11
|
+
const SKIP_NAMES = new Set(['.git', '.andorracode']);
|
|
12
|
+
// Listed (Git normally ignores it, so it arrives dimmed) but never walked into by a deep listing: its contents load
|
|
13
|
+
// only when it is the folder asked for.
|
|
14
|
+
const LIST_ONLY_NAMES = new Set(['node_modules']);
|
|
15
|
+
const MAX_IGNORED_BYTES = 4 * 1024 * 1024;
|
|
11
16
|
const MAX_TREE_ENTRIES = 5_000;
|
|
12
17
|
const MAX_TREE_DEPTH = 12;
|
|
13
18
|
const MAX_FILE_BYTES = 512 * 1024;
|
|
@@ -53,8 +58,41 @@ export async function workspaceRoot(value) {
|
|
|
53
58
|
throw new Error('The project path is not a folder');
|
|
54
59
|
return root;
|
|
55
60
|
}
|
|
61
|
+
/**
|
|
62
|
+
* The paths under `folder` (relative to `root`, `/`-separated) that Git ignores: files, and folders with a trailing
|
|
63
|
+
* slash that stand for everything inside them. Undefined outside a repository, or when Git fails or runs out of time,
|
|
64
|
+
* so the tree lists without marks rather than not at all.
|
|
65
|
+
*/
|
|
66
|
+
async function ignoredUnder(root, folder) {
|
|
67
|
+
if (!(await gitInScope(root)))
|
|
68
|
+
return undefined;
|
|
69
|
+
try {
|
|
70
|
+
const collected = await collectCapped('git', ['--literal-pathspecs', '-C', root, 'ls-files', '-z', '--others', '--ignored', '--exclude-standard', '--directory', '--', folder || '.'], MAX_IGNORED_BYTES, GIT_TIMEOUT_MS);
|
|
71
|
+
if (!collected.truncated && collected.code !== 0)
|
|
72
|
+
return undefined;
|
|
73
|
+
const records = collected.text.split('\0');
|
|
74
|
+
if (collected.truncated)
|
|
75
|
+
records.pop();
|
|
76
|
+
return records.filter(Boolean).map(record => record.split(sep).join('/'));
|
|
77
|
+
}
|
|
78
|
+
catch {
|
|
79
|
+
return undefined;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
/** Whether Git ignores `path`: it is listed itself, or it is inside a listed folder (`dir/`). */
|
|
83
|
+
function isIgnored(path, ignored) {
|
|
84
|
+
if (ignored.has(path) || ignored.has(`${path}/`))
|
|
85
|
+
return true;
|
|
86
|
+
for (let slash = path.indexOf('/'); slash !== -1; slash = path.indexOf('/', slash + 1)) {
|
|
87
|
+
if (ignored.has(path.slice(0, slash + 1)))
|
|
88
|
+
return true;
|
|
89
|
+
}
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
56
92
|
export async function readTree(root, options = {}) {
|
|
57
93
|
const start = await resolveInside(root, options.path);
|
|
94
|
+
const folder = relative(root, start).split(sep).join('/');
|
|
95
|
+
const ignoring = ignoredUnder(root, folder);
|
|
58
96
|
const deep = options.depth !== undefined;
|
|
59
97
|
const maxDepth = deep ? Math.max(1, Math.min(MAX_TREE_DEPTH, Number.isSafeInteger(options.depth) ? options.depth : MAX_TREE_DEPTH)) : 1;
|
|
60
98
|
const entries = [];
|
|
@@ -82,13 +120,25 @@ export async function readTree(root, options = {}) {
|
|
|
82
120
|
return;
|
|
83
121
|
}
|
|
84
122
|
entries.push(child);
|
|
85
|
-
if (child.type === 'directory' && depth < maxDepth)
|
|
123
|
+
if (child.type === 'directory' && depth < maxDepth && !LIST_ONLY_NAMES.has(child.name))
|
|
86
124
|
await walk(join(directory, child.name), depth + 1);
|
|
87
125
|
}
|
|
88
126
|
};
|
|
89
127
|
await walk(start, 1);
|
|
90
|
-
|
|
128
|
+
const ignored = await ignoring;
|
|
129
|
+
if (ignored?.length) {
|
|
130
|
+
const set = new Set(ignored);
|
|
131
|
+
for (const entry of entries)
|
|
132
|
+
if (isIgnored(entry.path, set))
|
|
133
|
+
entry.ignored = true;
|
|
134
|
+
}
|
|
135
|
+
return { path: folder, entries, truncated };
|
|
91
136
|
}
|
|
137
|
+
/**
|
|
138
|
+
* A project file's text, capped at 512 KB. A whole text file also carries `hash`, the SHA-256 of its bytes, which
|
|
139
|
+
* `workspace.write` checks before it saves; `notUtf8` marks one whose bytes are not UTF-8, so saving the decoded text
|
|
140
|
+
* would change them.
|
|
141
|
+
*/
|
|
92
142
|
export async function readWorkspaceFile(root, name) {
|
|
93
143
|
const path = await resolveInside(root, name);
|
|
94
144
|
const info = await stat(path);
|
|
@@ -101,25 +151,202 @@ export async function readWorkspaceFile(root, name) {
|
|
|
101
151
|
const head = buffer.subarray(0, bytesRead);
|
|
102
152
|
const binary = head.subarray(0, Math.min(head.length, 8000)).includes(0);
|
|
103
153
|
const content = new StringDecoder('utf8').write(head.subarray(0, MAX_FILE_BYTES));
|
|
104
|
-
|
|
154
|
+
// The size from stat can be stale by the time of the read; what was read decides whether it is the whole file.
|
|
155
|
+
const truncated = info.size > MAX_FILE_BYTES || bytesRead > MAX_FILE_BYTES;
|
|
156
|
+
const whole = !binary && !truncated
|
|
157
|
+
? { hash: createHash('sha256').update(head).digest('hex'), ...(Buffer.from(content, 'utf8').equals(head) ? {} : { notUtf8: true }) }
|
|
158
|
+
: {};
|
|
159
|
+
return { content: binary ? '' : content, truncated, binary, size: info.size, ...whole };
|
|
105
160
|
}
|
|
106
161
|
finally {
|
|
107
162
|
await handle.close();
|
|
108
163
|
}
|
|
109
164
|
}
|
|
110
|
-
|
|
165
|
+
/** The image types the Files tab previews (`workspace.image`), by extension. */
|
|
166
|
+
const IMAGE_TYPES = {
|
|
167
|
+
'.png': 'image/png', '.jpg': 'image/jpeg', '.jpeg': 'image/jpeg', '.gif': 'image/gif', '.webp': 'image/webp',
|
|
168
|
+
'.bmp': 'image/bmp', '.ico': 'image/x-icon', '.svg': 'image/svg+xml',
|
|
169
|
+
};
|
|
170
|
+
/** The largest image the Files tab previews; a larger one keeps the binary note. */
|
|
171
|
+
export const MAX_IMAGE_BYTES = 10 * 1024 * 1024;
|
|
172
|
+
export function imageTypeOf(path) {
|
|
173
|
+
return IMAGE_TYPES[extname(path).toLowerCase()];
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* A project image's bytes, base64, for the Files tab's preview (`workspace.image`). `tooLarge` past 10 MB, with no
|
|
177
|
+
* bytes. The type comes from the extension only: the desktop draws it in an <img>, which never runs an SVG's script.
|
|
178
|
+
*/
|
|
179
|
+
export async function readWorkspaceImage(root, name) {
|
|
180
|
+
const path = await resolveInside(root, name);
|
|
181
|
+
const mimeType = imageTypeOf(path);
|
|
182
|
+
if (!mimeType)
|
|
183
|
+
throw new Error('This file is not an image the Files tab can show');
|
|
184
|
+
const info = await stat(path);
|
|
185
|
+
if (!info.isFile())
|
|
186
|
+
throw new Error('Only files can be opened');
|
|
187
|
+
if (info.size > MAX_IMAGE_BYTES)
|
|
188
|
+
return { mimeType, size: info.size, tooLarge: true };
|
|
189
|
+
const bytes = await readFile(path);
|
|
190
|
+
if (bytes.length > MAX_IMAGE_BYTES)
|
|
191
|
+
return { mimeType, size: bytes.length, tooLarge: true };
|
|
192
|
+
return { mimeType, size: bytes.length, dataBase64: bytes.toString('base64') };
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Whether a project path matches a glob as ripgrep's `--glob` does: `*` and `?` stay inside one folder, `**` crosses
|
|
196
|
+
* folders, `{a,b}` and `[…]` work, and a leading dot is matched like any other character (Node's matcher skips
|
|
197
|
+
* dotfiles, so the built-in search's globs missed `.eslintrc.js` and `.github/…`).
|
|
198
|
+
*/
|
|
199
|
+
/** A path and each folder above it: `a/b/c.ts`, `a/b`, `a`. */
|
|
200
|
+
function withAncestors(path) {
|
|
201
|
+
const parts = path.split('/');
|
|
202
|
+
return parts.map((_, index) => parts.slice(0, parts.length - index).join('/'));
|
|
203
|
+
}
|
|
204
|
+
export function globMatches(path, glob) {
|
|
205
|
+
return globRegExp(glob).test(path);
|
|
206
|
+
}
|
|
207
|
+
const globCache = new Map();
|
|
208
|
+
const GLOB_CACHE_LIMIT = 500;
|
|
209
|
+
function globRegExp(raw) {
|
|
210
|
+
const cached = globCache.get(raw);
|
|
211
|
+
if (cached)
|
|
212
|
+
return cached;
|
|
213
|
+
// A leading slash anchors at the project, as every glob here already is; repeated `**/` mean one.
|
|
214
|
+
const glob = raw.replace(/^\/+/, '').replace(/(\*\*\/){2,}/g, '**/');
|
|
215
|
+
let out = '';
|
|
216
|
+
let braces = 0;
|
|
217
|
+
for (let index = 0; index < glob.length; index += 1) {
|
|
218
|
+
const char = glob[index];
|
|
219
|
+
if (char === '*') {
|
|
220
|
+
if (glob[index + 1] === '*') {
|
|
221
|
+
// `**` crosses folders only as a whole path part (`**/`, `/**`, or alone in an alternative); elsewhere, as in
|
|
222
|
+
// ripgrep, it is two `*`.
|
|
223
|
+
const before = index === 0 ? '/' : glob[index - 1];
|
|
224
|
+
const after = glob[index + 2] ?? '';
|
|
225
|
+
const whole = (before === '/' || (braces > 0 && (before === '{' || before === ','))) && (after === '' || after === '/' || (braces > 0 && (after === '}' || after === ',')));
|
|
226
|
+
if (whole && after === '/') {
|
|
227
|
+
out += '(?:.*/)?';
|
|
228
|
+
index += 2;
|
|
229
|
+
}
|
|
230
|
+
else if (whole) {
|
|
231
|
+
out += '.*';
|
|
232
|
+
index += 1;
|
|
233
|
+
}
|
|
234
|
+
else {
|
|
235
|
+
out += '[^/]*';
|
|
236
|
+
index += 1;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
else
|
|
240
|
+
out += '[^/]*';
|
|
241
|
+
}
|
|
242
|
+
else if (char === '?')
|
|
243
|
+
out += '[^/]';
|
|
244
|
+
else if (char === '{') {
|
|
245
|
+
out += '(?:';
|
|
246
|
+
braces += 1;
|
|
247
|
+
}
|
|
248
|
+
else if (char === '}' && braces > 0) {
|
|
249
|
+
out += ')';
|
|
250
|
+
braces -= 1;
|
|
251
|
+
}
|
|
252
|
+
else if (char === ',' && braces > 0)
|
|
253
|
+
out += '|';
|
|
254
|
+
else if (char === '[') {
|
|
255
|
+
const end = glob.indexOf(']', index + 2);
|
|
256
|
+
if (end < 0) {
|
|
257
|
+
out += '\\[';
|
|
258
|
+
continue;
|
|
259
|
+
}
|
|
260
|
+
let body = glob.slice(index + 1, end);
|
|
261
|
+
if (body.startsWith('!'))
|
|
262
|
+
body = `^${body.slice(1)}`;
|
|
263
|
+
out += `[${body.replace(/\\/g, '\\\\')}]`;
|
|
264
|
+
index = end;
|
|
265
|
+
}
|
|
266
|
+
else if (char === '\\' && index + 1 < glob.length) {
|
|
267
|
+
out += glob[index + 1].replace(/[.*+?^${}()|[\]\\/]/g, '\\$&');
|
|
268
|
+
index += 1;
|
|
269
|
+
}
|
|
270
|
+
else
|
|
271
|
+
out += char.replace(/[.*+?^${}()|[\]\\/]/g, '\\$&');
|
|
272
|
+
}
|
|
273
|
+
let pattern;
|
|
274
|
+
// Case-sensitive everywhere, as ripgrep's --glob is.
|
|
275
|
+
try {
|
|
276
|
+
pattern = new RegExp(`^${out}$`);
|
|
277
|
+
}
|
|
278
|
+
catch {
|
|
279
|
+
pattern = /(?!)/;
|
|
280
|
+
}
|
|
281
|
+
if (globCache.size >= GLOB_CACHE_LIMIT)
|
|
282
|
+
globCache.clear();
|
|
283
|
+
globCache.set(raw, pattern);
|
|
284
|
+
return pattern;
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* A glob list's problems ripgrep would refuse, said before a search: an unclosed `{`, or so many alternatives or
|
|
288
|
+
* `**` that matching could take seconds.
|
|
289
|
+
*/
|
|
290
|
+
export function globProblem(glob) {
|
|
291
|
+
let depth = 0;
|
|
292
|
+
for (let index = 0; index < glob.length; index += 1) {
|
|
293
|
+
const char = glob[index];
|
|
294
|
+
if (char === '\\') {
|
|
295
|
+
index += 1;
|
|
296
|
+
continue;
|
|
297
|
+
}
|
|
298
|
+
if (char === '{')
|
|
299
|
+
depth += 1;
|
|
300
|
+
else if (char === '}' && depth > 0)
|
|
301
|
+
depth -= 1;
|
|
302
|
+
}
|
|
303
|
+
if (depth > 0)
|
|
304
|
+
return `The glob "${glob}" has an unclosed {`;
|
|
305
|
+
if ((glob.match(/\{/g) ?? []).length > 8 || (glob.match(/\*\*/g) ?? []).length > 8)
|
|
306
|
+
return `The glob "${glob}" is too complex to search with`;
|
|
307
|
+
return undefined;
|
|
308
|
+
}
|
|
309
|
+
/** The most a save from the Files tab may write. */
|
|
310
|
+
export const MAX_WRITE_BYTES = 16 * 1024 * 1024;
|
|
311
|
+
/** `workspace.write`'s checked parameters: the file, as a project-relative path with `/`, and the bytes to save. */
|
|
312
|
+
export async function writeTarget(root, params) {
|
|
313
|
+
if (typeof params.content !== 'string')
|
|
314
|
+
throw new Error('content must be the text to save');
|
|
315
|
+
if (typeof params.expectedHash !== 'string' || !/^[0-9a-f]{64}$/.test(params.expectedHash))
|
|
316
|
+
throw new Error('expectedHash must be the SHA-256 workspace.read gave');
|
|
317
|
+
const bytes = Buffer.from(params.content, 'utf8');
|
|
318
|
+
if (bytes.length > MAX_WRITE_BYTES)
|
|
319
|
+
throw new Error('The file is too large to save from the Files tab');
|
|
320
|
+
const path = await resolveInside(root, params.path);
|
|
321
|
+
if (path === root)
|
|
322
|
+
throw new Error('Only files can be saved');
|
|
323
|
+
const info = await stat(path).catch(() => undefined);
|
|
324
|
+
if (info && !info.isFile())
|
|
325
|
+
throw new Error('Only files can be saved');
|
|
326
|
+
return { relative: relative(root, path).split(sep).join('/'), bytes, expectedHash: params.expectedHash };
|
|
327
|
+
}
|
|
328
|
+
/** The repository's top folder when Git answers for the project (it holds `.git`, or its immediate parent does). */
|
|
329
|
+
async function gitTop(root) {
|
|
111
330
|
const parent = dirname(root);
|
|
112
331
|
const holds = async (folder) => stat(join(folder, '.git')).then(() => true, () => false);
|
|
113
332
|
if (!(await holds(root)) && !(await holds(parent)))
|
|
114
|
-
return
|
|
333
|
+
return undefined;
|
|
115
334
|
const top = await collectCapped('git', ['-C', root, 'rev-parse', '--show-toplevel'], 4 * 1024, GIT_TIMEOUT_MS).catch(() => undefined);
|
|
116
335
|
if (!top || top.code !== 0)
|
|
117
|
-
return
|
|
118
|
-
|
|
336
|
+
return undefined;
|
|
337
|
+
const folder = top.text.trim();
|
|
338
|
+
return inside(root, folder) || inside(parent, folder) ? folder : undefined;
|
|
339
|
+
}
|
|
340
|
+
async function gitInScope(root) {
|
|
341
|
+
return (await gitTop(root)) !== undefined;
|
|
119
342
|
}
|
|
120
343
|
export async function gitStatus(root, options = {}) {
|
|
121
|
-
|
|
344
|
+
const top = await gitTop(root);
|
|
345
|
+
if (top === undefined)
|
|
122
346
|
return { repository: false, files: [] };
|
|
347
|
+
// Porcelain paths are named from the repository's top; say where the project sits so a reader can match its own paths.
|
|
348
|
+
const below = relative(await realpath(top).catch(() => resolve(top)), root).split(sep).join('/');
|
|
349
|
+
const prefix = below && !below.startsWith('..') && !isAbsolute(below) ? `${below}/` : undefined;
|
|
123
350
|
let collected;
|
|
124
351
|
try {
|
|
125
352
|
collected = await collectCapped('git', ['-C', root, 'status', '--porcelain=v1', '-z', '--branch', '--untracked-files=all'], options.maxBytes ?? MAX_STATUS_BYTES, GIT_TIMEOUT_MS);
|
|
@@ -152,7 +379,7 @@ export async function gitStatus(root, options = {}) {
|
|
|
152
379
|
if (renamed)
|
|
153
380
|
index++;
|
|
154
381
|
}
|
|
155
|
-
return { repository: true, branch, files, truncated: collected.truncated };
|
|
382
|
+
return { repository: true, branch, files, truncated: collected.truncated, ...(prefix ? { prefix } : {}) };
|
|
156
383
|
}
|
|
157
384
|
}
|
|
158
385
|
const NOTHING_TO_COMPARE = /not a git repository|bad revision 'HEAD'|ambiguous argument 'HEAD'|does not have any commits/i;
|
|
@@ -872,3 +1099,373 @@ export async function listExtensions(config, signal, directory) {
|
|
|
872
1099
|
notices,
|
|
873
1100
|
};
|
|
874
1101
|
}
|
|
1102
|
+
// --- Quick open and search across files (design/specs/files-tab-v3.md, items 1 and 2) -------------------------------
|
|
1103
|
+
const MAX_LISTED_FILES = 50_000;
|
|
1104
|
+
/**
|
|
1105
|
+
* Every file in the project for quick open, `/`-separated and relative to `root`, capped at 50,000. `.git` and
|
|
1106
|
+
* `.andorracode` are skipped, `node_modules` is not walked into (as the tree), symbolic links are not followed, and
|
|
1107
|
+
* files Git ignores are marked.
|
|
1108
|
+
*/
|
|
1109
|
+
export async function listWorkspaceFiles(root) {
|
|
1110
|
+
// Git's ignore list first, so ignored folders (a virtualenv, a build) are walked only after everything else and
|
|
1111
|
+
// cannot fill the cap ahead of the project's own files.
|
|
1112
|
+
const ignoredList = await ignoredUnder(root, '');
|
|
1113
|
+
const ignoredSet = new Set(ignoredList ?? []);
|
|
1114
|
+
const files = [];
|
|
1115
|
+
let truncated = false;
|
|
1116
|
+
const folders = [root];
|
|
1117
|
+
const ignoredFolders = [];
|
|
1118
|
+
while ((folders.length || ignoredFolders.length) && !truncated) {
|
|
1119
|
+
const directory = folders.length ? folders.shift() : ignoredFolders.shift();
|
|
1120
|
+
const found = [];
|
|
1121
|
+
try {
|
|
1122
|
+
for await (const entry of await opendir(directory)) {
|
|
1123
|
+
if (entry.isSymbolicLink() || SKIP_NAMES.has(entry.name))
|
|
1124
|
+
continue;
|
|
1125
|
+
const path = join(directory, entry.name);
|
|
1126
|
+
if (entry.isDirectory()) {
|
|
1127
|
+
if (LIST_ONLY_NAMES.has(entry.name))
|
|
1128
|
+
continue;
|
|
1129
|
+
if (isIgnored(relative(root, path).split(sep).join('/'), ignoredSet))
|
|
1130
|
+
ignoredFolders.push(path);
|
|
1131
|
+
else
|
|
1132
|
+
folders.push(path);
|
|
1133
|
+
}
|
|
1134
|
+
else if (entry.isFile())
|
|
1135
|
+
found.push(relative(root, path).split(sep).join('/'));
|
|
1136
|
+
}
|
|
1137
|
+
}
|
|
1138
|
+
catch { /* a folder that cannot be read is left out; the rest are listed */ }
|
|
1139
|
+
found.sort((left, right) => left.localeCompare(right));
|
|
1140
|
+
for (const path of found) {
|
|
1141
|
+
if (files.length >= MAX_LISTED_FILES) {
|
|
1142
|
+
truncated = true;
|
|
1143
|
+
break;
|
|
1144
|
+
}
|
|
1145
|
+
files.push({ path });
|
|
1146
|
+
}
|
|
1147
|
+
}
|
|
1148
|
+
if (ignoredSet.size)
|
|
1149
|
+
for (const file of files)
|
|
1150
|
+
if (isIgnored(file.path, ignoredSet))
|
|
1151
|
+
file.ignored = true;
|
|
1152
|
+
return { files, truncated };
|
|
1153
|
+
}
|
|
1154
|
+
export const SEARCH_MAX_FILES = 200;
|
|
1155
|
+
export const SEARCH_MAX_MATCHES = 2_000;
|
|
1156
|
+
const SEARCH_TIME_MS = 10_000;
|
|
1157
|
+
const SEARCH_LINE_CHARS = 400;
|
|
1158
|
+
const SEARCH_FALLBACK_FILE_BYTES = 1024 * 1024;
|
|
1159
|
+
const SEARCH_FALLBACK_FILES = 20_000;
|
|
1160
|
+
/** Comma-separated globs; a comma inside `{…}` or `[…]` belongs to its glob (`*.{ts,tsx}`). */
|
|
1161
|
+
export function globList(value) {
|
|
1162
|
+
if (value === undefined || value === '')
|
|
1163
|
+
return [];
|
|
1164
|
+
if (typeof value !== 'string' || value.length > 2000)
|
|
1165
|
+
throw new Error('include and exclude must be comma-separated globs');
|
|
1166
|
+
const parts = [];
|
|
1167
|
+
let depth = 0;
|
|
1168
|
+
let current = '';
|
|
1169
|
+
for (const char of value) {
|
|
1170
|
+
if (char === '{' || char === '[')
|
|
1171
|
+
depth += 1;
|
|
1172
|
+
else if ((char === '}' || char === ']') && depth > 0)
|
|
1173
|
+
depth -= 1;
|
|
1174
|
+
if (char === ',' && depth === 0) {
|
|
1175
|
+
parts.push(current);
|
|
1176
|
+
current = '';
|
|
1177
|
+
continue;
|
|
1178
|
+
}
|
|
1179
|
+
current += char;
|
|
1180
|
+
}
|
|
1181
|
+
parts.push(current);
|
|
1182
|
+
const globs = parts.map(part => part.trim()).filter(Boolean);
|
|
1183
|
+
for (const glob of globs) {
|
|
1184
|
+
const problem = globProblem(glob);
|
|
1185
|
+
if (problem)
|
|
1186
|
+
throw new Error(problem);
|
|
1187
|
+
}
|
|
1188
|
+
return globs;
|
|
1189
|
+
}
|
|
1190
|
+
export function searchOptions(params) {
|
|
1191
|
+
const query = params.query;
|
|
1192
|
+
if (typeof query !== 'string' || !query || query.length > 1000 || query.includes('\0'))
|
|
1193
|
+
throw new Error('A search needs a query of up to 1000 characters');
|
|
1194
|
+
const flag = (name) => params[name] === true;
|
|
1195
|
+
const options = { query, regex: flag('regex'), caseSensitive: flag('caseSensitive'), wholeWord: flag('wholeWord') };
|
|
1196
|
+
if (globList(params.include).length)
|
|
1197
|
+
options.include = params.include;
|
|
1198
|
+
if (globList(params.exclude).length)
|
|
1199
|
+
options.exclude = params.exclude;
|
|
1200
|
+
return options;
|
|
1201
|
+
}
|
|
1202
|
+
/** A line clipped to what the results show, with its match ranges clipped to match. */
|
|
1203
|
+
function clipLine(text, ranges) {
|
|
1204
|
+
const line = text.replace(/\r?\n$/, '');
|
|
1205
|
+
if (line.length <= SEARCH_LINE_CHARS)
|
|
1206
|
+
return { text: line, ranges };
|
|
1207
|
+
// Keep the first match in view: start a little before it.
|
|
1208
|
+
const from = Math.max(0, Math.min((ranges[0]?.start ?? 0) - 40, line.length - SEARCH_LINE_CHARS));
|
|
1209
|
+
const to = from + SEARCH_LINE_CHARS;
|
|
1210
|
+
return {
|
|
1211
|
+
text: line.slice(from, to),
|
|
1212
|
+
ranges: ranges.filter(range => range.start < to && range.end > from).map(range => ({ start: Math.max(0, range.start - from), end: Math.min(SEARCH_LINE_CHARS, range.end - from) })),
|
|
1213
|
+
};
|
|
1214
|
+
}
|
|
1215
|
+
/** Submatches kept per line: the line shown is clipped to 400 characters, so more cannot be shown anyway. */
|
|
1216
|
+
const SEARCH_RANGES_PER_LINE = 50;
|
|
1217
|
+
/**
|
|
1218
|
+
* Ripgrep's byte offsets (ordered) as string offsets, walking the line once; a plain ASCII line needs no walk. A
|
|
1219
|
+
* minified line of a megabyte with thousands of matches stays linear.
|
|
1220
|
+
*/
|
|
1221
|
+
export function charRanges(text, submatches) {
|
|
1222
|
+
const kept = submatches.slice(0, SEARCH_RANGES_PER_LINE);
|
|
1223
|
+
if (Buffer.byteLength(text, 'utf8') === text.length)
|
|
1224
|
+
return kept.map(sub => ({ start: sub.start, end: sub.end }));
|
|
1225
|
+
const out = [];
|
|
1226
|
+
let byte = 0;
|
|
1227
|
+
let index = 0;
|
|
1228
|
+
const advance = (target) => {
|
|
1229
|
+
while (byte < target && index < text.length) {
|
|
1230
|
+
const code = text.codePointAt(index);
|
|
1231
|
+
byte += code < 0x80 ? 1 : code < 0x800 ? 2 : code < 0x10000 ? 3 : 4;
|
|
1232
|
+
index += code > 0xffff ? 2 : 1;
|
|
1233
|
+
}
|
|
1234
|
+
return index;
|
|
1235
|
+
};
|
|
1236
|
+
for (const sub of kept) {
|
|
1237
|
+
const start = advance(sub.start);
|
|
1238
|
+
out.push({ start, end: advance(sub.end) });
|
|
1239
|
+
}
|
|
1240
|
+
return out;
|
|
1241
|
+
}
|
|
1242
|
+
class SearchCollector {
|
|
1243
|
+
files = [];
|
|
1244
|
+
matchCount = 0;
|
|
1245
|
+
full = false;
|
|
1246
|
+
add(path, match) {
|
|
1247
|
+
if (this.matchCount >= SEARCH_MAX_MATCHES) {
|
|
1248
|
+
this.full = true;
|
|
1249
|
+
return false;
|
|
1250
|
+
}
|
|
1251
|
+
let file = this.files[this.files.length - 1];
|
|
1252
|
+
if (!file || file.path !== path) {
|
|
1253
|
+
if (this.files.length >= SEARCH_MAX_FILES) {
|
|
1254
|
+
this.full = true;
|
|
1255
|
+
return false;
|
|
1256
|
+
}
|
|
1257
|
+
file = { path, matches: [] };
|
|
1258
|
+
this.files.push(file);
|
|
1259
|
+
}
|
|
1260
|
+
if (this.matchCount >= SEARCH_MAX_MATCHES) {
|
|
1261
|
+
this.full = true;
|
|
1262
|
+
return false;
|
|
1263
|
+
}
|
|
1264
|
+
file.matches.push(match);
|
|
1265
|
+
this.matchCount += 1;
|
|
1266
|
+
return true;
|
|
1267
|
+
}
|
|
1268
|
+
limitNote() { return `Showing the first ${SEARCH_MAX_MATCHES.toLocaleString('en-US')} results in ${SEARCH_MAX_FILES} files at most`; }
|
|
1269
|
+
}
|
|
1270
|
+
/**
|
|
1271
|
+
* Searches the project's files for `options.query`, with ripgrep when it is installed (it skips what Git ignores) and
|
|
1272
|
+
* a built-in scan otherwise. Results stop at 200 files, 2,000 matches or 10 seconds, and say so (`incomplete`).
|
|
1273
|
+
*/
|
|
1274
|
+
export async function searchWorkspace(root, options, signal, rg = null) {
|
|
1275
|
+
return rg ? searchWithRipgrep(root, options, rg, signal) : searchBuiltIn(root, options, signal);
|
|
1276
|
+
}
|
|
1277
|
+
function searchWithRipgrep(root, options, rg, signal) {
|
|
1278
|
+
// node_modules is skipped even where Git does not ignore it, as quick open and the tree skip it.
|
|
1279
|
+
const args = ['--json', '--hidden', '--glob', '!.git', '--glob', '!.andorracode', '--glob', '!**/node_modules/**', '--max-filesize', '2M',
|
|
1280
|
+
options.caseSensitive ? '--case-sensitive' : '--ignore-case'];
|
|
1281
|
+
if (options.wholeWord)
|
|
1282
|
+
args.push('--word-regexp');
|
|
1283
|
+
if (!options.regex)
|
|
1284
|
+
args.push('--fixed-strings');
|
|
1285
|
+
for (const glob of globList(options.include))
|
|
1286
|
+
args.push('--glob', glob);
|
|
1287
|
+
for (const glob of globList(options.exclude))
|
|
1288
|
+
args.push('--glob', `!${glob}`);
|
|
1289
|
+
args.push('--', options.query, '.');
|
|
1290
|
+
if (signal?.aborted)
|
|
1291
|
+
return Promise.resolve({ files: [], matchCount: 0, incomplete: 'Search cancelled', engine: 'ripgrep' });
|
|
1292
|
+
return new Promise((done, fail) => {
|
|
1293
|
+
const collected = new SearchCollector();
|
|
1294
|
+
let incomplete;
|
|
1295
|
+
let settled = false;
|
|
1296
|
+
const child = spawn(rg, args, { cwd: root, stdio: ['ignore', 'pipe', 'pipe'], windowsHide: true });
|
|
1297
|
+
const timer = setTimeout(() => { incomplete = `Search stopped after ${SEARCH_TIME_MS / 1000} seconds`; child.kill(); }, SEARCH_TIME_MS);
|
|
1298
|
+
const onAbort = () => { incomplete = 'Search cancelled'; child.kill(); };
|
|
1299
|
+
signal?.addEventListener('abort', onAbort, { once: true });
|
|
1300
|
+
let buffered = '';
|
|
1301
|
+
let errors = '';
|
|
1302
|
+
const decoder = new StringDecoder('utf8');
|
|
1303
|
+
const finish = (error) => {
|
|
1304
|
+
if (settled)
|
|
1305
|
+
return;
|
|
1306
|
+
settled = true;
|
|
1307
|
+
clearTimeout(timer);
|
|
1308
|
+
signal?.removeEventListener('abort', onAbort);
|
|
1309
|
+
if (error) {
|
|
1310
|
+
fail(error);
|
|
1311
|
+
return;
|
|
1312
|
+
}
|
|
1313
|
+
if (collected.full)
|
|
1314
|
+
incomplete ??= collected.limitNote();
|
|
1315
|
+
done({ files: collected.files, matchCount: collected.matchCount, ...(incomplete ? { incomplete } : {}), engine: 'ripgrep' });
|
|
1316
|
+
};
|
|
1317
|
+
child.stdout.on('data', (chunk) => {
|
|
1318
|
+
buffered += decoder.write(chunk);
|
|
1319
|
+
let newline = buffered.indexOf('\n');
|
|
1320
|
+
while (newline !== -1) {
|
|
1321
|
+
const record = buffered.slice(0, newline);
|
|
1322
|
+
buffered = buffered.slice(newline + 1);
|
|
1323
|
+
newline = buffered.indexOf('\n');
|
|
1324
|
+
if (collected.full)
|
|
1325
|
+
continue;
|
|
1326
|
+
let event;
|
|
1327
|
+
try {
|
|
1328
|
+
event = JSON.parse(record);
|
|
1329
|
+
}
|
|
1330
|
+
catch {
|
|
1331
|
+
continue;
|
|
1332
|
+
}
|
|
1333
|
+
if (event.type !== 'match' || !event.data?.path?.text || typeof event.data.lines?.text !== 'string' || !event.data.line_number)
|
|
1334
|
+
continue;
|
|
1335
|
+
const text = event.data.lines.text;
|
|
1336
|
+
const ranges = charRanges(text, event.data.submatches ?? []);
|
|
1337
|
+
const path = event.data.path.text.replace(/^\.[\\/]/, '').split(sep).join('/');
|
|
1338
|
+
if (!collected.add(path, { line: event.data.line_number, ...clipLine(text, ranges) }))
|
|
1339
|
+
child.kill();
|
|
1340
|
+
}
|
|
1341
|
+
});
|
|
1342
|
+
child.stderr.on('data', (chunk) => { if (errors.length < 4000)
|
|
1343
|
+
errors += chunk.toString('utf8'); });
|
|
1344
|
+
child.on('error', error => finish(error));
|
|
1345
|
+
child.on('close', code => {
|
|
1346
|
+
// 1 is "no matches"; a killed search keeps what it found.
|
|
1347
|
+
if (code === 2 && !collected.matchCount && !incomplete) {
|
|
1348
|
+
// ripgrep's first line is often only "regex parse error:"; the lines after it say what and where.
|
|
1349
|
+
const said = errors.trim().split('\n').map(line => line.trim()).filter(Boolean).slice(0, 4).join(' ').replace(/^rg:\s*/, '');
|
|
1350
|
+
finish(new Error(said ? `The search failed: ${said}` : 'The search failed'));
|
|
1351
|
+
return;
|
|
1352
|
+
}
|
|
1353
|
+
if (code === 2 && errors.trim())
|
|
1354
|
+
incomplete ??= 'Some files could not be searched';
|
|
1355
|
+
finish();
|
|
1356
|
+
});
|
|
1357
|
+
});
|
|
1358
|
+
}
|
|
1359
|
+
async function searchBuiltIn(root, options, signal) {
|
|
1360
|
+
const source = options.regex ? options.query : options.query.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
1361
|
+
let pattern;
|
|
1362
|
+
// A whole word is one not touched by word characters, so `retry()` and `.foo` match as ripgrep's --word-regexp does.
|
|
1363
|
+
try {
|
|
1364
|
+
pattern = new RegExp(options.wholeWord ? `(?<![\\p{L}\\p{N}_])(?:${source})(?![\\p{L}\\p{N}_])` : source, `gu${options.caseSensitive ? '' : 'i'}`);
|
|
1365
|
+
}
|
|
1366
|
+
catch (error) {
|
|
1367
|
+
throw new Error(`The regular expression is not valid: ${message(error)}`);
|
|
1368
|
+
}
|
|
1369
|
+
const include = globList(options.include);
|
|
1370
|
+
const exclude = globList(options.exclude);
|
|
1371
|
+
// A glob without a folder matches the file's name anywhere, as ripgrep's --glob does.
|
|
1372
|
+
const matchesGlob = (path, glob) => globMatches(path, glob) || (!glob.includes('/') && globMatches(posix.basename(path), glob));
|
|
1373
|
+
const collected = new SearchCollector();
|
|
1374
|
+
const deadline = Date.now() + SEARCH_TIME_MS;
|
|
1375
|
+
let incomplete;
|
|
1376
|
+
const { files } = await listWorkspaceFiles(root);
|
|
1377
|
+
let scanned = 0;
|
|
1378
|
+
for (const file of files) {
|
|
1379
|
+
if (signal?.aborted) {
|
|
1380
|
+
incomplete = 'Search cancelled';
|
|
1381
|
+
break;
|
|
1382
|
+
}
|
|
1383
|
+
if (file.ignored)
|
|
1384
|
+
continue;
|
|
1385
|
+
if (include.length && !include.some(glob => matchesGlob(file.path, glob)))
|
|
1386
|
+
continue;
|
|
1387
|
+
// An exclude that names a folder leaves out everything in it, as ripgrep's does.
|
|
1388
|
+
if (exclude.some(glob => withAncestors(file.path).some(path => matchesGlob(path, glob))))
|
|
1389
|
+
continue;
|
|
1390
|
+
if (scanned >= SEARCH_FALLBACK_FILES) {
|
|
1391
|
+
incomplete = `Search stopped at ${SEARCH_FALLBACK_FILES.toLocaleString('en-US')} files`;
|
|
1392
|
+
break;
|
|
1393
|
+
}
|
|
1394
|
+
if (Date.now() > deadline) {
|
|
1395
|
+
incomplete = `Search stopped after ${SEARCH_TIME_MS / 1000} seconds`;
|
|
1396
|
+
break;
|
|
1397
|
+
}
|
|
1398
|
+
const full = join(root, file.path);
|
|
1399
|
+
let content;
|
|
1400
|
+
try {
|
|
1401
|
+
const info = await stat(full);
|
|
1402
|
+
if (info.size > SEARCH_FALLBACK_FILE_BYTES)
|
|
1403
|
+
continue;
|
|
1404
|
+
const bytes = await readFile(full);
|
|
1405
|
+
if (bytes.subarray(0, 8000).includes(0))
|
|
1406
|
+
continue;
|
|
1407
|
+
content = bytes.toString('utf8');
|
|
1408
|
+
}
|
|
1409
|
+
catch {
|
|
1410
|
+
continue;
|
|
1411
|
+
}
|
|
1412
|
+
scanned += 1;
|
|
1413
|
+
const lines = content.split('\n');
|
|
1414
|
+
for (let index = 0; index < lines.length; index += 1) {
|
|
1415
|
+
const text = lines[index].replace(/\r$/, '');
|
|
1416
|
+
pattern.lastIndex = 0;
|
|
1417
|
+
const ranges = [];
|
|
1418
|
+
for (let hit = pattern.exec(text); hit; hit = pattern.exec(text)) {
|
|
1419
|
+
ranges.push({ start: hit.index, end: hit.index + hit[0].length });
|
|
1420
|
+
if (hit[0].length === 0)
|
|
1421
|
+
pattern.lastIndex += 1;
|
|
1422
|
+
}
|
|
1423
|
+
if (!ranges.length)
|
|
1424
|
+
continue;
|
|
1425
|
+
if (!collected.add(file.path, { line: index + 1, ...clipLine(text, ranges) }))
|
|
1426
|
+
break;
|
|
1427
|
+
}
|
|
1428
|
+
if (collected.full)
|
|
1429
|
+
break;
|
|
1430
|
+
}
|
|
1431
|
+
if (collected.full)
|
|
1432
|
+
incomplete ??= collected.limitNote();
|
|
1433
|
+
return { files: collected.files, matchCount: collected.matchCount, ...(incomplete ? { incomplete } : {}), engine: 'built-in' };
|
|
1434
|
+
}
|
|
1435
|
+
// --- Copy a link to lines (design/specs/files-tab-v3.md, item 5) ----------------------------------------------------
|
|
1436
|
+
/** A GitHub remote URL (https, ssh or git@) as its web address, or undefined for any other host. */
|
|
1437
|
+
export function githubWebUrl(remote) {
|
|
1438
|
+
const match = /^(?:https?:\/\/(?:[^@/]+@)?github\.com\/|git@github\.com:|ssh:\/\/git@github\.com\/)([\w.-]+)\/([\w.-]+?)(?:\.git)?\/?$/i.exec(remote.trim());
|
|
1439
|
+
return match ? `https://github.com/${match[1]}/${match[2]}` : undefined;
|
|
1440
|
+
}
|
|
1441
|
+
/**
|
|
1442
|
+
* What a GitHub link to lines needs: the repository's web address, the commit checked out, whether that commit is on
|
|
1443
|
+
* a remote branch (so the link resolves), and where the project sits in the repository. Fields are left out where Git
|
|
1444
|
+
* cannot say.
|
|
1445
|
+
*/
|
|
1446
|
+
export async function gitLinkBase(root) {
|
|
1447
|
+
const top = await gitTop(root);
|
|
1448
|
+
if (top === undefined)
|
|
1449
|
+
return { repository: false };
|
|
1450
|
+
const git = async (args) => {
|
|
1451
|
+
try {
|
|
1452
|
+
const collected = await collectCapped('git', ['-C', root, ...args], 64 * 1024, GIT_TIMEOUT_MS);
|
|
1453
|
+
return collected.code === 0 ? collected.text.trim() : undefined;
|
|
1454
|
+
}
|
|
1455
|
+
catch {
|
|
1456
|
+
return undefined;
|
|
1457
|
+
}
|
|
1458
|
+
};
|
|
1459
|
+
const [remote, head] = await Promise.all([git(['config', '--get', 'remote.origin.url']), git(['rev-parse', 'HEAD'])]);
|
|
1460
|
+
// The link names origin's repository, so the commit must be on one of origin's branches.
|
|
1461
|
+
const contains = head ? await git(['branch', '-r', '--contains', head, '--list', 'origin/*']) : undefined;
|
|
1462
|
+
const below = relative(await realpath(top).catch(() => resolve(top)), root).split(sep).join('/');
|
|
1463
|
+
const webUrl = remote ? githubWebUrl(remote) : undefined;
|
|
1464
|
+
return {
|
|
1465
|
+
repository: true,
|
|
1466
|
+
...(webUrl ? { webUrl } : {}),
|
|
1467
|
+
...(head && /^[0-9a-f]{40}$/.test(head) ? { head } : {}),
|
|
1468
|
+
...(contains !== undefined ? { pushed: contains.length > 0 } : {}),
|
|
1469
|
+
...(below && !below.startsWith('..') && !isAbsolute(below) ? { prefix: `${below}/` } : {}),
|
|
1470
|
+
};
|
|
1471
|
+
}
|