dsh-git-desk 0.18.0 → 0.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -0
- package/client.js +5 -4780
- package/dist/contract.js +5 -0
- package/{index.js → dist/host/index.js} +4 -5
- package/dist/host/lib/ai.js +266 -0
- package/dist/host/lib/exec.js +193 -0
- package/dist/host/lib/git.js +1317 -0
- package/dist/host/lib/http.js +168 -0
- package/dist/host/lib/parse.js +381 -0
- package/dist/host/lib/rpc.js +316 -0
- package/dist/host/lib/types.js +1 -0
- package/package.json +18 -4
- package/lib/ai.js +0 -187
- package/lib/exec.js +0 -168
- package/lib/git.js +0 -1037
- package/lib/http.js +0 -139
- package/lib/parse.js +0 -319
- package/lib/rpc.js +0 -266
package/lib/http.js
DELETED
|
@@ -1,139 +0,0 @@
|
|
|
1
|
-
const MAX_BODY = 2 * 1024 * 1024;
|
|
2
|
-
const PREFIX = '/git-rpc';
|
|
3
|
-
|
|
4
|
-
function respond(res, status, payload) {
|
|
5
|
-
const body = JSON.stringify(payload);
|
|
6
|
-
res.writeHead(status, {
|
|
7
|
-
'content-type': 'application/json',
|
|
8
|
-
'content-length': Buffer.byteLength(body, 'utf8'),
|
|
9
|
-
'cache-control': 'no-store',
|
|
10
|
-
});
|
|
11
|
-
res.end(body);
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
function failure(rpcId, code, message) {
|
|
15
|
-
return {
|
|
16
|
-
type: 'server-response',
|
|
17
|
-
rpcId,
|
|
18
|
-
result: {
|
|
19
|
-
ok: false,
|
|
20
|
-
error: { code, message, details: {} },
|
|
21
|
-
},
|
|
22
|
-
};
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
function readBody(req) {
|
|
26
|
-
return new Promise((resolve, reject) => {
|
|
27
|
-
const chunks = [];
|
|
28
|
-
let size = 0;
|
|
29
|
-
let tooLarge = false;
|
|
30
|
-
req.on('data', (chunk) => {
|
|
31
|
-
size += chunk.length;
|
|
32
|
-
if (size > MAX_BODY) {
|
|
33
|
-
tooLarge = true;
|
|
34
|
-
chunks.length = 0;
|
|
35
|
-
return;
|
|
36
|
-
}
|
|
37
|
-
if (!tooLarge) chunks.push(chunk);
|
|
38
|
-
});
|
|
39
|
-
req.on('end', () => {
|
|
40
|
-
if (tooLarge) reject(new Error('body-too-large'));
|
|
41
|
-
else resolve(Buffer.concat(chunks).toString('utf8'));
|
|
42
|
-
});
|
|
43
|
-
req.on('error', reject);
|
|
44
|
-
});
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* The generic RPC channel helper in this build reaches `webServer` from the
|
|
49
|
-
* connection context, which does not inject it. This route speaks the same
|
|
50
|
-
* envelope the browser caller expects and applies the connection admission
|
|
51
|
-
* check itself.
|
|
52
|
-
*/
|
|
53
|
-
export function registerGitRoute(ctx, handler) {
|
|
54
|
-
return ctx.webServer.register({
|
|
55
|
-
kind: 'prefix',
|
|
56
|
-
path: PREFIX,
|
|
57
|
-
handler: async (req, res) => {
|
|
58
|
-
let admission;
|
|
59
|
-
try {
|
|
60
|
-
admission = ctx.connection.admit(req);
|
|
61
|
-
} catch (error) {
|
|
62
|
-
respond(res, 500, failure('unknown', 'git/internal', String(error)));
|
|
63
|
-
return;
|
|
64
|
-
}
|
|
65
|
-
if ('rejection' in admission) {
|
|
66
|
-
res.writeHead(admission.rejection);
|
|
67
|
-
res.end(admission.rejection === 401 ? 'unauthorized' : 'forbidden');
|
|
68
|
-
return;
|
|
69
|
-
}
|
|
70
|
-
if (req.method !== 'POST') {
|
|
71
|
-
res.writeHead(405, { allow: 'POST' });
|
|
72
|
-
res.end('method not allowed');
|
|
73
|
-
return;
|
|
74
|
-
}
|
|
75
|
-
let endpoint;
|
|
76
|
-
try {
|
|
77
|
-
const url = new URL(req.url ?? '/', 'http://localhost');
|
|
78
|
-
if (!url.pathname.startsWith(`${PREFIX}/`)) {
|
|
79
|
-
respond(res, 404, failure('unknown', 'git/not-found', 'Unknown Git endpoint.'));
|
|
80
|
-
return;
|
|
81
|
-
}
|
|
82
|
-
endpoint = decodeURIComponent(url.pathname.slice(PREFIX.length + 1));
|
|
83
|
-
} catch {
|
|
84
|
-
respond(res, 400, failure('unknown', 'git/bad-request', 'The Git endpoint is invalid.'));
|
|
85
|
-
return;
|
|
86
|
-
}
|
|
87
|
-
let text;
|
|
88
|
-
try {
|
|
89
|
-
text = await readBody(req);
|
|
90
|
-
} catch (error) {
|
|
91
|
-
respond(res, 413, failure('unknown', 'git/body-too-large', 'The Git request is too large.'));
|
|
92
|
-
return;
|
|
93
|
-
}
|
|
94
|
-
let message;
|
|
95
|
-
try {
|
|
96
|
-
message = JSON.parse(text);
|
|
97
|
-
} catch {
|
|
98
|
-
respond(res, 400, failure('unknown', 'git/bad-request', 'The Git request is not valid JSON.'));
|
|
99
|
-
return;
|
|
100
|
-
}
|
|
101
|
-
if (
|
|
102
|
-
message === null ||
|
|
103
|
-
typeof message !== 'object' ||
|
|
104
|
-
message.type !== 'client-request' ||
|
|
105
|
-
typeof message.rpcId !== 'string' ||
|
|
106
|
-
typeof message.method !== 'string'
|
|
107
|
-
) {
|
|
108
|
-
respond(res, 400, failure('unknown', 'git/bad-request', 'The Git request envelope is invalid.'));
|
|
109
|
-
return;
|
|
110
|
-
}
|
|
111
|
-
if (message.method !== endpoint) {
|
|
112
|
-
respond(
|
|
113
|
-
res,
|
|
114
|
-
400,
|
|
115
|
-
failure(message.rpcId, 'git/bad-request', 'The Git endpoint does not match the request method.'),
|
|
116
|
-
);
|
|
117
|
-
return;
|
|
118
|
-
}
|
|
119
|
-
if (admission.peer.principal?.kind !== 'owner') {
|
|
120
|
-
respond(res, 200, failure(message.rpcId, 'git/forbidden', 'Git actions are available only to the local user.'));
|
|
121
|
-
return;
|
|
122
|
-
}
|
|
123
|
-
let result;
|
|
124
|
-
try {
|
|
125
|
-
result = await handler(endpoint, message.payload, undefined, admission.peer);
|
|
126
|
-
} catch (error) {
|
|
127
|
-
result = {
|
|
128
|
-
ok: false,
|
|
129
|
-
error: {
|
|
130
|
-
code: 'git/internal',
|
|
131
|
-
message: error instanceof Error ? error.message : String(error),
|
|
132
|
-
details: {},
|
|
133
|
-
},
|
|
134
|
-
};
|
|
135
|
-
}
|
|
136
|
-
respond(res, 200, { type: 'server-response', rpcId: message.rpcId, result });
|
|
137
|
-
},
|
|
138
|
-
});
|
|
139
|
-
}
|
package/lib/parse.js
DELETED
|
@@ -1,319 +0,0 @@
|
|
|
1
|
-
const STATUS_LETTERS = new Set(['A', 'C', 'D', 'M', 'R', 'T', 'U', 'X']);
|
|
2
|
-
|
|
3
|
-
/** Split a porcelain record after its fixed leading fields, keeping spaces in the path. */
|
|
4
|
-
function splitFields(record, count) {
|
|
5
|
-
let index = 0;
|
|
6
|
-
for (let field = 0; field < count; field += 1) {
|
|
7
|
-
const space = record.indexOf(' ', index);
|
|
8
|
-
if (space === -1) return null;
|
|
9
|
-
index = space + 1;
|
|
10
|
-
}
|
|
11
|
-
return [record.slice(0, index - 1), record.slice(index)];
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
function branchCode(entry, xy) {
|
|
15
|
-
const index = xy[0];
|
|
16
|
-
const worktree = xy[1];
|
|
17
|
-
const conflicted = entry.kind === 'u' || entry.kind === 'unmerged' || index === 'U' || worktree === 'U';
|
|
18
|
-
const untracked = entry.kind === '?' || entry.kind === 'untracked';
|
|
19
|
-
const staged = !untracked && index !== '.' && index !== '?';
|
|
20
|
-
const unstaged = untracked || (worktree !== '.' && worktree !== '?');
|
|
21
|
-
let letter = 'M';
|
|
22
|
-
if (conflicted) letter = 'C';
|
|
23
|
-
else if (untracked) letter = 'U';
|
|
24
|
-
else if (index === 'A') letter = 'A';
|
|
25
|
-
else if (index === 'D' || worktree === 'D') letter = 'D';
|
|
26
|
-
else if (index === 'R' || worktree === 'R') letter = 'R';
|
|
27
|
-
else if (index === 'C' || worktree === 'C') letter = 'C';
|
|
28
|
-
else if (index === 'T' || worktree === 'T') letter = 'T';
|
|
29
|
-
return {
|
|
30
|
-
index,
|
|
31
|
-
worktree,
|
|
32
|
-
conflicted,
|
|
33
|
-
untracked,
|
|
34
|
-
staged,
|
|
35
|
-
unstaged,
|
|
36
|
-
letter,
|
|
37
|
-
both: staged && unstaged,
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export function parseStatus(text) {
|
|
42
|
-
const branch = {
|
|
43
|
-
oid: null,
|
|
44
|
-
name: null,
|
|
45
|
-
upstream: null,
|
|
46
|
-
ahead: 0,
|
|
47
|
-
behind: 0,
|
|
48
|
-
detached: false,
|
|
49
|
-
initial: false,
|
|
50
|
-
};
|
|
51
|
-
const entries = [];
|
|
52
|
-
let skipNext = false;
|
|
53
|
-
let pendingRename = null;
|
|
54
|
-
|
|
55
|
-
for (const record of text.split('\0')) {
|
|
56
|
-
if (record === '') continue;
|
|
57
|
-
if (skipNext) {
|
|
58
|
-
skipNext = false;
|
|
59
|
-
if (pendingRename !== null) pendingRename.from = record;
|
|
60
|
-
pendingRename = null;
|
|
61
|
-
continue;
|
|
62
|
-
}
|
|
63
|
-
if (record.startsWith('# ')) {
|
|
64
|
-
const header = record.slice(2);
|
|
65
|
-
const space = header.indexOf(' ');
|
|
66
|
-
const key = space === -1 ? header : header.slice(0, space);
|
|
67
|
-
const value = space === -1 ? '' : header.slice(space + 1);
|
|
68
|
-
if (key === 'branch.oid') {
|
|
69
|
-
branch.oid = value === '(initial)' ? null : value;
|
|
70
|
-
branch.initial = value === '(initial)';
|
|
71
|
-
} else if (key === 'branch.head') {
|
|
72
|
-
branch.detached = value === '(detached)';
|
|
73
|
-
branch.name = value === '(detached)' ? null : value;
|
|
74
|
-
} else if (key === 'branch.upstream') {
|
|
75
|
-
branch.upstream = value;
|
|
76
|
-
} else if (key === 'branch.ab') {
|
|
77
|
-
const match = /^\+(\d+) -(\d+)$/.exec(value);
|
|
78
|
-
if (match) {
|
|
79
|
-
branch.ahead = Number(match[1]);
|
|
80
|
-
branch.behind = Number(match[2]);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
continue;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
if (record.startsWith('1 ')) {
|
|
87
|
-
const fields = splitFields(record, 8);
|
|
88
|
-
if (fields === null) continue;
|
|
89
|
-
const xy = fields[0].split(' ')[1];
|
|
90
|
-
const entry = { path: fields[1], kind: 'tracked', ...branchCode({ kind: 'tracked' }, xy) };
|
|
91
|
-
entries.push(entry);
|
|
92
|
-
continue;
|
|
93
|
-
}
|
|
94
|
-
if (record.startsWith('2 ')) {
|
|
95
|
-
const fields = splitFields(record, 9);
|
|
96
|
-
if (fields === null) continue;
|
|
97
|
-
const xy = fields[0].split(' ')[1];
|
|
98
|
-
const entry = {
|
|
99
|
-
path: fields[1],
|
|
100
|
-
kind: 'renamed',
|
|
101
|
-
from: null,
|
|
102
|
-
...branchCode({ kind: 'renamed' }, xy),
|
|
103
|
-
};
|
|
104
|
-
entries.push(entry);
|
|
105
|
-
pendingRename = entry;
|
|
106
|
-
skipNext = true;
|
|
107
|
-
continue;
|
|
108
|
-
}
|
|
109
|
-
if (record.startsWith('u ')) {
|
|
110
|
-
const fields = splitFields(record, 10);
|
|
111
|
-
if (fields === null) continue;
|
|
112
|
-
const xy = fields[0].split(' ')[1];
|
|
113
|
-
entries.push({ path: fields[1], kind: 'unmerged', ...branchCode({ kind: 'unmerged' }, xy) });
|
|
114
|
-
continue;
|
|
115
|
-
}
|
|
116
|
-
if (record.startsWith('? ')) {
|
|
117
|
-
const path = record.slice(2);
|
|
118
|
-
entries.push({ path, kind: 'untracked', ...branchCode({ kind: 'untracked' }, '??') });
|
|
119
|
-
continue;
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
for (const entry of entries) {
|
|
124
|
-
if (!STATUS_LETTERS.has(entry.letter)) entry.letter = 'M';
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
return {
|
|
128
|
-
branch,
|
|
129
|
-
entries,
|
|
130
|
-
merging: entries.some((entry) => entry.conflicted),
|
|
131
|
-
rebasing: false,
|
|
132
|
-
};
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
export function parseNumstat(text) {
|
|
136
|
-
const result = new Map();
|
|
137
|
-
for (const record of text.split('\0')) {
|
|
138
|
-
if (record === '') continue;
|
|
139
|
-
const parts = record.split('\t');
|
|
140
|
-
if (parts.length < 3) continue;
|
|
141
|
-
const path = parts.slice(2).join('\t');
|
|
142
|
-
const add = parts[0] === '-' ? null : Number(parts[0]);
|
|
143
|
-
const del = parts[1] === '-' ? null : Number(parts[1]);
|
|
144
|
-
result.set(path, {
|
|
145
|
-
add: Number.isFinite(add) ? add : null,
|
|
146
|
-
del: Number.isFinite(del) ? del : null,
|
|
147
|
-
binary: add === null || del === null,
|
|
148
|
-
});
|
|
149
|
-
}
|
|
150
|
-
return result;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
/** Parse the zero-context hunk boundaries Git already computed. */
|
|
154
|
-
export function parseHunks(patch) {
|
|
155
|
-
const hunks = [];
|
|
156
|
-
for (const line of patch.split('\n')) {
|
|
157
|
-
if (!line.startsWith('@@ ')) continue;
|
|
158
|
-
const match = /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/.exec(line);
|
|
159
|
-
if (match === null) continue;
|
|
160
|
-
hunks.push({
|
|
161
|
-
oldStart: Number(match[1]),
|
|
162
|
-
oldCount: match[2] === undefined ? 1 : Number(match[2]),
|
|
163
|
-
newStart: Number(match[3]),
|
|
164
|
-
newCount: match[4] === undefined ? 1 : Number(match[4]),
|
|
165
|
-
});
|
|
166
|
-
}
|
|
167
|
-
return hunks;
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
export function splitLines(text) {
|
|
171
|
-
if (text === '') return { lines: [], trailing: false };
|
|
172
|
-
const lines = text.split('\n');
|
|
173
|
-
let trailing = false;
|
|
174
|
-
if (lines[lines.length - 1] === '') {
|
|
175
|
-
lines.pop();
|
|
176
|
-
trailing = true;
|
|
177
|
-
}
|
|
178
|
-
return { lines, trailing };
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
export function joinLines(lines, trailing) {
|
|
182
|
-
if (lines.length === 0) return '';
|
|
183
|
-
return lines.join('\n') + (trailing ? '\n' : '');
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
/**
|
|
187
|
-
* Build display rows from two complete versions and Git's hunk boundaries.
|
|
188
|
-
* Every unchanged line becomes a context row, so the caller can render the
|
|
189
|
-
* full file without asking Git for context.
|
|
190
|
-
*/
|
|
191
|
-
export function buildRows(leftLines, rightLines, hunks) {
|
|
192
|
-
const rows = [];
|
|
193
|
-
const blocks = [];
|
|
194
|
-
let left = 0;
|
|
195
|
-
let right = 0;
|
|
196
|
-
|
|
197
|
-
const pushContext = (count) => {
|
|
198
|
-
for (let index = 0; index < count; index += 1) {
|
|
199
|
-
rows.push({ l: left + 1, r: right + 1, k: 'ctx' });
|
|
200
|
-
left += 1;
|
|
201
|
-
right += 1;
|
|
202
|
-
}
|
|
203
|
-
};
|
|
204
|
-
|
|
205
|
-
for (const hunk of hunks) {
|
|
206
|
-
if (hunk.oldCount === 0) {
|
|
207
|
-
pushContext(Math.max(0, hunk.oldStart - left));
|
|
208
|
-
const row = rows.length;
|
|
209
|
-
for (let index = 0; index < hunk.newCount; index += 1) {
|
|
210
|
-
rows.push({ l: null, r: right + 1, k: 'add' });
|
|
211
|
-
right += 1;
|
|
212
|
-
}
|
|
213
|
-
blocks.push({
|
|
214
|
-
leftStart: hunk.oldStart + 1,
|
|
215
|
-
leftEnd: hunk.oldStart,
|
|
216
|
-
rightStart: hunk.newStart,
|
|
217
|
-
rightEnd: hunk.newStart + hunk.newCount - 1,
|
|
218
|
-
kind: 'add',
|
|
219
|
-
row,
|
|
220
|
-
});
|
|
221
|
-
continue;
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
if (hunk.newCount === 0) {
|
|
225
|
-
pushContext(Math.max(0, hunk.oldStart - 1 - left));
|
|
226
|
-
const row = rows.length;
|
|
227
|
-
for (let index = 0; index < hunk.oldCount; index += 1) {
|
|
228
|
-
rows.push({ l: left + 1, r: null, k: 'del' });
|
|
229
|
-
left += 1;
|
|
230
|
-
}
|
|
231
|
-
blocks.push({
|
|
232
|
-
leftStart: hunk.oldStart,
|
|
233
|
-
leftEnd: hunk.oldStart + hunk.oldCount - 1,
|
|
234
|
-
rightStart: hunk.newStart + 1,
|
|
235
|
-
rightEnd: hunk.newStart,
|
|
236
|
-
kind: 'del',
|
|
237
|
-
row,
|
|
238
|
-
});
|
|
239
|
-
continue;
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
pushContext(Math.max(0, hunk.oldStart - 1 - left));
|
|
243
|
-
const row = rows.length;
|
|
244
|
-
const count = Math.max(hunk.oldCount, hunk.newCount);
|
|
245
|
-
for (let index = 0; index < count; index += 1) {
|
|
246
|
-
const hasLeft = index < hunk.oldCount;
|
|
247
|
-
const hasRight = index < hunk.newCount;
|
|
248
|
-
rows.push({
|
|
249
|
-
l: hasLeft ? left + 1 : null,
|
|
250
|
-
r: hasRight ? right + 1 : null,
|
|
251
|
-
k: hasLeft && hasRight ? 'mod' : hasLeft ? 'del' : 'add',
|
|
252
|
-
});
|
|
253
|
-
if (hasLeft) left += 1;
|
|
254
|
-
if (hasRight) right += 1;
|
|
255
|
-
}
|
|
256
|
-
blocks.push({
|
|
257
|
-
leftStart: hunk.oldStart,
|
|
258
|
-
leftEnd: hunk.oldStart + hunk.oldCount - 1,
|
|
259
|
-
rightStart: hunk.newStart,
|
|
260
|
-
rightEnd: hunk.newStart + hunk.newCount - 1,
|
|
261
|
-
kind: 'mod',
|
|
262
|
-
row,
|
|
263
|
-
});
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
pushContext(Math.max(0, leftLines.length - left));
|
|
267
|
-
|
|
268
|
-
let add = 0;
|
|
269
|
-
let del = 0;
|
|
270
|
-
for (const row of rows) {
|
|
271
|
-
if (row.k === 'add') add += 1;
|
|
272
|
-
else if (row.k === 'del') del += 1;
|
|
273
|
-
else if (row.k === 'mod') {
|
|
274
|
-
add += 1;
|
|
275
|
-
del += 1;
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
return { rows, blocks, add, del };
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
export function parseLog(text) {
|
|
282
|
-
const commits = [];
|
|
283
|
-
for (const record of text.split('\x1e')) {
|
|
284
|
-
const raw = record.trim();
|
|
285
|
-
if (raw === '') continue;
|
|
286
|
-
const [rawHash, parents, author, email, date, refs, subject] = raw.split('\x00');
|
|
287
|
-
const hash = rawHash?.trim();
|
|
288
|
-
if (!hash) continue;
|
|
289
|
-
commits.push({
|
|
290
|
-
hash,
|
|
291
|
-
parents: parents ? parents.split(' ').filter(Boolean) : [],
|
|
292
|
-
author,
|
|
293
|
-
email,
|
|
294
|
-
date,
|
|
295
|
-
refs: refs
|
|
296
|
-
? refs
|
|
297
|
-
.split(', ')
|
|
298
|
-
.map((ref) => ref.trim())
|
|
299
|
-
.filter(Boolean)
|
|
300
|
-
: [],
|
|
301
|
-
subject,
|
|
302
|
-
});
|
|
303
|
-
}
|
|
304
|
-
return commits;
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
export function parseStashes(text) {
|
|
308
|
-
const stashes = [];
|
|
309
|
-
for (const record of text.split('\x1e')) {
|
|
310
|
-
const raw = record.trim();
|
|
311
|
-
if (raw === '') continue;
|
|
312
|
-
const [rawRef, message, rawHash] = raw.split('\x00');
|
|
313
|
-
const ref = rawRef?.trim();
|
|
314
|
-
const hash = rawHash?.trim();
|
|
315
|
-
if (!ref) continue;
|
|
316
|
-
stashes.push({ ref, message, hash });
|
|
317
|
-
}
|
|
318
|
-
return stashes;
|
|
319
|
-
}
|