Skip to content

Commit 74a922b

Browse files
juliusmarmingecodex
andcommitted
Generate temporary worktree suffixes from random hex
Co-authored-by: codex <[email protected]>
1 parent 667516c commit 74a922b

4 files changed

Lines changed: 16 additions & 6 deletions

File tree

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ import { resolveShortcutCommand, shortcutLabelForCommand } from "../keybindings"
105105
import PlanSidebar from "./PlanSidebar";
106106
import ThreadTerminalDrawer from "./ThreadTerminalDrawer";
107107
import { ChevronDownIcon, TriangleAlertIcon, WifiOffIcon } from "lucide-react";
108-
import { cn, randomUUID } from "~/lib/utils";
108+
import { cn, randomHex, randomUUID } from "~/lib/utils";
109109
import { stackedThreadToast, toastManager } from "./ui/toast";
110110
import { decodeProjectScriptKeybindingRule } from "~/lib/projectScriptKeybindings";
111111
import { type NewProjectScriptInput } from "./ProjectScriptsControl";
@@ -2846,7 +2846,7 @@ export default function ChatView(props: ChatViewProps) {
28462846
prepareWorktree: {
28472847
projectCwd: activeProject.cwd,
28482848
baseBranch: baseBranchForWorktree,
2849-
branch: buildTemporaryWorktreeBranchName(randomUUID),
2849+
branch: buildTemporaryWorktreeBranchName(randomHex),
28502850
},
28512851
runSetupScript: true,
28522852
}

‎apps/web/src/lib/utils.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { CommandId, MessageId, ProjectId, ThreadId } from "@t3tools/contracts";
22
import { type CxOptions, cx } from "class-variance-authority";
3+
import * as Encoding from "effect/Encoding";
34
import { twMerge } from "tailwind-merge";
45
import { DraftId } from "../composerDraftStore";
56

@@ -19,11 +20,15 @@ export function isLinuxPlatform(platform: string): boolean {
1920
return /linux/i.test(platform);
2021
}
2122

23+
export function randomHex(byteLength: number): string {
24+
return Encoding.encodeHex(globalThis.crypto.getRandomValues(new Uint8Array(byteLength)));
25+
}
26+
2227
export function randomUUID(): string {
2328
const bytes = globalThis.crypto.getRandomValues(new Uint8Array(16));
2429
bytes[6] = (bytes[6]! & 0x0f) | 0x40;
2530
bytes[8] = (bytes[8]! & 0x3f) | 0x80;
26-
const hex = Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0")).join("");
31+
const hex = Encoding.encodeHex(bytes);
2732
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
2833
}
2934

‎packages/shared/src/git.test.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ describe("isTemporaryWorktreeBranch", () => {
5757
it("matches the generated temporary worktree refName format", () => {
5858
expect(
5959
isTemporaryWorktreeBranch(
60-
buildTemporaryWorktreeBranchName(() => "deadbeef-dead-4eef-8ead-deadbeefdead"),
60+
buildTemporaryWorktreeBranchName((byteLength) => {
61+
expect(byteLength).toBe(4);
62+
return "DEADBEEF";
63+
}),
6164
),
6265
).toBe(true);
6366
});

‎packages/shared/src/git.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,10 @@ export function deriveLocalBranchNameFromRemoteRef(branchName: string): string {
8484
return branchName.slice(firstSeparatorIndex + 1);
8585
}
8686

87-
export function buildTemporaryWorktreeBranchName(randomUUID: () => string): string {
88-
const token = randomUUID().replace(/-/g, "").slice(0, 8).toLowerCase();
87+
export function buildTemporaryWorktreeBranchName(
88+
randomHex: (byteLength: number) => string,
89+
): string {
90+
const token = randomHex(4).toLowerCase();
8991
return `${WORKTREE_BRANCH_PREFIX}/${token}`;
9092
}
9193

0 commit comments

Comments
 (0)