Skip to content

Commit 8fd500e

Browse files
authored
Git - Add calcellation support for getRefs (microsoft#165938)
1 parent 0308afc commit 8fd500e

File tree

2 files changed

+35
-18
lines changed

2 files changed

+35
-18
lines changed

extensions/git/src/git.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ async function exec(child: cp.ChildProcess, cancellationToken?: CancellationToke
198198
}
199199

200200
if (cancellationToken && cancellationToken.isCancellationRequested) {
201-
throw new GitError({ message: 'Cancelled' });
201+
throw new CancellationError();
202202
}
203203

204204
const disposables: IDisposable[] = [];
@@ -239,7 +239,7 @@ async function exec(child: cp.ChildProcess, cancellationToken?: CancellationToke
239239
// noop
240240
}
241241

242-
e(new GitError({ message: 'Cancelled' }));
242+
e(new CancellationError());
243243
});
244244
});
245245

@@ -568,12 +568,21 @@ export class Git {
568568
}
569569

570570
const startExec = Date.now();
571-
const bufferResult = await exec(child, options.cancellationToken);
572-
const durExec = Date.now() - startExec;
571+
let bufferResult: IExecutionResult<Buffer>;
572+
573+
try {
574+
bufferResult = await exec(child, options.cancellationToken);
575+
} catch (ex) {
576+
if (ex instanceof CancellationError) {
577+
this.log(`> git ${args.join(' ')} [${Date.now() - startExec}ms] (cancelled)\n`);
578+
}
579+
580+
throw ex;
581+
}
573582

574583
if (options.log !== false) {
575584
// command
576-
this.log(`> git ${args.join(' ')} [${durExec}ms]\n`);
585+
this.log(`> git ${args.join(' ')} [${Date.now() - startExec}ms]\n`);
577586

578587
// stdout
579588
if (bufferResult.stdout.length > 0 && args.find(a => this.commandsToLog.includes(a))) {
@@ -2119,7 +2128,11 @@ export class Repository {
21192128
.map(([ref]) => ({ name: ref, type: RefType.Head } as Branch));
21202129
}
21212130

2122-
async getRefs(opts?: { sort?: 'alphabetically' | 'committerdate'; contains?: string; pattern?: string; count?: number }): Promise<Ref[]> {
2131+
async getRefs(opts?: { sort?: 'alphabetically' | 'committerdate'; contains?: string; pattern?: string; count?: number; cancellationToken?: CancellationToken }): Promise<Ref[]> {
2132+
if (opts?.cancellationToken && opts?.cancellationToken.isCancellationRequested) {
2133+
throw new CancellationError();
2134+
}
2135+
21232136
const args = ['for-each-ref'];
21242137

21252138
if (opts?.count) {
@@ -2140,7 +2153,7 @@ export class Repository {
21402153
args.push('--contains', opts.contains);
21412154
}
21422155

2143-
const result = await this.exec(args);
2156+
const result = await this.exec(args, { cancellationToken: opts?.cancellationToken });
21442157

21452158
const fn = (line: string): Ref | null => {
21462159
let match: RegExpExecArray | null;

extensions/git/src/repository.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2059,33 +2059,37 @@ export class Repository implements Disposable {
20592059
this._updateResourceGroupsState(optimisticResourcesGroups);
20602060
}
20612061

2062-
const config = workspace.getConfiguration('git');
2063-
let sort = config.get<'alphabetically' | 'committerdate'>('branchSortOrder') || 'alphabetically';
2064-
if (sort !== 'alphabetically' && sort !== 'committerdate') {
2065-
sort = 'alphabetically';
2066-
}
2067-
2068-
const [HEAD, refs, remotes, submodules, rebaseCommit, mergeInProgress, commitTemplate] =
2062+
const [HEAD, remotes, submodules, rebaseCommit, mergeInProgress, commitTemplate] =
20692063
await Promise.all([
20702064
this.repository.getHEADBranch(),
2071-
this.repository.getRefs({ sort }),
20722065
this.repository.getRemotes(),
20732066
this.repository.getSubmodules(),
20742067
this.getRebaseCommit(),
20752068
this.isMergeInProgress(),
20762069
this.getInputTemplate()]);
20772070

20782071
this._HEAD = HEAD;
2079-
this._refs = refs!;
20802072
this._remotes = remotes!;
20812073
this._submodules = submodules!;
20822074
this.rebaseCommit = rebaseCommit;
20832075
this.mergeInProgress = mergeInProgress;
20842076

20852077
this._sourceControl.commitTemplate = commitTemplate;
20862078

2087-
// Update resource states based on status data
2088-
this._updateResourceGroupsState(await this.getStatus(cancellationToken));
2079+
// Execute cancellable long-running operations
2080+
const config = workspace.getConfiguration('git');
2081+
let sort = config.get<'alphabetically' | 'committerdate'>('branchSortOrder') || 'alphabetically';
2082+
if (sort !== 'alphabetically' && sort !== 'committerdate') {
2083+
sort = 'alphabetically';
2084+
}
2085+
2086+
const [resourceGroups, refs] =
2087+
await Promise.all([
2088+
this.getStatus(cancellationToken),
2089+
this.repository.getRefs({ sort, cancellationToken })]);
2090+
2091+
this._refs = refs!;
2092+
this._updateResourceGroupsState(resourceGroups);
20892093

20902094
this._onDidChangeStatus.fire();
20912095
}

0 commit comments

Comments
 (0)