yats-toolkit 0.4.2 → 0.5.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/bin/setup.js +5 -4
- package/connect/claude/SKILL.md +36 -0
- package/connect/claude/mcp.json +8 -0
- package/connect/codex/AGENTS.md +37 -0
- package/connect/codex/config.toml +9 -0
- package/connect/copilot/instructions.md +18 -0
- package/connect/copilot/mcp.json +10 -0
- package/connect/cursor/mcp.json +7 -0
- package/connect/cursor/rules.mdc +27 -0
- package/connect/gemini/GEMINI.md +33 -0
- package/connect/gemini/mcp.json +9 -0
- package/package.json +4 -2
- package/src/benchmark.js +23 -136
- package/src/connect.js +219 -52
- package/src/indexer.js +118 -13
- package/src/indexer.test.js +87 -0
- package/src/watch.js +336 -109
- package/src/watch.test.js +153 -0
package/src/watch.js
CHANGED
|
@@ -1,134 +1,253 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
/**
|
|
3
|
-
* yats watch
|
|
3
|
+
* yats watch <path> [--live] — keeps the YATS index in sync with the
|
|
4
|
+
* repository's *commits*, not the working tree (P1).
|
|
4
5
|
*
|
|
5
6
|
* Usage:
|
|
6
|
-
* yats watch <path>
|
|
7
|
+
* yats watch <path> # commit-based (default)
|
|
8
|
+
* yats watch <path> --live # also index on every file save
|
|
7
9
|
*
|
|
8
|
-
*
|
|
10
|
+
* Commit-based mode:
|
|
11
|
+
* - Polls `git rev-parse HEAD` every YATS_WATCH_POLL_MS (default 2000ms).
|
|
12
|
+
* - When HEAD changes (new commit OR branch checkout), diffs
|
|
13
|
+
* <lastIndexedCommit>..HEAD and streams only the changed files.
|
|
14
|
+
* - Saving files WITHOUT committing does NOT touch the index — the graph
|
|
15
|
+
* always reflects the last commit.
|
|
16
|
+
* - On startup, if the repo has no recorded indexed commit, it runs a full
|
|
17
|
+
* `yats index` first so it never starts from a half-indexed state.
|
|
18
|
+
*
|
|
19
|
+
* --live mode restores save-based indexing (fs.watch + debounce) on top of
|
|
20
|
+
* the commit loop, for agents working on uncommitted code. Without a git
|
|
21
|
+
* repository, --live is the only mode available.
|
|
22
|
+
*
|
|
23
|
+
* Zero dependencies — Node built-ins only. git runs on the host (the server
|
|
24
|
+
* may live in a container without git).
|
|
9
25
|
*/
|
|
10
26
|
|
|
11
|
-
import { watch, statSync, readFileSync } from "node:fs";
|
|
12
|
-
import { resolve,
|
|
13
|
-
import {
|
|
27
|
+
import { watch as fsWatch, statSync, readFileSync } from "node:fs";
|
|
28
|
+
import { resolve, relative } from "node:path";
|
|
29
|
+
import { execSync } from "node:child_process";
|
|
30
|
+
import indexRepo, { IGNORED } from "./indexer.js";
|
|
31
|
+
|
|
32
|
+
// ============================================================
|
|
33
|
+
// Config
|
|
34
|
+
// ============================================================
|
|
35
|
+
|
|
36
|
+
const DEFAULT_URL = "http://localhost:5555";
|
|
37
|
+
const POLL_MS = parseInt(process.env.YATS_WATCH_POLL_MS ?? "2000", 10);
|
|
38
|
+
const DEBOUNCE_MS = parseInt(process.env.YATS_WATCH_DEBOUNCE_MS ?? "500", 10);
|
|
39
|
+
const SKIP_PATTERNS = [
|
|
40
|
+
/node_modules/,
|
|
41
|
+
/\.git\//,
|
|
42
|
+
/vendor\//,
|
|
43
|
+
/__pycache__/,
|
|
44
|
+
/\.next\//,
|
|
45
|
+
/dist\//,
|
|
46
|
+
/\.yarn\//,
|
|
47
|
+
];
|
|
48
|
+
|
|
49
|
+
// ============================================================
|
|
50
|
+
// git helpers (host side — the server container has no git)
|
|
51
|
+
// ============================================================
|
|
52
|
+
|
|
53
|
+
function git(cwd, cmd) {
|
|
54
|
+
return execSync(`git ${cmd}`, { cwd, encoding: "utf-8", stdio: "pipe" }).trim();
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function isGitRepo(repoPath) {
|
|
58
|
+
try {
|
|
59
|
+
git(repoPath, "rev-parse --git-dir");
|
|
60
|
+
return true;
|
|
61
|
+
} catch {
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Current HEAD, or null for an unborn branch (no commits yet). */
|
|
67
|
+
function headCommit(repoPath) {
|
|
68
|
+
try {
|
|
69
|
+
return git(repoPath, "rev-parse HEAD");
|
|
70
|
+
} catch {
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Parse `git diff --name-status` output.
|
|
77
|
+
* Handles the rename format (`R100\told\tnew`) and quoted paths.
|
|
78
|
+
*/
|
|
79
|
+
export function parseNameStatus(output) {
|
|
80
|
+
const changes = { added: [], modified: [], deleted: [], renamed: [] };
|
|
81
|
+
|
|
82
|
+
for (const rawLine of output.split("\n")) {
|
|
83
|
+
const line = rawLine.trim();
|
|
84
|
+
if (!line) continue;
|
|
85
|
+
const [status, ...rest] = line.split("\t");
|
|
86
|
+
if (!status) continue;
|
|
87
|
+
|
|
88
|
+
const unquote = (p) =>
|
|
89
|
+
p && p.startsWith('"') && p.endsWith('"') ? p.slice(1, -1) : p;
|
|
90
|
+
|
|
91
|
+
if (status.startsWith("R") && rest.length >= 2) {
|
|
92
|
+
changes.renamed.push({ from: unquote(rest[0]), to: unquote(rest[1]) });
|
|
93
|
+
} else if (status.startsWith("A")) {
|
|
94
|
+
changes.added.push(unquote(rest[0]));
|
|
95
|
+
} else if (status.startsWith("M")) {
|
|
96
|
+
changes.modified.push(unquote(rest[0]));
|
|
97
|
+
} else if (status.startsWith("D")) {
|
|
98
|
+
changes.deleted.push(unquote(rest[0]));
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return changes;
|
|
103
|
+
}
|
|
14
104
|
|
|
15
105
|
// ============================================================
|
|
16
106
|
// HTTP helpers
|
|
17
107
|
// ============================================================
|
|
18
108
|
|
|
19
109
|
function post(baseUrl, path, body) {
|
|
20
|
-
return
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
{
|
|
25
|
-
hostname: url.hostname,
|
|
26
|
-
port: url.port,
|
|
27
|
-
path: url.pathname,
|
|
28
|
-
method: "POST",
|
|
29
|
-
headers: {
|
|
30
|
-
"Content-Type": "application/json",
|
|
31
|
-
"Content-Length": Buffer.byteLength(data),
|
|
32
|
-
},
|
|
33
|
-
},
|
|
34
|
-
(res) => {
|
|
35
|
-
let body = "";
|
|
36
|
-
res.on("data", (chunk) => (body += chunk));
|
|
37
|
-
res.on("end", () => {
|
|
38
|
-
if (res.statusCode === 200) {
|
|
39
|
-
resolve(body);
|
|
40
|
-
} else {
|
|
41
|
-
reject(new Error(`HTTP ${res.statusCode}: ${body}`));
|
|
42
|
-
}
|
|
43
|
-
});
|
|
44
|
-
},
|
|
45
|
-
);
|
|
46
|
-
req.on("error", reject);
|
|
47
|
-
req.write(data);
|
|
48
|
-
req.end();
|
|
110
|
+
return fetch(`${baseUrl}${path}`, {
|
|
111
|
+
method: "POST",
|
|
112
|
+
headers: { "Content-Type": "application/json" },
|
|
113
|
+
body: JSON.stringify(body),
|
|
49
114
|
});
|
|
50
115
|
}
|
|
51
116
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
117
|
+
/** The repository identity is its full path — never the basename. */
|
|
118
|
+
function repoNameFor(repoPath) {
|
|
119
|
+
return repoPath;
|
|
120
|
+
}
|
|
56
121
|
|
|
57
|
-
|
|
58
|
-
const
|
|
122
|
+
async function getRecordedCommit(baseUrl, repoName) {
|
|
123
|
+
const res = await fetch(
|
|
124
|
+
`${baseUrl}/index/commit?repository=${encodeURIComponent(repoName)}`,
|
|
125
|
+
);
|
|
126
|
+
if (!res.ok) return null;
|
|
127
|
+
try {
|
|
128
|
+
const data = await res.json();
|
|
129
|
+
return data.commit ?? null;
|
|
130
|
+
} catch {
|
|
131
|
+
return null;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
59
134
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
const
|
|
135
|
+
function shouldSkipPath(relPath) {
|
|
136
|
+
if (!relPath || relPath.startsWith(".")) return true;
|
|
137
|
+
const segments = relPath.split("/");
|
|
138
|
+
return segments.some((seg) => IGNORED.has(seg)) || SKIP_PATTERNS.some((p) => p.test(relPath));
|
|
139
|
+
}
|
|
63
140
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
process.exit(1);
|
|
141
|
+
async function indexFile(baseUrl, repoPath, repoName, relPath) {
|
|
142
|
+
if (shouldSkipPath(relPath)) return;
|
|
143
|
+
const fullPath = resolve(repoPath, relPath);
|
|
144
|
+
let content;
|
|
145
|
+
try {
|
|
146
|
+
content = readFileSync(fullPath, "utf-8");
|
|
147
|
+
} catch {
|
|
148
|
+
return; // deleted between diff and read — next commit will catch it
|
|
73
149
|
}
|
|
150
|
+
if (content.includes("\0") || content.length > 1_000_000) return; // binary/huge
|
|
74
151
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
}
|
|
152
|
+
console.log(` ↻ Indexing: ${relPath}`);
|
|
153
|
+
try {
|
|
154
|
+
const res = await post(baseUrl, "/index/file", {
|
|
155
|
+
repoName,
|
|
156
|
+
filePath: relPath,
|
|
157
|
+
content,
|
|
158
|
+
});
|
|
159
|
+
if (!res.ok) throw new Error(`HTTP ${res.status}`);
|
|
160
|
+
console.log(` ✓ Done: ${relPath}`);
|
|
161
|
+
} catch (err) {
|
|
162
|
+
console.error(` ✗ Failed: ${relPath} — ${err.message}`);
|
|
87
163
|
}
|
|
164
|
+
}
|
|
88
165
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
}
|
|
166
|
+
async function removeFile(baseUrl, repoName, relPath) {
|
|
167
|
+
if (shouldSkipPath(relPath)) return;
|
|
168
|
+
console.log(` ↻ Removing from index: ${relPath}`);
|
|
169
|
+
try {
|
|
170
|
+
const res = await post(baseUrl, "/index/remove", {
|
|
171
|
+
repository: repoName,
|
|
172
|
+
path: relPath,
|
|
173
|
+
});
|
|
174
|
+
if (!res.ok) throw new Error(`HTTP ${res.status}`);
|
|
175
|
+
console.log(` ✓ Removed: ${relPath}`);
|
|
176
|
+
} catch (err) {
|
|
177
|
+
console.error(` ✗ Failed: ${relPath} — ${err.message}`);
|
|
98
178
|
}
|
|
179
|
+
}
|
|
99
180
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
181
|
+
// ============================================================
|
|
182
|
+
// Commit diff application
|
|
183
|
+
// ============================================================
|
|
103
184
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
185
|
+
/**
|
|
186
|
+
* Bring the index from `from` to `to` by streaming only the changed files.
|
|
187
|
+
* Exported for tests (baseUrl is injectable).
|
|
188
|
+
*/
|
|
189
|
+
export async function applyDiff(repoPath, repoName, from, to, baseUrl = DEFAULT_URL) {
|
|
190
|
+
let output;
|
|
191
|
+
try {
|
|
192
|
+
output = git(repoPath, `diff --name-status ${from}..${to}`);
|
|
193
|
+
} catch (err) {
|
|
194
|
+
// E.g. the recorded commit was garbage-collected — full re-index is the
|
|
195
|
+
// only safe path.
|
|
196
|
+
console.log(` ⚠ Cannot diff ${from.slice(0, 8)}..${to.slice(0, 8)} — full re-index...`);
|
|
197
|
+
await indexRepo([repoPath]);
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
114
200
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
201
|
+
const changes = parseNameStatus(output);
|
|
202
|
+
const total =
|
|
203
|
+
changes.added.length + changes.modified.length +
|
|
204
|
+
changes.deleted.length + changes.renamed.length;
|
|
205
|
+
console.log(
|
|
206
|
+
` ${total} changed file(s): +${changes.added.length} ` +
|
|
207
|
+
`~${changes.modified.length} -${changes.deleted.length} ` +
|
|
208
|
+
`⟲${changes.renamed.length}`,
|
|
209
|
+
);
|
|
210
|
+
|
|
211
|
+
for (const p of changes.deleted) await removeFile(baseUrl, repoName, p);
|
|
212
|
+
for (const r of changes.renamed) await removeFile(baseUrl, repoName, r.from);
|
|
213
|
+
for (const p of [...changes.added, ...changes.modified]) {
|
|
214
|
+
await indexFile(baseUrl, repoPath, repoName, p);
|
|
215
|
+
}
|
|
216
|
+
for (const r of changes.renamed) {
|
|
217
|
+
await indexFile(baseUrl, repoPath, repoName, r.to);
|
|
118
218
|
}
|
|
119
219
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
220
|
+
// Flush cross-file relationships and record the new commit. Both are
|
|
221
|
+
// non-fatal: the server also flushes on its debounce timer, and watch
|
|
222
|
+
// re-syncs on the next poll if the commit was not recorded.
|
|
223
|
+
try {
|
|
224
|
+
await post(baseUrl, "/index/complete", { repository: repoName });
|
|
225
|
+
} catch {
|
|
226
|
+
/* non-fatal */
|
|
227
|
+
}
|
|
228
|
+
try {
|
|
229
|
+
await post(baseUrl, "/index/commit", { repository: repoName, commit: to });
|
|
230
|
+
} catch {
|
|
231
|
+
/* non-fatal */
|
|
232
|
+
}
|
|
233
|
+
console.log(` ✓ Index synced to commit ${to.slice(0, 8)}`);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// ============================================================
|
|
237
|
+
// Live mode — save-based indexing (fs.watch + debounce)
|
|
238
|
+
// ============================================================
|
|
239
|
+
|
|
240
|
+
function startLiveWatch(repoPath, repoName, baseUrl) {
|
|
241
|
+
const pending = new Map();
|
|
125
242
|
|
|
126
|
-
|
|
127
|
-
if (!filename ||
|
|
243
|
+
fsWatch(repoPath, { recursive: true }, (_eventType, filename) => {
|
|
244
|
+
if (!filename || SKIP_PATTERNS.some((p) => p.test(filename))) return;
|
|
245
|
+
if (filename.startsWith(".")) return;
|
|
128
246
|
|
|
129
247
|
const fullPath = resolve(repoPath, filename);
|
|
248
|
+
const relPath = relative(repoPath, fullPath);
|
|
249
|
+
if (shouldSkipPath(relPath)) return;
|
|
130
250
|
|
|
131
|
-
// Debounce: group rapid changes to the same file
|
|
132
251
|
const existing = pending.get(fullPath);
|
|
133
252
|
if (existing) clearTimeout(existing);
|
|
134
253
|
|
|
@@ -136,24 +255,132 @@ export default async function watchRepo(args = []) {
|
|
|
136
255
|
fullPath,
|
|
137
256
|
setTimeout(async () => {
|
|
138
257
|
pending.delete(fullPath);
|
|
139
|
-
|
|
140
|
-
// Check if file still exists (or was deleted)
|
|
141
258
|
try {
|
|
142
259
|
statSync(fullPath);
|
|
143
|
-
await indexFile(
|
|
260
|
+
await indexFile(baseUrl, repoPath, repoName, relPath);
|
|
144
261
|
} catch {
|
|
145
|
-
await removeFile(
|
|
262
|
+
await removeFile(baseUrl, repoName, relPath);
|
|
146
263
|
}
|
|
147
264
|
}, DEBOUNCE_MS),
|
|
148
265
|
);
|
|
149
266
|
});
|
|
150
267
|
|
|
151
|
-
|
|
152
|
-
|
|
268
|
+
console.log(` (live saves: every save re-indexes the file, debounced ${DEBOUNCE_MS}ms)`);
|
|
269
|
+
}
|
|
153
270
|
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
271
|
+
// ============================================================
|
|
272
|
+
// Main
|
|
273
|
+
// ============================================================
|
|
274
|
+
|
|
275
|
+
export default async function watchRepo(args = []) {
|
|
276
|
+
const live = args.includes("--live");
|
|
277
|
+
const baseUrl = process.env.YATS_URL ?? DEFAULT_URL;
|
|
278
|
+
const cleanArgs = args.filter((a) => a !== "--live" && a !== "--repo");
|
|
279
|
+
|
|
280
|
+
const repoPath = resolve(cleanArgs[0] ?? process.cwd());
|
|
281
|
+
|
|
282
|
+
if (!cleanArgs[0] || cleanArgs[0] === "--help" || cleanArgs[0] === "-h") {
|
|
283
|
+
console.log("Usage: yats watch <path> [--live]");
|
|
284
|
+
console.log("");
|
|
285
|
+
console.log("Keeps the YATS index in sync with the repository's commits.");
|
|
286
|
+
console.log("Saving files without committing does NOT touch the index —");
|
|
287
|
+
console.log("the graph always reflects the last commit.");
|
|
288
|
+
console.log("");
|
|
289
|
+
console.log(" --live also re-index on every file save (uncommitted code)");
|
|
290
|
+
console.log("");
|
|
291
|
+
console.log(" yats watch ~/my-project");
|
|
292
|
+
console.log(" yats watch ~/my-project --live");
|
|
293
|
+
process.exit(1);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
const repoName = repoNameFor(repoPath);
|
|
297
|
+
|
|
298
|
+
// Server reachability
|
|
299
|
+
try {
|
|
300
|
+
await fetch(`${baseUrl}/health`);
|
|
301
|
+
} catch {
|
|
302
|
+
console.error(`Cannot reach YATS at ${baseUrl}. Is it running?`);
|
|
303
|
+
process.exit(1);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
console.log(`👀 Watching: ${repoPath}`);
|
|
307
|
+
console.log(` Repo: ${repoName}`);
|
|
308
|
+
console.log(` Server: ${baseUrl}`);
|
|
309
|
+
console.log(` Mode: ${live ? "commits + live saves" : "commits only"}`);
|
|
310
|
+
console.log("");
|
|
311
|
+
|
|
312
|
+
const gitOk = isGitRepo(repoPath);
|
|
313
|
+
|
|
314
|
+
if (live) {
|
|
315
|
+
// Ensure metadata registration so the repo shows up in list_repositories
|
|
316
|
+
try {
|
|
317
|
+
await post(baseUrl, "/index", { path: repoPath });
|
|
318
|
+
} catch {
|
|
319
|
+
/* non-fatal */
|
|
320
|
+
}
|
|
321
|
+
startLiveWatch(repoPath, repoName, baseUrl);
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
if (!gitOk) {
|
|
325
|
+
if (live) {
|
|
326
|
+
console.log(" (not a git repository — live saves only)");
|
|
327
|
+
process.stdin.resume();
|
|
328
|
+
process.on("SIGINT", () => {
|
|
329
|
+
console.log("\n👋 Stopped watching.");
|
|
330
|
+
process.exit(0);
|
|
331
|
+
});
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
console.error(
|
|
335
|
+
"Not a git repository — commit-based watch requires git.\n" +
|
|
336
|
+
"Use `yats watch <path> --live` to index on every save instead.",
|
|
337
|
+
);
|
|
338
|
+
process.exit(1);
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
// ============================================================
|
|
342
|
+
// Commit-based loop
|
|
343
|
+
// ============================================================
|
|
344
|
+
|
|
345
|
+
let recorded = await getRecordedCommit(baseUrl, repoName);
|
|
346
|
+
let head = headCommit(repoPath);
|
|
347
|
+
|
|
348
|
+
if (recorded == null || recorded.length === 0) {
|
|
349
|
+
if (head) {
|
|
350
|
+
console.log(" No recorded index commit — running a full index first...");
|
|
351
|
+
await indexRepo([repoPath]);
|
|
352
|
+
recorded = headCommit(repoPath);
|
|
353
|
+
} else {
|
|
354
|
+
console.log(" Repo has no commits yet — waiting for the first commit...");
|
|
355
|
+
recorded = null;
|
|
356
|
+
}
|
|
357
|
+
} else if (head && recorded !== head) {
|
|
358
|
+
console.log(
|
|
359
|
+
` Indexed commit ${recorded.slice(0, 8)} ≠ HEAD ${head.slice(0, 8)} — syncing...`,
|
|
360
|
+
);
|
|
361
|
+
await applyDiff(repoPath, repoName, recorded, head, baseUrl);
|
|
362
|
+
recorded = head;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
if (recorded) {
|
|
366
|
+
console.log(` ✓ In sync with commit ${recorded.slice(0, 8)}`);
|
|
367
|
+
}
|
|
368
|
+
console.log(` (polling HEAD every ${POLL_MS}ms — commits trigger re-index)`);
|
|
369
|
+
console.log("");
|
|
370
|
+
|
|
371
|
+
while (true) {
|
|
372
|
+
await new Promise((r) => setTimeout(r, POLL_MS));
|
|
373
|
+
head = headCommit(repoPath);
|
|
374
|
+
if (!head || head === recorded) continue;
|
|
375
|
+
|
|
376
|
+
console.log(`\n ⬆ HEAD moved ${recorded ? recorded.slice(0, 8) + " → " : ""}${head.slice(0, 8)}`);
|
|
377
|
+
if (recorded) {
|
|
378
|
+
await applyDiff(repoPath, repoName, recorded, head, baseUrl);
|
|
379
|
+
} else {
|
|
380
|
+
// First commit after an unborn HEAD — nothing to diff from: full index.
|
|
381
|
+
await indexRepo([repoPath]);
|
|
382
|
+
}
|
|
383
|
+
recorded = head;
|
|
384
|
+
console.log("");
|
|
385
|
+
}
|
|
159
386
|
}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests — P1 commit-based watch.
|
|
3
|
+
*
|
|
4
|
+
* Covers parseNameStatus (git diff --name-status parsing) and applyDiff: a
|
|
5
|
+
* commit with added/modified/deleted/renamed files must produce the right
|
|
6
|
+
* HTTP calls to the YATS server (file/remove/complete/commit).
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { describe, it, before, after } from "node:test";
|
|
10
|
+
import assert from "node:assert/strict";
|
|
11
|
+
import { createServer } from "node:http";
|
|
12
|
+
import { join } from "node:path";
|
|
13
|
+
import { writeFileSync, mkdirSync, rmSync } from "node:fs";
|
|
14
|
+
import { execSync } from "node:child_process";
|
|
15
|
+
import { tmpdir } from "node:os";
|
|
16
|
+
|
|
17
|
+
import { parseNameStatus, applyDiff } from "./watch.js";
|
|
18
|
+
|
|
19
|
+
function git(cwd, cmd) {
|
|
20
|
+
return execSync(`git ${cmd}`, { cwd, encoding: "utf-8", stdio: "pipe" }).trim();
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
describe("parseNameStatus", () => {
|
|
24
|
+
it("parses added, modified, deleted, and renamed lines", () => {
|
|
25
|
+
const changes = parseNameStatus(
|
|
26
|
+
"A\tsrc/new.ts\n" +
|
|
27
|
+
"M\tsrc/edited.ts\n" +
|
|
28
|
+
"D\tsrc/old.ts\n" +
|
|
29
|
+
"R100\tsrc/before.ts\tsrc/after.ts\n",
|
|
30
|
+
);
|
|
31
|
+
assert.deepEqual(changes, {
|
|
32
|
+
added: ["src/new.ts"],
|
|
33
|
+
modified: ["src/edited.ts"],
|
|
34
|
+
deleted: ["src/old.ts"],
|
|
35
|
+
renamed: [{ from: "src/before.ts", to: "src/after.ts" }],
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("unquotes paths with spaces and ignores blank lines", () => {
|
|
40
|
+
const changes = parseNameStatus(
|
|
41
|
+
'\nM\t"src/my file.ts"\nD\t"src/old dir/file.ts"\n\n',
|
|
42
|
+
);
|
|
43
|
+
assert.deepEqual(changes.added, []);
|
|
44
|
+
assert.deepEqual(changes.modified, ["src/my file.ts"]);
|
|
45
|
+
assert.deepEqual(changes.deleted, ["src/old dir/file.ts"]);
|
|
46
|
+
assert.deepEqual(changes.renamed, []);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("handles empty output", () => {
|
|
50
|
+
assert.deepEqual(parseNameStatus(""), {
|
|
51
|
+
added: [],
|
|
52
|
+
modified: [],
|
|
53
|
+
deleted: [],
|
|
54
|
+
renamed: [],
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
describe("applyDiff — commit-based sync (P1)", () => {
|
|
60
|
+
let workDir;
|
|
61
|
+
let server;
|
|
62
|
+
let requests = [];
|
|
63
|
+
let baseUrl;
|
|
64
|
+
let repoName;
|
|
65
|
+
let v1;
|
|
66
|
+
let v2;
|
|
67
|
+
|
|
68
|
+
before(async () => {
|
|
69
|
+
// Temp git repo with two commits covering every change kind
|
|
70
|
+
workDir = join(tmpdir(), `yats-watch-test-${Date.now()}`);
|
|
71
|
+
mkdirSync(workDir, { recursive: true });
|
|
72
|
+
git(workDir, "init");
|
|
73
|
+
git(workDir, "config user.email test@test.com");
|
|
74
|
+
git(workDir, "config user.name Test");
|
|
75
|
+
|
|
76
|
+
writeFileSync(join(workDir, "a.txt"), "one\n");
|
|
77
|
+
writeFileSync(join(workDir, "b.txt"), "two\n");
|
|
78
|
+
writeFileSync(join(workDir, "d.txt"), "four\n");
|
|
79
|
+
git(workDir, "add -A");
|
|
80
|
+
git(workDir, "commit -m 'v1'");
|
|
81
|
+
v1 = git(workDir, "rev-parse HEAD");
|
|
82
|
+
|
|
83
|
+
writeFileSync(join(workDir, "a.txt"), "one (modified)\n");
|
|
84
|
+
writeFileSync(join(workDir, "c.txt"), "three\n");
|
|
85
|
+
rmSync(join(workDir, "b.txt"));
|
|
86
|
+
git(workDir, "mv d.txt e.txt");
|
|
87
|
+
git(workDir, "add -A");
|
|
88
|
+
git(workDir, "commit -m 'v2'");
|
|
89
|
+
v2 = git(workDir, "rev-parse HEAD");
|
|
90
|
+
|
|
91
|
+
// Fake YATS server that captures requests
|
|
92
|
+
server = createServer((req, res) => {
|
|
93
|
+
let body = "";
|
|
94
|
+
req.on("data", (chunk) => (body += chunk));
|
|
95
|
+
req.on("end", () => {
|
|
96
|
+
requests.push({ method: req.method, url: req.url, body: body ? JSON.parse(body) : null });
|
|
97
|
+
res.writeHead(200, { "Content-Type": "application/json" });
|
|
98
|
+
res.end(JSON.stringify({ ok: true }));
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
|
|
102
|
+
baseUrl = `http://127.0.0.1:${server.address().port}`;
|
|
103
|
+
repoName = workDir;
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
after(() => {
|
|
107
|
+
server?.close();
|
|
108
|
+
try { rmSync(workDir, { recursive: true }); } catch {}
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it("streams exactly the changed files and records the new commit", async () => {
|
|
112
|
+
await applyDiff(workDir, repoName, v1, v2, baseUrl);
|
|
113
|
+
|
|
114
|
+
const byUrl = (path) => requests.filter((r) => r.url === path);
|
|
115
|
+
|
|
116
|
+
const fileRequests = byUrl("/index/file");
|
|
117
|
+
const sentPaths = fileRequests.map((r) => r.body.filePath).sort();
|
|
118
|
+
assert.deepEqual(
|
|
119
|
+
sentPaths,
|
|
120
|
+
["a.txt", "c.txt", "e.txt"],
|
|
121
|
+
"modified + added + renamed-to files are sent",
|
|
122
|
+
);
|
|
123
|
+
// Content is sent for the modified file
|
|
124
|
+
const aReq = fileRequests.find((r) => r.body.filePath === "a.txt");
|
|
125
|
+
assert.equal(aReq.body.content, "one (modified)\n");
|
|
126
|
+
assert.equal(aReq.body.repoName, repoName, "repository identity is the full path");
|
|
127
|
+
|
|
128
|
+
const removeRequests = byUrl("/index/remove");
|
|
129
|
+
const removedPaths = removeRequests.map((r) => r.body.path).sort();
|
|
130
|
+
assert.deepEqual(
|
|
131
|
+
removedPaths,
|
|
132
|
+
["b.txt", "d.txt"],
|
|
133
|
+
"deleted and renamed-from files are removed",
|
|
134
|
+
);
|
|
135
|
+
|
|
136
|
+
const complete = byUrl("/index/complete");
|
|
137
|
+
assert.equal(complete.length, 1, "relationships are finalized");
|
|
138
|
+
assert.equal(complete[0].body.repository, repoName);
|
|
139
|
+
|
|
140
|
+
const commit = byUrl("/index/commit");
|
|
141
|
+
assert.equal(commit.length, 1, "new commit is recorded");
|
|
142
|
+
assert.equal(commit[0].body.repository, repoName);
|
|
143
|
+
assert.equal(commit[0].body.commit, v2);
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it("reports no-op when HEAD did not change files", async () => {
|
|
147
|
+
requests = [];
|
|
148
|
+
await applyDiff(workDir, repoName, v2, v2, baseUrl);
|
|
149
|
+
assert.equal(requests.filter((r) => r.url === "/index/file").length, 0);
|
|
150
|
+
// The commit is still recorded (idempotent sync)
|
|
151
|
+
assert.equal(requests.filter((r) => r.url === "/index/commit").length, 1);
|
|
152
|
+
});
|
|
153
|
+
});
|