History

Polish git worktree helpers after rebase

Julius Marminge committed 9 months ago

Commit d1ecacd1 parent 64744902 files · +9 −16Browse files

Unified · Split

2 files
  1. apps/desktop/src/git.test.ts +0 −10
  2. apps/desktop/src/git.ts +9 −6
apps/desktop/src/git.test.ts+0−10
@@ -404,16 +404,6 @@ describe("git integration", () => { · 403 unmodified lines
404404 await git(tmp.path, "add .");
405405 await git(tmp.path, "commit -m 'diverge'");
406406
407− // Go back to default branch
408− const defaultBranch = (await git(tmp.path, "rev-parse --abbrev-ref HEAD")).includes(
409− "diverged",
410− )
411− ? // we're on diverged, need to find the other one
412− (await listGitBranches({ cwd: tmp.path })).branches.find(
413− (b) => !b.current && b.name !== "diverged",
414− )!.name
415− : await git(tmp.path, "rev-parse --abbrev-ref HEAD");
416−
417407 // Actually, let's just get back to the initial branch explicitly
418408 const allBranches = await listGitBranches({ cwd: tmp.path });
419409 const initialBranch = allBranches.branches.find((b) => b.name !== "diverged")!.name;
apps/desktop/src/git.ts+9−6
@@ -14,6 +14,10 @@ import type { · 13 unmodified lines
1414 TerminalCommandResult,
1515 } from "@t3tools/contracts";
1616
17+function escapeSingleQuotes(value: string): string {
18+ return value.replace(/'/g, "'\\''");
19+}
20+
1721 export async function runTerminalCommand(
1822 input: TerminalCommandInput,
1923 ): Promise<TerminalCommandResult> {
@@ -111,7 +115,7 @@ export async function listGitBranches( · 91 unmodified lines
111115 current: line.startsWith("* "),
112116 isDefault: line.replace(/^[*+]\s+/, "") === defaultBranch,
113117 }))
114− .sort((a, b) => {
118+ .toSorted((a, b) => {
115119 if (a.current !== b.current) return a.current ? -1 : 1;
116120 if (a.isDefault !== b.isDefault) return a.isDefault ? -1 : 1;
117121 return a.name.localeCompare(b.name);
@@ -123,7 +127,6 @@ export async function listGitBranches( · 5 unmodified lines
123127 export async function createGitWorktree(
124128 input: GitCreateWorktreeInput,
125129 ): Promise<GitCreateWorktreeResult> {
126− const esc = (s: string) => s.replace(/'/g, "'\\''");
127130 const sanitizedBranch = input.newBranch.replace(/\//g, "-");
128131 const repoName = path.basename(input.cwd);
129132 const worktreePath =
@@ -132,7 +135,7 @@ export async function createGitWorktree( · 2 unmodified lines
132135
133136 // Create a new branch from the base branch in a new worktree
134137 const result = await runTerminalCommand({
135− command: `git worktree add -b '${esc(input.newBranch)}' '${esc(worktreePath)}' '${esc(input.branch)}'`,
138+ command: `git worktree add -b '${escapeSingleQuotes(input.newBranch)}' '${escapeSingleQuotes(worktreePath)}' '${escapeSingleQuotes(input.branch)}'`,
136139 cwd: input.cwd,
137140 timeoutMs: 30_000,
138141 });
@@ -153,7 +156,7 @@ export async function removeGitWorktree( · 14 unmodified lines
153156 input: GitRemoveWorktreeInput,
154157 ): Promise<void> {
155158 const result = await runTerminalCommand({
156− command: `git worktree remove '${input.path.replace(/'/g, "'\\''")}'`,
159+ command: `git worktree remove '${escapeSingleQuotes(input.path)}'`,
157160 cwd: input.cwd,
158161 timeoutMs: 15_000,
159162 });
@@ -167,7 +170,7 @@ export async function createGitBranch( · 7 unmodified lines
167170 input: GitCreateBranchInput,
168171 ): Promise<void> {
169172 const result = await runTerminalCommand({
170− command: `git branch '${input.branch.replace(/'/g, "'\\''")}'`,
173+ command: `git branch '${escapeSingleQuotes(input.branch)}'`,
171174 cwd: input.cwd,
172175 timeoutMs: 10_000,
173176 });
@@ -181,7 +184,7 @@ export async function checkoutGitBranch( · 7 unmodified lines
181184 input: GitCheckoutInput,
182185 ): Promise<void> {
183186 const result = await runTerminalCommand({
184− command: `git checkout '${input.branch.replace(/'/g, "'\\''")}'`,
187+ command: `git checkout '${escapeSingleQuotes(input.branch)}'`,
185188 cwd: input.cwd,
186189 timeoutMs: 10_000,
187190 });

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t