Skip to content

Commit ab59a63

Browse files
committed
feat(code): enable branch linking for local tasks
1 parent 817e5e0 commit ab59a63

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { MAIN_TOKENS } from "../../di/tokens";
2222
import { logger } from "../../utils/logger";
2323
import { TypedEventEmitter } from "../../utils/typed-event-emitter";
2424
import { deriveWorktreePath } from "../../utils/worktree-helpers";
25+
import { AgentServiceEvent } from "../agent/schemas";
2526
import type { AgentService } from "../agent/service";
2627
import { FileWatcherEvent } from "../file-watcher/schemas";
2728
import type { FileWatcherService } from "../file-watcher/service";
@@ -237,6 +238,11 @@ export class WorkspaceService extends TypedEventEmitter<WorkspaceServiceEvents>
237238
this.handleFocusBranchRenamed.bind(this),
238239
);
239240

241+
this.agentService.on(
242+
AgentServiceEvent.AgentFileActivity,
243+
this.handleAgentFileActivity.bind(this),
244+
);
245+
240246
log.info("Branch watcher initialized");
241247
}
242248

@@ -310,6 +316,35 @@ export class WorkspaceService extends TypedEventEmitter<WorkspaceServiceEvents>
310316
}
311317
}
312318

319+
private async handleAgentFileActivity({
320+
taskId,
321+
branchName,
322+
}: {
323+
taskId: string;
324+
branchName: string | null;
325+
}): Promise<void> {
326+
if (!branchName) return;
327+
328+
const dbRow = this.workspaceRepo.findByTaskId(taskId);
329+
if (!dbRow || dbRow.mode !== "local") return;
330+
if (!dbRow.repositoryId) return;
331+
332+
const folderPath = this.getFolderPath(dbRow.repositoryId);
333+
if (!folderPath) return;
334+
335+
try {
336+
const defaultBranch = await getDefaultBranch(folderPath);
337+
if (branchName === defaultBranch) return;
338+
} catch {
339+
// If we can't determine the default branch, still allow linking
340+
}
341+
342+
const currentLinked = dbRow.linkedBranch ?? null;
343+
if (currentLinked === branchName) return;
344+
345+
this.linkBranch(taskId, branchName);
346+
}
347+
313348
private updateAssociationBranchName(
314349
_taskId: string,
315350
_branchName: string,

apps/code/src/renderer/features/git-interaction/hooks/useGitInteraction.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,17 @@ export function useGitInteraction(
276276
}
277277
if (store.createPrNeedsBranch) {
278278
invalidateGitBranchQueries(repoPath);
279+
trpcClient.workspace.linkBranch
280+
.mutate({ taskId, branchName: store.branchName.trim() })
281+
.catch((err) =>
282+
log.warn("Failed to link branch to task", { taskId, err }),
283+
);
284+
} else if (git.currentBranch) {
285+
trpcClient.workspace.linkBranch
286+
.mutate({ taskId, branchName: git.currentBranch })
287+
.catch((err) =>
288+
log.warn("Failed to link branch to task", { taskId, err }),
289+
);
279290
}
280291

281292
if (result.prUrl) {
@@ -529,6 +540,12 @@ export function useGitInteraction(
529540
trackGitAction(taskId, "branch-here", true);
530541
await queryClient.invalidateQueries(trpc.workspace.getAll.pathFilter());
531542

543+
trpcClient.workspace.linkBranch
544+
.mutate({ taskId, branchName: store.branchName.trim() })
545+
.catch((err) =>
546+
log.warn("Failed to link branch to task", { taskId, err }),
547+
);
548+
532549
modal.closeBranch();
533550
} catch (error) {
534551
log.error("Failed to create branch", error);

0 commit comments

Comments
 (0)