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 Dec 3, 2024
1 parent c2fdbaf commit bf93067
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 18 deletions.
22 changes: 11 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19567,29 +19567,29 @@
"bundle": "webpack --mode production",
"bundle:extension": "webpack --mode production --config-name extension:node",
"clean": "pnpx rimraf --glob dist out .vscode-test .vscode-test-web .eslintcache* tsconfig*.tsbuildinfo",
"clean:all": "npm run clean && npm run clean:deps",
"clean:all": "pnpm run clean && pnpm run clean:deps",
"clean:lint": "pnpx rimraf .eslintcache",
"clean:deps": "pnpx rimraf node_modules",
"copy:images": "webpack --config webpack.config.images.mjs",
"graph:link": "npm link @gitkraken/gitkraken-components",
"graph:link:main": "pushd \"../GitKrakenComponents\" && npm link && popd && npm graph:link",
"graph:unlink": "npm unlink @gitkraken/gitkraken-components && npm install --force",
"graph:unlink:main": "npm graph:unlink && pushd \"../GitKrakenComponents\" && npm unlink && popd",
"graph:link": "pnpm link @gitkraken/gitkraken-components",
"graph:link:main": "pushd \"../GitKrakenComponents\" && pnpm link && popd && pnpm graph:link",
"graph:unlink": "pnpm unlink @gitkraken/gitkraken-components && pnpm install --force",
"graph:unlink:main": "pnpm graph:unlink && pushd \"../GitKrakenComponents\" && pnpm unlink && popd",
"icons:apply": "node ./scripts/applyIconsContribution.mjs",
"icons:svgo": "svgo -q -f ./images/icons/ --config svgo.config.js",
"lint": "npm run clean:lint && eslint .",
"lint:fix": "npm run clean:lint && eslint . --fix",
"lint:webviews": "npm run clean:lint && eslint \"src/webviews/apps/**/*.ts?(x)\"",
"lint": "pnpm run clean:lint && eslint .",
"lint:fix": "pnpm run clean:lint && eslint . --fix",
"lint:webviews": "pnpm run clean:lint && eslint \"src/webviews/apps/**/*.ts?(x)\"",
"package": "vsce package --no-dependencies",
"package-pre": "npm run patch-pre && npm run package --pre-release",
"package-pre": "pnpm run patch-pre && pnpm run package --pre-release",
"patch-pre": "node ./scripts/applyPreReleasePatch.mjs",
"prep-release": "node ./scripts/prep-release.mjs",
"pretty": "prettier --config .prettierrc --write .",
"pretty:check": "prettier --config .prettierrc --check .",
"pub": "vsce publish --no-dependencies",
"pub-pre": "vsce publish --no-dependencies --pre-release",
"rebuild": "npm run reset && npm run build",
"reset": "npm run clean && npm install --force",
"rebuild": "pnpm run reset && pnpm run build",
"reset": "pnpm run clean && pnpm install --force",
"test": "vscode-test",
"test:e2e": "playwright test -c tests/e2e/playwright.config.ts",
"watch": "webpack --watch --mode development",
Expand Down
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 bf93067

Please sign in to comment.