| 80 | const FALLBACK_TRUNK_CANDIDATES = ["main", "master", "trunk", "develop", "default"]; |
| 81 | |
| 82 | export async function detectDefaultTrunkBranch( |
| 83 | projectPath: string, |
| 84 | branches?: string[] |
| 85 | ): Promise<string> { |
| 86 | const branchList = branches ?? (await listLocalBranches(projectPath)); |
| 87 | |
| 88 | if (branchList.length === 0) { |
| 89 | throw new Error(`No branches available in repository ${projectPath}`); |
| 90 | } |
| 91 | |
| 92 | const branchSet = new Set(branchList); |
| 93 | const currentBranch = await getCurrentBranch(projectPath); |
| 94 | |
| 95 | if (currentBranch && branchSet.has(currentBranch)) { |
| 96 | return currentBranch; |
| 97 | } |
| 98 | |
| 99 | for (const candidate of FALLBACK_TRUNK_CANDIDATES) { |
| 100 | if (branchSet.has(candidate)) { |
| 101 | return candidate; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | return branchList[0]; |
| 106 | } |
| 107 | |
| 108 | export async function createWorktree( |
| 109 | config: Config, |