Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/main/agent-hooks/first-work-branch-rename.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,9 @@ async function runAutoRename(
const newBranch = await resolveUniqueBranchName(
exec,
slug,
// Use the non-throwing builder here: the prefix was already validated at
// worktree-create time, and this best-effort background rename has its own
// retry/stop handling, so it must not throw on prefix issues.
(slugLeaf) => computeBranchName(slugLeaf, settings, username),
currentBranch
)
Expand Down
42 changes: 33 additions & 9 deletions src/main/ipc/worktree-branch-name.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,54 @@
import {
assertBranchPrefixValid,
normalizeBranchPrefix,
selectBranchPrefixInput,
type BranchPrefixSettings
} from '../../shared/branch-prefix'

/**
* Resolve the branch prefix segment (the part before `/`) the configured
* strategy will prepend, or null when no prefix applies. Exposed so callers can
* detect a prefix the user already typed (or a generation model leaked) before
* it gets prepended a second time.
*
* The returned prefix is normalized (surrounding whitespace/slashes stripped) so
* a custom value like `team/` cannot produce a `team//name` branch that git
* check-ref-format rejects.
*/
export function getConfiguredBranchPrefix(
settings: { branchPrefix: string; branchPrefixCustom?: string },
settings: BranchPrefixSettings,
gitUsername: string | null
): string | null {
if (settings.branchPrefix === 'git-username') {
return gitUsername || null
}
if (settings.branchPrefix === 'custom' && settings.branchPrefixCustom) {
return settings.branchPrefixCustom
}
return null
const raw = selectBranchPrefixInput(settings, gitUsername)
return raw ? normalizeBranchPrefix(raw) || null : null
}

/**
* Compute the full branch name by applying the configured prefix strategy.
*/
export function computeBranchName(
sanitizedName: string,
settings: { branchPrefix: string; branchPrefixCustom?: string },
settings: BranchPrefixSettings,
gitUsername: string | null
): string {
const prefix = getConfiguredBranchPrefix(settings, gitUsername)
return prefix ? `${prefix}/${sanitizedName}` : sanitizedName
}

/**
* Compute a branch name and fail fast when the configured prefix is invalid.
* Used on worktree-create paths so users get a clear settings hint instead of
* an opaque git check-ref-format failure.
*/
export function computeValidatedBranchName(
sanitizedName: string,
settings: BranchPrefixSettings,
gitUsername: string | null
): string {
const prefix = getConfiguredBranchPrefix(settings, gitUsername)
if (prefix === null) {
return sanitizedName
}
assertBranchPrefixValid(prefix)
return `${prefix}/${sanitizedName}`
}
47 changes: 47 additions & 0 deletions src/main/ipc/worktree-logic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
ensurePathWithinWorkspace,
computeBranchName,
getConfiguredBranchPrefix,
computeValidatedBranchName,
computeWorktreePath,
computeRemoteWorktreePath,
computeWorkspaceRoot,
Expand Down Expand Up @@ -148,6 +149,18 @@ describe('computeBranchName', () => {
it('returns bare name when branchPrefix is none', () => {
expect(computeBranchName('feature', { branchPrefix: 'none' }, 'jdoe')).toBe('feature')
})

it('does not double the slash when a custom prefix ends in one', () => {
expect(
computeBranchName('feature', { branchPrefix: 'custom', branchPrefixCustom: 'team/' }, null)
).toBe('team/feature')
})

it('normalizes a trailing slash on a git username prefix', () => {
expect(computeBranchName('feature', { branchPrefix: 'git-username' }, 'jdoe/')).toBe(
'jdoe/feature'
)
})
})

describe('getConfiguredBranchPrefix', () => {
Expand All @@ -174,6 +187,40 @@ describe('getConfiguredBranchPrefix', () => {
it('returns null when no prefix strategy applies', () => {
expect(getConfiguredBranchPrefix({ branchPrefix: 'none' }, 'jdoe')).toBeNull()
})

it('normalizes a trailing slash out of the custom prefix', () => {
expect(
getConfiguredBranchPrefix({ branchPrefix: 'custom', branchPrefixCustom: 'team/' }, null)
).toBe('team')
})

it('returns null when the custom prefix normalizes away to empty', () => {
expect(
getConfiguredBranchPrefix({ branchPrefix: 'custom', branchPrefixCustom: '/' }, null)
).toBeNull()
})
})

describe('computeValidatedBranchName', () => {
it('returns the computed branch name when the prefix is valid', () => {
expect(
computeValidatedBranchName(
'feature',
{ branchPrefix: 'custom', branchPrefixCustom: 'team' },
null
)
).toBe('team/feature')
})

it('throws when the configured prefix is invalid', () => {
expect(() =>
computeValidatedBranchName(
'feature',
{ branchPrefix: 'custom', branchPrefixCustom: 'team x' },
null
)
).toThrow('contains characters git rejects')
})
})

describe('computeWorktreePath', () => {
Expand Down
6 changes: 5 additions & 1 deletion src/main/ipc/worktree-logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import { getWslHome, parseWslPath } from '../wsl'
type WorktreePathSettings = Pick<GlobalSettings, 'nestWorkspaces' | 'workspaceDir'>
type WorktreeBasePathRepo = Pick<Repo, 'path' | 'worktreeBasePath'>

export { computeBranchName, getConfiguredBranchPrefix } from './worktree-branch-name'
export {
computeBranchName,
getConfiguredBranchPrefix,
computeValidatedBranchName
} from './worktree-branch-name'
export { mergeWorktree } from './worktree-metadata-merge'
export { areWorktreePathsEqual } from './worktree-path-comparison'

Expand Down
11 changes: 6 additions & 5 deletions src/main/ipc/worktree-remote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ type CreateWorktreeArgsWithSystemProvenance = CreateWorktreeArgs & {
import {
sanitizeWorktreeName,
sanitizeWorktreeDisplayName,
computeBranchName,
computeValidatedBranchName,
computeWorktreePath,
computeRemoteWorktreePath,
computeWorkspaceRoot,
Expand All @@ -82,6 +82,7 @@ import {
mergeWorktree
} from './worktree-logic'
import { findCreatedWorktree } from './created-worktree-reconciliation'
import type { BranchPrefixSettings } from '../../shared/branch-prefix'
import { getRepoIdFromWorktreeId } from '../../shared/worktree-id'
import { parseWorkspaceKey, worktreeWorkspaceKey } from '../../shared/workspace-scope'
import {
Expand Down Expand Up @@ -518,12 +519,12 @@ async function resolveCreateBranchName(
repoPath: string,
branchNameOverride: string | undefined,
sanitizedName: string,
settings: { branchPrefix: string; branchPrefixCustom?: string },
settings: BranchPrefixSettings,
username: string | null,
gitOptions: { wslDistro?: string } = {}
): Promise<string> {
if (!branchNameOverride) {
return computeBranchName(sanitizedName, settings, username)
return computeValidatedBranchName(sanitizedName, settings, username)
}
if (branchNameOverride.startsWith('-')) {
throw new Error('Branch name must not start with "-"')
Expand All @@ -540,11 +541,11 @@ async function resolveCreateBranchNameSsh(
repoPath: string,
branchNameOverride: string | undefined,
sanitizedName: string,
settings: { branchPrefix: string; branchPrefixCustom?: string },
settings: BranchPrefixSettings,
username: string | null
): Promise<string> {
if (!branchNameOverride) {
return computeBranchName(sanitizedName, settings, username)
return computeValidatedBranchName(sanitizedName, settings, username)
}
if (branchNameOverride.startsWith('-')) {
throw new Error('Branch name must not start with "-"')
Expand Down
11 changes: 9 additions & 2 deletions src/main/runtime/orca-runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ import type {
ForceDeleteWorktreeBranchResult,
GitHubPrStartPoint,
GitPushTarget,
BranchPrefixStrategy,
GitWorktreeInfo,
GitHubCreateIssueFields,
GitHubOwnerRepo,
Expand Down Expand Up @@ -796,7 +797,7 @@ import type { Store } from '../persistence'
import type { StatsCollector } from '../stats/collector'
import { AgentDetector } from '../stats/agent-detector'
import {
computeBranchName,
computeValidatedBranchName,
computeWorktreePath,
computeWorkspaceRoot,
ensurePathWithinWorkspace,
Expand Down Expand Up @@ -1886,7 +1887,13 @@ async function resolveCreateBranchName(
gitOptions: { wslDistro?: string } = {}
): Promise<string> {
if (!branchNameOverride) {
return computeBranchName(sanitizedName, settings, username)
// The runtime store's getSettings() types branchPrefix loosely as string;
// it is always one of the BranchPrefixStrategy literals at runtime.
return computeValidatedBranchName(
sanitizedName,
{ ...settings, branchPrefix: settings.branchPrefix as BranchPrefixStrategy },
username
)
}
if (branchNameOverride.startsWith('-')) {
throw new Error('Branch name must not start with "-"')
Expand Down
35 changes: 35 additions & 0 deletions src/renderer/src/components/settings/BranchPrefixFeedback.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import React from 'react'
import { renderToStaticMarkup } from 'react-dom/server'
import { describe, expect, it } from 'vitest'
import { BranchPrefixFeedback } from './BranchPrefixFeedback'

function render(rawPrefix: string): string {
return renderToStaticMarkup(React.createElement(BranchPrefixFeedback, { rawPrefix }))
}

describe('BranchPrefixFeedback', () => {
it('previews the resulting branch name and drops a redundant trailing slash', () => {
const html = render('team/')
expect(html).toContain('team/feature')
expect(html).not.toContain('team//feature')
expect(html).toContain('text-muted-foreground')
})

it('warns when the prefix contains invalid characters', () => {
const html = render('team x')
expect(html).toContain('Prefix cannot contain spaces')
expect(html).toContain('text-destructive')
})

it('reports when a slashes-only prefix collapses to no prefix', () => {
const html = render('///')
expect(html).toContain('No prefix will be applied')
})

it('renders no message for an empty prefix', () => {
const html = render('')
expect(html).not.toContain('feature')
expect(html).not.toContain('Prefix cannot contain spaces')
expect(html).not.toContain('No prefix will be applied')
})
})
47 changes: 47 additions & 0 deletions src/renderer/src/components/settings/BranchPrefixFeedback.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import type { ReactNode } from 'react'
import { getBranchPrefixIssue, normalizeBranchPrefix } from '../../../../shared/branch-prefix'
import { translate } from '@/i18n/i18n'

type BranchPrefixFeedbackProps = {
rawPrefix: string
}

export function BranchPrefixFeedback({ rawPrefix }: BranchPrefixFeedbackProps): ReactNode {
const issue = getBranchPrefixIssue(rawPrefix)
const normalized = normalizeBranchPrefix(rawPrefix)

let message: ReactNode = null
if (issue) {
message = (
<span className="text-destructive">
{translate(
'auto.components.settings.BranchPrefixFeedback.6c40c0908f',
'Prefix cannot contain spaces or special characters like ~ ^ : ? * [ \\'
)}
</span>
)
} else if (normalized) {
message = (
<span className="text-muted-foreground">
{translate(
'auto.components.settings.BranchPrefixFeedback.64d70b156a',
'Branches will be named {{example}}',
{ example: `${normalized}/feature` }
)}
</span>
)
} else if (rawPrefix.trim()) {
message = (
<span className="text-muted-foreground">
{translate(
'auto.components.settings.BranchPrefixFeedback.808f9a726e',
'No prefix will be applied'
)}
</span>
)
}

// Reserve a line of height so the message swapping in/out as the user types
// does not reflow the settings list below it.
return <p className="min-h-4 text-xs">{message}</p>
}
74 changes: 74 additions & 0 deletions src/renderer/src/components/settings/GitPane.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import React from 'react'
import { renderToStaticMarkup } from 'react-dom/server'
import { describe, expect, it } from 'vitest'
import type { GlobalSettings } from '../../../../shared/types'
import { getDefaultSettings } from '../../../../shared/constants'
import { TooltipProvider } from '../ui/tooltip'
import { GitPane } from './GitPane'

function renderGitPane(settings: GlobalSettings, displayedGitUsername = 'jdoe'): string {
return renderToStaticMarkup(
React.createElement(
TooltipProvider,
null,
React.createElement(GitPane, {
settings,
updateSettings: () => {},
writeSourceControlAiSettings: async () => {},
displayedGitUsername
})
)
)
}

function customPrefixSettings(branchPrefixCustom: string): GlobalSettings {
return {
...getDefaultSettings('/home/test'),
branchPrefix: 'custom',
branchPrefixCustom
}
}

function gitUsernamePrefixSettings(): GlobalSettings {
return {
...getDefaultSettings('/home/test'),
branchPrefix: 'git-username',
branchPrefixCustom: ''
}
}

describe('GitPane branch prefix feedback', () => {
it('previews the resulting branch name and drops a redundant trailing slash', () => {
const html = renderGitPane(customPrefixSettings('team/'))
expect(html).toContain('team/feature')
expect(html).not.toContain('team//feature')
})

it('warns when the custom prefix contains invalid characters', () => {
const html = renderGitPane(customPrefixSettings('team x'))
expect(html).toContain('Prefix cannot contain spaces')
})

it('shows neither preview nor warning when no custom prefix is set', () => {
const html = renderGitPane(customPrefixSettings(''))
expect(html).not.toContain('/feature')
expect(html).not.toContain('Prefix cannot contain spaces')
expect(html).not.toContain('No prefix will be applied')
})

it('explains when a custom prefix normalizes away to empty', () => {
const html = renderGitPane(customPrefixSettings('/'))
expect(html).toContain('No prefix will be applied')
expect(html).not.toContain('/feature')
})

it('warns in git-username mode when the displayed username is invalid', () => {
const html = renderGitPane(gitUsernamePrefixSettings(), 'team x')
expect(html).toContain('Prefix cannot contain spaces')
})

it('previews in git-username mode when the displayed username is valid', () => {
const html = renderGitPane(gitUsernamePrefixSettings(), 'jdoe/')
expect(html).toContain('jdoe/feature')
})
})
Loading