|
1 | | -import { exec } from "node:child_process"; |
| 1 | +import { exec, execFile } from "node:child_process"; |
2 | 2 | import * as fs from "node:fs"; |
3 | 3 | import path from "node:path"; |
4 | 4 | import { promisify } from "node:util"; |
@@ -30,6 +30,7 @@ import { cleanupWorkspaceSessions, ScriptRunner } from "./scriptRunner"; |
30 | 30 | import { buildWorkspaceEnv } from "./workspaceEnv"; |
31 | 31 |
|
32 | 32 | const execAsync = promisify(exec); |
| 33 | +const execFileAsync = promisify(execFile); |
33 | 34 |
|
34 | 35 | function getTaskAssociations(): TaskFolderAssociation[] { |
35 | 36 | return foldersStore.get("taskAssociations", []); |
@@ -136,13 +137,29 @@ export class WorkspaceService extends TypedEventEmitter<WorkspaceServiceEvents> |
136 | 137 | // try to create the branch, if it already exists just switch to it |
137 | 138 | if (branch) { |
138 | 139 | try { |
139 | | - log.info(`Creating/switching to branch ${branch} for task ${taskId}`); |
140 | | - try { |
141 | | - await execAsync(`git checkout -b "${branch}"`, { cwd: folderPath }); |
142 | | - log.info(`Created and switched to new branch ${branch}`); |
143 | | - } catch (_error) { |
144 | | - await execAsync(`git checkout "${branch}"`, { cwd: folderPath }); |
145 | | - log.info(`Switched to existing branch ${branch}`); |
| 140 | + // Check if already on the target branch |
| 141 | + const { stdout: currentBranch } = await execFileAsync( |
| 142 | + "git", |
| 143 | + ["rev-parse", "--abbrev-ref", "HEAD"], |
| 144 | + { cwd: folderPath }, |
| 145 | + ); |
| 146 | + if (currentBranch.trim() === branch) { |
| 147 | + log.info(`Already on branch ${branch}, skipping checkout`); |
| 148 | + } else { |
| 149 | + log.info( |
| 150 | + `Creating/switching to branch ${branch} for task ${taskId}`, |
| 151 | + ); |
| 152 | + try { |
| 153 | + await execFileAsync("git", ["checkout", "-b", branch], { |
| 154 | + cwd: folderPath, |
| 155 | + }); |
| 156 | + log.info(`Created and switched to new branch ${branch}`); |
| 157 | + } catch (_error) { |
| 158 | + await execFileAsync("git", ["checkout", branch], { |
| 159 | + cwd: folderPath, |
| 160 | + }); |
| 161 | + log.info(`Switched to existing branch ${branch}`); |
| 162 | + } |
146 | 163 | } |
147 | 164 | } catch (error) { |
148 | 165 | const message = `Could not switch to branch "${branch}". Please commit or stash your changes first.`; |
|
0 commit comments