@upend/cli 0.1.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.
@@ -0,0 +1,151 @@
1
+ // git worktree management for isolated editing sessions
2
+
3
+ const PROJECT_ROOT = process.env.UPEND_PROJECT || process.cwd();
4
+ const SESSIONS_DIR = `${PROJECT_ROOT}/sessions`;
5
+
6
+ // word lists for generating session names
7
+ const adjectives = ["bright","calm","cool","dark","eager","fast","gentle","happy","keen","lively","neat","proud","quick","rare","sharp","swift","warm","wise","bold","crisp"];
8
+ const nouns = ["anchor","beacon","castle","delta","ember","falcon","grove","harbor","island","jade","kite","lantern","mesa","north","oasis","peak","quartz","ridge","summit","tide"];
9
+
10
+ export function generateSessionName(): string {
11
+ const adj = adjectives[Math.floor(Math.random() * adjectives.length)];
12
+ const noun = nouns[Math.floor(Math.random() * nouns.length)];
13
+ const num = Math.floor(Math.random() * 100);
14
+ return `${adj}-${noun}-${num}`;
15
+ }
16
+
17
+ async function git(...args: string[]): Promise<{ stdout: string; stderr: string; exitCode: number }> {
18
+ const proc = Bun.spawn(["git", ...args], {
19
+ cwd: PROJECT_ROOT,
20
+ stdout: "pipe",
21
+ stderr: "pipe",
22
+ });
23
+ const stdout = await new Response(proc.stdout).text();
24
+ const stderr = await new Response(proc.stderr).text();
25
+ const exitCode = await proc.exited;
26
+ return { stdout: stdout.trim(), stderr: stderr.trim(), exitCode };
27
+ }
28
+
29
+ async function gitIn(cwd: string, ...args: string[]): Promise<{ stdout: string; stderr: string; exitCode: number }> {
30
+ const proc = Bun.spawn(["git", ...args], {
31
+ cwd,
32
+ stdout: "pipe",
33
+ stderr: "pipe",
34
+ });
35
+ const stdout = await new Response(proc.stdout).text();
36
+ const stderr = await new Response(proc.stderr).text();
37
+ const exitCode = await proc.exited;
38
+ return { stdout: stdout.trim(), stderr: stderr.trim(), exitCode };
39
+ }
40
+
41
+ // create a worktree for a new session
42
+ export async function createWorktree(name: string): Promise<{ path: string; branch: string }> {
43
+ const branch = `session/${name}`;
44
+ const worktreePath = `${SESSIONS_DIR}/${name}`;
45
+
46
+ // ensure sessions dir exists
47
+ await Bun.spawn(["mkdir", "-p", SESSIONS_DIR]).exited;
48
+
49
+ // create branch from current HEAD
50
+ const result = await git("worktree", "add", "-b", branch, worktreePath);
51
+ if (result.exitCode !== 0) {
52
+ throw new Error(`failed to create worktree: ${result.stderr}`);
53
+ }
54
+
55
+ // copy node_modules symlink so bun works in the worktree
56
+ await Bun.spawn(["ln", "-sf", `${PROJECT_ROOT}/node_modules`, `${worktreePath}/node_modules`]).exited;
57
+ // copy .env.keys so dotenvx works
58
+ await Bun.spawn(["cp", `${PROJECT_ROOT}/.env.keys`, `${worktreePath}/.env.keys`]).exited;
59
+ // copy .keys for JWT
60
+ await Bun.spawn(["cp", "-r", `${PROJECT_ROOT}/.keys`, `${worktreePath}/.keys`]).exited;
61
+
62
+ console.log(`[worktree] created ${name} at ${worktreePath} (branch: ${branch})`);
63
+ return { path: worktreePath, branch };
64
+ }
65
+
66
+ // commit all changes in a worktree
67
+ export async function commitWorktree(name: string, message: string): Promise<string> {
68
+ const worktreePath = `${SESSIONS_DIR}/${name}`;
69
+ await gitIn(worktreePath, "add", "-A");
70
+ const result = await gitIn(worktreePath, "commit", "-m", message, "--allow-empty");
71
+ console.log(`[worktree] committed ${name}: ${result.stdout}`);
72
+ return result.stdout;
73
+ }
74
+
75
+ // check if a session can merge cleanly into live
76
+ export async function checkMergeable(name: string): Promise<{ mergeable: boolean; conflicts: string[] }> {
77
+ const branch = `session/${name}`;
78
+
79
+ // auto-commit any pending changes in the worktree first
80
+ const worktreePath = `${SESSIONS_DIR}/${name}`;
81
+ await gitIn(worktreePath, "add", "-A");
82
+ await gitIn(worktreePath, "commit", "-m", `auto-commit before merge check`, "--allow-empty");
83
+
84
+ // try a dry-run merge
85
+ const result = await git("merge", "--no-commit", "--no-ff", branch);
86
+
87
+ if (result.exitCode === 0) {
88
+ // clean merge — abort it (we were just checking)
89
+ await git("merge", "--abort");
90
+ return { mergeable: true, conflicts: [] };
91
+ }
92
+
93
+ // get conflict list
94
+ const conflictResult = await git("diff", "--name-only", "--diff-filter=U");
95
+ const conflicts = conflictResult.stdout.split("\n").filter(Boolean);
96
+
97
+ // abort the failed merge
98
+ await git("merge", "--abort");
99
+
100
+ return { mergeable: false, conflicts };
101
+ }
102
+
103
+ // merge a session into live (main branch)
104
+ export async function mergeToLive(name: string, user: string): Promise<{ success: boolean; message: string }> {
105
+ const branch = `session/${name}`;
106
+ const worktreePath = `${SESSIONS_DIR}/${name}`;
107
+
108
+ // commit any pending changes in the worktree
109
+ await gitIn(worktreePath, "add", "-A");
110
+ await gitIn(worktreePath, "commit", "-m", `session ${name}: final changes`, "--allow-empty");
111
+
112
+ // merge into main
113
+ const result = await git("merge", branch, "-m", `merge session ${name} (by ${user})`);
114
+
115
+ if (result.exitCode !== 0) {
116
+ console.error(`[worktree] merge failed for ${name}: ${result.stderr}`);
117
+ // abort failed merge
118
+ await git("merge", "--abort");
119
+ return { success: false, message: `merge conflict: ${result.stderr}` };
120
+ }
121
+
122
+ console.log(`[worktree] merged ${name} into live: ${result.stdout}`);
123
+ return { success: true, message: result.stdout };
124
+ }
125
+
126
+ // clean up a worktree after merge
127
+ export async function removeWorktree(name: string): Promise<void> {
128
+ const worktreePath = `${SESSIONS_DIR}/${name}`;
129
+ const branch = `session/${name}`;
130
+ await git("worktree", "remove", worktreePath, "--force");
131
+ await git("branch", "-D", branch);
132
+ console.log(`[worktree] removed ${name}`);
133
+ }
134
+
135
+ // list active worktrees
136
+ export async function listWorktrees(): Promise<string[]> {
137
+ const result = await git("worktree", "list", "--porcelain");
138
+ const worktrees = result.stdout.split("\n\n")
139
+ .filter(block => block.includes("/sessions/"))
140
+ .map(block => {
141
+ const match = block.match(/worktree .*\/sessions\/(.+)/);
142
+ return match ? match[1] : null;
143
+ })
144
+ .filter(Boolean) as string[];
145
+ return worktrees;
146
+ }
147
+
148
+ // get the worktree path for a session
149
+ export function getWorktreePath(name: string): string {
150
+ return `${SESSIONS_DIR}/${name}`;
151
+ }