Polish git worktree helpers after rebase
Co-authored-by: codex <[email protected]>
This commit is contained in:
1 parent
647449069d
commit
d1ecacda43
2 files changed
+9
-16
No files matched your search
@@ -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;
|
||||
|
||||
@@ -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<TerminalCommandResult> {
|
||||
@@ -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<GitCreateWorktreeResult> {
|
||||
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<void> {
|
||||
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<void> {
|
||||
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<void> {
|
||||
const result = await runTerminalCommand({
|
||||
command: `git checkout '${input.branch.replace(/'/g, "'\\''")}'`,
|
||||
command: `git checkout '${escapeSingleQuotes(input.branch)}'`,
|
||||
cwd: input.cwd,
|
||||
timeoutMs: 10_000,
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user