Repository navigation
Feat/custom worktree branch name #6428
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
80dd94d
e14def9
4932a30
efd6128
5285085
0eadfa0
4e276cf
024f743
64bb942
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,6 +28,10 @@ import { | |
| import { BranchToolbarBranchSelector } from "./BranchToolbarBranchSelector"; | ||
| import { BranchToolbarEnvironmentSelector } from "./BranchToolbarEnvironmentSelector"; | ||
| import { BranchToolbarEnvModeSelector } from "./BranchToolbarEnvModeSelector"; | ||
| import { | ||
| BranchToolbarWorktreeNameInput, | ||
| type WorktreeBranchNameStatus, | ||
| } from "./BranchToolbarWorktreeNameInput"; | ||
| import { Button } from "./ui/button"; | ||
| import { | ||
| Menu, | ||
|
|
@@ -52,6 +56,7 @@ interface BranchToolbarProps { | |
| onActiveThreadBranchOverrideChange?: (branch: string | null) => void; | ||
| startFromOrigin: boolean; | ||
| onStartFromOriginChange: (startFromOrigin: boolean) => void; | ||
| onWorktreeBranchNameStatusChange?: (status: WorktreeBranchNameStatus | null) => void; | ||
| envLocked: boolean; | ||
| onCheckoutPullRequestRequest?: (reference: string) => void; | ||
| onComposerFocusRequest?: () => void; | ||
|
|
@@ -126,7 +131,7 @@ const MobileRunContextSelector = memo(function MobileRunContextSelector({ | |
|
|
||
| if (isLocked) { | ||
| return ( | ||
| <span className="inline-flex h-7 min-w-0 max-w-[48%] flex-1 items-center justify-start gap-1 rounded-md border border-transparent px-[calc(--spacing(2)-1px)] text-sm font-medium text-muted-foreground/70 sm:h-6 md:hidden"> | ||
| <span className="inline-flex h-7 min-w-0 max-w-[48%] shrink items-center justify-start gap-1 rounded-md border border-transparent px-[calc(--spacing(2)-1px)] text-sm font-medium text-muted-foreground/70 sm:h-6 md:hidden"> | ||
| {triggerContent} | ||
| </span> | ||
| ); | ||
|
|
@@ -136,7 +141,7 @@ const MobileRunContextSelector = memo(function MobileRunContextSelector({ | |
| <Menu> | ||
| <MenuTrigger | ||
| render={<Button variant="ghost" size="xs" />} | ||
| className="min-w-0 max-w-[48%] flex-1 justify-start text-muted-foreground/70 hover:text-foreground/80 md:hidden" | ||
| className="min-w-0 max-w-[48%] shrink justify-start text-muted-foreground/70 hover:text-foreground/80 md:hidden" | ||
| > | ||
| {triggerContent} | ||
| <ChevronDownIcon className="size-3 shrink-0 opacity-50" /> | ||
|
|
@@ -384,6 +389,7 @@ export const BranchToolbar = memo(function BranchToolbar({ | |
| onActiveThreadBranchOverrideChange, | ||
| startFromOrigin, | ||
| onStartFromOriginChange, | ||
| onWorktreeBranchNameStatusChange, | ||
| envLocked, | ||
| onCheckoutPullRequestRequest, | ||
| onComposerFocusRequest, | ||
|
|
@@ -463,29 +469,64 @@ export const BranchToolbar = memo(function BranchToolbar({ | |
| const [stripElement, setStripElement] = useState<HTMLDivElement | null>(null); | ||
| const labelsOverflow = useLabelsOverflow(stripElement); | ||
|
|
||
| // Naming the next worktree's branch only makes sense before the first send, | ||
| // when a brand-new worktree (not a reused one) is about to be created. | ||
| const showWorktreeNameInput = | ||
| showGitControls && | ||
| serverThread === null && | ||
| draftThread !== null && | ||
| effectiveEnvMode === "worktree" && | ||
| activeWorktreePath === null && | ||
| !envLocked; | ||
| const onWorktreeBranchNameChange = useCallback( | ||
| (value: string) => { | ||
| setDraftThreadContext(draftId ?? threadRef, { | ||
| worktreeBranchName: value.length > 0 ? value : null, | ||
| }); | ||
| }, | ||
| [draftId, setDraftThreadContext, threadRef], | ||
| ); | ||
|
|
||
| if (!hasActiveThread || !activeProject) return null; | ||
|
|
||
| // Rendered in both layouts: a stored name the user can't see is a name they | ||
| // can't clear, and the send path ignores it while the input is unmounted. | ||
| const worktreeNameInput = showWorktreeNameInput ? ( | ||
| <BranchToolbarWorktreeNameInput | ||
| environmentId={environmentId} | ||
| cwd={activeProject.workspaceRoot} | ||
| value={draftThread?.worktreeBranchName ?? ""} | ||
| onValueChange={onWorktreeBranchNameChange} | ||
| {...(onWorktreeBranchNameStatusChange | ||
| ? { onStatusChange: onWorktreeBranchNameStatusChange } | ||
| : {})} | ||
| /> | ||
| ) : null; | ||
|
|
||
| return ( | ||
| <div | ||
| ref={setStripElement} | ||
| data-compact={labelsOverflow ? "" : undefined} | ||
| className="chat-composer-context-strip group/composer-context -mt-4 mx-auto flex w-[calc(100%-2.75rem)] max-w-[calc(48rem-2.75rem)] items-center gap-2 overflow-x-clip overflow-y-visible ps-1 pe-2 pt-5 pb-1" | ||
| > | ||
| {isMobile && showGitControls ? ( | ||
| <MobileRunContextSelector | ||
| envLocked={envLocked} | ||
| envModeLocked={envModeLocked} | ||
| environmentId={environmentId} | ||
| availableEnvironments={availableEnvironments} | ||
| showEnvironmentPicker={showEnvironmentPicker} | ||
| showEnvironmentIndicator={showEnvironmentIndicator} | ||
| onEnvironmentChange={onEnvironmentChange} | ||
| effectiveEnvMode={effectiveEnvMode} | ||
| activeWorktreePath={activeWorktreePath} | ||
| onEnvModeChange={onEnvModeChange} | ||
| previousWorktreeLabel={previousWorktreeLabel} | ||
| onUsePreviousWorktree={onUsePreviousWorktree} | ||
| /> | ||
| <> | ||
| <MobileRunContextSelector | ||
| envLocked={envLocked} | ||
| envModeLocked={envModeLocked} | ||
| environmentId={environmentId} | ||
| availableEnvironments={availableEnvironments} | ||
| showEnvironmentPicker={showEnvironmentPicker} | ||
| showEnvironmentIndicator={showEnvironmentIndicator} | ||
| onEnvironmentChange={onEnvironmentChange} | ||
| effectiveEnvMode={effectiveEnvMode} | ||
| activeWorktreePath={activeWorktreePath} | ||
| onEnvModeChange={onEnvModeChange} | ||
| previousWorktreeLabel={previousWorktreeLabel} | ||
| onUsePreviousWorktree={onUsePreviousWorktree} | ||
| /> | ||
| {worktreeNameInput} | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the mobile branch this input becomes a direct child of the strip, but Because the same undercount applies in both directions, the expand test in compact mode ( Smallest fix: give the mobile pair the same group container the desktop layout uses, e.g. wrap Posted via Macroscope — UI Consistency |
||
| </> | ||
| ) : ( | ||
| <div className="flex min-w-0 flex-1 items-center gap-1"> | ||
| {showEnvironmentIndicator && availableEnvironments && ( | ||
|
|
@@ -515,12 +556,13 @@ export const BranchToolbar = memo(function BranchToolbar({ | |
| onUsePreviousWorktree={onUsePreviousWorktree} | ||
| /> | ||
| ) : null} | ||
| {worktreeNameInput} | ||
| </div> | ||
| )} | ||
|
|
||
| {showGitControls ? ( | ||
| <BranchToolbarBranchSelector | ||
| className="min-w-0 flex-1 justify-end md:ml-auto md:flex-none" | ||
| className="min-w-0 flex-auto justify-end md:ml-auto md:flex-none" | ||
| environmentId={environmentId} | ||
| threadId={threadId} | ||
| {...(draftId ? { draftId } : {})} | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| import type { EnvironmentId } from "@t3tools/contracts"; | ||
| import { normalizeWorktreeBranchName, sanitizeWorktreeBranchNameInput } from "@t3tools/shared/git"; | ||
| import { useDeferredValue, useEffect, useRef } from "react"; | ||
|
|
||
| import { cn } from "../lib/utils"; | ||
| import { Tooltip, TooltipPopup, TooltipTrigger } from "./ui/tooltip"; | ||
| import { useEnvironmentQuery } from "../state/query"; | ||
| import { vcsEnvironment } from "../state/vcs"; | ||
|
|
||
| /** | ||
| * What the send path knows about the typed name. `null` means no name is in | ||
| * play (empty input, or the input isn't mounted to validate one), so the send | ||
| * path must fall back to the generated branch name. | ||
| */ | ||
| export interface WorktreeBranchNameStatus { | ||
| /** The normalized name — the branch that would actually be created. */ | ||
| name: string; | ||
| state: "checking" | "available" | "conflict"; | ||
| } | ||
|
|
||
| interface BranchToolbarWorktreeNameInputProps { | ||
| environmentId: EnvironmentId; | ||
| /** Project root the refs are checked against (the worktree doesn't exist yet). */ | ||
| cwd: string; | ||
| value: string; | ||
| onValueChange: (value: string) => void; | ||
| onStatusChange?: (status: WorktreeBranchNameStatus | null) => void; | ||
| } | ||
|
|
||
| /** | ||
| * Low-profile input naming the branch the next worktree is created with. | ||
| * Left empty, the branch name is generated from the first message instead. | ||
| * Marks itself invalid when the name collides with an existing local branch. | ||
| */ | ||
| export function BranchToolbarWorktreeNameInput({ | ||
| environmentId, | ||
| cwd, | ||
| value, | ||
| onValueChange, | ||
| onStatusChange, | ||
| }: BranchToolbarWorktreeNameInputProps) { | ||
| const normalizedValue = normalizeWorktreeBranchName(value); | ||
| const deferredNormalizedValue = useDeferredValue(normalizedValue); | ||
| const conflictRefsQuery = useEnvironmentQuery( | ||
| deferredNormalizedValue === null | ||
| ? null | ||
| : vcsEnvironment.listRefs({ | ||
| environmentId, | ||
| input: { | ||
| cwd, | ||
| query: deferredNormalizedValue, | ||
| // `collision` makes the server answer over every local ref, so | ||
| // `totalCount` is the whole answer and paging can't hide a match. | ||
| // Locals only — a remote ref can't block worktree creation. | ||
| queryMode: "collision", | ||
| refKind: "local", | ||
| limit: 1, | ||
| }, | ||
| }), | ||
| ); | ||
| // Only a settled lookup for the value currently typed can clear a name for | ||
| // send; while the deferred value lags or the query is in flight the answer | ||
| // belongs to a different name. A failed lookup counts as settled — the | ||
| // server rejects a duplicate branch anyway. | ||
| const checked = | ||
| deferredNormalizedValue === normalizedValue && | ||
| (conflictRefsQuery.data !== null || conflictRefsQuery.error !== null); | ||
| const conflict = checked && (conflictRefsQuery.data?.totalCount ?? 0) > 0; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Slash and dash names share pathsMedium Severity The new collision check only compares git ref hierarchy, but Additional Locations (2)Reviewed by Cursor Bugbot for commit 64bb942. Configure here. |
||
| // The collision may be a parent or child of the typed name rather than the | ||
| // name itself, so the message names the ref actually in the way. | ||
| const conflictingRef = conflict ? (conflictRefsQuery.data?.refs[0]?.name ?? null) : null; | ||
| const conflictMessage = | ||
| conflictingRef === null | ||
| ? null | ||
| : conflictingRef === normalizedValue | ||
| ? `Branch "${conflictingRef}" already exists.` | ||
| : `Branch "${conflictingRef}" already exists, so "${normalizedValue}" can't be created.`; | ||
| const state = checked ? (conflict ? "conflict" : "available") : "checking"; | ||
|
cursor[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| const onStatusChangeRef = useRef(onStatusChange); | ||
| onStatusChangeRef.current = onStatusChange; | ||
| useEffect(() => { | ||
| onStatusChangeRef.current?.(normalizedValue === null ? null : { name: normalizedValue, state }); | ||
| }, [normalizedValue, state]); | ||
| // The send gate must not outlive the input (e.g. switching back to | ||
| // "Current checkout"), or it would block sends it no longer applies to. | ||
| useEffect(() => () => onStatusChangeRef.current?.(null), []); | ||
|
|
||
| // The tooltip stays mounted and merely disabled so a collision appearing | ||
| // mid-keystroke can't remount the input out from under the caret. | ||
| return ( | ||
| <Tooltip disabled={conflictMessage === null}> | ||
| <TooltipTrigger | ||
| render={ | ||
| <input | ||
| type="text" | ||
| value={value} | ||
| onChange={(event) => onValueChange(sanitizeWorktreeBranchNameInput(event.target.value))} | ||
| placeholder="custom branch name" | ||
| spellCheck={false} | ||
| autoComplete="off" | ||
| aria-label="Branch name for the new worktree" | ||
| data-composer-context-control | ||
| aria-invalid={conflict || undefined} | ||
| className={cn( | ||
| "h-7 w-44 min-w-0 shrink rounded-md bg-transparent px-2 font-mono text-xs outline-none transition-colors sm:h-6", | ||
| "placeholder:font-sans placeholder:text-muted-foreground/50", | ||
| "hover:bg-muted/40 focus:bg-muted/40 focus-visible:ring-2 focus-visible:ring-ring", | ||
| conflict ? "text-destructive" : "text-muted-foreground/70 focus:text-foreground/80", | ||
| )} | ||
| /> | ||
| } | ||
| /> | ||
| <TooltipPopup side="top">{conflictMessage}</TooltipPopup> | ||
| </Tooltip> | ||
| ); | ||
| } | ||


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Collision check ignores letter case
Medium Severity
refCollidesWithNamecompares refs with case-sensitive equality and prefix checks. Sibling helpers in the same package already treat branch collisions as case-insensitive, and git withcore.ignorecase(default on macOS and Windows) refuses a new branch that differs from an existing one only by case. The input can mark a name available and the send path will proceed, thengit worktree add -bfails.Additional Locations (1)
apps/web/src/components/BranchToolbarWorktreeNameInput.tsx#L64-L78Reviewed by Cursor Bugbot for commit 64bb942. Configure here.