Skip to content

Commit 54c6639

Browse files
committed
add git.supportCancellation setting
2 parents 1c781cb + 6dbf6b5 commit 54c6639

5 files changed

Lines changed: 46 additions & 11 deletions

File tree

extensions/git/package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1318,6 +1318,12 @@
13181318
"scope": "resource",
13191319
"default": true,
13201320
"description": "%config.openDiffOnClick%"
1321+
},
1322+
"git.supportCancellation": {
1323+
"type": "boolean",
1324+
"scope": "resource",
1325+
"default": false,
1326+
"description": "%config.supportCancellation%"
13211327
}
13221328
}
13231329
},
@@ -1483,4 +1489,4 @@
14831489
"@types/which": "^1.0.28",
14841490
"mocha": "^3.2.0"
14851491
}
1486-
}
1492+
}

extensions/git/package.nls.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,12 @@
119119
"config.useForcePushWithLease": "Controls whether force pushing uses the safer force-with-lease variant.",
120120
"config.confirmForcePush": "Controls whether to ask for confirmation before force-pushing.",
121121
"config.openDiffOnClick": "Controls whether the diff editor should be opened when clicking a change. Otherwise the regular editor will be opened.",
122+
"config.supportCancellation": "Controls whether a notification comes up when running the Sync action, which allows the user to cancel the operation.",
122123
"colors.added": "Color for added resources.",
123124
"colors.modified": "Color for modified resources.",
124125
"colors.deleted": "Color for deleted resources.",
125126
"colors.untracked": "Color for untracked resources.",
126127
"colors.ignored": "Color for ignored resources.",
127128
"colors.conflict": "Color for resources with conflicts.",
128129
"colors.submodule": "Color for submodule resources."
129-
}
130+
}

extensions/git/src/commands.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1956,8 +1956,16 @@ export class CommandCenter {
19561956
}
19571957

19581958
@command('git.sync', { repository: true })
1959-
sync(repository: Repository): Promise<void> {
1960-
return this._sync(repository, false);
1959+
async sync(repository: Repository): Promise<void> {
1960+
try {
1961+
await this._sync(repository, false);
1962+
} catch (err) {
1963+
if (/Cancelled/i.test(err && (err.message || err.stderr || ''))) {
1964+
return;
1965+
}
1966+
1967+
throw err;
1968+
}
19611969
}
19621970

19631971
@command('git._syncAll')
@@ -1974,8 +1982,16 @@ export class CommandCenter {
19741982
}
19751983

19761984
@command('git.syncRebase', { repository: true })
1977-
syncRebase(repository: Repository): Promise<void> {
1978-
return this._sync(repository, true);
1985+
async syncRebase(repository: Repository): Promise<void> {
1986+
try {
1987+
await this._sync(repository, true);
1988+
} catch (err) {
1989+
if (/Cancelled/i.test(err && (err.message || err.stderr || ''))) {
1990+
return;
1991+
}
1992+
1993+
throw err;
1994+
}
19791995
}
19801996

19811997
@command('git.publish', { repository: true })

extensions/git/src/git.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,7 @@ export interface CommitOptions {
649649
export interface PullOptions {
650650
unshallow?: boolean;
651651
tags?: boolean;
652+
readonly cancellationToken?: CancellationToken;
652653
}
653654

654655
export enum ForcePushMode {
@@ -1396,7 +1397,7 @@ export class Repository {
13961397
}
13971398

13981399
try {
1399-
await this.run(args);
1400+
await this.run(args, options);
14001401
} catch (err) {
14011402
if (/^CONFLICT \([^)]+\): \b/m.test(err.stdout || '')) {
14021403
err.gitErrorCode = GitErrorCodes.Conflict;

extensions/git/src/repository.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
import { commands, Uri, Command, EventEmitter, Event, scm, SourceControl, SourceControlInputBox, SourceControlResourceGroup, SourceControlResourceState, SourceControlResourceDecorations, SourceControlInputBoxValidation, Disposable, ProgressLocation, window, workspace, WorkspaceEdit, ThemeColor, DecorationData, Memento, SourceControlInputBoxValidationType, OutputChannel, LogLevel, env } from 'vscode';
6+
import { commands, Uri, Command, EventEmitter, Event, scm, SourceControl, SourceControlInputBox, SourceControlResourceGroup, SourceControlResourceState, SourceControlResourceDecorations, SourceControlInputBoxValidation, Disposable, ProgressLocation, window, workspace, WorkspaceEdit, ThemeColor, DecorationData, Memento, SourceControlInputBoxValidationType, OutputChannel, LogLevel, env, ProgressOptions, CancellationToken } from 'vscode';
77
import { Repository as BaseRepository, Commit, Stash, GitError, Submodule, CommitOptions, ForcePushMode } from './git';
88
import { anyEvent, filterEvent, eventToPromise, dispose, find, isDescendant, IDisposable, onceEvent, EmptyDisposable, debounceEvent, combinedDisposable, watch, IFileWatcher } from './util';
99
import { memoize, throttle, debounce } from './decorators';
@@ -1146,11 +1146,22 @@ export class Repository implements Disposable {
11461146
const config = workspace.getConfiguration('git', Uri.file(this.root));
11471147
const fetchOnPull = config.get<boolean>('fetchOnPull');
11481148
const tags = config.get<boolean>('pullTags');
1149+
const supportCancellation = config.get<boolean>('supportCancellation');
11491150

1150-
if (fetchOnPull) {
1151-
await this.repository.pull(rebase, undefined, undefined, { tags });
1151+
const fn = fetchOnPull
1152+
? async (cancellationToken?: CancellationToken) => await this.repository.pull(rebase, undefined, undefined, { tags, cancellationToken })
1153+
: async (cancellationToken?: CancellationToken) => await this.repository.pull(rebase, remoteName, pullBranch, { tags, cancellationToken });
1154+
1155+
if (supportCancellation) {
1156+
const opts: ProgressOptions = {
1157+
location: ProgressLocation.Notification,
1158+
title: localize('sync is unpredictable', "Syncing. Cancelling may cause serious damages to the repository"),
1159+
cancellable: true
1160+
};
1161+
1162+
await window.withProgress(opts, (_, token) => fn(token));
11521163
} else {
1153-
await this.repository.pull(rebase, remoteName, pullBranch, { tags });
1164+
await fn();
11541165
}
11551166

11561167
const remote = this.remotes.find(r => r.name === remoteName);

0 commit comments

Comments
 (0)