Skip to content

Commit 5d90ed8

Browse files
committed
retry if local changes error
1 parent 42255ad commit 5d90ed8

File tree

4 files changed

+56
-7
lines changed

4 files changed

+56
-7
lines changed

src/commands/git/revert.ts

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1+
import { Commands } from '../../constants.commands';
12
import type { Container } from '../../container';
3+
import { RevertError, RevertErrorReason } from '../../git/errors';
24
import type { GitBranch } from '../../git/models/branch';
35
import type { GitLog } from '../../git/models/log';
46
import type { GitRevisionReference } from '../../git/models/reference';
57
import { getReferenceLabel } from '../../git/models/reference';
68
import type { Repository } from '../../git/models/repository';
7-
import { showGenericErrorMessage } from '../../messages';
9+
import { showGenericErrorMessage, showShouldCommitOrStashPrompt } from '../../messages';
810
import type { FlagsQuickPickItem } from '../../quickpicks/items/flags';
911
import { createFlagsQuickPickItem } from '../../quickpicks/items/flags';
1012
import { Logger } from '../../system/logger';
13+
import { executeCommand, executeCoreCommand } from '../../system/vscode/command';
1114
import type { ViewsWithRepositoryFolders } from '../../views/viewBase';
1215
import type {
1316
PartialStepState,
@@ -74,11 +77,37 @@ export class RevertGitCommand extends QuickCommand<State> {
7477
}
7578

7679
async execute(state: RevertStepState<State<GitRevisionReference[]>>) {
77-
const references = state.references.map(c => c.ref).reverse();
78-
for (const ref of references) {
80+
for (const ref of state.references.reverse()) {
7981
try {
80-
await state.repo.git.revert(ref, state.flags);
82+
await state.repo.git.revert(ref.ref, state.flags);
8183
} catch (ex) {
84+
if (ex instanceof RevertError) {
85+
let shouldRetry = false;
86+
if (ex.reason === RevertErrorReason.LocalChangesWouldBeOverwritten) {
87+
const response = await showShouldCommitOrStashPrompt();
88+
if (response === 'Stash') {
89+
await executeCommand(Commands.GitCommandsStashPush);
90+
shouldRetry = true;
91+
} else if (response === 'Commit') {
92+
await executeCoreCommand('workbench.view.scm');
93+
shouldRetry = true;
94+
} else {
95+
continue;
96+
}
97+
}
98+
99+
if (shouldRetry) {
100+
try {
101+
await state.repo.git.revert(ref.ref, state.flags);
102+
} catch (ex) {
103+
Logger.error(ex, this.title);
104+
void showGenericErrorMessage(ex.message);
105+
}
106+
}
107+
108+
continue;
109+
}
110+
82111
Logger.error(ex, this.title);
83112
void showGenericErrorMessage(ex.message);
84113
}

src/env/node/git/git.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ const revertErrorAndReason = [
180180
[GitErrors.badRevision, RevertErrorReason.BadRevision],
181181
[GitErrors.invalidObjectName, RevertErrorReason.InvalidObjectName],
182182
[GitErrors.revertHasConflicts, RevertErrorReason.Conflict],
183+
[GitErrors.changesWouldBeOverwritten, RevertErrorReason.LocalChangesWouldBeOverwritten],
183184
];
184185

185186
export class Git {
@@ -1597,13 +1598,13 @@ export class Git {
15971598
return this.git<string>({ cwd: repoPath }, 'reset', '-q', '--', ...pathspecs);
15981599
}
15991600

1600-
revert(repoPath: string, ...args: string[]) {
1601+
async revert(repoPath: string, ...args: string[]) {
16011602
try {
1602-
return this.git<string>({ cwd: repoPath }, 'revert', ...args);
1603+
await this.git<string>({ cwd: repoPath }, 'revert', ...args);
16031604
} catch (ex) {
16041605
const msg: string = ex?.toString() ?? '';
16051606
for (const [error, reason] of revertErrorAndReason) {
1606-
if (error.test(msg)) {
1607+
if (error.test(msg) || error.test(ex.stderr ?? '')) {
16071608
throw new RevertError(reason, ex);
16081609
}
16091610
}

src/git/errors.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,7 @@ export const enum RevertErrorReason {
572572
BadRevision,
573573
InvalidObjectName,
574574
Conflict,
575+
LocalChangesWouldBeOverwritten,
575576
Other,
576577
}
577578

@@ -621,6 +622,8 @@ export class RevertError extends Error {
621622
return `${baseMessage} because it is not a valid object name.`;
622623
case RevertErrorReason.Conflict:
623624
return `${baseMessage} it has unresolved conflicts. Resolve the conflicts and try again.`;
625+
case RevertErrorReason.LocalChangesWouldBeOverwritten:
626+
return `${baseMessage} because local changes would be overwritten. Commit or stash your changes first.`;
624627
default:
625628
return `${baseMessage}.`;
626629
}

src/messages.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,22 @@ export function showIntegrationRequestTimedOutWarningMessage(providerName: strin
230230
);
231231
}
232232

233+
export async function showShouldCommitOrStashPrompt(): Promise<string | undefined> {
234+
const stash = { title: 'Stash' };
235+
const commit = { title: 'Commit' };
236+
const cancel = { title: 'Cancel', isCloseAffordance: true };
237+
const result = await showMessage(
238+
'warn',
239+
'You have changes in your working tree. Commit or stash them before reverting',
240+
undefined,
241+
null,
242+
stash,
243+
commit,
244+
cancel,
245+
);
246+
return result?.title;
247+
}
248+
233249
export async function showWhatsNewMessage(majorVersion: string) {
234250
const confirm = { title: 'OK', isCloseAffordance: true };
235251
const releaseNotes = { title: 'View Release Notes' };

0 commit comments

Comments
 (0)