Skip to content

Commit 415d0bc

Browse files
Copilotalexr00
andcommitted
Address code review feedback: improve error handling and validation
Co-authored-by: alexr00 <38270282+alexr00@users.noreply.github.com>
1 parent a3320a8 commit 415d0bc

File tree

1 file changed

+46
-25
lines changed

1 file changed

+46
-25
lines changed

src/github/folderRepositoryManager.ts

Lines changed: 46 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2505,42 +2505,63 @@ export class FolderRepositoryManager extends Disposable {
25052505
);
25062506

25072507
if (result === resetToRemote) {
2508+
if (!branch.upstream || !branch.name) {
2509+
vscode.window.showErrorMessage(vscode.l10n.t('Cannot reset branch: missing upstream or branch name'));
2510+
return;
2511+
}
2512+
2513+
let tempBranchName: string | undefined;
2514+
let originalBranchDeleted = false;
25082515
try {
2509-
if (branch.upstream) {
2510-
// Fetch to ensure we have the latest remote state
2511-
await this._repository.fetch(branch.upstream.remote, branch.name);
2516+
// Fetch to ensure we have the latest remote state
2517+
await this._repository.fetch(branch.upstream.remote, branch.name);
25122518

2513-
// Get the remote branch reference
2514-
const remoteBranchRef = `refs/remotes/${branch.upstream.remote}/${branch.upstream.name}`;
2515-
const remoteBranch = await this._repository.getBranch(remoteBranchRef);
2516-
const currentBranchName = branch.name!;
2519+
// Get the remote branch reference
2520+
const remoteBranchRef = `refs/remotes/${branch.upstream.remote}/${branch.upstream.name}`;
2521+
const remoteBranch = await this._repository.getBranch(remoteBranchRef);
2522+
const currentBranchName = branch.name;
25172523

2518-
// Create a temp branch at the remote commit
2519-
const tempBranchName = `temp-pr-update-${Date.now()}`;
2520-
await this._repository.createBranch(tempBranchName, false, remoteBranch.commit);
2521-
await this._repository.setBranchUpstream(tempBranchName, remoteBranchRef);
2524+
// Create a temp branch at the remote commit with better uniqueness
2525+
tempBranchName = `temp-pr-update-${Date.now()}-${Math.random().toString(36).substring(7)}`;
2526+
await this._repository.createBranch(tempBranchName, false, remoteBranch.commit);
2527+
await this._repository.setBranchUpstream(tempBranchName, remoteBranchRef);
25222528

2523-
// Checkout the temp branch
2524-
await this._repository.checkout(tempBranchName);
2529+
// Checkout the temp branch
2530+
await this._repository.checkout(tempBranchName);
25252531

2526-
// Delete the old branch (force delete since it has un-merged commits)
2527-
await this._repository.deleteBranch(currentBranchName, true);
2532+
// Delete the old branch (force delete since it has un-merged commits)
2533+
await this._repository.deleteBranch(currentBranchName, true);
2534+
originalBranchDeleted = true;
25282535

2529-
// Recreate the original branch at the same commit
2530-
await this._repository.createBranch(currentBranchName, false, remoteBranch.commit);
2531-
await this._repository.setBranchUpstream(currentBranchName, remoteBranchRef);
2536+
// Recreate the original branch at the same commit
2537+
await this._repository.createBranch(currentBranchName, false, remoteBranch.commit);
2538+
await this._repository.setBranchUpstream(currentBranchName, remoteBranchRef);
25322539

2533-
// Checkout the recreated branch
2534-
await this._repository.checkout(currentBranchName);
2540+
// Checkout the recreated branch
2541+
await this._repository.checkout(currentBranchName);
25352542

2536-
// Delete the temp branch
2537-
await this._repository.deleteBranch(tempBranchName, true);
2543+
// Delete the temp branch
2544+
await this._repository.deleteBranch(tempBranchName, true);
2545+
tempBranchName = undefined;
25382546

2539-
Logger.appendLine(`Successfully reset branch ${currentBranchName} to remote ${remoteBranchRef}`, this.id);
2540-
}
2547+
Logger.appendLine(`Successfully reset branch ${currentBranchName} to remote ${remoteBranchRef}`, this.id);
25412548
} catch (e) {
25422549
Logger.error(`Error resetting branch to remote: ${e}`, this.id);
2543-
vscode.window.showErrorMessage(vscode.l10n.t('Failed to reset branch to remote: {0}', e.message || e));
2550+
2551+
// Attempt cleanup of any created resources
2552+
if (tempBranchName) {
2553+
try {
2554+
// If we're still on the temp branch, try to get back to a safe state
2555+
if (!originalBranchDeleted) {
2556+
await this._repository.checkout(branch.name);
2557+
}
2558+
await this._repository.deleteBranch(tempBranchName, true);
2559+
} catch (cleanupError) {
2560+
Logger.error(`Error during cleanup: ${cleanupError}`, this.id);
2561+
}
2562+
}
2563+
2564+
vscode.window.showErrorMessage(vscode.l10n.t('Failed to reset branch to remote: {0}', e?.message || String(e)));
25442565
}
25452566
}
25462567
// If cancel, do nothing

0 commit comments

Comments
 (0)