@ricsam/r5d-worker 0.0.74 → 0.0.76
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -4
- package/dist/cjs/main.cjs +1031 -976
- package/dist/cjs/package.json +1 -1
- package/dist/cjs/project-workspace-state.cjs +777 -0
- package/dist/cjs/project-worktrees.cjs +559 -0
- package/dist/cjs/working-tree-mirror.cjs +225 -0
- package/dist/cjs/workspace-git-sync.cjs +565 -0
- package/dist/cjs/workspace-incident-state.cjs +2 -38
- package/dist/mjs/main.mjs +1048 -987
- package/dist/mjs/package.json +1 -1
- package/dist/mjs/project-workspace-state.mjs +745 -0
- package/dist/mjs/project-worktrees.mjs +513 -0
- package/dist/mjs/working-tree-mirror.mjs +190 -0
- package/dist/mjs/workspace-git-sync.mjs +524 -0
- package/dist/mjs/workspace-incident-state.mjs +1 -35
- package/dist/types/main.d.ts +35 -55
- package/dist/types/project-workspace-state.d.ts +144 -0
- package/dist/types/project-worktrees.d.ts +112 -0
- package/dist/types/working-tree-mirror.d.ts +24 -0
- package/dist/types/workspace-git-sync.d.ts +97 -0
- package/dist/types/workspace-incident-state.d.ts +0 -17
- package/dist/types/workspace-mutation-gate.d.ts +3 -3
- package/package.json +1 -1
- package/dist/cjs/workspace-convergence.cjs +0 -280
- package/dist/cjs/workspace-manifest-admission.cjs +0 -60
- package/dist/cjs/workspace-sync.cjs +0 -2469
- package/dist/mjs/workspace-convergence.mjs +0 -236
- package/dist/mjs/workspace-manifest-admission.mjs +0 -26
- package/dist/mjs/workspace-sync.mjs +0 -2417
- package/dist/types/workspace-convergence.d.ts +0 -93
- package/dist/types/workspace-manifest-admission.d.ts +0 -22
- package/dist/types/workspace-sync.d.ts +0 -167
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
function isGitMetadataPath(relativePath) {
|
|
4
|
+
return relativePath.split("/").includes(".git");
|
|
5
|
+
}
|
|
6
|
+
function normalizeRelativePath(value) {
|
|
7
|
+
const normalized = value.split(path.sep).join(path.posix.sep).replace(/^\.\/+/, "").replace(/^\/+|\/+$/g, "");
|
|
8
|
+
if (!normalized || normalized === "." || normalized.split("/").includes("..") || isGitMetadataPath(normalized)) return null;
|
|
9
|
+
return normalized;
|
|
10
|
+
}
|
|
11
|
+
function assertInside(root, candidate) {
|
|
12
|
+
const relative = path.relative(path.resolve(root), path.resolve(candidate));
|
|
13
|
+
if (relative === ".." || relative.startsWith(`..${path.sep}`) || path.isAbsolute(relative)) {
|
|
14
|
+
throw new Error(`Working-tree path escapes its root: ${candidate}`);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
function gitEligibleRoots(root) {
|
|
18
|
+
const result = Bun.spawnSync(
|
|
19
|
+
[
|
|
20
|
+
"git",
|
|
21
|
+
"-c",
|
|
22
|
+
"submodule.recurse=false",
|
|
23
|
+
"-c",
|
|
24
|
+
"fetch.recurseSubmodules=false",
|
|
25
|
+
"-c",
|
|
26
|
+
"push.recurseSubmodules=false",
|
|
27
|
+
"ls-files",
|
|
28
|
+
"-z",
|
|
29
|
+
"--cached",
|
|
30
|
+
"--others",
|
|
31
|
+
"--exclude-standard",
|
|
32
|
+
"--",
|
|
33
|
+
"."
|
|
34
|
+
],
|
|
35
|
+
{
|
|
36
|
+
cwd: root,
|
|
37
|
+
stdout: "pipe",
|
|
38
|
+
stderr: "pipe",
|
|
39
|
+
env: { ...process.env, GIT_TERMINAL_PROMPT: "0" }
|
|
40
|
+
}
|
|
41
|
+
);
|
|
42
|
+
if (result.exitCode !== 0) {
|
|
43
|
+
throw new Error(`Inspect Git working tree: ${result.stderr.toString().trim() || `git exited ${result.exitCode}`}`);
|
|
44
|
+
}
|
|
45
|
+
return result.stdout.toString().split("\0").flatMap((entry) => {
|
|
46
|
+
const normalized = normalizeRelativePath(entry);
|
|
47
|
+
return normalized ? [normalized] : [];
|
|
48
|
+
}).sort();
|
|
49
|
+
}
|
|
50
|
+
function addEntry(entries, root, relativePath, recurseDirectories) {
|
|
51
|
+
const normalized = normalizeRelativePath(relativePath);
|
|
52
|
+
if (!normalized || entries.has(normalized)) return;
|
|
53
|
+
const absolutePath = path.join(root, ...normalized.split("/"));
|
|
54
|
+
assertInside(root, absolutePath);
|
|
55
|
+
let stat;
|
|
56
|
+
try {
|
|
57
|
+
stat = fs.lstatSync(absolutePath);
|
|
58
|
+
} catch (error) {
|
|
59
|
+
if (error.code === "ENOENT") return;
|
|
60
|
+
throw error;
|
|
61
|
+
}
|
|
62
|
+
if (stat.isSymbolicLink()) {
|
|
63
|
+
entries.set(normalized, { kind: "symlink", mode: stat.mode, target: fs.readlinkSync(absolutePath) });
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
if (stat.isFile()) {
|
|
67
|
+
entries.set(normalized, { kind: "file", mode: stat.mode, size: stat.size });
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
if (!stat.isDirectory()) return;
|
|
71
|
+
entries.set(normalized, { kind: "directory", mode: stat.mode });
|
|
72
|
+
if (!recurseDirectories) return;
|
|
73
|
+
for (const child of fs.readdirSync(absolutePath).sort()) {
|
|
74
|
+
addEntry(entries, root, path.posix.join(normalized, child), true);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
function addParentDirectories(entries, root) {
|
|
78
|
+
for (const relativePath of [...entries.keys()]) {
|
|
79
|
+
let parent = path.posix.dirname(relativePath);
|
|
80
|
+
while (parent !== ".") {
|
|
81
|
+
if (!entries.has(parent)) {
|
|
82
|
+
const absolutePath = path.join(root, ...parent.split("/"));
|
|
83
|
+
const stat = fs.lstatSync(absolutePath);
|
|
84
|
+
entries.set(parent, { kind: "directory", mode: stat.mode });
|
|
85
|
+
}
|
|
86
|
+
parent = path.posix.dirname(parent);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
function inspectWorkingTree(root, mode) {
|
|
91
|
+
const entries = /* @__PURE__ */ new Map();
|
|
92
|
+
if (!fs.existsSync(root)) return entries;
|
|
93
|
+
if (mode === "all") {
|
|
94
|
+
for (const child of fs.readdirSync(root).sort()) addEntry(entries, root, child, true);
|
|
95
|
+
} else {
|
|
96
|
+
for (const relativePath of gitEligibleRoots(root)) {
|
|
97
|
+
addEntry(entries, root, relativePath, true);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
addParentDirectories(entries, root);
|
|
101
|
+
return entries;
|
|
102
|
+
}
|
|
103
|
+
function removeEntry(targetRoot, relativePath) {
|
|
104
|
+
const targetPath = path.join(targetRoot, ...relativePath.split("/"));
|
|
105
|
+
assertInside(targetRoot, targetPath);
|
|
106
|
+
fs.rmSync(targetPath, { recursive: true, force: true });
|
|
107
|
+
}
|
|
108
|
+
function ensureDirectory(targetRoot, relativePath, mode) {
|
|
109
|
+
const targetPath = path.join(targetRoot, ...relativePath.split("/"));
|
|
110
|
+
assertInside(targetRoot, targetPath);
|
|
111
|
+
if (fs.existsSync(targetPath) && !fs.lstatSync(targetPath).isDirectory()) removeEntry(targetRoot, relativePath);
|
|
112
|
+
fs.mkdirSync(targetPath, { recursive: true, mode: mode & 511 });
|
|
113
|
+
}
|
|
114
|
+
function filesEqual(leftPath, rightPath, size) {
|
|
115
|
+
let rightStat;
|
|
116
|
+
try {
|
|
117
|
+
rightStat = fs.lstatSync(rightPath);
|
|
118
|
+
} catch {
|
|
119
|
+
return false;
|
|
120
|
+
}
|
|
121
|
+
if (!rightStat.isFile() || rightStat.size !== size) return false;
|
|
122
|
+
const left = fs.openSync(leftPath, "r");
|
|
123
|
+
const right = fs.openSync(rightPath, "r");
|
|
124
|
+
const leftBuffer = Buffer.allocUnsafe(64 * 1024);
|
|
125
|
+
const rightBuffer = Buffer.allocUnsafe(64 * 1024);
|
|
126
|
+
try {
|
|
127
|
+
let offset = 0;
|
|
128
|
+
while (offset < size) {
|
|
129
|
+
const length = Math.min(leftBuffer.length, size - offset);
|
|
130
|
+
const leftRead = fs.readSync(left, leftBuffer, 0, length, offset);
|
|
131
|
+
const rightRead = fs.readSync(right, rightBuffer, 0, length, offset);
|
|
132
|
+
if (leftRead !== rightRead || !leftBuffer.subarray(0, leftRead).equals(rightBuffer.subarray(0, rightRead))) return false;
|
|
133
|
+
offset += leftRead;
|
|
134
|
+
}
|
|
135
|
+
return true;
|
|
136
|
+
} finally {
|
|
137
|
+
fs.closeSync(left);
|
|
138
|
+
fs.closeSync(right);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
function copyEntry(sourceRoot, targetRoot, relativePath, entry) {
|
|
142
|
+
const sourcePath = path.join(sourceRoot, ...relativePath.split("/"));
|
|
143
|
+
const targetPath = path.join(targetRoot, ...relativePath.split("/"));
|
|
144
|
+
assertInside(sourceRoot, sourcePath);
|
|
145
|
+
assertInside(targetRoot, targetPath);
|
|
146
|
+
const parent = path.posix.dirname(relativePath);
|
|
147
|
+
if (parent !== ".") ensureDirectory(targetRoot, parent, 493);
|
|
148
|
+
if (entry.kind === "directory") {
|
|
149
|
+
ensureDirectory(targetRoot, relativePath, entry.mode);
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
if (entry.kind === "symlink") {
|
|
153
|
+
let unchanged = false;
|
|
154
|
+
try {
|
|
155
|
+
unchanged = fs.lstatSync(targetPath).isSymbolicLink() && fs.readlinkSync(targetPath) === entry.target;
|
|
156
|
+
} catch {
|
|
157
|
+
unchanged = false;
|
|
158
|
+
}
|
|
159
|
+
if (!unchanged) {
|
|
160
|
+
removeEntry(targetRoot, relativePath);
|
|
161
|
+
fs.symlinkSync(entry.target, targetPath);
|
|
162
|
+
}
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
if (!filesEqual(sourcePath, targetPath, entry.size)) {
|
|
166
|
+
if (fs.existsSync(targetPath) && !fs.lstatSync(targetPath).isFile()) removeEntry(targetRoot, relativePath);
|
|
167
|
+
fs.copyFileSync(sourcePath, targetPath);
|
|
168
|
+
}
|
|
169
|
+
fs.chmodSync(targetPath, entry.mode & 511);
|
|
170
|
+
}
|
|
171
|
+
function mirrorWorkingTree(input) {
|
|
172
|
+
const sourceRoot = path.resolve(input.sourceRoot);
|
|
173
|
+
const targetRoot = path.resolve(input.targetRoot);
|
|
174
|
+
fs.mkdirSync(targetRoot, { recursive: true });
|
|
175
|
+
const desired = inspectWorkingTree(sourceRoot, input.sourceMode);
|
|
176
|
+
const existing = inspectWorkingTree(targetRoot, input.deletionMode === "git" ? "git" : "all");
|
|
177
|
+
for (const relativePath of [...existing.keys()].sort((left, right) => right.split("/").length - left.split("/").length)) {
|
|
178
|
+
if (!desired.has(relativePath)) removeEntry(targetRoot, relativePath);
|
|
179
|
+
}
|
|
180
|
+
for (const [relativePath, entry] of [...desired.entries()].sort(
|
|
181
|
+
([left], [right]) => left.split("/").length - right.split("/").length || left.localeCompare(right)
|
|
182
|
+
)) {
|
|
183
|
+
copyEntry(sourceRoot, targetRoot, relativePath, entry);
|
|
184
|
+
}
|
|
185
|
+
return { paths: [...desired.keys()].filter((relativePath) => desired.get(relativePath)?.kind !== "directory").sort() };
|
|
186
|
+
}
|
|
187
|
+
export {
|
|
188
|
+
inspectWorkingTree,
|
|
189
|
+
mirrorWorkingTree
|
|
190
|
+
};
|