gsd-pi 0.2.9 → 0.3.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 +23 -0
- package/dist/cli.js +47 -5
- package/dist/wizard.js +2 -1
- package/package.json +1 -1
- package/src/resources/extensions/gsd/commands.ts +9 -3
- package/src/resources/extensions/gsd/dashboard-overlay.ts +6 -1
- package/src/resources/extensions/gsd/files.ts +7 -7
- package/src/resources/extensions/gsd/gitignore.ts +1 -0
- package/src/resources/extensions/gsd/index.ts +36 -1
- package/src/resources/extensions/gsd/migrate/command.ts +215 -0
- package/src/resources/extensions/gsd/migrate/index.ts +42 -0
- package/src/resources/extensions/gsd/migrate/parser.ts +323 -0
- package/src/resources/extensions/gsd/migrate/parsers.ts +624 -0
- package/src/resources/extensions/gsd/migrate/preview.ts +48 -0
- package/src/resources/extensions/gsd/migrate/transformer.ts +346 -0
- package/src/resources/extensions/gsd/migrate/types.ts +370 -0
- package/src/resources/extensions/gsd/migrate/validator.ts +53 -0
- package/src/resources/extensions/gsd/migrate/writer.ts +539 -0
- package/src/resources/extensions/gsd/prompts/review-migration.md +66 -0
- package/src/resources/extensions/gsd/prompts/worktree-merge.md +89 -0
- package/src/resources/extensions/gsd/tests/migrate-command.test.ts +390 -0
- package/src/resources/extensions/gsd/tests/migrate-parser.test.ts +786 -0
- package/src/resources/extensions/gsd/tests/migrate-transformer.test.ts +657 -0
- package/src/resources/extensions/gsd/tests/migrate-validator-parsers.test.ts +443 -0
- package/src/resources/extensions/gsd/tests/migrate-writer-integration.test.ts +318 -0
- package/src/resources/extensions/gsd/tests/migrate-writer.test.ts +420 -0
- package/src/resources/extensions/gsd/tests/worktree-manager.test.ts +160 -0
- package/src/resources/extensions/gsd/worktree-command.ts +527 -0
- package/src/resources/extensions/gsd/worktree-manager.ts +302 -0
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GSD Worktree Manager
|
|
3
|
+
*
|
|
4
|
+
* Creates and manages git worktrees under .gsd/worktrees/<name>/.
|
|
5
|
+
* Each worktree gets its own branch (worktree/<name>) and a full
|
|
6
|
+
* working copy of the project, enabling parallel work streams.
|
|
7
|
+
*
|
|
8
|
+
* The merge helper compares .gsd/ artifacts between a worktree and
|
|
9
|
+
* the main branch, then dispatches an LLM-guided merge flow.
|
|
10
|
+
*
|
|
11
|
+
* Flow:
|
|
12
|
+
* 1. create() — git worktree add .gsd/worktrees/<name> -b worktree/<name>
|
|
13
|
+
* 2. user works in the worktree (new plans, milestones, etc.)
|
|
14
|
+
* 3. merge() — LLM-guided reconciliation of .gsd/ artifacts back to main
|
|
15
|
+
* 4. remove() — git worktree remove + branch cleanup
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { existsSync, mkdirSync, realpathSync } from "node:fs";
|
|
19
|
+
import { execSync } from "node:child_process";
|
|
20
|
+
import { join, relative, resolve } from "node:path";
|
|
21
|
+
|
|
22
|
+
// ─── Types ─────────────────────────────────────────────────────────────────
|
|
23
|
+
|
|
24
|
+
export interface WorktreeInfo {
|
|
25
|
+
name: string;
|
|
26
|
+
path: string;
|
|
27
|
+
branch: string;
|
|
28
|
+
exists: boolean;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface WorktreeDiffSummary {
|
|
32
|
+
/** Files only in the worktree .gsd/ (new artifacts) */
|
|
33
|
+
added: string[];
|
|
34
|
+
/** Files in both but with different content */
|
|
35
|
+
modified: string[];
|
|
36
|
+
/** Files only in main .gsd/ (deleted in worktree) */
|
|
37
|
+
removed: string[];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// ─── Git Helpers ───────────────────────────────────────────────────────────
|
|
41
|
+
|
|
42
|
+
function runGit(cwd: string, args: string[], opts: { allowFailure?: boolean } = {}): string {
|
|
43
|
+
try {
|
|
44
|
+
return execSync(`git ${args.join(" ")}`, {
|
|
45
|
+
cwd,
|
|
46
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
47
|
+
encoding: "utf-8",
|
|
48
|
+
}).trim();
|
|
49
|
+
} catch (error) {
|
|
50
|
+
if (opts.allowFailure) return "";
|
|
51
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
52
|
+
throw new Error(`git ${args.join(" ")} failed in ${cwd}: ${message}`);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function getMainBranch(basePath: string): string {
|
|
57
|
+
const symbolic = runGit(basePath, ["symbolic-ref", "refs/remotes/origin/HEAD"], { allowFailure: true });
|
|
58
|
+
if (symbolic) {
|
|
59
|
+
const match = symbolic.match(/refs\/remotes\/origin\/(.+)$/);
|
|
60
|
+
if (match) return match[1]!;
|
|
61
|
+
}
|
|
62
|
+
if (runGit(basePath, ["show-ref", "--verify", "refs/heads/main"], { allowFailure: true })) return "main";
|
|
63
|
+
if (runGit(basePath, ["show-ref", "--verify", "refs/heads/master"], { allowFailure: true })) return "master";
|
|
64
|
+
return runGit(basePath, ["branch", "--show-current"]);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// ─── Path Helpers ──────────────────────────────────────────────────────────
|
|
68
|
+
|
|
69
|
+
export function worktreesDir(basePath: string): string {
|
|
70
|
+
return join(basePath, ".gsd", "worktrees");
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function worktreePath(basePath: string, name: string): string {
|
|
74
|
+
return join(worktreesDir(basePath), name);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function worktreeBranchName(name: string): string {
|
|
78
|
+
return `worktree/${name}`;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// ─── Core Operations ───────────────────────────────────────────────────────
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Create a new git worktree under .gsd/worktrees/<name>/ with branch worktree/<name>.
|
|
85
|
+
* The branch is created from the current HEAD of the main branch.
|
|
86
|
+
*/
|
|
87
|
+
export function createWorktree(basePath: string, name: string): WorktreeInfo {
|
|
88
|
+
// Validate name: alphanumeric, hyphens, underscores only
|
|
89
|
+
if (!/^[a-zA-Z0-9_-]+$/.test(name)) {
|
|
90
|
+
throw new Error(`Invalid worktree name "${name}". Use only letters, numbers, hyphens, and underscores.`);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const wtPath = worktreePath(basePath, name);
|
|
94
|
+
const branch = worktreeBranchName(name);
|
|
95
|
+
|
|
96
|
+
if (existsSync(wtPath)) {
|
|
97
|
+
throw new Error(`Worktree "${name}" already exists at ${wtPath}`);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// Ensure the .gsd/worktrees/ directory exists
|
|
101
|
+
const wtDir = worktreesDir(basePath);
|
|
102
|
+
mkdirSync(wtDir, { recursive: true });
|
|
103
|
+
|
|
104
|
+
// Prune any stale worktree entries from a previous removal
|
|
105
|
+
runGit(basePath, ["worktree", "prune"], { allowFailure: true });
|
|
106
|
+
|
|
107
|
+
// Check if the branch already exists (leftover from a previous worktree)
|
|
108
|
+
const branchExists = runGit(basePath, ["show-ref", "--verify", `refs/heads/${branch}`], { allowFailure: true });
|
|
109
|
+
const mainBranch = getMainBranch(basePath);
|
|
110
|
+
|
|
111
|
+
if (branchExists) {
|
|
112
|
+
// Reset the stale branch to current main, then attach worktree to it
|
|
113
|
+
runGit(basePath, ["branch", "-f", branch, mainBranch]);
|
|
114
|
+
runGit(basePath, ["worktree", "add", wtPath, branch]);
|
|
115
|
+
} else {
|
|
116
|
+
runGit(basePath, ["worktree", "add", "-b", branch, wtPath, mainBranch]);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return {
|
|
120
|
+
name,
|
|
121
|
+
path: wtPath,
|
|
122
|
+
branch,
|
|
123
|
+
exists: true,
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* List all GSD-managed worktrees.
|
|
129
|
+
* Parses `git worktree list` and filters to those under .gsd/worktrees/.
|
|
130
|
+
*/
|
|
131
|
+
export function listWorktrees(basePath: string): WorktreeInfo[] {
|
|
132
|
+
// Resolve real paths to handle symlinks (e.g. /tmp → /private/tmp on macOS)
|
|
133
|
+
const resolvedBase = existsSync(basePath) ? realpathSync(basePath) : resolve(basePath);
|
|
134
|
+
const wtDir = join(resolvedBase, ".gsd", "worktrees");
|
|
135
|
+
const rawList = runGit(basePath, ["worktree", "list", "--porcelain"]);
|
|
136
|
+
|
|
137
|
+
if (!rawList.trim()) return [];
|
|
138
|
+
|
|
139
|
+
const worktrees: WorktreeInfo[] = [];
|
|
140
|
+
const entries = rawList.split("\n\n").filter(Boolean);
|
|
141
|
+
|
|
142
|
+
for (const entry of entries) {
|
|
143
|
+
const lines = entry.split("\n");
|
|
144
|
+
const wtLine = lines.find(l => l.startsWith("worktree "));
|
|
145
|
+
const branchLine = lines.find(l => l.startsWith("branch "));
|
|
146
|
+
|
|
147
|
+
if (!wtLine || !branchLine) continue;
|
|
148
|
+
|
|
149
|
+
const entryPath = wtLine.replace("worktree ", "");
|
|
150
|
+
const branch = branchLine.replace("branch refs/heads/", "");
|
|
151
|
+
|
|
152
|
+
// Only include worktrees under .gsd/worktrees/
|
|
153
|
+
if (!entryPath.startsWith(wtDir)) continue;
|
|
154
|
+
|
|
155
|
+
const name = relative(wtDir, entryPath);
|
|
156
|
+
// Skip nested paths — only direct children
|
|
157
|
+
if (name.includes("/") || name.includes("\\")) continue;
|
|
158
|
+
|
|
159
|
+
worktrees.push({
|
|
160
|
+
name,
|
|
161
|
+
path: entryPath,
|
|
162
|
+
branch,
|
|
163
|
+
exists: existsSync(entryPath),
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
return worktrees;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Remove a worktree and optionally delete its branch.
|
|
172
|
+
* If the process is currently inside the worktree, chdir out first.
|
|
173
|
+
*/
|
|
174
|
+
export function removeWorktree(
|
|
175
|
+
basePath: string,
|
|
176
|
+
name: string,
|
|
177
|
+
opts: { deleteBranch?: boolean; force?: boolean } = {},
|
|
178
|
+
): void {
|
|
179
|
+
const wtPath = worktreePath(basePath, name);
|
|
180
|
+
const resolvedWtPath = existsSync(wtPath) ? realpathSync(wtPath) : wtPath;
|
|
181
|
+
const branch = worktreeBranchName(name);
|
|
182
|
+
const { deleteBranch = true, force = false } = opts;
|
|
183
|
+
|
|
184
|
+
// If we're inside the worktree, move out first — git can't remove an in-use directory
|
|
185
|
+
const cwd = process.cwd();
|
|
186
|
+
const resolvedCwd = existsSync(cwd) ? realpathSync(cwd) : cwd;
|
|
187
|
+
if (resolvedCwd === resolvedWtPath || resolvedCwd.startsWith(resolvedWtPath + "/")) {
|
|
188
|
+
process.chdir(basePath);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
if (!existsSync(wtPath)) {
|
|
192
|
+
runGit(basePath, ["worktree", "prune"], { allowFailure: true });
|
|
193
|
+
if (deleteBranch) {
|
|
194
|
+
runGit(basePath, ["branch", "-D", branch], { allowFailure: true });
|
|
195
|
+
}
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// Force-remove to handle dirty worktrees
|
|
200
|
+
runGit(basePath, ["worktree", "remove", "--force", wtPath], { allowFailure: true });
|
|
201
|
+
|
|
202
|
+
// If the directory is still there (e.g. locked), try harder
|
|
203
|
+
if (existsSync(wtPath)) {
|
|
204
|
+
runGit(basePath, ["worktree", "remove", "--force", "--force", wtPath], { allowFailure: true });
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// Prune stale entries so git knows the worktree is gone
|
|
208
|
+
runGit(basePath, ["worktree", "prune"], { allowFailure: true });
|
|
209
|
+
|
|
210
|
+
if (deleteBranch) {
|
|
211
|
+
runGit(basePath, ["branch", "-D", branch], { allowFailure: true });
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Diff the .gsd/ directory between the worktree branch and main branch.
|
|
217
|
+
* Returns a summary of added, modified, and removed GSD artifacts.
|
|
218
|
+
*/
|
|
219
|
+
export function diffWorktreeGSD(basePath: string, name: string): WorktreeDiffSummary {
|
|
220
|
+
const branch = worktreeBranchName(name);
|
|
221
|
+
const mainBranch = getMainBranch(basePath);
|
|
222
|
+
|
|
223
|
+
// Use git diff to compare .gsd/ between branches
|
|
224
|
+
const diffOutput = runGit(basePath, [
|
|
225
|
+
"diff", "--name-status", `${mainBranch}...${branch}`, "--", ".gsd/",
|
|
226
|
+
], { allowFailure: true });
|
|
227
|
+
|
|
228
|
+
const added: string[] = [];
|
|
229
|
+
const modified: string[] = [];
|
|
230
|
+
const removed: string[] = [];
|
|
231
|
+
|
|
232
|
+
if (!diffOutput.trim()) return { added, modified, removed };
|
|
233
|
+
|
|
234
|
+
for (const line of diffOutput.split("\n").filter(Boolean)) {
|
|
235
|
+
const [status, ...pathParts] = line.split("\t");
|
|
236
|
+
const filePath = pathParts.join("\t");
|
|
237
|
+
|
|
238
|
+
// Skip worktree-internal paths (e.g. .gsd/worktrees/, .gsd/runtime/)
|
|
239
|
+
if (filePath.startsWith(".gsd/worktrees/") || filePath.startsWith(".gsd/runtime/")) continue;
|
|
240
|
+
// Skip gitignored runtime files
|
|
241
|
+
if (filePath === ".gsd/STATE.md" || filePath === ".gsd/auto.lock" || filePath === ".gsd/metrics.json") continue;
|
|
242
|
+
if (filePath.startsWith(".gsd/activity/")) continue;
|
|
243
|
+
|
|
244
|
+
switch (status) {
|
|
245
|
+
case "A": added.push(filePath); break;
|
|
246
|
+
case "M": modified.push(filePath); break;
|
|
247
|
+
case "D": removed.push(filePath); break;
|
|
248
|
+
default:
|
|
249
|
+
// Renames, copies — treat as modified
|
|
250
|
+
if (status?.startsWith("R") || status?.startsWith("C")) {
|
|
251
|
+
modified.push(filePath);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
return { added, modified, removed };
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* Get the full diff content for .gsd/ between the worktree branch and main.
|
|
261
|
+
* Returns the raw unified diff for LLM consumption.
|
|
262
|
+
*/
|
|
263
|
+
export function getWorktreeGSDDiff(basePath: string, name: string): string {
|
|
264
|
+
const branch = worktreeBranchName(name);
|
|
265
|
+
const mainBranch = getMainBranch(basePath);
|
|
266
|
+
|
|
267
|
+
return runGit(basePath, [
|
|
268
|
+
"diff", `${mainBranch}...${branch}`, "--", ".gsd/",
|
|
269
|
+
], { allowFailure: true });
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* Get commit log for the worktree branch since it diverged from main.
|
|
274
|
+
*/
|
|
275
|
+
export function getWorktreeLog(basePath: string, name: string): string {
|
|
276
|
+
const branch = worktreeBranchName(name);
|
|
277
|
+
const mainBranch = getMainBranch(basePath);
|
|
278
|
+
|
|
279
|
+
return runGit(basePath, [
|
|
280
|
+
"log", "--oneline", `${mainBranch}..${branch}`,
|
|
281
|
+
], { allowFailure: true });
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* Merge the worktree branch into main using squash merge.
|
|
286
|
+
* Must be called from the main working tree (not the worktree itself).
|
|
287
|
+
* Returns the merge commit message.
|
|
288
|
+
*/
|
|
289
|
+
export function mergeWorktreeToMain(basePath: string, name: string, commitMessage: string): string {
|
|
290
|
+
const branch = worktreeBranchName(name);
|
|
291
|
+
const mainBranch = getMainBranch(basePath);
|
|
292
|
+
const current = runGit(basePath, ["branch", "--show-current"]);
|
|
293
|
+
|
|
294
|
+
if (current !== mainBranch) {
|
|
295
|
+
throw new Error(`Must be on ${mainBranch} to merge. Currently on ${current}.`);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
runGit(basePath, ["merge", "--squash", branch]);
|
|
299
|
+
runGit(basePath, ["commit", "-m", commitMessage]);
|
|
300
|
+
|
|
301
|
+
return commitMessage;
|
|
302
|
+
}
|