Skip to content

Commit eb62869

Browse files
authored
Background - validate upstream branch before creating the worktree (#308953)
* Background - validate upstream branch before creating the worktree * Pull request feedback
1 parent e9d8794 commit eb62869

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

extensions/copilot/src/extension/chatSessions/vscode-node/chatSessionWorktreeServiceImpl.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ export class ChatSessionWorktreeService extends Disposable implements IChatSessi
7272

7373
const autoCommit = this.configurationService.getConfig<boolean>(ConfigKey.Advanced.CLIAutoCommitEnabled);
7474

75+
let baseCommit: string | undefined = undefined;
7576
const branch = await this.generateBranchName(branchName, activeRepository);
7677

7778
// 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
8283
// Attempt to resolve the provided base branch
8384
const branchDetails = await this.gitService.getBranch(activeRepository.rootUri, baseBranch);
8485
if (branchDetails?.upstream?.remote && branchDetails.upstream?.name) {
85-
// If the base branch has an upstream, use it as the base for the worktree
86-
baseBranch = `${branchDetails.upstream.remote}/${branchDetails.upstream.name}`;
86+
const upstreamBranchName = `${branchDetails.upstream.remote}/${branchDetails.upstream.name}`;
87+
88+
try {
89+
// Attempt to resolve the upstream branch before using it as the base for the worktree
90+
const upstreamBranch = await this.gitService.getBranch(activeRepository.rootUri, upstreamBranchName);
91+
if (upstreamBranch) {
92+
baseBranch = upstreamBranchName;
93+
baseCommit = upstreamBranch.commit;
94+
}
95+
} catch (error) {
96+
const errorMessage = error instanceof Error ? error.message : String(error);
97+
this.logService.warn(`[ChatSessionWorktreeService][_createWorktree] Failed to resolve upstream branch ${upstreamBranchName}. Error: ${errorMessage}`);
98+
}
8799
}
88100
} catch (error) {
89101
const errorMessage = error instanceof Error ? error.message : String(error);
@@ -97,8 +109,7 @@ export class ChatSessionWorktreeService extends Disposable implements IChatSessi
97109
const baseBranchName = baseBranch ?? activeRepository.headBranchName;
98110
const baseBranchProtected = await this.gitService.isBranchProtected(activeRepository.rootUri, baseBranchName);
99111

100-
let baseCommit: string | undefined = undefined;
101-
if (baseBranch) {
112+
if (baseBranch && !baseCommit) {
102113
const refs = await this.gitService.getRefs(activeRepository.rootUri, { pattern: `refs/heads/${baseBranch}` });
103114
baseCommit = refs.length === 1 && refs[0].commit ? refs[0].commit : undefined;
104115
}

0 commit comments

Comments
 (0)