Skip to content

Commit d440a0d

Browse files
committed
Replace dangerous branch deletion with safe unique branch creation
- Addressed feedback from @alexr00 about dangerous branch deletion - Instead of deleting user's local branch, create unique branch name - Use existing calculateUniqueBranchNameForPR function for naming - Preserve user's work by never touching their original branch - New approach follows existing PR branch naming patterns
1 parent bd9552e commit d440a0d

2 files changed

Lines changed: 28 additions & 23 deletions

File tree

src/@types/vscode.proposed.chatParticipantPrivate.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ declare module 'vscode' {
241241
export class ExtendedLanguageModelToolResult extends LanguageModelToolResult {
242242
toolResultMessage?: string | MarkdownString;
243243
toolResultDetails?: Array<Uri | Location>;
244+
toolMetadata?: unknown;
244245
}
245246

246247
// #region Chat participant detection

src/github/pullRequestGitHelper.ts

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -87,63 +87,67 @@ export class PullRequestGitHelper {
8787
return PullRequestGitHelper.checkoutFromFork(repository, pullRequest, remote && remote.remoteName, progress);
8888
}
8989

90-
const branchName = pullRequest.head.ref;
90+
const originalBranchName = pullRequest.head.ref;
9191
const remoteName = remote.remoteName;
9292
let branch: Branch;
93+
let localBranchName = originalBranchName; // This will be the branch we actually checkout
9394

9495
// Always fetch the remote branch first to ensure we have the latest commits
95-
const trackedBranchName = `refs/remotes/${remoteName}/${branchName}`;
96+
const trackedBranchName = `refs/remotes/${remoteName}/${originalBranchName}`;
9697
Logger.appendLine(`Fetch tracked branch ${trackedBranchName}`, PullRequestGitHelper.ID);
97-
progress.report({ message: vscode.l10n.t('Fetching branch {0}', branchName) });
98-
await repository.fetch(remoteName, branchName);
98+
progress.report({ message: vscode.l10n.t('Fetching branch {0}', originalBranchName) });
99+
await repository.fetch(remoteName, originalBranchName);
99100
const trackedBranch = await repository.getBranch(trackedBranchName);
100101

101102
try {
102-
branch = await repository.getBranch(branchName);
103+
branch = await repository.getBranch(localBranchName);
103104
// Check if local branch is pointing to the same commit as the remote
104105
if (branch.commit !== trackedBranch.commit) {
105-
Logger.debug(`Local branch ${branchName} commit ${branch.commit} differs from remote commit ${trackedBranch.commit}. Updating local branch.`, PullRequestGitHelper.ID);
106-
progress.report({ message: vscode.l10n.t('Updating branch {0} to match remote', branchName) });
107-
// Delete and recreate the local branch to point to the remote commit
108-
await repository.deleteBranch(branchName, true);
109-
await repository.createBranch(branchName, false, trackedBranch.commit);
110-
// Get the updated branch reference
111-
branch = await repository.getBranch(branchName);
106+
Logger.debug(`Local branch ${localBranchName} commit ${branch.commit} differs from remote commit ${trackedBranch.commit}. Creating new branch to avoid overwriting user's work.`, PullRequestGitHelper.ID);
107+
// Instead of deleting the user's branch, create a unique branch name to avoid conflicts
108+
const uniqueBranchName = await PullRequestGitHelper.calculateUniqueBranchNameForPR(repository, pullRequest);
109+
Logger.debug(`Creating branch ${uniqueBranchName} for PR checkout`, PullRequestGitHelper.ID);
110+
progress.report({ message: vscode.l10n.t('Creating branch {0} for pull request', uniqueBranchName) });
111+
await repository.createBranch(uniqueBranchName, false, trackedBranch.commit);
112+
await repository.setBranchUpstream(uniqueBranchName, trackedBranchName);
113+
// Use the unique branch name for checkout
114+
localBranchName = uniqueBranchName;
115+
branch = await repository.getBranch(localBranchName);
112116
}
113117

114118
// Make sure we aren't already on this branch
115119
if (repository.state.HEAD?.name === branch.name) {
116-
Logger.appendLine(`Tried to checkout ${branchName}, but branch is already checked out.`, PullRequestGitHelper.ID);
120+
Logger.appendLine(`Tried to checkout ${localBranchName}, but branch is already checked out.`, PullRequestGitHelper.ID);
117121
return;
118122
}
119123

120-
Logger.debug(`Checkout ${branchName}`, PullRequestGitHelper.ID);
121-
progress.report({ message: vscode.l10n.t('Checking out {0}', branchName) });
122-
await repository.checkout(branchName);
124+
Logger.debug(`Checkout ${localBranchName}`, PullRequestGitHelper.ID);
125+
progress.report({ message: vscode.l10n.t('Checking out {0}', localBranchName) });
126+
await repository.checkout(localBranchName);
123127

124128
if (!branch.upstream) {
125129
// this branch is not associated with upstream yet
126-
await repository.setBranchUpstream(branchName, trackedBranchName);
130+
await repository.setBranchUpstream(localBranchName, trackedBranchName);
127131
}
128132

129133
if (branch.behind !== undefined && branch.behind > 0 && branch.ahead === 0) {
130134
Logger.debug(`Pull from upstream`, PullRequestGitHelper.ID);
131-
progress.report({ message: vscode.l10n.t('Pulling {0}', branchName) });
135+
progress.report({ message: vscode.l10n.t('Pulling {0}', localBranchName) });
132136
await repository.pull();
133137
}
134138
} catch (err) {
135139
// there is no local branch with the same name, so we are good to create and checkout the remote branch.
136140
Logger.appendLine(
137-
`Branch ${branchName} doesn't exist on local disk yet. Creating from remote.`,
141+
`Branch ${localBranchName} doesn't exist on local disk yet. Creating from remote.`,
138142
PullRequestGitHelper.ID,
139143
);
140144
// create branch
141-
progress.report({ message: vscode.l10n.t('Creating and checking out branch {0}', branchName) });
142-
await repository.createBranch(branchName, true, trackedBranch.commit);
143-
await repository.setBranchUpstream(branchName, trackedBranchName);
145+
progress.report({ message: vscode.l10n.t('Creating and checking out branch {0}', localBranchName) });
146+
await repository.createBranch(localBranchName, true, trackedBranch.commit);
147+
await repository.setBranchUpstream(localBranchName, trackedBranchName);
144148
}
145149

146-
await PullRequestGitHelper.associateBranchWithPullRequest(repository, pullRequest, branchName);
150+
await PullRequestGitHelper.associateBranchWithPullRequest(repository, pullRequest, localBranchName);
147151
}
148152

149153
static async checkoutExistingPullRequestBranch(repository: Repository, pullRequest: PullRequestModel, progress: vscode.Progress<{ message?: string; increment?: number }>) {

0 commit comments

Comments
 (0)