Skip to content

Commit bf93067

Browse files
committed
retry if local changes error
1 parent c2fdbaf commit bf93067

File tree

5 files changed

+67
-18
lines changed

5 files changed

+67
-18
lines changed

package.json

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19567,29 +19567,29 @@
1956719567
"bundle": "webpack --mode production",
1956819568
"bundle:extension": "webpack --mode production --config-name extension:node",
1956919569
"clean": "pnpx rimraf --glob dist out .vscode-test .vscode-test-web .eslintcache* tsconfig*.tsbuildinfo",
19570-
"clean:all": "npm run clean && npm run clean:deps",
19570+
"clean:all": "pnpm run clean && pnpm run clean:deps",
1957119571
"clean:lint": "pnpx rimraf .eslintcache",
1957219572
"clean:deps": "pnpx rimraf node_modules",
1957319573
"copy:images": "webpack --config webpack.config.images.mjs",
19574-
"graph:link": "npm link @gitkraken/gitkraken-components",
19575-
"graph:link:main": "pushd \"../GitKrakenComponents\" && npm link && popd && npm graph:link",
19576-
"graph:unlink": "npm unlink @gitkraken/gitkraken-components && npm install --force",
19577-
"graph:unlink:main": "npm graph:unlink && pushd \"../GitKrakenComponents\" && npm unlink && popd",
19574+
"graph:link": "pnpm link @gitkraken/gitkraken-components",
19575+
"graph:link:main": "pushd \"../GitKrakenComponents\" && pnpm link && popd && pnpm graph:link",
19576+
"graph:unlink": "pnpm unlink @gitkraken/gitkraken-components && pnpm install --force",
19577+
"graph:unlink:main": "pnpm graph:unlink && pushd \"../GitKrakenComponents\" && pnpm unlink && popd",
1957819578
"icons:apply": "node ./scripts/applyIconsContribution.mjs",
1957919579
"icons:svgo": "svgo -q -f ./images/icons/ --config svgo.config.js",
19580-
"lint": "npm run clean:lint && eslint .",
19581-
"lint:fix": "npm run clean:lint && eslint . --fix",
19582-
"lint:webviews": "npm run clean:lint && eslint \"src/webviews/apps/**/*.ts?(x)\"",
19580+
"lint": "pnpm run clean:lint && eslint .",
19581+
"lint:fix": "pnpm run clean:lint && eslint . --fix",
19582+
"lint:webviews": "pnpm run clean:lint && eslint \"src/webviews/apps/**/*.ts?(x)\"",
1958319583
"package": "vsce package --no-dependencies",
19584-
"package-pre": "npm run patch-pre && npm run package --pre-release",
19584+
"package-pre": "pnpm run patch-pre && pnpm run package --pre-release",
1958519585
"patch-pre": "node ./scripts/applyPreReleasePatch.mjs",
1958619586
"prep-release": "node ./scripts/prep-release.mjs",
1958719587
"pretty": "prettier --config .prettierrc --write .",
1958819588
"pretty:check": "prettier --config .prettierrc --check .",
1958919589
"pub": "vsce publish --no-dependencies",
1959019590
"pub-pre": "vsce publish --no-dependencies --pre-release",
19591-
"rebuild": "npm run reset && npm run build",
19592-
"reset": "npm run clean && npm install --force",
19591+
"rebuild": "pnpm run reset && pnpm run build",
19592+
"reset": "pnpm run clean && pnpm install --force",
1959319593
"test": "vscode-test",
1959419594
"test:e2e": "playwright test -c tests/e2e/playwright.config.ts",
1959519595
"watch": "webpack --watch --mode development",

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)