Skip to content
Merged
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
20 changes: 13 additions & 7 deletions src/main/ipc/worktrees.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,27 +43,31 @@ export function registerWorktreeHandlers(mainWindow: BrowserWindow, store: Store

const settings = store.getSettings()

const requestedName = args.name
// Sanitize name for use in branch names and directory paths
// (git branch names cannot contain spaces; collapse runs of spaces to a single hyphen)
const sanitizedName = args.name.replace(/\s+/g, '-')

// Compute branch name with prefix
let branchName = args.name
let branchName = sanitizedName
if (settings.branchPrefix === 'git-username') {
const username = getGitUsername(repo.path)
if (username) {
branchName = `${username}/${args.name}`
branchName = `${username}/${sanitizedName}`
}
} else if (settings.branchPrefix === 'custom' && settings.branchPrefixCustom) {
branchName = `${settings.branchPrefixCustom}/${args.name}`
branchName = `${settings.branchPrefixCustom}/${sanitizedName}`
}

const requestedName = args.name
branchName = await getAvailableBranchName(repo.path, branchName)

// Compute worktree path
let worktreePath: string
if (settings.nestWorkspaces) {
const repoName = basename(repo.path).replace(/\.git$/, '')
worktreePath = join(settings.workspaceDir, repoName, requestedName)
worktreePath = join(settings.workspaceDir, repoName, sanitizedName)
} else {
worktreePath = join(settings.workspaceDir, requestedName)
worktreePath = join(settings.workspaceDir, sanitizedName)
}

// Determine base branch
Expand All @@ -78,7 +82,9 @@ export function registerWorktreeHandlers(mainWindow: BrowserWindow, store: Store

const worktreeId = `${repo.id}::${worktreePath}`
const metaUpdates: Partial<WorktreeMeta> =
branchName === requestedName ? {} : { displayName: requestedName }
branchName === requestedName && sanitizedName === requestedName
? {}
: { displayName: requestedName }
const meta = store.setWorktreeMeta(worktreeId, metaUpdates)
const worktree = mergeWorktree(repo.id, created, meta)

Expand Down