Skip to content

Commit b78acae

Browse files
worktree error handling in creation of worktrees and checking out branches (microsoft#257328)
* worktree error handling in creation of worktrees and checking out branches * code clean up
1 parent 90072b4 commit b78acae

File tree

1 file changed

+37
-24
lines changed

1 file changed

+37
-24
lines changed

extensions/git/src/commands.ts

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2917,10 +2917,15 @@ export class CommandCenter {
29172917
try {
29182918
await item.run(repository, opts);
29192919
} catch (err) {
2920-
if (err.gitErrorCode !== GitErrorCodes.DirtyWorkTree) {
2920+
if (err.gitErrorCode !== GitErrorCodes.DirtyWorkTree && err.gitErrorCode !== GitErrorCodes.WorktreeAlreadyExists) {
29212921
throw err;
29222922
}
29232923

2924+
if (err.gitErrorCode === GitErrorCodes.WorktreeAlreadyExists) {
2925+
this.handleWorktreeError(err);
2926+
return false;
2927+
}
2928+
29242929
const stash = l10n.t('Stash & Checkout');
29252930
const migrate = l10n.t('Migrate Changes');
29262931
const force = l10n.t('Force Checkout');
@@ -3458,37 +3463,45 @@ export class CommandCenter {
34583463
try {
34593464
await repository.worktree({ name: name, path: worktreePath });
34603465
} catch (err) {
3461-
if (err.gitErrorCode === GitErrorCodes.WorktreeAlreadyExists) {
3462-
const errorMessage = err.stderr;
3463-
const match = errorMessage.match(/worktree at '([^']+)'/) || errorMessage.match(/'([^']+)'/);
3464-
const path = match ? match[1] : undefined;
3466+
if (err.gitErrorCode !== GitErrorCodes.WorktreeAlreadyExists) {
3467+
throw err;
3468+
}
34653469

3466-
if (!path) {
3467-
return;
3468-
}
3470+
this.handleWorktreeError(err);
3471+
return;
34693472

3470-
const openWorktree = l10n.t('Open in current window');
3471-
const openWorktreeInNewWindow = l10n.t('Open in new window');
3472-
const message = l10n.t(errorMessage || 'A worktree for branch \'{0}\' already exists at \'{1}\'.', name, path);
3473-
const choice = await window.showWarningMessage(message, { modal: true }, openWorktree, openWorktreeInNewWindow);
3473+
}
3474+
}
34743475

3475-
const worktreeRepository = this.model.getRepository(path) || this.model.getRepository(Uri.file(path));
3476+
private async handleWorktreeError(err: any): Promise<void> {
3477+
const errorMessage = err.stderr;
3478+
const match = errorMessage.match(/worktree at '([^']+)'/) || errorMessage.match(/'([^']+)'/);
3479+
const path = match ? match[1] : undefined;
34763480

3477-
if (!worktreeRepository) {
3478-
return;
3479-
}
3481+
if (!path) {
3482+
return;
3483+
}
34803484

3481-
if (choice === openWorktree) {
3482-
await this.openWorktreeInCurrentWindow(worktreeRepository);
3483-
} else if (choice === openWorktreeInNewWindow) {
3484-
await this.openWorktreeInNewWindow(worktreeRepository);
3485-
}
3485+
const worktreeRepository = this.model.getRepository(path) || this.model.getRepository(Uri.file(path));
34863486

3487-
return;
3488-
}
3487+
if (!worktreeRepository) {
3488+
return;
3489+
}
34893490

3490-
throw err;
3491+
const openWorktree = l10n.t('Open in current window');
3492+
const openWorktreeInNewWindow = l10n.t('Open in new window');
3493+
const message = l10n.t(errorMessage);
3494+
const choice = await window.showWarningMessage(message, { modal: true }, openWorktree, openWorktreeInNewWindow);
3495+
3496+
3497+
3498+
if (choice === openWorktree) {
3499+
await this.openWorktreeInCurrentWindow(worktreeRepository);
3500+
} else if (choice === openWorktreeInNewWindow) {
3501+
await this.openWorktreeInNewWindow(worktreeRepository);
34913502
}
3503+
3504+
return;
34923505
}
34933506

34943507
@command('git.deleteWorktree', { repository: true })

0 commit comments

Comments
 (0)