Skip to content

Commit

Permalink
retry if local changes error
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiolms committed Nov 27, 2024
1 parent 42255ad commit 5d90ed8
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 7 deletions.
37 changes: 33 additions & 4 deletions src/commands/git/revert.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { Commands } from '../../constants.commands';
import type { Container } from '../../container';
import { RevertError, RevertErrorReason } from '../../git/errors';
import type { GitBranch } from '../../git/models/branch';
import type { GitLog } from '../../git/models/log';
import type { GitRevisionReference } from '../../git/models/reference';
import { getReferenceLabel } from '../../git/models/reference';
import type { Repository } from '../../git/models/repository';
import { showGenericErrorMessage } from '../../messages';
import { showGenericErrorMessage, showShouldCommitOrStashPrompt } from '../../messages';
import type { FlagsQuickPickItem } from '../../quickpicks/items/flags';
import { createFlagsQuickPickItem } from '../../quickpicks/items/flags';
import { Logger } from '../../system/logger';
import { executeCommand, executeCoreCommand } from '../../system/vscode/command';
import type { ViewsWithRepositoryFolders } from '../../views/viewBase';
import type {
PartialStepState,
Expand Down Expand Up @@ -74,11 +77,37 @@ export class RevertGitCommand extends QuickCommand<State> {
}

async execute(state: RevertStepState<State<GitRevisionReference[]>>) {
const references = state.references.map(c => c.ref).reverse();
for (const ref of references) {
for (const ref of state.references.reverse()) {
try {
await state.repo.git.revert(ref, state.flags);
await state.repo.git.revert(ref.ref, state.flags);
} catch (ex) {
if (ex instanceof RevertError) {
let shouldRetry = false;
if (ex.reason === RevertErrorReason.LocalChangesWouldBeOverwritten) {
const response = await showShouldCommitOrStashPrompt();
if (response === 'Stash') {
await executeCommand(Commands.GitCommandsStashPush);
shouldRetry = true;
} else if (response === 'Commit') {
await executeCoreCommand('workbench.view.scm');
shouldRetry = true;
} else {
continue;
}
}

if (shouldRetry) {
try {
await state.repo.git.revert(ref.ref, state.flags);
} catch (ex) {
Logger.error(ex, this.title);
void showGenericErrorMessage(ex.message);
}
}

continue;
}

Logger.error(ex, this.title);
void showGenericErrorMessage(ex.message);
}
Expand Down
7 changes: 4 additions & 3 deletions src/env/node/git/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ const revertErrorAndReason = [
[GitErrors.badRevision, RevertErrorReason.BadRevision],
[GitErrors.invalidObjectName, RevertErrorReason.InvalidObjectName],
[GitErrors.revertHasConflicts, RevertErrorReason.Conflict],
[GitErrors.changesWouldBeOverwritten, RevertErrorReason.LocalChangesWouldBeOverwritten],
];

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

revert(repoPath: string, ...args: string[]) {
async revert(repoPath: string, ...args: string[]) {
try {
return this.git<string>({ cwd: repoPath }, 'revert', ...args);
await this.git<string>({ cwd: repoPath }, 'revert', ...args);
} catch (ex) {
const msg: string = ex?.toString() ?? '';
for (const [error, reason] of revertErrorAndReason) {
if (error.test(msg)) {
if (error.test(msg) || error.test(ex.stderr ?? '')) {
throw new RevertError(reason, ex);
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/git/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,7 @@ export const enum RevertErrorReason {
BadRevision,
InvalidObjectName,
Conflict,
LocalChangesWouldBeOverwritten,
Other,
}

Expand Down Expand Up @@ -621,6 +622,8 @@ export class RevertError extends Error {
return `${baseMessage} because it is not a valid object name.`;
case RevertErrorReason.Conflict:
return `${baseMessage} it has unresolved conflicts. Resolve the conflicts and try again.`;
case RevertErrorReason.LocalChangesWouldBeOverwritten:
return `${baseMessage} because local changes would be overwritten. Commit or stash your changes first.`;
default:
return `${baseMessage}.`;
}
Expand Down
16 changes: 16 additions & 0 deletions src/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,22 @@ export function showIntegrationRequestTimedOutWarningMessage(providerName: strin
);
}

export async function showShouldCommitOrStashPrompt(): Promise<string | undefined> {
const stash = { title: 'Stash' };
const commit = { title: 'Commit' };
const cancel = { title: 'Cancel', isCloseAffordance: true };
const result = await showMessage(
'warn',
'You have changes in your working tree. Commit or stash them before reverting',
undefined,
null,
stash,
commit,
cancel,
);
return result?.title;
}

export async function showWhatsNewMessage(majorVersion: string) {
const confirm = { title: 'OK', isCloseAffordance: true };
const releaseNotes = { title: 'View Release Notes' };
Expand Down

0 comments on commit 5d90ed8

Please sign in to comment.