Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions apps/code/src/main/services/workspace/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { MAIN_TOKENS } from "../../di/tokens";
import { logger } from "../../utils/logger";
import { TypedEventEmitter } from "../../utils/typed-event-emitter";
import { deriveWorktreePath } from "../../utils/worktree-helpers";
import { AgentServiceEvent } from "../agent/schemas";
import type { AgentService } from "../agent/service";
import { FileWatcherEvent } from "../file-watcher/schemas";
import type { FileWatcherService } from "../file-watcher/service";
Expand Down Expand Up @@ -237,6 +238,11 @@ export class WorkspaceService extends TypedEventEmitter<WorkspaceServiceEvents>
this.handleFocusBranchRenamed.bind(this),
);

this.agentService.on(
AgentServiceEvent.AgentFileActivity,
this.handleAgentFileActivity.bind(this),
);

log.info("Branch watcher initialized");
}

Expand Down Expand Up @@ -310,6 +316,35 @@ export class WorkspaceService extends TypedEventEmitter<WorkspaceServiceEvents>
}
}

private async handleAgentFileActivity({
taskId,
branchName,
}: {
taskId: string;
branchName: string | null;
}): Promise<void> {
if (!branchName) return;

const dbRow = this.workspaceRepo.findByTaskId(taskId);
if (!dbRow || dbRow.mode !== "local") return;
if (!dbRow.repositoryId) return;

const folderPath = this.getFolderPath(dbRow.repositoryId);
if (!folderPath) return;

try {
const defaultBranch = await getDefaultBranch(folderPath);
if (branchName === defaultBranch) return;
} catch {
// If we can't determine the default branch, still allow linking
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if that's the right choice here. Maybe we can fall back to a more sensible catch all here (e.g. main/master etc) :thinking_face:

}

const currentLinked = dbRow.linkedBranch ?? null;
if (currentLinked === branchName) return;

this.linkBranch(taskId, branchName);
}

private updateAssociationBranchName(
_taskId: string,
_branchName: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,17 @@ export function useGitInteraction(
}
if (store.createPrNeedsBranch) {
invalidateGitBranchQueries(repoPath);
trpcClient.workspace.linkBranch
.mutate({ taskId, branchName: store.branchName.trim() })
.catch((err) =>
log.warn("Failed to link branch to task", { taskId, err }),
);
} else if (git.currentBranch) {
trpcClient.workspace.linkBranch
.mutate({ taskId, branchName: git.currentBranch })
.catch((err) =>
log.warn("Failed to link branch to task", { taskId, err }),
);
}

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

trpcClient.workspace.linkBranch
.mutate({ taskId, branchName: store.branchName.trim() })
.catch((err) =>
log.warn("Failed to link branch to task", { taskId, err }),
);

modal.closeBranch();
} catch (error) {
log.error("Failed to create branch", error);
Expand Down
Loading