diff --git a/extensions/copilot/src/extension/chatSessions/vscode-node/chatSessionWorktreeServiceImpl.ts b/extensions/copilot/src/extension/chatSessions/vscode-node/chatSessionWorktreeServiceImpl.ts index 41c8967cc4a20..c12da9763d6f8 100644 --- a/extensions/copilot/src/extension/chatSessions/vscode-node/chatSessionWorktreeServiceImpl.ts +++ b/extensions/copilot/src/extension/chatSessions/vscode-node/chatSessionWorktreeServiceImpl.ts @@ -72,6 +72,7 @@ export class ChatSessionWorktreeService extends Disposable implements IChatSessi const autoCommit = this.configurationService.getConfig(ConfigKey.Advanced.CLIAutoCommitEnabled); + let baseCommit: string | undefined = undefined; const branch = await this.generateBranchName(branchName, activeRepository); // When a base branch is provided, we attempt to resolve it, to see whether it has an @@ -82,8 +83,19 @@ export class ChatSessionWorktreeService extends Disposable implements IChatSessi // Attempt to resolve the provided base branch const branchDetails = await this.gitService.getBranch(activeRepository.rootUri, baseBranch); if (branchDetails?.upstream?.remote && branchDetails.upstream?.name) { - // If the base branch has an upstream, use it as the base for the worktree - baseBranch = `${branchDetails.upstream.remote}/${branchDetails.upstream.name}`; + const upstreamBranchName = `${branchDetails.upstream.remote}/${branchDetails.upstream.name}`; + + try { + // Attempt to resolve the upstream branch before using it as the base for the worktree + const upstreamBranch = await this.gitService.getBranch(activeRepository.rootUri, upstreamBranchName); + if (upstreamBranch) { + baseBranch = upstreamBranchName; + baseCommit = upstreamBranch.commit; + } + } catch (error) { + const errorMessage = error instanceof Error ? error.message : String(error); + this.logService.warn(`[ChatSessionWorktreeService][_createWorktree] Failed to resolve upstream branch ${upstreamBranchName}. Error: ${errorMessage}`); + } } } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error); @@ -97,8 +109,7 @@ export class ChatSessionWorktreeService extends Disposable implements IChatSessi const baseBranchName = baseBranch ?? activeRepository.headBranchName; const baseBranchProtected = await this.gitService.isBranchProtected(activeRepository.rootUri, baseBranchName); - let baseCommit: string | undefined = undefined; - if (baseBranch) { + if (baseBranch && !baseCommit) { const refs = await this.gitService.getRefs(activeRepository.rootUri, { pattern: `refs/heads/${baseBranch}` }); baseCommit = refs.length === 1 && refs[0].commit ? refs[0].commit : undefined; }