* feat: add worktree validation and deleteLocalBranch option Add API to validate and create worktrees with new payload types Allow deleting local branches when removing worktrees via UI and API Introduce OpenCode style random names for worktrees when not provided * feat: enable SSH/HTTPS transport detection for PR picker Load remotes for the current project directory to inform PR picker options. Determine preferred push transport from remotes and apply it. Expose sshUrl in API for frontend to build SSH clone URLs * feat: extend head repo with sshUrl and improve push error messages Add sshUrl field to head repo mapping Enhance push failure handling to display stderr or stdout details Return push details on success * fix: worktree path * feat: worktree set upstream on creation Enable pushing to upstream by default when no remote is specified Remove per-remote dropdown for push actions and auto-use first/upstream remote Update server and VSCode git services to support push without explicit remote and set upstream * fix: worktree-name sanitization * feat: rename worktree path field and branch prefix * feat(worktrees): add git.worktree facade, validation endpoint, upstream/remote-aware creation, and non-blocking setup execution * refactor(git): use git.worktree namespace in branch picker * feat(worktrees): sync OpenCode sandbox metadata on create/remove * fix(worktrees): accept new path key in workspace guard and validate remote startRef * chore(docs): remove temporary worktree testing plan * feat: add git worktree management API (list/create/delete/validate) for vscode * feat: wire root tracking remote and defaults for new worktrees Add resolveRootTrackingRemote to detect upstream remote for root branch Apply upstream defaults when creating new worktrees to auto-set upstream Replace validation and creation flow to use new worktreeCreate APIs * feat(worktrees): enable root tracking remote handling
121 lines
3.6 KiB
TypeScript
121 lines
3.6 KiB
TypeScript
import { getGitBranches, getGitStatus } from '@/lib/gitApi';
|
|
import type { CreateWorktreeArgs, ProjectRef } from '@/lib/worktrees/worktreeManager';
|
|
import { createWorktree } from '@/lib/worktrees/worktreeManager';
|
|
import { getRootBranch } from '@/lib/worktrees/worktreeStatus';
|
|
|
|
const parseTrackingRef = (tracking: string | null | undefined): { remote: string; branch: string } | null => {
|
|
const value = String(tracking || '').trim().replace(/^remotes\//, '');
|
|
if (!value) {
|
|
return null;
|
|
}
|
|
|
|
const separatorIndex = value.indexOf('/');
|
|
if (separatorIndex <= 0 || separatorIndex >= value.length - 1) {
|
|
return null;
|
|
}
|
|
|
|
return {
|
|
remote: value.slice(0, separatorIndex),
|
|
branch: value.slice(separatorIndex + 1),
|
|
};
|
|
};
|
|
|
|
const normalizeBranchName = (value: string): string => {
|
|
return String(value || '')
|
|
.trim()
|
|
.replace(/^refs\/heads\//, '')
|
|
.replace(/^heads\//, '')
|
|
.replace(/^remotes\//, '');
|
|
};
|
|
|
|
const resolveLocalBranchName = (args: CreateWorktreeArgs): string => {
|
|
if (args.branchName) {
|
|
return normalizeBranchName(args.branchName);
|
|
}
|
|
if (args.mode === 'existing') {
|
|
return normalizeBranchName(args.existingBranch || args.preferredName || '');
|
|
}
|
|
return normalizeBranchName(args.preferredName || '');
|
|
};
|
|
|
|
export const resolveRootTrackingRemote = async (projectDirectory: string): Promise<string | null> => {
|
|
const rootBranch = await getRootBranch(projectDirectory);
|
|
|
|
try {
|
|
const branchState = await getGitBranches(projectDirectory);
|
|
const tracking = branchState.branches?.[rootBranch]?.tracking || null;
|
|
const parsed = parseTrackingRef(tracking);
|
|
if (parsed?.remote) {
|
|
return parsed.remote;
|
|
}
|
|
} catch {
|
|
// ignore and fallback to status tracking
|
|
}
|
|
|
|
try {
|
|
const status = await getGitStatus(projectDirectory);
|
|
const parsed = parseTrackingRef(status.tracking);
|
|
if (parsed?.remote) {
|
|
return parsed.remote;
|
|
}
|
|
} catch {
|
|
// ignore
|
|
}
|
|
|
|
return null;
|
|
};
|
|
|
|
export const resolveWorktreeUpstreamDefaults = async (
|
|
projectDirectory: string,
|
|
localBranch: string
|
|
): Promise<{ setUpstream: true; upstreamRemote: string; upstreamBranch: string } | null> => {
|
|
const remote = await resolveRootTrackingRemote(projectDirectory);
|
|
const normalizedBranch = normalizeBranchName(localBranch);
|
|
if (!remote || !normalizedBranch) {
|
|
return null;
|
|
}
|
|
|
|
return {
|
|
setUpstream: true,
|
|
upstreamRemote: remote,
|
|
upstreamBranch: normalizedBranch,
|
|
};
|
|
};
|
|
|
|
export const withWorktreeUpstreamDefaults = async (
|
|
projectDirectory: string,
|
|
args: CreateWorktreeArgs,
|
|
options?: { resolvedRootTrackingRemote?: string | null }
|
|
): Promise<CreateWorktreeArgs> => {
|
|
const localBranch = resolveLocalBranchName(args);
|
|
const resolvedRemote = options?.resolvedRootTrackingRemote;
|
|
const defaults = resolvedRemote === undefined
|
|
? await resolveWorktreeUpstreamDefaults(projectDirectory, localBranch)
|
|
: (resolvedRemote && normalizeBranchName(localBranch)
|
|
? {
|
|
setUpstream: true as const,
|
|
upstreamRemote: resolvedRemote,
|
|
upstreamBranch: normalizeBranchName(localBranch),
|
|
}
|
|
: null);
|
|
if (!defaults) {
|
|
return args;
|
|
}
|
|
|
|
return {
|
|
...args,
|
|
setUpstream: args.setUpstream ?? defaults.setUpstream,
|
|
upstreamRemote: args.upstreamRemote || defaults.upstreamRemote,
|
|
upstreamBranch: args.upstreamBranch || defaults.upstreamBranch,
|
|
};
|
|
};
|
|
|
|
export const createWorktreeWithDefaults = async (
|
|
project: ProjectRef,
|
|
args: CreateWorktreeArgs,
|
|
options?: { resolvedRootTrackingRemote?: string | null }
|
|
) => {
|
|
const resolvedArgs = await withWorktreeUpstreamDefaults(project.path, args, options);
|
|
return createWorktree(project, resolvedArgs);
|
|
};
|