The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 25, 19:45 UTC
kewl
Julius Marminge committed
Feb 9, 2026 at 13:28 UTC
2a610f92b9c6254119d13bbc83b50c9ca7d288b4
4 files changed
+61
-23
apps/desktop/src/git.test.ts
+41
-20
@@ -194,15 +194,19 @@ describe("git integration", () => {
194
// ── createGitWorktree + removeGitWorktree ──
195
196
describe("createGitWorktree", () => {
197
- it("creates a worktree directory at the expected path", async () => {
197
+ it("creates a worktree with a new branch from the base branch", async () => {
198
await using tmp = await makeTmpDir();
199
await initRepoWithCommit(tmp.path);
200
- await createGitBranch({ cwd: tmp.path, branch: "wt-branch" });
200
201
const wtPath = path.join(tmp.path, "worktree-out");
202
+ const currentBranch = (await listGitBranches({ cwd: tmp.path })).branches.find(
203
+ (b) => b.current,
204
+ )!.name;
205
+
206
const result = await createGitWorktree({
207
cwd: tmp.path,
205
- branch: "wt-branch",
208
+ branch: currentBranch,
209
+ newBranch: "wt-branch",
210
path: wtPath,
211
});
212
@@ -215,37 +219,44 @@ describe("git integration", () => {
219
await removeGitWorktree({ cwd: tmp.path, path: wtPath });
220
});
221
218
- it("worktree has the correct branch checked out", async () => {
222
+ it("worktree has the new branch checked out", async () => {
223
await using tmp = await makeTmpDir();
224
await initRepoWithCommit(tmp.path);
221
- await createGitBranch({ cwd: tmp.path, branch: "wt-check" });
225
226
const wtPath = path.join(tmp.path, "wt-check-dir");
227
+ const currentBranch = (await listGitBranches({ cwd: tmp.path })).branches.find(
228
+ (b) => b.current,
229
+ )!.name;
230
+
231
await createGitWorktree({
232
cwd: tmp.path,
226
- branch: "wt-check",
233
+ branch: currentBranch,
234
+ newBranch: "wt-check",
235
path: wtPath,
236
});
237
230
- // Verify the worktree is on the right branch
238
+ // Verify the worktree is on the new branch
239
const branchOutput = await git(wtPath, "branch --show-current");
240
expect(branchOutput).toBe("wt-check");
241
242
await removeGitWorktree({ cwd: tmp.path, path: wtPath });
243
});
244
237
- it("throws when branch is already checked out in main worktree", async () => {
245
+ it("throws when new branch name already exists", async () => {
246
await using tmp = await makeTmpDir();
247
await initRepoWithCommit(tmp.path);
240
- // Try to create a worktree for the current branch (already checked out)
241
- const branches = await listGitBranches({ cwd: tmp.path });
242
- const currentBranch = branches.branches.find((b) => b.current)!.name;
248
+ await createGitBranch({ cwd: tmp.path, branch: "existing" });
249
250
const wtPath = path.join(tmp.path, "wt-conflict");
251
+ const currentBranch = (await listGitBranches({ cwd: tmp.path })).branches.find(
252
+ (b) => b.current,
253
+ )!.name;
254
+
255
await expect(
256
createGitWorktree({
257
cwd: tmp.path,
258
branch: currentBranch,
259
+ newBranch: "existing",
260
path: wtPath,
261
}),
262
).rejects.toThrow();
@@ -254,12 +265,16 @@ describe("git integration", () => {
265
it("removeGitWorktree cleans up the worktree", async () => {
266
await using tmp = await makeTmpDir();
267
await initRepoWithCommit(tmp.path);
257
- await createGitBranch({ cwd: tmp.path, branch: "wt-remove" });
268
269
const wtPath = path.join(tmp.path, "wt-remove-dir");
270
+ const currentBranch = (await listGitBranches({ cwd: tmp.path })).branches.find(
271
+ (b) => b.current,
272
+ )!.name;
273
+
274
await createGitWorktree({
275
cwd: tmp.path,
262
- branch: "wt-remove",
276
+ branch: currentBranch,
277
+ newBranch: "wt-remove",
278
path: wtPath,
279
});
280
expect(existsSync(wtPath)).toBe(true);
@@ -284,18 +299,22 @@ describe("git integration", () => {
299
});
300
});
301
287
- // ── Full flow: worktree creation from selected branch ──
302
+ // ── Full flow: worktree creation from base branch ──
303
304
describe("full flow: worktree creation", () => {
290
- it("creates worktree from a non-current branch", async () => {
305
+ it("creates worktree with new branch from current branch", async () => {
306
await using tmp = await makeTmpDir();
307
await initRepoWithCommit(tmp.path);
293
- await createGitBranch({ cwd: tmp.path, branch: "feature-wt" });
308
+
309
+ const currentBranch = (await listGitBranches({ cwd: tmp.path })).branches.find(
310
+ (b) => b.current,
311
+ )!.name;
312
313
const wtPath = path.join(tmp.path, "my-worktree");
314
const result = await createGitWorktree({
315
cwd: tmp.path,
298
- branch: "feature-wt",
316
+ branch: currentBranch,
317
+ newBranch: "feature-wt",
318
path: wtPath,
319
});
320
@@ -305,9 +324,9 @@ describe("git integration", () => {
324
// Main repo still on original branch
325
const mainBranches = await listGitBranches({ cwd: tmp.path });
326
const mainCurrent = mainBranches.branches.find((b) => b.current);
308
- expect(mainCurrent!.name).not.toBe("feature-wt");
327
+ expect(mainCurrent!.name).toBe(currentBranch);
328
310
- // Worktree is on feature-wt
329
+ // Worktree is on the new branch
330
const wtBranch = await git(wtPath, "branch --show-current");
331
expect(wtBranch).toBe("feature-wt");
332
@@ -356,7 +375,9 @@ describe("git integration", () => {
375
await git(tmp.path, "commit -m 'diverge'");
376
377
// Go back to default branch
359
- const defaultBranch = (await git(tmp.path, "rev-parse --abbrev-ref HEAD")).includes("diverged")
378
+ const defaultBranch = (await git(tmp.path, "rev-parse --abbrev-ref HEAD")).includes(
379
+ "diverged",
380
+ )
381
? // we're on diverged, need to find the other one
382
(await listGitBranches({ cwd: tmp.path })).branches.find(
383
(b) => !b.current && b.name !== "diverged",
apps/desktop/src/git.ts
+5
-3
@@ -123,14 +123,16 @@ export async function listGitBranches(
123
export async function createGitWorktree(
124
input: GitCreateWorktreeInput,
125
): Promise<GitCreateWorktreeResult> {
126
- const sanitizedBranch = input.branch.replace(/\//g, "-");
126
+ const esc = (s: string) => s.replace(/'/g, "'\\''");
127
+ const sanitizedBranch = input.newBranch.replace(/\//g, "-");
128
const repoName = path.basename(input.cwd);
129
const worktreePath =
130
input.path ??
131
path.join(input.cwd, "..", `${repoName}-worktrees`, sanitizedBranch);
132
133
+ // Create a new branch from the base branch in a new worktree
134
const result = await runTerminalCommand({
133
- command: `git worktree add '${worktreePath.replace(/'/g, "'\\''")}' '${input.branch.replace(/'/g, "'\\''")}'`,
135
+ command: `git worktree add -b '${esc(input.newBranch)}' '${esc(worktreePath)}' '${esc(input.branch)}'`,
136
cwd: input.cwd,
137
timeoutMs: 30_000,
138
});
@@ -142,7 +144,7 @@ export async function createGitWorktree(
144
return {
145
worktree: {
146
path: worktreePath,
145
- branch: input.branch,
147
+ branch: input.newBranch,
148
},
149
};
150
}
apps/web/src/hooks/useGitState.ts
new
+12
@@ -0,0 +1,12 @@
1
+import { queryOptions, skipToken } from "@tanstack/react-query";
2
+import { readNativeApi } from "../session-logic";
3
+
4
+export const listBranchesQuery = (cwd: string | undefined) => {
5
+ const api = readNativeApi();
6
+
7
+ return queryOptions({
8
+ queryKey: ["git-branches", cwd],
9
+ queryFn: api && cwd ? () => api.git.listBranches({ cwd }) : skipToken,
10
+ refetchOnWindowFocus: true,
11
+ });
12
+};
packages/contracts/src/git.ts
+3
@@ -8,7 +8,10 @@ export const gitListBranchesInputSchema = z.object({
8
9
export const gitCreateWorktreeInputSchema = z.object({
10
cwd: z.string().min(1),
11
+ /** Base branch to create the worktree from. */
12
branch: z.string().min(1),
13
+ /** New branch name to create in the worktree. */
14
+ newBranch: z.string().min(1),
15
path: z.string().min(1).optional(),
16
});
17