@anhlp/alp-daemon 0.0.0-stage → 0.0.1-beta.4
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/LICENSE +202 -0
- package/NOTICE +7 -0
- package/README.md +14 -2
- package/dist/THIRD-PARTY-NOTICES.txt +431 -0
- package/dist/alp-templates.d.ts +5 -0
- package/dist/alpd.js +49562 -0
- package/dist/attachments.d.ts +211 -0
- package/dist/attachments.js +819 -0
- package/dist/build-manifest.json +131 -0
- package/dist/conversation.d.ts +119 -0
- package/dist/conversation.js +672 -0
- package/dist/files.d.ts +123 -0
- package/dist/files.js +365 -0
- package/dist/git.d.ts +87 -0
- package/dist/git.js +1093 -0
- package/dist/gitignore.d.ts +62 -0
- package/dist/gitignore.js +254 -0
- package/dist/history.d.ts +24 -0
- package/dist/history.js +149 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +25 -0
- package/dist/lifecycle.d.ts +101 -0
- package/dist/lifecycle.js +239 -0
- package/dist/lock.d.ts +21 -0
- package/dist/lock.js +42 -0
- package/dist/main.d.ts +1 -0
- package/dist/main.js +277 -0
- package/dist/operations.d.ts +23 -0
- package/dist/operations.js +275 -0
- package/dist/registry.d.ts +89 -0
- package/dist/registry.js +343 -0
- package/dist/rpc-error.d.ts +6 -0
- package/dist/rpc-error.js +11 -0
- package/dist/scheduler.d.ts +96 -0
- package/dist/scheduler.js +290 -0
- package/dist/schedules.d.ts +69 -0
- package/dist/schedules.js +244 -0
- package/dist/server.d.ts +58 -0
- package/dist/server.js +1780 -0
- package/dist/store.d.ts +176 -0
- package/dist/store.js +385 -0
- package/dist/terminals.d.ts +157 -0
- package/dist/terminals.js +487 -0
- package/dist/worktrees.d.ts +41 -0
- package/dist/worktrees.js +246 -0
- package/package.json +159 -4
- package/src/alp-templates.d.ts +5 -0
- package/src/attachments.ts +870 -0
- package/src/conversation.ts +837 -0
- package/src/files.ts +459 -0
- package/src/git.ts +1045 -0
- package/src/gitignore.ts +277 -0
- package/src/history.ts +118 -0
- package/src/index.ts +11 -0
- package/src/lifecycle.ts +283 -0
- package/src/lock.ts +53 -0
- package/src/main.ts +292 -0
- package/src/operations.ts +300 -0
- package/src/registry.ts +238 -0
- package/src/rpc-error.ts +4 -0
- package/src/scheduler.ts +422 -0
- package/src/schedules.ts +294 -0
- package/src/server.ts +2056 -0
- package/src/store.ts +448 -0
- package/src/terminals.ts +591 -0
- package/src/worktrees.ts +287 -0
package/src/git.ts
ADDED
|
@@ -0,0 +1,1045 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import { createHash, createHmac, randomBytes, timingSafeEqual } from 'node:crypto';
|
|
3
|
+
import { lstat, realpath } from 'node:fs/promises';
|
|
4
|
+
import os from 'node:os';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import { user_worktree } from '@anhlp/alp-core';
|
|
7
|
+
import { ERROR, RPC_ERROR, WORKSPACE_GIT, blockedFilePath } from '@anhlp/alp-protocol';
|
|
8
|
+
import type { GitChange, GitCompare, GitDiffFile, GitDiffHunk, GitDiffPage, GitStatus, GitStatusFile } from '@anhlp/alp-protocol';
|
|
9
|
+
import { FileAccessError, PRIVATE_KEY, likelyBinary, readConfinedFile, relativeSegments } from './files.js';
|
|
10
|
+
import { IgnoreUnknown, resolveGit, searchPathOutside } from './gitignore.js';
|
|
11
|
+
import { RegistryError } from './registry.js';
|
|
12
|
+
import { RpcError } from './rpc-error.js';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* `workspace.git.status` and `workspace.git.diff` (docs/protocol/alp-json-rpc-v1.md §12.5): the
|
|
16
|
+
* read-only Git state of a registry workspace, for Paseo's branch header and Changes panel.
|
|
17
|
+
*
|
|
18
|
+
* The workspace and its repository configuration are not trusted, so Git runs boxed in:
|
|
19
|
+
* - the Git found on the absolute PATH entries outside the workspace, by its real path
|
|
20
|
+
* (gitignore.ts resolveGit), spawned with an argument list, no shell, in the workspace's real
|
|
21
|
+
* path, its own process group killed at the call's deadline;
|
|
22
|
+
* - GIT_DIR and GIT_WORK_TREE set by alpd to the repository it checked (the workspace's `.git`,
|
|
23
|
+
* validated as the registry validates worktrees: core repositoryIdentity), every other GIT_*
|
|
24
|
+
* variable of alpd's dropped, no system or global configuration, no optional locks, no prompts,
|
|
25
|
+
* no lazy fetch, no replace objects, PATH without the workspace's entries;
|
|
26
|
+
* - configuration overrides so nothing the repository names runs: no fsmonitor, hooks to the null
|
|
27
|
+
* device, no pager, no colour, no external diff or textconv (flags too), every
|
|
28
|
+
* filter.<driver>.{clean,smudge,process} the repository's configuration names emptied and
|
|
29
|
+
* `required` false, no submodule recursion (`--ignore-submodules=dirty`: a gitlink's recorded
|
|
30
|
+
* commit only, never a Git run inside it), no network protocol, `diff.autoRefreshIndex` off (a
|
|
31
|
+
* porcelain `git diff` would otherwise rewrite the index even with GIT_OPTIONAL_LOCKS=0);
|
|
32
|
+
* - `--literal-pathspecs`: a path is never pathspec magic, and always after `--`; a base branch is
|
|
33
|
+
* a validated name resolved only as `refs/remotes/origin/<name>` then `refs/heads/<name>` with
|
|
34
|
+
* `rev-parse --verify --end-of-options`, never a revision expression;
|
|
35
|
+
* - every output read as a stream with a bound; at most GIT_READ.maxConcurrent Git processes at
|
|
36
|
+
* once in alpd.
|
|
37
|
+
* Content alpd shows is checked first: both sides of every file are scanned for a private key
|
|
38
|
+
* (files.ts PRIVATE_KEY), historical and deleted ones included; untracked files are read by alpd
|
|
39
|
+
* itself, confined as workspace.files.read confines a read. Errors carry fixed messages; the
|
|
40
|
+
* cause is logged, never answered.
|
|
41
|
+
*/
|
|
42
|
+
|
|
43
|
+
export const GIT_READ = Object.freeze({
|
|
44
|
+
/** One call (all of its Git processes, waiting for a turn included) gets this long. */
|
|
45
|
+
timeoutMs: 15_000,
|
|
46
|
+
/** Git processes these reads run at once in alpd, and processes that may wait for one. */
|
|
47
|
+
maxConcurrent: 4,
|
|
48
|
+
maxQueued: 32,
|
|
49
|
+
maxErrorBytes: 16 * 1024,
|
|
50
|
+
/** Configuration, change list and untracked list output, at most. */
|
|
51
|
+
maxListBytes: 16 * 1024 * 1024,
|
|
52
|
+
/** One patch stream, at most (its files are at most a window's worth of content). */
|
|
53
|
+
maxPatchStreamBytes: 32 * 1024 * 1024,
|
|
54
|
+
/** One side of a file scanned for a private key; a larger side is `too_large`. */
|
|
55
|
+
maxScanBytes: 8 * 1024 * 1024,
|
|
56
|
+
/** Files whose content one window reads and diffs together, and their content's total. */
|
|
57
|
+
windowFiles: 64,
|
|
58
|
+
windowBytes: 4 * 1024 * 1024,
|
|
59
|
+
/** Pathspec bytes on one command line. */
|
|
60
|
+
maxArgBytes: process.platform === 'win32' ? 24 * 1024 : 256 * 1024,
|
|
61
|
+
/** Worktree files whose size and time enter a diff's snapshot. */
|
|
62
|
+
maxSnapshotStats: 5_000,
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
const FIXED = Object.freeze({
|
|
66
|
+
notRepository: 'The workspace is not a Git repository',
|
|
67
|
+
notRoot: 'The workspace is not the top level of its Git repository',
|
|
68
|
+
unavailable: 'No Git is available to alpd',
|
|
69
|
+
tooOld: 'The Git available to alpd is older than 2.31, which these reads need',
|
|
70
|
+
failed: 'Git could not read the workspace',
|
|
71
|
+
invalidCompare: 'compare.baseRef must name a branch',
|
|
72
|
+
unknownRef: 'compare.baseRef names no branch of the repository',
|
|
73
|
+
stale: 'The checkout changed since the cursor was given; ask again without a cursor',
|
|
74
|
+
invalidCursor: 'cursor is not one alpd gave for this workspace and comparison',
|
|
75
|
+
notFound: 'No workspace with this workspaceId',
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
/** A Git read that failed: answered as `1000 git_failed` with a fixed message; `cause` is logged only. */
|
|
79
|
+
export class GitReadFailed extends Error {
|
|
80
|
+
constructor(readonly cause: string) { super(FIXED.failed); }
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const failed = (why: string): never => { throw new GitReadFailed(why); };
|
|
84
|
+
const gitFailed = () => new RpcError(ERROR.failed, FIXED.failed, { reason: 'git_failed' });
|
|
85
|
+
|
|
86
|
+
// ---- Git processes ----
|
|
87
|
+
|
|
88
|
+
let running = 0;
|
|
89
|
+
const waiting: Array<() => void> = [];
|
|
90
|
+
function acquire(deadline: number): Promise<() => void> {
|
|
91
|
+
let released = false;
|
|
92
|
+
const release = () => { if (released) return; released = true; running--; waiting.shift()?.(); };
|
|
93
|
+
if (running < GIT_READ.maxConcurrent) { running++; return Promise.resolve(release); }
|
|
94
|
+
if (waiting.length >= GIT_READ.maxQueued) return Promise.reject(new GitReadFailed('too many Git reads waiting'));
|
|
95
|
+
return new Promise((resolve, reject) => {
|
|
96
|
+
const turn = () => { clearTimeout(timer); running++; resolve(release); };
|
|
97
|
+
const timer = setTimeout(() => {
|
|
98
|
+
const index = waiting.indexOf(turn);
|
|
99
|
+
if (index >= 0) waiting.splice(index, 1);
|
|
100
|
+
reject(new GitReadFailed('timed out waiting for a turn'));
|
|
101
|
+
}, Math.max(0, deadline - Date.now()));
|
|
102
|
+
waiting.push(turn);
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** Git processes these reads run now, for tests. */
|
|
107
|
+
export const gitReadsRunning = () => running;
|
|
108
|
+
|
|
109
|
+
type Box = { git: string; root: string; env: NodeJS.ProcessEnv; deadline: number; overrides: string[] };
|
|
110
|
+
|
|
111
|
+
/** Flags before every subcommand: nothing the repository names may run, nothing is paged or coloured. */
|
|
112
|
+
const BOXED = [
|
|
113
|
+
'--no-pager', '--literal-pathspecs',
|
|
114
|
+
'-c', 'core.fsmonitor=false', '-c', `core.hooksPath=${os.devNull}`, '-c', 'core.pager=cat', '-c', 'core.untrackedCache=false',
|
|
115
|
+
'-c', 'core.quotePath=false', '-c', 'color.ui=never', '-c', 'diff.autoRefreshIndex=false', '-c', 'diff.noprefix=false',
|
|
116
|
+
'-c', 'diff.mnemonicPrefix=false', '-c', 'diff.relative=false', '-c', 'diff.suppressBlankEmpty=false', '-c', `diff.orderFile=${os.devNull}`,
|
|
117
|
+
'-c', 'log.showSignature=false', '-c', 'submodule.recurse=false', '-c', 'protocol.allow=never', '-c', 'maintenance.auto=false',
|
|
118
|
+
'-c', 'gc.auto=0', '-c', 'status.showStash=false', '-c', 'status.renames=true', '-c', 'diff.renames=true',
|
|
119
|
+
];
|
|
120
|
+
|
|
121
|
+
type RunOptions = {
|
|
122
|
+
stdin?: string;
|
|
123
|
+
okCodes?: number[];
|
|
124
|
+
/** Collect at most this much stdout; more fails (GitReadFailed). */
|
|
125
|
+
maxBytes?: number;
|
|
126
|
+
/** Stream stdout instead: return true to stop Git (killed; the result says `stopped`). */
|
|
127
|
+
onData?: (chunk: Buffer) => boolean | void;
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
function runGit(box: Box, args: string[], { stdin, okCodes = [0], maxBytes = GIT_READ.maxListBytes, onData }: RunOptions = {}): Promise<{ code: number; stdout: Buffer; stopped: boolean }> {
|
|
131
|
+
return acquire(box.deadline).then(release => new Promise((resolve, reject) => {
|
|
132
|
+
let settled = false;
|
|
133
|
+
let stopped = false;
|
|
134
|
+
let child: ReturnType<typeof spawn>;
|
|
135
|
+
const group = process.platform !== 'win32';
|
|
136
|
+
const kill = () => {
|
|
137
|
+
try {
|
|
138
|
+
if (group && child.pid) process.kill(-child.pid, 'SIGKILL');
|
|
139
|
+
else child.kill('SIGKILL');
|
|
140
|
+
} catch {
|
|
141
|
+
// already gone
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
145
|
+
const finish = (outcome: () => void) => {
|
|
146
|
+
if (settled) return;
|
|
147
|
+
settled = true;
|
|
148
|
+
if (timer) clearTimeout(timer);
|
|
149
|
+
release();
|
|
150
|
+
outcome();
|
|
151
|
+
};
|
|
152
|
+
try {
|
|
153
|
+
child = spawn(box.git, [...BOXED, ...box.overrides, ...args], {
|
|
154
|
+
cwd: box.root, env: box.env, shell: false, windowsHide: true, detached: group, stdio: [stdin === undefined ? 'ignore' : 'pipe', 'pipe', 'pipe'],
|
|
155
|
+
});
|
|
156
|
+
} catch (error) {
|
|
157
|
+
finish(() => reject(new GitReadFailed(`spawn: ${(error as Error).message}`)));
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
timer = setTimeout(() => finish(() => { kill(); reject(new GitReadFailed('timed out')); }), Math.max(1, box.deadline - Date.now()));
|
|
161
|
+
const out: Buffer[] = [];
|
|
162
|
+
let outBytes = 0;
|
|
163
|
+
let err = '';
|
|
164
|
+
child.stdout!.on('data', (chunk: Buffer) => {
|
|
165
|
+
if (settled || stopped) return;
|
|
166
|
+
if (onData) {
|
|
167
|
+
if (onData(chunk)) { stopped = true; kill(); }
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
outBytes += chunk.length;
|
|
171
|
+
if (outBytes > maxBytes) finish(() => { kill(); reject(new GitReadFailed('output over its bound')); });
|
|
172
|
+
else out.push(chunk);
|
|
173
|
+
});
|
|
174
|
+
child.stderr!.on('data', (chunk: Buffer) => { if (err.length < GIT_READ.maxErrorBytes) err += chunk.toString('utf8'); });
|
|
175
|
+
child.on('error', error => finish(() => reject(new GitReadFailed(`spawn: ${error.message}`))));
|
|
176
|
+
child.on('close', code => finish(() => {
|
|
177
|
+
if (stopped) { resolve({ code: code ?? -1, stdout: Buffer.alloc(0), stopped: true }); return; }
|
|
178
|
+
if (code === null || !okCodes.includes(code)) { reject(new GitReadFailed(`${args[0]} exit ${code}: ${err.trim().slice(0, 1000)}`)); return; }
|
|
179
|
+
resolve({ code, stdout: Buffer.concat(out), stopped: false });
|
|
180
|
+
}));
|
|
181
|
+
if (stdin !== undefined) {
|
|
182
|
+
child.stdin!.on('error', () => {});
|
|
183
|
+
child.stdin!.end(stdin);
|
|
184
|
+
}
|
|
185
|
+
}));
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* The oldest Git these reads run: `rev-parse --end-of-options` (2.30) and GIT_CONFIG_COUNT (2.31).
|
|
190
|
+
* An older one would take `--end-of-options` as a revision and answer HEAD as missing: refused.
|
|
191
|
+
*/
|
|
192
|
+
const MINIMUM_GIT = [2, 31] as const;
|
|
193
|
+
const versions = new Map<string, Promise<boolean>>();
|
|
194
|
+
function recentEnough(box: Box): Promise<boolean> {
|
|
195
|
+
const known = versions.get(box.git);
|
|
196
|
+
if (known) return known;
|
|
197
|
+
const checked = runGit(box, ['--version'], { maxBytes: 4096 }).then(({ stdout }) => {
|
|
198
|
+
const found = /^git version (\d+)\.(\d+)/.exec(stdout.toString('utf8'));
|
|
199
|
+
if (!found) failed('unrecognized git --version');
|
|
200
|
+
const [major, minor] = [Number(found![1]), Number(found![2])];
|
|
201
|
+
return major > MINIMUM_GIT[0] || (major === MINIMUM_GIT[0] && minor >= MINIMUM_GIT[1]);
|
|
202
|
+
});
|
|
203
|
+
versions.set(box.git, checked);
|
|
204
|
+
checked.catch(() => versions.delete(box.git));
|
|
205
|
+
return checked;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// ---- The workspace and its repository ----
|
|
209
|
+
|
|
210
|
+
type Located = { kind: 'not_repository' } | { kind: 'unsupported' } | { kind: 'repository'; root: string; gitDir: string };
|
|
211
|
+
|
|
212
|
+
const missing = (error: unknown) => ['ENOENT', 'ENOTDIR'].includes((error as NodeJS.ErrnoException)?.code ?? '');
|
|
213
|
+
|
|
214
|
+
/** Whether `root` is a repository's top level, inside one (a `.git` above it), or in none. */
|
|
215
|
+
async function locate(root: string): Promise<Located> {
|
|
216
|
+
let entry = true;
|
|
217
|
+
try { await lstat(path.join(root, '.git')); } catch (error) { if (!missing(error)) failed('cannot examine .git'); entry = false; }
|
|
218
|
+
if (!entry) {
|
|
219
|
+
for (let directory = path.dirname(root); ; directory = path.dirname(directory)) {
|
|
220
|
+
try { await lstat(path.join(directory, '.git')); return { kind: 'unsupported' }; } catch (error) { if (!missing(error)) failed('cannot examine .git above the workspace'); }
|
|
221
|
+
if (path.dirname(directory) === directory) return { kind: 'not_repository' };
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
let gitDir: string;
|
|
225
|
+
try {
|
|
226
|
+
// The registry's own check of a checkout: a canonical root, a real .git directory or a
|
|
227
|
+
// gitdir file (one name, bounded), a linked worktree's backlink to this very .git.
|
|
228
|
+
({ gitDir } = await user_worktree.repositoryIdentity(root));
|
|
229
|
+
} catch (error) {
|
|
230
|
+
failed(`the workspace's .git cannot be used: ${(error as Error).message}`);
|
|
231
|
+
}
|
|
232
|
+
return { kind: 'repository', root, gitDir: gitDir! };
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/** alpd's environment for Git: none of its own GIT_* variables, only those naming the checked repository. */
|
|
236
|
+
function environment(root: string, gitDir: string): NodeJS.ProcessEnv {
|
|
237
|
+
const env: NodeJS.ProcessEnv = {};
|
|
238
|
+
for (const [key, value] of Object.entries(process.env)) if (!/^GIT_/i.test(key) && !/^path$/i.test(key)) env[key] = value;
|
|
239
|
+
let searchPath: string;
|
|
240
|
+
try { searchPath = searchPathOutside(root); } catch { failed('the workspace cannot be examined'); }
|
|
241
|
+
return {
|
|
242
|
+
...env, PATH: searchPath!, GIT_DIR: gitDir, GIT_WORK_TREE: root, GIT_CONFIG_NOSYSTEM: '1', GIT_CONFIG_GLOBAL: os.devNull, GIT_ATTR_NOSYSTEM: '1',
|
|
243
|
+
GIT_OPTIONAL_LOCKS: '0', GIT_TERMINAL_PROMPT: '0', GIT_NO_LAZY_FETCH: '1', GIT_NO_REPLACE_OBJECTS: '1', GIT_PROTOCOL_FROM_USER: '0', LC_ALL: 'C', LANG: 'C',
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
const EMPTY_TREE = { sha1: '4b825dc642cb6eb9a060e54bf8d69288fbee4904', sha256: '6ef19b41225c5369f1c104d45d8d85efa9b057b53b14b4b9b939dd74decc5321' };
|
|
248
|
+
|
|
249
|
+
type Repository = Box & { remoteUrl: string | null; emptyTree: string; gitDir: string; config: string };
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* The box for a located repository: the configuration overrides that neutralize every filter it
|
|
253
|
+
* names, origin's URL, its object format. A filter name `-c` cannot carry, or a work tree
|
|
254
|
+
* configured elsewhere than the workspace, refuses (unsupported or failed), never runs Git
|
|
255
|
+
* without the overrides.
|
|
256
|
+
*/
|
|
257
|
+
/** The configuration a box is built from: origin, the work tree, the object format and every filter driver. */
|
|
258
|
+
const CONFIG_READ = ['config', '--null', '--get-regexp', '^(remote\\.origin\\.url|core\\.worktree|extensions\\.objectformat|filter\\..+\\.(smudge|clean|process|required))$'];
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Every Git process of a call reads the repository's configuration again. A filter driver (or
|
|
262
|
+
* work tree) added while the call ran was not neutralized: the answer is refused (fails closed).
|
|
263
|
+
*/
|
|
264
|
+
async function sameConfig(repo: Repository): Promise<void> {
|
|
265
|
+
// Read as the box's first read was: without the overrides (`-c` entries are listed as configuration).
|
|
266
|
+
const { stdout } = await runGit({ ...repo, overrides: [] }, CONFIG_READ, { okCodes: [0, 1] });
|
|
267
|
+
if (hash(stdout) !== repo.config) failed('the repository configuration changed during the read');
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
async function boxFor(located: Extract<Located, { kind: 'repository' }>, git: string, deadline: number): Promise<Repository | 'unsupported'> {
|
|
271
|
+
const box: Box = { git, root: located.root, env: environment(located.root, located.gitDir), deadline, overrides: [] };
|
|
272
|
+
if (!(await recentEnough(box))) throw new RpcError(ERROR.unavailable, FIXED.tooOld, { reason: 'git_unavailable' });
|
|
273
|
+
const { stdout } = await runGit(box, CONFIG_READ, { okCodes: [0, 1] });
|
|
274
|
+
let remoteUrl: string | null = null;
|
|
275
|
+
let worktree: string | undefined;
|
|
276
|
+
let format = 'sha1';
|
|
277
|
+
const neutral = new Set<string>();
|
|
278
|
+
for (const record of stdout.toString('utf8').split('\0')) {
|
|
279
|
+
if (!record) continue;
|
|
280
|
+
const newline = record.indexOf('\n');
|
|
281
|
+
const key = newline < 0 ? record : record.slice(0, newline);
|
|
282
|
+
const value = newline < 0 ? '' : record.slice(newline + 1);
|
|
283
|
+
if (key === 'remote.origin.url') remoteUrl = value;
|
|
284
|
+
else if (key === 'core.worktree') worktree = value;
|
|
285
|
+
else if (key === 'extensions.objectformat') format = value.toLowerCase();
|
|
286
|
+
else if (key.startsWith('filter.')) {
|
|
287
|
+
// `-c name=value` splits at the first '=': a driver whose name holds one cannot be emptied.
|
|
288
|
+
if (key.includes('=') || /[\0\n]/.test(key)) failed('a filter driver name cannot be neutralized');
|
|
289
|
+
neutral.add(key);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
if (worktree !== undefined) {
|
|
293
|
+
// A submodule's checkout names its own work tree; any other place is not this workspace.
|
|
294
|
+
const named = await realpath(path.resolve(located.gitDir, worktree)).catch(() => undefined);
|
|
295
|
+
if (named !== located.root) return 'unsupported';
|
|
296
|
+
}
|
|
297
|
+
const emptyTree = format === 'sha256' ? EMPTY_TREE.sha256 : format === 'sha1' ? EMPTY_TREE.sha1 : failed('unknown object format');
|
|
298
|
+
box.overrides = [...neutral].sort().flatMap(key => ['-c', `${key}=${key.endsWith('.required') ? 'false' : ''}`]);
|
|
299
|
+
return { ...box, remoteUrl, emptyTree, gitDir: located.gitDir, config: hash(stdout) };
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
// ---- Names, URLs, snapshots, cursors ----
|
|
303
|
+
|
|
304
|
+
/** A branch name as `git check-ref-format --branch` accepts one, never starting with '-' (no option, no revision syntax). */
|
|
305
|
+
export function validBranchName(name: unknown): name is string {
|
|
306
|
+
if (typeof name !== 'string' || !name || name.length > WORKSPACE_GIT.maxRefLength) return false;
|
|
307
|
+
if (name.startsWith('-') || name === 'HEAD' || name === '@') return false;
|
|
308
|
+
if (/[\x00-\x20\x7f~^:?*[\\]/.test(name) || name.includes('..') || name.includes('@{') || name.includes('//')) return false;
|
|
309
|
+
if (name.startsWith('/') || name.endsWith('/') || name.endsWith('.')) return false;
|
|
310
|
+
return name.split('/').every(part => part && !part.startsWith('.') && !part.endsWith('.lock'));
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* origin's URL as a client may see it: no user, password or token, no query or fragment; a local
|
|
315
|
+
* path, a `file:` URL or anything unrecognized is null.
|
|
316
|
+
*/
|
|
317
|
+
export function scrubRemoteUrl(url: string | null): string | null {
|
|
318
|
+
const value = url?.trim();
|
|
319
|
+
if (!value || /[\x00-\x1f\x7f]/.test(value) || value.includes('::')) return null;
|
|
320
|
+
const scheme = /^([A-Za-z][A-Za-z0-9+.-]*):\/\/([^/?#]*)([^?#]*)/.exec(value);
|
|
321
|
+
if (scheme) {
|
|
322
|
+
const protocol = scheme[1]!.toLowerCase();
|
|
323
|
+
if (protocol === 'file') return null;
|
|
324
|
+
const host = scheme[2]!.slice(scheme[2]!.lastIndexOf('@') + 1);
|
|
325
|
+
return host ? `${protocol}://${host}${scheme[3]}` : null;
|
|
326
|
+
}
|
|
327
|
+
if (/^file:/i.test(value)) return null;
|
|
328
|
+
// scp-like `[user[:password]@]host:path`: a colon before any slash, and not a Windows drive
|
|
329
|
+
// letter. User information is everything up to the last '@' before the first slash.
|
|
330
|
+
const slash = value.indexOf('/');
|
|
331
|
+
const at = (slash < 0 ? value : value.slice(0, slash)).lastIndexOf('@');
|
|
332
|
+
const scp = /^([^@/:]+):(.*)$/.exec(at < 0 ? value : value.slice(at + 1));
|
|
333
|
+
if (scp && scp[1]!.length > 1 && !scp[2]!.startsWith('\\')) return `${scp[1]}:${scp[2]}`;
|
|
334
|
+
return null;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
const hash = (...parts: Array<string | Buffer>) => {
|
|
338
|
+
const digest = createHash('sha256');
|
|
339
|
+
for (const part of parts) { digest.update(typeof part === 'string' ? part : part); digest.update('\0'); }
|
|
340
|
+
return digest.digest('hex');
|
|
341
|
+
};
|
|
342
|
+
|
|
343
|
+
/** A file's identity and change marks for a snapshot, or a mark that it is not there. */
|
|
344
|
+
async function statMark(file: string): Promise<string> {
|
|
345
|
+
try {
|
|
346
|
+
const info = await lstat(file, { bigint: true });
|
|
347
|
+
return `${info.dev}:${info.ino}:${info.mode}:${info.size}:${info.mtimeNs}:${info.ctimeNs}`;
|
|
348
|
+
} catch {
|
|
349
|
+
return '-';
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
type CursorBody = { offset: number; snapshot: string };
|
|
354
|
+
|
|
355
|
+
function cursorScope(workspaceId: string, compare: GitCompare, relative: string | undefined, body: CursorBody) {
|
|
356
|
+
return JSON.stringify([workspaceId, compare.mode, compare.baseRef ?? null, compare.ignoreWhitespace === true, relative ?? null, body.snapshot, body.offset]);
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
// ---- Parsing Git's answers ----
|
|
360
|
+
|
|
361
|
+
/** An all-zero mode (no such side) or object id (the work tree, not hashed). */
|
|
362
|
+
const ZERO = /^0+$/;
|
|
363
|
+
const GITLINK = '160000';
|
|
364
|
+
const SYMLINK = '120000';
|
|
365
|
+
|
|
366
|
+
type Entry = {
|
|
367
|
+
path: string; oldPath?: string; change: GitChange;
|
|
368
|
+
srcMode: string; dstMode: string; srcOid: string; dstOid: string;
|
|
369
|
+
untracked?: true; conflicted?: true;
|
|
370
|
+
};
|
|
371
|
+
|
|
372
|
+
const RAW_CHANGE: Record<string, GitChange> = { A: 'added', C: 'copied', D: 'deleted', M: 'modified', R: 'renamed', T: 'typechange', U: 'conflicted' };
|
|
373
|
+
|
|
374
|
+
/** `git diff --raw -z` records: `:<srcmode> <dstmode> <srcoid> <dstoid> <status>\0<path>\0[<path>\0]`. */
|
|
375
|
+
function parseRaw(output: Buffer): Entry[] {
|
|
376
|
+
const fields = output.toString('utf8').split('\0');
|
|
377
|
+
const entries: Entry[] = [];
|
|
378
|
+
const conflicted = new Set<string>();
|
|
379
|
+
for (let index = 0; index < fields.length;) {
|
|
380
|
+
const meta = fields[index++];
|
|
381
|
+
if (!meta) continue;
|
|
382
|
+
const match = /^:(\d{6}) (\d{6}) ([0-9a-f]+) ([0-9a-f]+) ([ACDMRTUX])(\d*)$/.exec(meta);
|
|
383
|
+
if (!match) failed('unexpected change list');
|
|
384
|
+
const [, srcMode, dstMode, srcOid, dstOid, letter] = match!;
|
|
385
|
+
const first = fields[index++];
|
|
386
|
+
const second = letter === 'R' || letter === 'C' ? fields[index++] : undefined;
|
|
387
|
+
if (!first || (second !== undefined && !second)) failed('unexpected change list');
|
|
388
|
+
if (letter === 'X') continue;
|
|
389
|
+
const entry: Entry = second !== undefined
|
|
390
|
+
? { path: second, oldPath: first!, change: RAW_CHANGE[letter!]!, srcMode: srcMode!, dstMode: dstMode!, srcOid: srcOid!, dstOid: dstOid! }
|
|
391
|
+
: { path: first!, change: RAW_CHANGE[letter!]!, srcMode: srcMode!, dstMode: dstMode!, srcOid: srcOid!, dstOid: dstOid! };
|
|
392
|
+
if (letter === 'U') { conflicted.add(entry.path); entry.conflicted = true; }
|
|
393
|
+
entries.push(entry);
|
|
394
|
+
}
|
|
395
|
+
// Index against work tree in a conflict: Git lists the path unmerged and again against stage 2
|
|
396
|
+
// ("ours"); one entry, conflicted, compared as stage 2 against the work tree.
|
|
397
|
+
const merged: Entry[] = [];
|
|
398
|
+
for (const entry of entries) {
|
|
399
|
+
if (!conflicted.has(entry.path)) { merged.push(entry); continue; }
|
|
400
|
+
if (entry.conflicted) {
|
|
401
|
+
const ours = entries.find(other => other.path === entry.path && !other.conflicted);
|
|
402
|
+
merged.push(ours ? { ...ours, change: 'conflicted', conflicted: true } : entry);
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
return merged;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/** Git's C-style quoting of a header path (quote.c, core.quotePath=false). */
|
|
409
|
+
function quoted(name: string): string {
|
|
410
|
+
if (!/[\x00-\x1f"\\\x7f]/.test(name)) return name;
|
|
411
|
+
let out = '"';
|
|
412
|
+
for (const byte of Buffer.from(name, 'utf8')) {
|
|
413
|
+
const letter = { 7: 'a', 8: 'b', 9: 't', 10: 'n', 11: 'v', 12: 'f', 13: 'r' }[byte];
|
|
414
|
+
if (letter) out += `\\${letter}`;
|
|
415
|
+
else if (byte === 0x22) out += '\\"';
|
|
416
|
+
else if (byte === 0x5c) out += '\\\\';
|
|
417
|
+
else if (byte < 0x20 || byte === 0x7f) out += `\\${byte.toString(8).padStart(3, '0')}`;
|
|
418
|
+
else out += String.fromCharCode(byte);
|
|
419
|
+
}
|
|
420
|
+
// Bytes over 0x7f were added one by one: put the UTF-8 back together.
|
|
421
|
+
return Buffer.from(`${out}"`, 'latin1').toString('utf8');
|
|
422
|
+
}
|
|
423
|
+
const headerOf = (entry: Entry) => `diff --git ${quoted(`a/${entry.oldPath ?? entry.path}`)} ${quoted(`b/${entry.path}`)}`;
|
|
424
|
+
|
|
425
|
+
type Section = { header: string; bytes: number; chunks: Buffer[] };
|
|
426
|
+
|
|
427
|
+
/**
|
|
428
|
+
* A patch stream cut into its files' sections, each begun by a `diff --git ` line (no content
|
|
429
|
+
* line can start so: they start with ' ', '+', '-' or '\'). A section keeps at most
|
|
430
|
+
* `keepBytes` (then only its size is counted); the stream at most GIT_READ.maxPatchStreamBytes.
|
|
431
|
+
*/
|
|
432
|
+
function sectionsOf(keepBytes: number) {
|
|
433
|
+
const sections: Section[] = [];
|
|
434
|
+
let buffer: Buffer = Buffer.alloc(0);
|
|
435
|
+
let atLineStart = true;
|
|
436
|
+
let inHeader = false;
|
|
437
|
+
let total = 0;
|
|
438
|
+
const MARK = Buffer.from('diff --git ');
|
|
439
|
+
// A conflicted path compared as stage 2 is announced so, outside any section.
|
|
440
|
+
const UNMERGED = Buffer.from('* Unmerged path ');
|
|
441
|
+
let skipping = false;
|
|
442
|
+
const append = (bytes: Buffer) => {
|
|
443
|
+
const current = sections.at(-1);
|
|
444
|
+
if (!current) failed('patch output before a header');
|
|
445
|
+
current!.bytes += bytes.length;
|
|
446
|
+
if (current!.bytes <= keepBytes + 1) current!.chunks.push(Buffer.from(bytes));
|
|
447
|
+
};
|
|
448
|
+
function consume(ended: boolean) {
|
|
449
|
+
let at = 0;
|
|
450
|
+
for (;;) {
|
|
451
|
+
if (inHeader) {
|
|
452
|
+
const newline = buffer.indexOf(10, at);
|
|
453
|
+
if (newline < 0) { if (buffer.length - at > 64 * 1024) failed('patch header over its bound'); break; }
|
|
454
|
+
const line = buffer.subarray(at, newline);
|
|
455
|
+
sections.push({ header: line.toString('utf8'), bytes: line.length + 1, chunks: [] });
|
|
456
|
+
at = newline + 1;
|
|
457
|
+
inHeader = false;
|
|
458
|
+
atLineStart = true;
|
|
459
|
+
continue;
|
|
460
|
+
}
|
|
461
|
+
if (at >= buffer.length) break;
|
|
462
|
+
if (skipping) {
|
|
463
|
+
const newline = buffer.indexOf(10, at);
|
|
464
|
+
if (newline < 0) { if (buffer.length - at > 64 * 1024) failed('patch marker over its bound'); break; }
|
|
465
|
+
at = newline + 1;
|
|
466
|
+
skipping = false;
|
|
467
|
+
atLineStart = true;
|
|
468
|
+
continue;
|
|
469
|
+
}
|
|
470
|
+
if (atLineStart) {
|
|
471
|
+
if (buffer.length - at < UNMERGED.length && !ended) break;
|
|
472
|
+
if (buffer.subarray(at, at + MARK.length).equals(MARK)) { inHeader = true; continue; }
|
|
473
|
+
if (buffer.subarray(at, at + UNMERGED.length).equals(UNMERGED)) { skipping = true; continue; }
|
|
474
|
+
atLineStart = false;
|
|
475
|
+
}
|
|
476
|
+
const newline = buffer.indexOf(10, at);
|
|
477
|
+
if (newline < 0) { append(buffer.subarray(at)); at = buffer.length; break; }
|
|
478
|
+
append(buffer.subarray(at, newline + 1));
|
|
479
|
+
at = newline + 1;
|
|
480
|
+
atLineStart = true;
|
|
481
|
+
}
|
|
482
|
+
buffer = buffer.subarray(at);
|
|
483
|
+
if (ended && (buffer.length || inHeader || skipping)) failed('unterminated patch');
|
|
484
|
+
}
|
|
485
|
+
return {
|
|
486
|
+
sections,
|
|
487
|
+
write(chunk: Buffer) {
|
|
488
|
+
total += chunk.length;
|
|
489
|
+
if (total > GIT_READ.maxPatchStreamBytes) failed('patch output over its bound');
|
|
490
|
+
buffer = buffer.length ? Buffer.concat([buffer, chunk]) : chunk;
|
|
491
|
+
consume(false);
|
|
492
|
+
},
|
|
493
|
+
end() { consume(true); },
|
|
494
|
+
};
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
const DECODER = new TextDecoder('utf-8', { fatal: false, ignoreBOM: true });
|
|
498
|
+
|
|
499
|
+
/** One file's section as the contract's structure: binary per Git, or its hunks. */
|
|
500
|
+
function parseSection(section: Section): { binary: boolean; hunks: GitDiffHunk[]; additions: number; deletions: number } {
|
|
501
|
+
const lines = DECODER.decode(Buffer.concat(section.chunks)).split('\n');
|
|
502
|
+
const hunks: GitDiffHunk[] = [];
|
|
503
|
+
let additions = 0;
|
|
504
|
+
let deletions = 0;
|
|
505
|
+
let hunk: GitDiffHunk | undefined;
|
|
506
|
+
let oldLeft = 0;
|
|
507
|
+
let newLeft = 0;
|
|
508
|
+
for (const line of lines) {
|
|
509
|
+
if (!hunk && (line === 'GIT binary patch' || (line.startsWith('Binary files ') && line.endsWith(' differ')))) return { binary: true, hunks: [], additions: 0, deletions: 0 };
|
|
510
|
+
const header = /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/.exec(line);
|
|
511
|
+
if (header && oldLeft <= 0 && newLeft <= 0) {
|
|
512
|
+
const context = line.slice(header[0].length).replace(/^ /, '');
|
|
513
|
+
hunk = { oldStart: Number(header[1]), oldCount: Number(header[2] ?? 1), newStart: Number(header[3]), newCount: Number(header[4] ?? 1), ...(context ? { context } : {}), lines: [] };
|
|
514
|
+
oldLeft = hunk.oldCount;
|
|
515
|
+
newLeft = hunk.newCount;
|
|
516
|
+
hunks.push(hunk);
|
|
517
|
+
continue;
|
|
518
|
+
}
|
|
519
|
+
if (!hunk) continue;
|
|
520
|
+
if (line.startsWith('\\')) { const last = hunk.lines.at(-1); if (last) last.noNewline = true; continue; }
|
|
521
|
+
if (oldLeft <= 0 && newLeft <= 0) continue;
|
|
522
|
+
if (line.startsWith('+')) { hunk.lines.push({ type: 'add', content: line.slice(1) }); additions++; newLeft--; }
|
|
523
|
+
else if (line.startsWith('-')) { hunk.lines.push({ type: 'remove', content: line.slice(1) }); deletions++; oldLeft--; }
|
|
524
|
+
else if (line.startsWith(' ') || line === '') { hunk.lines.push({ type: 'context', content: line.slice(1) }); oldLeft--; newLeft--; }
|
|
525
|
+
}
|
|
526
|
+
return { binary: false, hunks, additions, deletions };
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
/** An untracked file's content as one hunk of added lines. */
|
|
530
|
+
function addedHunks(text: string): { hunks: GitDiffHunk[]; additions: number } {
|
|
531
|
+
if (!text) return { hunks: [], additions: 0 };
|
|
532
|
+
const endsWithNewline = text.endsWith('\n');
|
|
533
|
+
const parts = (endsWithNewline ? text.slice(0, -1) : text).split('\n');
|
|
534
|
+
const lines = parts.map(content => ({ type: 'add' as const, content }));
|
|
535
|
+
if (!endsWithNewline) (lines.at(-1) as { noNewline?: true }).noNewline = true;
|
|
536
|
+
return { hunks: [{ oldStart: 0, oldCount: 0, newStart: 1, newCount: lines.length, lines }], additions: lines.length };
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
const holdsKey = (bytes: Buffer) => PRIVATE_KEY.test(bytes.toString('latin1'));
|
|
540
|
+
|
|
541
|
+
/**
|
|
542
|
+
* A file as it is answered, and what it costs a page: its JSON size. The limits hold for what is
|
|
543
|
+
* sent, not for Git's bytes: decoding replaces each invalid UTF-8 byte with U+FFFD (three bytes),
|
|
544
|
+
* and every line adds its JSON structure. Text whose decoded content is over
|
|
545
|
+
* WORKSPACE_GIT.maxFileBytes, or whose answer alone would be over maxTotalBytes, is too_large.
|
|
546
|
+
*/
|
|
547
|
+
function sized(file: GitDiffFile): { file: GitDiffFile; cost: number } {
|
|
548
|
+
const cost = (value: GitDiffFile) => Buffer.byteLength(JSON.stringify(value)) + 1;
|
|
549
|
+
if (file.kind !== 'text') return { file, cost: cost(file) };
|
|
550
|
+
let content = 0;
|
|
551
|
+
for (const hunk of file.hunks) for (const line of hunk.lines) content += Buffer.byteLength(line.content, 'utf8');
|
|
552
|
+
const bytes = cost(file);
|
|
553
|
+
if (content <= WORKSPACE_GIT.maxFileBytes && bytes <= PAGE_BYTES) return { file, cost: bytes };
|
|
554
|
+
const large: GitDiffFile = { ...file, kind: 'too_large', hunks: [] };
|
|
555
|
+
return { file: large, cost: cost(large) };
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
/** What one page's files may take as JSON: WORKSPACE_GIT.maxTotalBytes, less room for the rest of the answer. */
|
|
559
|
+
const PAGE_BYTES = WORKSPACE_GIT.maxTotalBytes - 16 * 1024;
|
|
560
|
+
|
|
561
|
+
// ---- The service ----
|
|
562
|
+
|
|
563
|
+
export type GitReaderOptions = {
|
|
564
|
+
registry: { resolve(workspaceId: string): Promise<string> };
|
|
565
|
+
/** Which git: a name looked up on PATH (outside the workspace), or an absolute path (tests). */
|
|
566
|
+
binary?: string;
|
|
567
|
+
timeoutMs?: number;
|
|
568
|
+
log?: (message: string, fields: Record<string, unknown>) => void;
|
|
569
|
+
/** Tests only: called after a window's contents were scanned and before Git writes their patch, where a racing writer could change a file. */
|
|
570
|
+
probe?: (step: 'scanned') => Promise<void> | void;
|
|
571
|
+
};
|
|
572
|
+
|
|
573
|
+
type DiffParams = { workspaceId: string; compare: GitCompare; path?: string; cursor?: string };
|
|
574
|
+
|
|
575
|
+
export function createGitReader({ registry, binary = 'git', timeoutMs = GIT_READ.timeoutMs, log = () => {}, probe }: GitReaderOptions) {
|
|
576
|
+
const key = randomBytes(32);
|
|
577
|
+
const mac = (scope: string) => createHmac('sha256', key).update(scope).digest('base64url').slice(0, 32);
|
|
578
|
+
|
|
579
|
+
/** The workspace's real path, or the registry's refusal (its messages are fixed). */
|
|
580
|
+
async function workspaceRoot(workspaceId: unknown): Promise<string> {
|
|
581
|
+
if (typeof workspaceId !== 'string' || !workspaceId) throw new RpcError(RPC_ERROR.invalidParams, 'workspaceId is required', { reason: 'invalid_params' });
|
|
582
|
+
try {
|
|
583
|
+
return await registry.resolve(workspaceId);
|
|
584
|
+
} catch (error) {
|
|
585
|
+
if (error instanceof RegistryError) throw new RpcError(error.code, error.message, error.data);
|
|
586
|
+
if (missing(error)) throw new RpcError(ERROR.notFound, FIXED.notFound, { reason: 'not_found' });
|
|
587
|
+
throw error;
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
async function open(workspaceId: unknown): Promise<{ state: 'not_repository' | 'unsupported' } | { state: 'repository'; repo: Repository }> {
|
|
592
|
+
const root = await workspaceRoot(workspaceId);
|
|
593
|
+
const deadline = Date.now() + timeoutMs;
|
|
594
|
+
const located = await locate(root);
|
|
595
|
+
if (located.kind !== 'repository') return { state: located.kind };
|
|
596
|
+
let git: string | undefined;
|
|
597
|
+
try { git = resolveGit(binary, undefined, root); } catch (error) { if (error instanceof IgnoreUnknown) failed('the workspace cannot be examined'); throw error; }
|
|
598
|
+
if (!git) throw new RpcError(ERROR.unavailable, FIXED.unavailable, { reason: 'git_unavailable' });
|
|
599
|
+
const repo = await boxFor(located, git, deadline);
|
|
600
|
+
return repo === 'unsupported' ? { state: 'unsupported' } : { state: 'repository', repo };
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
/** The commit an exact full ref names, or undefined. */
|
|
604
|
+
async function commitOf(repo: Repository, ref: string): Promise<string | undefined> {
|
|
605
|
+
const { stdout, code } = await runGit(repo, ['rev-parse', '--verify', '--quiet', '--end-of-options', `${ref}^{commit}`], { okCodes: [0, 1, 128] });
|
|
606
|
+
const oid = stdout.toString('utf8').trim();
|
|
607
|
+
return code === 0 && /^[0-9a-f]{40,64}$/.test(oid) ? oid : undefined;
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
/** A base branch resolved as origin's copy, else the local branch: its full ref and commit. */
|
|
611
|
+
async function resolveBase(repo: Repository, name: string): Promise<{ ref: string; oid: string } | undefined> {
|
|
612
|
+
for (const ref of [`refs/remotes/origin/${name}`, `refs/heads/${name}`]) {
|
|
613
|
+
const oid = await commitOf(repo, ref);
|
|
614
|
+
if (oid) return { ref, oid };
|
|
615
|
+
}
|
|
616
|
+
return undefined;
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
/** The default base branch: origin's HEAD, else main, else master (local or origin's). */
|
|
620
|
+
async function defaultBase(repo: Repository): Promise<string | null> {
|
|
621
|
+
const candidates = ['refs/remotes/origin/HEAD', 'refs/heads/main', 'refs/remotes/origin/main', 'refs/heads/master', 'refs/remotes/origin/master'];
|
|
622
|
+
const { stdout } = await runGit(repo, ['for-each-ref', '--format=%(refname)%00%(symref)', '--', ...candidates]);
|
|
623
|
+
const found = new Map<string, string>();
|
|
624
|
+
for (const line of stdout.toString('utf8').split('\n')) {
|
|
625
|
+
const [ref, symref = ''] = line.split('\0');
|
|
626
|
+
if (ref && candidates.includes(ref)) found.set(ref, symref);
|
|
627
|
+
}
|
|
628
|
+
const remoteHead = found.get('refs/remotes/origin/HEAD');
|
|
629
|
+
if (remoteHead?.startsWith('refs/remotes/origin/')) {
|
|
630
|
+
const name = remoteHead.slice('refs/remotes/origin/'.length);
|
|
631
|
+
if (validBranchName(name)) return name;
|
|
632
|
+
}
|
|
633
|
+
for (const name of ['main', 'master']) if (found.has(`refs/heads/${name}`) || found.has(`refs/remotes/origin/${name}`)) return name;
|
|
634
|
+
return null;
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
const emptyStatus = (state: GitStatus['state']): GitStatus => ({
|
|
638
|
+
state, ...(state === 'unsupported' ? { reason: 'not_repository_root' } : {}), branch: null, detached: false, head: null, upstream: null, baseRef: null,
|
|
639
|
+
aheadBehind: null, hasRemote: false, remoteUrl: null, dirty: false, files: [], hiddenCount: 0, truncated: false, snapshot: '',
|
|
640
|
+
});
|
|
641
|
+
|
|
642
|
+
async function status(workspaceId: unknown): Promise<GitStatus> {
|
|
643
|
+
const opened = await open(workspaceId);
|
|
644
|
+
if (opened.state !== 'repository') return emptyStatus(opened.state);
|
|
645
|
+
const { repo } = opened;
|
|
646
|
+
const files: GitStatusFile[] = [];
|
|
647
|
+
let hiddenCount = 0;
|
|
648
|
+
let truncated = false;
|
|
649
|
+
let dirty = false;
|
|
650
|
+
const branch: { oid?: string; head?: string; upstream?: string; ab?: [number, number] } = {};
|
|
651
|
+
const digest = createHash('sha256');
|
|
652
|
+
let pending = '';
|
|
653
|
+
let pendingRename: string[] | undefined;
|
|
654
|
+
const take = (row: GitStatusFile) => {
|
|
655
|
+
dirty = true;
|
|
656
|
+
if (blockedFilePath(row.path) || (row.oldPath && blockedFilePath(row.oldPath)) || [...row.path].length > 4096 || [...(row.oldPath ?? '')].length > 4096) { hiddenCount++; return false; }
|
|
657
|
+
if (files.length >= WORKSPACE_GIT.maxStatusFiles) { truncated = true; return true; }
|
|
658
|
+
files.push(row);
|
|
659
|
+
return false;
|
|
660
|
+
};
|
|
661
|
+
const record = (field: string): boolean => {
|
|
662
|
+
if (pendingRename) {
|
|
663
|
+
const [kind, xy] = pendingRename;
|
|
664
|
+
pendingRename = undefined;
|
|
665
|
+
return take({ path: kind!, oldPath: field, change: xy!.includes('C') ? 'copied' : 'renamed', staged: xy![0] !== '.', unstaged: xy![1] !== '.' });
|
|
666
|
+
}
|
|
667
|
+
if (field.startsWith('# ')) {
|
|
668
|
+
const [name, ...rest] = field.slice(2).split(' ');
|
|
669
|
+
const value = rest.join(' ');
|
|
670
|
+
if (name === 'branch.oid') branch.oid = value;
|
|
671
|
+
else if (name === 'branch.head') branch.head = value;
|
|
672
|
+
else if (name === 'branch.upstream') branch.upstream = value;
|
|
673
|
+
else if (name === 'branch.ab') { const ab = /^\+(\d+) -(\d+)$/.exec(value); if (ab) branch.ab = [Number(ab[1]), Number(ab[2])]; }
|
|
674
|
+
return false;
|
|
675
|
+
}
|
|
676
|
+
const kind = field[0];
|
|
677
|
+
if (kind === '1' || kind === '2' || kind === 'u') {
|
|
678
|
+
const parts = field.split(' ');
|
|
679
|
+
const count = kind === '1' ? 8 : kind === '2' ? 9 : 10;
|
|
680
|
+
const xy = parts[1] ?? '..';
|
|
681
|
+
const rowPath = parts.slice(count).join(' ');
|
|
682
|
+
if (!rowPath) failed('unexpected status');
|
|
683
|
+
if (kind === '2') { pendingRename = [rowPath, xy]; return false; }
|
|
684
|
+
if (kind === 'u') return take({ path: rowPath, change: 'conflicted', staged: true, unstaged: true });
|
|
685
|
+
const [x, y] = [xy[0], xy[1]];
|
|
686
|
+
const change: GitChange = x === 'A' ? 'added' : x === 'D' || y === 'D' ? 'deleted' : x === 'T' || y === 'T' ? 'typechange' : 'modified';
|
|
687
|
+
return take({ path: rowPath, change, staged: x !== '.', unstaged: y !== '.' });
|
|
688
|
+
}
|
|
689
|
+
if (kind === '?') { const rowPath = field.slice(2).replace(/\/$/, ''); return rowPath ? take({ path: rowPath, change: 'untracked', staged: false, unstaged: true }) : false; }
|
|
690
|
+
if (kind === '!') return false;
|
|
691
|
+
failed('unexpected status');
|
|
692
|
+
return false;
|
|
693
|
+
};
|
|
694
|
+
let seen = 0;
|
|
695
|
+
const { stopped } = await runGit(repo, ['status', '--porcelain=v2', '-z', '--branch', '--ahead-behind', '--untracked-files=all', '--ignored=no', '--ignore-submodules=dirty', '--find-renames'], {
|
|
696
|
+
onData: chunk => {
|
|
697
|
+
seen += chunk.length;
|
|
698
|
+
digest.update(chunk);
|
|
699
|
+
const fields = (pending + chunk.toString('utf8')).split('\0');
|
|
700
|
+
pending = fields.pop()!;
|
|
701
|
+
for (const field of fields) if (field && record(field)) return true;
|
|
702
|
+
if (seen > GIT_READ.maxListBytes) { truncated = true; return true; }
|
|
703
|
+
return false;
|
|
704
|
+
},
|
|
705
|
+
});
|
|
706
|
+
if (!stopped && pending) failed('unterminated status');
|
|
707
|
+
const head = branch.oid && /^[0-9a-f]{40,64}$/.test(branch.oid) ? branch.oid : null;
|
|
708
|
+
const detached = branch.head === '(detached)';
|
|
709
|
+
const current = !detached && branch.head && validBranchName(branch.head) ? branch.head : null;
|
|
710
|
+
const upstream = current && branch.upstream && branch.ab ? { ref: branch.upstream, ahead: branch.ab[0], behind: branch.ab[1] } : null;
|
|
711
|
+
const baseRef = await defaultBase(repo);
|
|
712
|
+
let aheadBehind: GitStatus['aheadBehind'] = null;
|
|
713
|
+
if (baseRef && current && current !== baseRef && head) {
|
|
714
|
+
const base = await resolveBase(repo, baseRef);
|
|
715
|
+
if (base) {
|
|
716
|
+
const { stdout } = await runGit(repo, ['rev-list', '--left-right', '--count', '--end-of-options', `${base.oid}...${head}`], { okCodes: [0, 128] });
|
|
717
|
+
const counts = stdout.toString('utf8').trim().split(/\s+/).map(Number);
|
|
718
|
+
if (counts.length === 2 && counts.every(Number.isSafeInteger)) aheadBehind = { behind: counts[0]!, ahead: counts[1]! };
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
digest.update(await statMark(path.join(repo.gitDir, 'index')));
|
|
722
|
+
await sameConfig(repo);
|
|
723
|
+
return {
|
|
724
|
+
state: 'repository', branch: current, detached, head, upstream, baseRef, aheadBehind, hasRemote: repo.remoteUrl !== null,
|
|
725
|
+
remoteUrl: scrubRemoteUrl(repo.remoteUrl), dirty, files, hiddenCount, truncated, snapshot: `s1.${digest.digest('hex').slice(0, 40)}`,
|
|
726
|
+
};
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
/** The untracked files Git does not ignore, at most `limit` that are not blocked (more: `more`). */
|
|
730
|
+
async function untrackedOf(repo: Repository, relative: string | undefined, limit: number) {
|
|
731
|
+
const found: string[] = [];
|
|
732
|
+
let hidden = 0;
|
|
733
|
+
let more = false;
|
|
734
|
+
let pending = '';
|
|
735
|
+
const { stopped } = await runGit(repo, ['ls-files', '-z', '--others', '--exclude-standard', '--', ...(relative ? [relative] : [])], {
|
|
736
|
+
onData: chunk => {
|
|
737
|
+
const fields = (pending + chunk.toString('utf8')).split('\0');
|
|
738
|
+
pending = fields.pop()!;
|
|
739
|
+
for (const name of fields) {
|
|
740
|
+
// A nested repository is listed as its directory: not a file.
|
|
741
|
+
if (!name || name.endsWith('/')) continue;
|
|
742
|
+
if (relative && name !== relative) continue;
|
|
743
|
+
if (blockedFilePath(name) || [...name].length > 4096) { hidden++; continue; }
|
|
744
|
+
if (found.length >= limit) { more = true; return true; }
|
|
745
|
+
found.push(name);
|
|
746
|
+
}
|
|
747
|
+
return false;
|
|
748
|
+
},
|
|
749
|
+
});
|
|
750
|
+
if (!stopped && pending) failed('unterminated untracked list');
|
|
751
|
+
return { found, hidden, more };
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
async function diff(params: DiffParams): Promise<GitDiffPage> {
|
|
755
|
+
const { workspaceId, compare, path: relative, cursor } = params ?? ({} as DiffParams);
|
|
756
|
+
const mode = compare?.mode;
|
|
757
|
+
if (!['uncommitted', 'staged', 'unstaged', 'base'].includes(mode)) throw new RpcError(RPC_ERROR.invalidParams, 'compare.mode is not one alpd knows', { reason: 'invalid_compare' });
|
|
758
|
+
if (compare.baseRef !== undefined && (mode !== 'base' || !validBranchName(compare.baseRef))) throw new RpcError(RPC_ERROR.invalidParams, FIXED.invalidCompare, { reason: 'invalid_compare' });
|
|
759
|
+
if (relative !== undefined) {
|
|
760
|
+
try { relativeSegments(relative); } catch (error) {
|
|
761
|
+
if (error instanceof FileAccessError) throw new RpcError(error.code, error.message, { reason: error.reason });
|
|
762
|
+
throw error;
|
|
763
|
+
}
|
|
764
|
+
}
|
|
765
|
+
let body: CursorBody | undefined;
|
|
766
|
+
if (cursor !== undefined) {
|
|
767
|
+
const match = typeof cursor === 'string' ? /^c1\.(\d{1,9})\.(s1\.[0-9a-f]{40})\.([A-Za-z0-9_-]{32})$/.exec(cursor) : null;
|
|
768
|
+
if (!match) throw new RpcError(RPC_ERROR.invalidParams, FIXED.invalidCursor, { reason: 'invalid_cursor' });
|
|
769
|
+
body = { offset: Number(match[1]), snapshot: match[2]! };
|
|
770
|
+
const expected = Buffer.from(mac(cursorScope(workspaceId, compare, relative, body)));
|
|
771
|
+
if (!timingSafeEqual(expected, Buffer.from(match[3]!))) throw new RpcError(RPC_ERROR.invalidParams, FIXED.invalidCursor, { reason: 'invalid_cursor' });
|
|
772
|
+
}
|
|
773
|
+
const ignoreWhitespace = compare.ignoreWhitespace === true;
|
|
774
|
+
const opened = await open(workspaceId);
|
|
775
|
+
if (opened.state !== 'repository') throw new RpcError(ERROR.notFound, opened.state === 'unsupported' ? FIXED.notRoot : FIXED.notRepository, { reason: 'not_repository' });
|
|
776
|
+
const { repo } = opened;
|
|
777
|
+
const head = await commitOf(repo, 'HEAD');
|
|
778
|
+
const done = (files: GitDiffFile[], hiddenCount: number, snapshot: string, extra: { baseRef?: string; truncated?: boolean; nextCursor?: string } = {}): GitDiffPage => ({
|
|
779
|
+
compare: { mode, ...(extra.baseRef ? { baseRef: extra.baseRef } : {}), ignoreWhitespace }, snapshot, files, hiddenCount,
|
|
780
|
+
truncated: extra.truncated ?? false, ...(extra.nextCursor ? { nextCursor: extra.nextCursor } : {}),
|
|
781
|
+
});
|
|
782
|
+
|
|
783
|
+
// What is compared.
|
|
784
|
+
let range: string[];
|
|
785
|
+
let baseRef: string | undefined;
|
|
786
|
+
const marks: string[] = [mode, head ?? '-'];
|
|
787
|
+
if (mode === 'base') {
|
|
788
|
+
baseRef = compare.baseRef ?? (await defaultBase(repo)) ?? undefined;
|
|
789
|
+
const base = baseRef ? await resolveBase(repo, baseRef) : undefined;
|
|
790
|
+
if (compare.baseRef && !base) throw new RpcError(ERROR.notFound, FIXED.unknownRef, { reason: 'unknown_ref' });
|
|
791
|
+
const mergeBase = base && head ? (await runGit(repo, ['merge-base', '--end-of-options', base.oid, head], { okCodes: [0, 1] })).stdout.toString('utf8').trim() : '';
|
|
792
|
+
marks.push(baseRef ?? '-', base?.oid ?? '-', mergeBase || '-');
|
|
793
|
+
if (!/^[0-9a-f]{40,64}$/.test(mergeBase)) {
|
|
794
|
+
// No base branch, no commit yet, or unrelated histories: nothing committed to compare.
|
|
795
|
+
const snapshot = `s1.${hash(...marks).slice(0, 40)}`;
|
|
796
|
+
if (body && body.snapshot !== snapshot) throw new RpcError(ERROR.conflict, FIXED.stale, { reason: 'stale_snapshot' });
|
|
797
|
+
return done([], 0, snapshot, { baseRef });
|
|
798
|
+
}
|
|
799
|
+
range = [mergeBase, head!];
|
|
800
|
+
} else if (mode === 'staged') {
|
|
801
|
+
range = ['--cached', head ?? repo.emptyTree];
|
|
802
|
+
} else if (mode === 'uncommitted') {
|
|
803
|
+
range = [head ?? repo.emptyTree];
|
|
804
|
+
} else {
|
|
805
|
+
range = [];
|
|
806
|
+
}
|
|
807
|
+
const diffFlags = ['--no-color', '--no-ext-diff', '--no-textconv', '--ignore-submodules=dirty', '--submodule=short'];
|
|
808
|
+
const listed = await runGit(repo, ['diff', '--raw', '-z', '--no-abbrev', '-M', ...diffFlags, ...range, '--']);
|
|
809
|
+
const all = parseRaw(listed.stdout);
|
|
810
|
+
const worktree = mode === 'uncommitted' || mode === 'unstaged';
|
|
811
|
+
const untracked = worktree ? await untrackedOf(repo, relative, WORKSPACE_GIT.maxUntracked) : { found: [], hidden: 0, more: false };
|
|
812
|
+
|
|
813
|
+
// The snapshot: HEAD (and the base), the index, the change list, the untracked files, and the
|
|
814
|
+
// work tree files compared (their identity, size and times).
|
|
815
|
+
marks.push(hash(listed.stdout), untracked.found.join('\0'), String(untracked.more));
|
|
816
|
+
if (mode !== 'base') marks.push(await statMark(path.join(repo.gitDir, 'index')));
|
|
817
|
+
if (worktree) {
|
|
818
|
+
const files = [...all.filter(entry => entry.dstMode !== '000000' && ZERO.test(entry.dstOid)).map(entry => entry.path), ...untracked.found].slice(0, GIT_READ.maxSnapshotStats);
|
|
819
|
+
for (const file of files) marks.push(file, await statMark(path.join(repo.root, ...file.split('/'))));
|
|
820
|
+
}
|
|
821
|
+
const snapshot = `s1.${hash(...marks).slice(0, 40)}`;
|
|
822
|
+
if (body && body.snapshot !== snapshot) throw new RpcError(ERROR.conflict, FIXED.stale, { reason: 'stale_snapshot' });
|
|
823
|
+
|
|
824
|
+
// What may be shown: never a blocked path on either side; with `path`, that file only.
|
|
825
|
+
let hiddenCount = untracked.hidden;
|
|
826
|
+
const entries: Entry[] = [];
|
|
827
|
+
for (const entry of all) {
|
|
828
|
+
if (relative !== undefined && entry.path !== relative && entry.oldPath !== relative) continue;
|
|
829
|
+
if (blockedFilePath(entry.path) || (entry.oldPath && blockedFilePath(entry.oldPath)) || [...entry.path].length > 4096 || [...(entry.oldPath ?? '')].length > 4096) { hiddenCount++; continue; }
|
|
830
|
+
entries.push(entry);
|
|
831
|
+
}
|
|
832
|
+
for (const name of untracked.found) entries.push({ path: name, change: 'untracked', srcMode: '000000', dstMode: '100644', srcOid: '', dstOid: '', untracked: true });
|
|
833
|
+
const offset = body?.offset ?? 0;
|
|
834
|
+
if (offset > entries.length) throw new RpcError(RPC_ERROR.invalidParams, FIXED.invalidCursor, { reason: 'invalid_cursor' });
|
|
835
|
+
|
|
836
|
+
const files: GitDiffFile[] = [];
|
|
837
|
+
let used = 0;
|
|
838
|
+
let next: number | undefined;
|
|
839
|
+
for (let start = offset; start < entries.length && next === undefined;) {
|
|
840
|
+
const window = await windowOf(repo, entries, start, mode);
|
|
841
|
+
const results = await diffWindow(repo, window.entries, mode, range, diffFlags, ignoreWhitespace);
|
|
842
|
+
for (let index = 0; index < results.length; index++) {
|
|
843
|
+
const result = results[index];
|
|
844
|
+
if (result === undefined) continue; // no content change (a file only touched)
|
|
845
|
+
const cost = result.cost;
|
|
846
|
+
if (files.length && used + cost > PAGE_BYTES) { next = start + index; break; }
|
|
847
|
+
used += cost;
|
|
848
|
+
files.push(result.file);
|
|
849
|
+
}
|
|
850
|
+
start += window.entries.length;
|
|
851
|
+
}
|
|
852
|
+
await sameConfig(repo);
|
|
853
|
+
const nextCursor = next === undefined ? undefined : (() => {
|
|
854
|
+
const nextBody = { offset: next, snapshot };
|
|
855
|
+
return `c1.${next}.${snapshot}.${mac(cursorScope(workspaceId, compare, relative, nextBody))}`;
|
|
856
|
+
})();
|
|
857
|
+
return done(files, hiddenCount, snapshot, { baseRef, truncated: next !== undefined || untracked.more, nextCursor });
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
type Side = { oid: string } | { file: string } | undefined;
|
|
861
|
+
type Planned = Entry & { old: Side; new: Side; sizes: [number, number] };
|
|
862
|
+
|
|
863
|
+
/** The entries from `start` whose content one window reads: GIT_READ.windowFiles, GIT_READ.windowBytes (at least one). */
|
|
864
|
+
async function windowOf(repo: Repository, entries: Entry[], start: number, mode: GitCompare['mode']): Promise<{ entries: Planned[] }> {
|
|
865
|
+
const candidates = entries.slice(start, start + GIT_READ.windowFiles).map((entry): Planned => {
|
|
866
|
+
const special = entry.srcMode === GITLINK || entry.dstMode === GITLINK || entry.srcMode === SYMLINK || entry.dstMode === SYMLINK || entry.untracked || (entry.conflicted && mode === 'staged');
|
|
867
|
+
const old: Side = special || ZERO.test(entry.srcMode) ? undefined : { oid: entry.srcOid };
|
|
868
|
+
const now: Side = special || ZERO.test(entry.dstMode) ? undefined : ZERO.test(entry.dstOid) ? { file: entry.path } : { oid: entry.dstOid };
|
|
869
|
+
return { ...entry, old, new: now, sizes: [0, 0] };
|
|
870
|
+
});
|
|
871
|
+
const oids = [...new Set(candidates.flatMap(entry => [entry.old, entry.new]).flatMap(side => side && 'oid' in side ? [side.oid] : []))];
|
|
872
|
+
const sizes = new Map<string, number>();
|
|
873
|
+
if (oids.length) {
|
|
874
|
+
const { stdout } = await runGit(repo, ['cat-file', '--batch-check=%(objectname) %(objecttype) %(objectsize)'], { stdin: `${oids.join('\n')}\n` });
|
|
875
|
+
for (const line of stdout.toString('utf8').split('\n')) {
|
|
876
|
+
const [oid, type, size] = line.split(' ');
|
|
877
|
+
if (oid && type === 'blob' && size !== undefined) sizes.set(oid, Number(size));
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
const sizeOf = async (side: Side) => {
|
|
881
|
+
if (!side) return 0;
|
|
882
|
+
if ('oid' in side) return sizes.get(side.oid) ?? Number.MAX_SAFE_INTEGER;
|
|
883
|
+
try { return Number((await lstat(path.join(repo.root, ...side.file.split('/')))).size); } catch { return 0; }
|
|
884
|
+
};
|
|
885
|
+
const chosen: Planned[] = [];
|
|
886
|
+
let total = 0;
|
|
887
|
+
for (const entry of candidates) {
|
|
888
|
+
entry.sizes = [await sizeOf(entry.old), await sizeOf(entry.new)];
|
|
889
|
+
if (entry.untracked) entry.sizes = [0, await sizeOf({ file: entry.path })];
|
|
890
|
+
// A side over the scan bound is not read: the file is too_large.
|
|
891
|
+
const reads = entry.sizes.reduce((sum, size) => sum + (size > GIT_READ.maxScanBytes ? 0 : size), 0);
|
|
892
|
+
if (chosen.length && total + reads > GIT_READ.windowBytes) break;
|
|
893
|
+
total += reads;
|
|
894
|
+
chosen.push(entry);
|
|
895
|
+
}
|
|
896
|
+
return { entries: chosen };
|
|
897
|
+
}
|
|
898
|
+
|
|
899
|
+
/** Blob contents by id, each at most GIT_READ.maxScanBytes as batch-check sized them (`bytes` in all). */
|
|
900
|
+
async function blobs(repo: Repository, oids: string[], bytes: number): Promise<Map<string, Buffer>> {
|
|
901
|
+
const contents = new Map<string, Buffer>();
|
|
902
|
+
if (!oids.length) return contents;
|
|
903
|
+
const { stdout } = await runGit(repo, ['cat-file', '--batch'], { stdin: `${oids.join('\n')}\n`, maxBytes: bytes + oids.length * 200 + 1024 });
|
|
904
|
+
let at = 0;
|
|
905
|
+
while (at < stdout.length) {
|
|
906
|
+
const newline = stdout.indexOf(10, at);
|
|
907
|
+
if (newline < 0) failed('unterminated object');
|
|
908
|
+
const [oid, type, size] = stdout.subarray(at, newline).toString('utf8').split(' ');
|
|
909
|
+
at = newline + 1;
|
|
910
|
+
if (type === 'missing' || size === undefined) continue;
|
|
911
|
+
const length = Number(size);
|
|
912
|
+
if (!Number.isSafeInteger(length) || at + length > stdout.length) failed('unexpected object');
|
|
913
|
+
contents.set(oid!, stdout.subarray(at, at + length));
|
|
914
|
+
at += length + 1;
|
|
915
|
+
}
|
|
916
|
+
return contents;
|
|
917
|
+
}
|
|
918
|
+
|
|
919
|
+
type Result = { file: GitDiffFile; cost: number } | undefined;
|
|
920
|
+
|
|
921
|
+
async function diffWindow(repo: Repository, planned: Planned[], mode: GitCompare['mode'], range: string[], diffFlags: string[], ignoreWhitespace: boolean): Promise<Result[]> {
|
|
922
|
+
const shown = (entry: Entry, kind: GitDiffFile['kind'], extra: Partial<GitDiffFile> = {}, _cost = 0): Result => sized({ path: entry.path, ...(entry.oldPath ? { oldPath: entry.oldPath } : {}), change: entry.change, kind, additions: 0, deletions: 0, hunks: [], ...extra });
|
|
923
|
+
const readable = (size: number) => size <= GIT_READ.maxScanBytes;
|
|
924
|
+
const wanted = new Map<string, number>();
|
|
925
|
+
for (const entry of planned) {
|
|
926
|
+
for (const [side, size] of [[entry.old, entry.sizes[0]], [entry.new, entry.sizes[1]]] as const) if (side && 'oid' in side && readable(size)) wanted.set(side.oid, size);
|
|
927
|
+
}
|
|
928
|
+
const contents = await blobs(repo, [...wanted.keys()], [...wanted.values()].reduce((sum, size) => sum + size, 0));
|
|
929
|
+
const results: Result[] = new Array(planned.length).fill(undefined);
|
|
930
|
+
const patched: Array<{ index: number; entry: Planned; group: 'plain' | 'renamed' | 'ours' }> = [];
|
|
931
|
+
/** The digest of each work tree side as scanned: Git reads the file again for the patch. */
|
|
932
|
+
const scanned = new Map<string, string>();
|
|
933
|
+
for (const [index, entry] of planned.entries()) {
|
|
934
|
+
if (entry.srcMode === GITLINK || entry.dstMode === GITLINK) { results[index] = shown(entry, 'submodule'); continue; }
|
|
935
|
+
if (entry.srcMode === SYMLINK || entry.dstMode === SYMLINK) { results[index] = shown(entry, 'symlink'); continue; }
|
|
936
|
+
if (entry.untracked) { results[index] = await untrackedFile(repo, entry, shown); continue; }
|
|
937
|
+
if (entry.conflicted && mode === 'staged') { results[index] = shown(entry, 'text'); continue; }
|
|
938
|
+
if (entry.sizes.some(size => !readable(size))) { results[index] = shown(entry, 'too_large'); continue; }
|
|
939
|
+
const sides: Buffer[] = [];
|
|
940
|
+
let refused = false;
|
|
941
|
+
for (const side of [entry.old, entry.new]) {
|
|
942
|
+
if (!side) continue;
|
|
943
|
+
if ('oid' in side) { const bytes = contents.get(side.oid); if (bytes) sides.push(bytes); else refused = true; continue; }
|
|
944
|
+
try {
|
|
945
|
+
const bytes = await readConfinedFile(repo.root, side.file.split('/'), side.file, GIT_READ.maxScanBytes);
|
|
946
|
+
if (bytes === 'too_large') { results[index] = shown(entry, 'too_large'); refused = true; break; }
|
|
947
|
+
scanned.set(side.file, hash(bytes));
|
|
948
|
+
sides.push(bytes);
|
|
949
|
+
} catch {
|
|
950
|
+
// A link on the way, a second name, a change while read, gone: not shown.
|
|
951
|
+
refused = true;
|
|
952
|
+
}
|
|
953
|
+
}
|
|
954
|
+
if (results[index]) continue;
|
|
955
|
+
if (refused || sides.some(holdsKey)) { results[index] = shown(entry, 'withheld'); continue; }
|
|
956
|
+
if (sides.some(likelyBinary)) { results[index] = shown(entry, 'binary'); continue; }
|
|
957
|
+
patched.push({ index, entry, group: entry.conflicted ? 'ours' : entry.oldPath ? 'renamed' : 'plain' });
|
|
958
|
+
}
|
|
959
|
+
if (patched.length) await probe?.('scanned');
|
|
960
|
+
for (const group of ['plain', 'renamed', 'ours'] as const) {
|
|
961
|
+
const members = patched.filter(item => item.group === group);
|
|
962
|
+
// Command lines stay bounded: the paths go in chunks.
|
|
963
|
+
for (let from = 0; from < members.length;) {
|
|
964
|
+
const chunk: typeof members = [];
|
|
965
|
+
let bytes = 0;
|
|
966
|
+
while (from < members.length && (chunk.length === 0 || bytes < GIT_READ.maxArgBytes)) {
|
|
967
|
+
const item = members[from++]!;
|
|
968
|
+
bytes += Buffer.byteLength(item.entry.path) + Buffer.byteLength(item.entry.oldPath ?? '') + 2;
|
|
969
|
+
chunk.push(item);
|
|
970
|
+
}
|
|
971
|
+
const flags = [...diffFlags, '-p', '-U3', '--inter-hunk-context=0', '--src-prefix=a/', '--dst-prefix=b/', group === 'renamed' ? '-M' : '--no-renames', ...(ignoreWhitespace ? ['-w'] : [])];
|
|
972
|
+
const args = group === 'ours' ? ['diff', '-2', ...flags, '--'] : ['diff', ...flags, ...range, '--'];
|
|
973
|
+
const stream = sectionsOf(WORKSPACE_GIT.maxFileBytes);
|
|
974
|
+
await runGit(repo, [...args, ...chunk.flatMap(item => item.entry.oldPath ? [item.entry.oldPath, item.entry.path] : [item.entry.path])], { onData: data => { stream.write(data); } });
|
|
975
|
+
stream.end();
|
|
976
|
+
const byHeader = new Map(chunk.map(item => [headerOf(item.entry), item]));
|
|
977
|
+
const seen = new Set<string>();
|
|
978
|
+
for (const section of stream.sections) {
|
|
979
|
+
const item = byHeader.get(section.header);
|
|
980
|
+
// A section Git gave for no file asked, or twice: its pairing is not the list's.
|
|
981
|
+
if (!item || seen.has(section.header)) failed('a patch section for no listed file');
|
|
982
|
+
seen.add(section.header);
|
|
983
|
+
if (section.bytes > WORKSPACE_GIT.maxFileBytes) { results[item!.index] = shown(item!.entry, 'too_large'); continue; }
|
|
984
|
+
// The scan above read the content; Git read it again for this patch. A work tree file
|
|
985
|
+
// that changed in between, or a patch that shows a key, is withheld.
|
|
986
|
+
if (holdsKey(Buffer.concat(section.chunks)) || !(await unchanged(repo, item!.entry, scanned))) { results[item!.index] = shown(item!.entry, 'withheld'); continue; }
|
|
987
|
+
const parsed = parseSection(section);
|
|
988
|
+
results[item!.index] = parsed.binary
|
|
989
|
+
? shown(item!.entry, 'binary')
|
|
990
|
+
: shown(item!.entry, 'text', { additions: parsed.additions, deletions: parsed.deletions, hunks: parsed.hunks }, section.bytes);
|
|
991
|
+
}
|
|
992
|
+
// A file Git listed but gave no section for has no content change (its stat only changed).
|
|
993
|
+
}
|
|
994
|
+
}
|
|
995
|
+
return results;
|
|
996
|
+
}
|
|
997
|
+
|
|
998
|
+
/** Whether the entry's work tree side still has the content scanned before its patch (none: nothing to check). */
|
|
999
|
+
async function unchanged(repo: Repository, entry: Planned, scanned: Map<string, string>): Promise<boolean> {
|
|
1000
|
+
const side = entry.new;
|
|
1001
|
+
if (!side || !('file' in side)) return true;
|
|
1002
|
+
const before = scanned.get(side.file);
|
|
1003
|
+
if (before === undefined) return false;
|
|
1004
|
+
try {
|
|
1005
|
+
const bytes = await readConfinedFile(repo.root, side.file.split('/'), side.file, GIT_READ.maxScanBytes);
|
|
1006
|
+
return bytes !== 'too_large' && hash(bytes) === before;
|
|
1007
|
+
} catch {
|
|
1008
|
+
return false;
|
|
1009
|
+
}
|
|
1010
|
+
}
|
|
1011
|
+
|
|
1012
|
+
async function untrackedFile(repo: Repository, entry: Entry, shown: (entry: Entry, kind: GitDiffFile['kind'], extra?: Partial<GitDiffFile>, cost?: number) => Result): Promise<Result> {
|
|
1013
|
+
const file = path.join(repo.root, ...entry.path.split('/'));
|
|
1014
|
+
let info;
|
|
1015
|
+
try { info = await lstat(file); } catch { return undefined; }
|
|
1016
|
+
if (info.isSymbolicLink()) return shown(entry, 'symlink');
|
|
1017
|
+
if (!info.isFile()) return undefined;
|
|
1018
|
+
let bytes: Buffer | 'too_large';
|
|
1019
|
+
try {
|
|
1020
|
+
bytes = await readConfinedFile(repo.root, entry.path.split('/'), entry.path, WORKSPACE_GIT.maxFileBytes);
|
|
1021
|
+
} catch (error) {
|
|
1022
|
+
if (error instanceof FileAccessError && error.reason === 'not_found') return undefined;
|
|
1023
|
+
return shown(entry, 'withheld');
|
|
1024
|
+
}
|
|
1025
|
+
if (bytes === 'too_large') return shown(entry, 'too_large');
|
|
1026
|
+
if (holdsKey(bytes)) return shown(entry, 'withheld');
|
|
1027
|
+
if (likelyBinary(bytes)) return shown(entry, 'binary');
|
|
1028
|
+
const { hunks, additions } = addedHunks(DECODER.decode(bytes));
|
|
1029
|
+
return shown(entry, 'text', { additions, hunks }, bytes.length + additions);
|
|
1030
|
+
}
|
|
1031
|
+
|
|
1032
|
+
/** Runs a read, answering any failure that is not a refusal as `1000 git_failed` (its cause logged). */
|
|
1033
|
+
const guarded = <T>(method: string, run: () => Promise<T>) => run().catch(error => {
|
|
1034
|
+
if (error instanceof RpcError) throw error;
|
|
1035
|
+
log(`${method} failed`, { cause: error instanceof GitReadFailed ? error.cause : String((error as Error)?.message ?? error) });
|
|
1036
|
+
throw gitFailed();
|
|
1037
|
+
});
|
|
1038
|
+
|
|
1039
|
+
return {
|
|
1040
|
+
status: (workspaceId: unknown) => guarded('workspace.git.status', () => status(workspaceId)),
|
|
1041
|
+
diff: (params: DiffParams) => guarded('workspace.git.diff', () => diff(params)),
|
|
1042
|
+
};
|
|
1043
|
+
}
|
|
1044
|
+
|
|
1045
|
+
export type GitReader = ReturnType<typeof createGitReader>;
|