Skip to content

Commit 3b76891

Browse files
authored
fix: check if we're already on a branch before switching to it. (#412)
1 parent 932e7d6 commit 3b76891

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

apps/array/src/main/services/workspace/service.ts

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { exec } from "node:child_process";
1+
import { exec, execFile } from "node:child_process";
22
import * as fs from "node:fs";
33
import path from "node:path";
44
import { promisify } from "node:util";
@@ -30,6 +30,7 @@ import { cleanupWorkspaceSessions, ScriptRunner } from "./scriptRunner";
3030
import { buildWorkspaceEnv } from "./workspaceEnv";
3131

3232
const execAsync = promisify(exec);
33+
const execFileAsync = promisify(execFile);
3334

3435
function getTaskAssociations(): TaskFolderAssociation[] {
3536
return foldersStore.get("taskAssociations", []);
@@ -136,13 +137,29 @@ export class WorkspaceService extends TypedEventEmitter<WorkspaceServiceEvents>
136137
// try to create the branch, if it already exists just switch to it
137138
if (branch) {
138139
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+
}
146163
}
147164
} catch (error) {
148165
const message = `Could not switch to branch "${branch}". Please commit or stash your changes first.`;

0 commit comments

Comments
 (0)