diff --git a/apps/desktop/src/git.test.ts b/apps/desktop/src/git.test.ts index 0683069558069216ddbdfddfafdbb2c4e10877c5..dd73725c03240c5079dd94178c3684fb3a409133 100644 --- a/apps/desktop/src/git.test.ts +++ b/apps/desktop/src/git.test.ts @@ -404,16 +404,6 @@ describe("git integration", () => { await git(tmp.path, "add ."); await git(tmp.path, "commit -m 'diverge'"); - // Go back to default branch - const defaultBranch = (await git(tmp.path, "rev-parse --abbrev-ref HEAD")).includes( - "diverged", - ) - ? // we're on diverged, need to find the other one - (await listGitBranches({ cwd: tmp.path })).branches.find( - (b) => !b.current && b.name !== "diverged", - )!.name - : await git(tmp.path, "rev-parse --abbrev-ref HEAD"); - // Actually, let's just get back to the initial branch explicitly const allBranches = await listGitBranches({ cwd: tmp.path }); const initialBranch = allBranches.branches.find((b) => b.name !== "diverged")!.name; diff --git a/apps/desktop/src/git.ts b/apps/desktop/src/git.ts index 072fc37e4ce2ac5681d0deda58eef3b7a2375af9..9273672834af3906977136dff6efdd9dc3b979c6 100644 --- a/apps/desktop/src/git.ts +++ b/apps/desktop/src/git.ts @@ -14,6 +14,10 @@ import type { TerminalCommandResult, } from "@t3tools/contracts"; +function escapeSingleQuotes(value: string): string { + return value.replace(/'/g, "'\\''"); +} + export async function runTerminalCommand( input: TerminalCommandInput, ): Promise { @@ -111,7 +115,7 @@ export async function listGitBranches( current: line.startsWith("* "), isDefault: line.replace(/^[*+]\s+/, "") === defaultBranch, })) - .sort((a, b) => { + .toSorted((a, b) => { if (a.current !== b.current) return a.current ? -1 : 1; if (a.isDefault !== b.isDefault) return a.isDefault ? -1 : 1; return a.name.localeCompare(b.name); @@ -123,7 +127,6 @@ export async function listGitBranches( export async function createGitWorktree( input: GitCreateWorktreeInput, ): Promise { - const esc = (s: string) => s.replace(/'/g, "'\\''"); const sanitizedBranch = input.newBranch.replace(/\//g, "-"); const repoName = path.basename(input.cwd); const worktreePath = @@ -132,7 +135,7 @@ export async function createGitWorktree( // Create a new branch from the base branch in a new worktree const result = await runTerminalCommand({ - command: `git worktree add -b '${esc(input.newBranch)}' '${esc(worktreePath)}' '${esc(input.branch)}'`, + command: `git worktree add -b '${escapeSingleQuotes(input.newBranch)}' '${escapeSingleQuotes(worktreePath)}' '${escapeSingleQuotes(input.branch)}'`, cwd: input.cwd, timeoutMs: 30_000, }); @@ -153,7 +156,7 @@ export async function removeGitWorktree( input: GitRemoveWorktreeInput, ): Promise { const result = await runTerminalCommand({ - command: `git worktree remove '${input.path.replace(/'/g, "'\\''")}'`, + command: `git worktree remove '${escapeSingleQuotes(input.path)}'`, cwd: input.cwd, timeoutMs: 15_000, }); @@ -167,7 +170,7 @@ export async function createGitBranch( input: GitCreateBranchInput, ): Promise { const result = await runTerminalCommand({ - command: `git branch '${input.branch.replace(/'/g, "'\\''")}'`, + command: `git branch '${escapeSingleQuotes(input.branch)}'`, cwd: input.cwd, timeoutMs: 10_000, }); @@ -181,7 +184,7 @@ export async function checkoutGitBranch( input: GitCheckoutInput, ): Promise { const result = await runTerminalCommand({ - command: `git checkout '${input.branch.replace(/'/g, "'\\''")}'`, + command: `git checkout '${escapeSingleQuotes(input.branch)}'`, cwd: input.cwd, timeoutMs: 10_000, });