123
}
124
125
// Resolve the real default branch from the remote
126
- const defaultRef = await runGit(["symbolic-ref", "refs/remotes/origin/HEAD"], input.cwd, 5_000);
126
+ const [defaultRef, worktreeList] = await Promise.all([
127
+ runGit(["symbolic-ref", "refs/remotes/origin/HEAD"], input.cwd, 5_000),
128
+ runGit(["worktree", "list", "--porcelain"], input.cwd, 5_000),
129
+ ]);
130
const defaultBranch =
131
defaultRef.code === 0 ? defaultRef.stdout.trim().replace(/^refs\/remotes\/origin\//, "") : null;
132
133
+ // Build branch-name → worktree-path map from porcelain output.
134
+ const worktreeMap = new Map<string, string>();
135
+ if (worktreeList.code === 0) {
136
+ let currentPath: string | null = null;
137
+ for (const line of worktreeList.stdout.split("\n")) {
138
+ if (line.startsWith("worktree ")) {
139
+ currentPath = line.slice("worktree ".length);
140
+ } else if (line.startsWith("branch refs/heads/") && currentPath) {
141
+ worktreeMap.set(line.slice("branch refs/heads/".length), currentPath);
142
+ } else if (line === "") {
143
+ currentPath = null;
144
+ }
145
+ }
146
+ }
147
+
148
const branches = result.stdout
149
.split("\n")
150
.map((line) => line.trim())
151
.filter((line) => line.length > 0)
134
- .map((line) => ({
135
- name: line.replace(/^[*+]\s+/, ""),
136
- current: line.startsWith("* "),
137
- isDefault: line.replace(/^[*+]\s+/, "") === defaultBranch,
138
- }))
152
+ .map((line) => {
153
+ const name = line.replace(/^[*+]\s+/, "");
154
+ return {
155
+ name,
156
+ current: line.startsWith("* "),
157
+ isDefault: name === defaultBranch,
158
+ worktreePath: worktreeMap.get(name) ?? null,
159
+ };
160
+ })
161
.toSorted((a, b) => {
162
if (a.current !== b.current) return a.current ? -1 : 1;
163
if (a.isDefault !== b.isDefault) return a.isDefault ? -1 : 1;