Skip to content

Commit e02d0c8

Browse files
committed
Adds remote caching
1 parent afa6cf8 commit e02d0c8

File tree

5 files changed

+59
-48
lines changed

5 files changed

+59
-48
lines changed

src/env/node/git/localGitProvider.ts

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ import { GitStatusParser } from '../../../git/parsers/statusParser';
123123
import { GitTagParser } from '../../../git/parsers/tagParser';
124124
import { GitTreeParser } from '../../../git/parsers/treeParser';
125125
import { GitWorktreeParser } from '../../../git/parsers/worktreeParser';
126-
import type { RemoteProviders } from '../../../git/remotes/remoteProviders';
127126
import { getRemoteProviderMatcher, loadRemoteProviders } from '../../../git/remotes/remoteProviders';
128127
import type { GitSearch, GitSearchResultData, GitSearchResults, SearchQuery } from '../../../git/search';
129128
import { getGitArgsFromSearchQuery, getSearchQueryComparisonKey } from '../../../git/search';
@@ -239,6 +238,7 @@ export class LocalGitProvider implements GitProvider, Disposable {
239238
private readonly _contributorsCache = new Map<string, Promise<GitContributor[]>>();
240239
private readonly _mergeStatusCache = new Map<string, GitMergeStatus | null>();
241240
private readonly _rebaseStatusCache = new Map<string, GitRebaseStatus | null>();
241+
private readonly _remotesCache = new Map<string, Promise<GitRemote[]>>();
242242
private readonly _repoInfoCache = new Map<string, RepositoryInfo>();
243243
private readonly _stashesCache = new Map<string, GitStash | null>();
244244
private readonly _tagsCache = new Map<string, Promise<PagedResult<GitTag>>>();
@@ -253,6 +253,11 @@ export class LocalGitProvider implements GitProvider, Disposable {
253253
this.git.setLocator(this.ensureGit.bind(this));
254254

255255
this._disposables.push(
256+
configuration.onDidChange(e => {
257+
if (configuration.changed(e, 'remotes')) {
258+
this.resetCaches('remotes');
259+
}
260+
}, this),
256261
this.container.events.on('git:cache:reset', e =>
257262
e.data.repoPath
258263
? this.resetCache(e.data.repoPath, ...(e.data.caches ?? emptyArray))
@@ -280,6 +285,10 @@ export class LocalGitProvider implements GitProvider, Disposable {
280285
this._contributorsCache.delete(`stats|${repo.path}`);
281286
}
282287

288+
if (e.changed(RepositoryChange.Remotes, RepositoryChange.RemoteProviders, RepositoryChangeComparisonMode.Any)) {
289+
this._remotesCache.delete(repo.path);
290+
}
291+
283292
if (e.changed(RepositoryChange.Index, RepositoryChange.Unknown, RepositoryChangeComparisonMode.Any)) {
284293
this._trackedPaths.clear();
285294
}
@@ -1069,6 +1078,10 @@ export class LocalGitProvider implements GitProvider, Disposable {
10691078
this._contributorsCache.delete(repoPath);
10701079
}
10711080

1081+
if (caches.length === 0 || caches.includes('remotes')) {
1082+
this._remotesCache.delete(repoPath);
1083+
}
1084+
10721085
if (caches.length === 0 || caches.includes('stashes')) {
10731086
this._stashesCache.delete(repoPath);
10741087
}
@@ -1098,6 +1111,10 @@ export class LocalGitProvider implements GitProvider, Disposable {
10981111
this._contributorsCache.clear();
10991112
}
11001113

1114+
if (caches.length === 0 || caches.includes('remotes')) {
1115+
this._remotesCache.clear();
1116+
}
1117+
11011118
if (caches.length === 0 || caches.includes('stashes')) {
11021119
this._stashesCache.clear();
11031120
}
@@ -3989,28 +4006,46 @@ export class LocalGitProvider implements GitProvider, Disposable {
39894006
}
39904007

39914008
@log({ args: { 1: false } })
3992-
async getRemotes(
3993-
repoPath: string | undefined,
3994-
options?: { providers?: RemoteProviders; sort?: boolean },
3995-
): Promise<GitRemote[]> {
4009+
async getRemotes(repoPath: string | undefined, options?: { sort?: boolean }): Promise<GitRemote[]> {
39964010
if (repoPath == null) return [];
39974011

3998-
const providers = options?.providers ?? loadRemoteProviders(configuration.get('remotes', null));
4012+
let remotesPromise = this.useCaching ? this._remotesCache.get(repoPath) : undefined;
4013+
if (remotesPromise == null) {
4014+
async function load(this: LocalGitProvider): Promise<GitRemote[]> {
4015+
const providers = loadRemoteProviders(
4016+
configuration.get('remotes', this.container.git.getRepository(repoPath!)?.folder?.uri ?? null),
4017+
);
39994018

4000-
try {
4001-
const data = await this.git.remote(repoPath);
4002-
const remotes = GitRemoteParser.parse(data, repoPath, getRemoteProviderMatcher(this.container, providers));
4003-
if (remotes == null) return [];
4019+
try {
4020+
const data = await this.git.remote(repoPath!);
4021+
const remotes = GitRemoteParser.parse(
4022+
data,
4023+
repoPath!,
4024+
getRemoteProviderMatcher(this.container, providers),
4025+
);
4026+
if (remotes == null) return [];
40044027

4005-
if (options?.sort) {
4006-
GitRemote.sort(remotes);
4028+
return remotes;
4029+
} catch (ex) {
4030+
this._remotesCache.delete(repoPath!);
4031+
Logger.error(ex);
4032+
return [];
4033+
}
40074034
}
40084035

4009-
return remotes;
4010-
} catch (ex) {
4011-
Logger.error(ex);
4012-
return [];
4036+
remotesPromise = load.call(this);
4037+
4038+
if (this.useCaching) {
4039+
this._remotesCache.set(repoPath, remotesPromise);
4040+
}
4041+
}
4042+
4043+
const remotes = await remotesPromise;
4044+
if (options?.sort) {
4045+
GitRemote.sort(remotes);
40134046
}
4047+
4048+
return remotes;
40144049
}
40154050

40164051
@gate()

src/git/gitProvider.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import type { GitTag, TagSortOptions } from './models/tag';
2323
import type { GitTreeEntry } from './models/tree';
2424
import type { GitUser } from './models/user';
2525
import type { GitWorktree } from './models/worktree';
26-
import type { RemoteProviders } from './remotes/remoteProviders';
2726
import type { GitSearch, SearchQuery } from './search';
2827

2928
export type GitCaches = 'branches' | 'contributors' | 'providers' | 'remotes' | 'stashes' | 'status' | 'tags';
@@ -379,10 +378,7 @@ export interface GitProvider extends Disposable {
379378
skip?: number | undefined;
380379
},
381380
): Promise<GitReflog | undefined>;
382-
getRemotes(
383-
repoPath: string | undefined,
384-
options?: { providers?: RemoteProviders; sort?: boolean },
385-
): Promise<GitRemote[]>;
381+
getRemotes(repoPath: string | undefined, options?: { sort?: boolean }): Promise<GitRemote[]>;
386382
getRevisionContent(repoPath: string, path: string, ref: string): Promise<Uint8Array | undefined>;
387383
getStash(repoPath: string | undefined): Promise<GitStash | undefined>;
388384
getStatusForFile(repoPath: string, uri: Uri): Promise<GitStatusFile | undefined>;

src/git/gitProviderService.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ import type { GitTreeEntry } from './models/tree';
7777
import type { GitUser } from './models/user';
7878
import type { GitWorktree } from './models/worktree';
7979
import type { RemoteProvider } from './remotes/remoteProvider';
80-
import type { RemoteProviders } from './remotes/remoteProviders';
8180
import type { RichRemoteProvider } from './remotes/richRemoteProvider';
8281
import type { GitSearch, SearchQuery } from './search';
8382

@@ -2272,10 +2271,7 @@ export class GitProviderService implements Disposable {
22722271
}
22732272

22742273
@log({ args: { 1: false } })
2275-
async getRemotes(
2276-
repoPath: string | Uri | undefined,
2277-
options?: { providers?: RemoteProviders; sort?: boolean },
2278-
): Promise<GitRemote[]> {
2274+
async getRemotes(repoPath: string | Uri | undefined, options?: { sort?: boolean }): Promise<GitRemote[]> {
22792275
if (repoPath == null) return [];
22802276

22812277
const { provider, path } = this.getProvider(repoPath);

src/git/models/repository.ts

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ import { getLogScope } from '../../system/logger.scope';
2222
import { updateRecordValue } from '../../system/object';
2323
import { basename, normalizePath } from '../../system/path';
2424
import type { GitDir, GitProviderDescriptor, GitRepositoryCaches } from '../gitProvider';
25-
import type { RemoteProviders } from '../remotes/remoteProviders';
26-
import { loadRemoteProviders } from '../remotes/remoteProviders';
2725
import type { RichRemoteProvider } from '../remotes/richRemoteProvider';
2826
import type { GitSearch, SearchQuery } from '../search';
2927
import type { BranchSortOptions, GitBranch } from './branch';
@@ -216,7 +214,6 @@ export class Repository implements Disposable {
216214
private _fsWatcherDisposable: Disposable | undefined;
217215
private _pendingFileSystemChange?: RepositoryFileSystemChangeEvent;
218216
private _pendingRepoChange?: RepositoryChangeEvent;
219-
private _providers: RemoteProviders | undefined;
220217
private _remotes: Promise<GitRemote[]> | undefined;
221218
private _remotesDisposable: Disposable | undefined;
222219
private _suspended: boolean;
@@ -347,13 +344,9 @@ export class Repository implements Disposable {
347344
}
348345

349346
private onConfigurationChanged(e?: ConfigurationChangeEvent) {
350-
if (configuration.changed(e, 'remotes', this.folder?.uri)) {
351-
this._providers = loadRemoteProviders(configuration.get('remotes', this.folder?.uri ?? null));
352-
353-
if (e != null) {
354-
this.resetCaches('remotes');
355-
this.fireChange(RepositoryChange.Remotes);
356-
}
347+
if (e != null && configuration.changed(e, 'remotes', this.folder?.uri)) {
348+
this.resetCaches('remotes');
349+
this.fireChange(RepositoryChange.Remotes);
357350
}
358351
}
359352

@@ -685,13 +678,8 @@ export class Repository implements Disposable {
685678

686679
async getRemotes(options?: { filter?: (remote: GitRemote) => boolean; sort?: boolean }): Promise<GitRemote[]> {
687680
if (this._remotes == null) {
688-
if (this._providers == null) {
689-
const remotesCfg = configuration.get('remotes', this.folder?.uri ?? null);
690-
this._providers = loadRemoteProviders(remotesCfg);
691-
}
692-
693681
// Since we are caching the results, always sort
694-
this._remotes = this.container.git.getRemotes(this.path, { providers: this._providers, sort: true });
682+
this._remotes = this.container.git.getRemotes(this.path, { sort: true });
695683
void this.subscribeToRemotes(this._remotes);
696684
}
697685

src/plus/github/githubGitProvider.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ import { getTagId, GitTag, sortTags } from '../../git/models/tag';
7474
import type { GitTreeEntry } from '../../git/models/tree';
7575
import type { GitUser } from '../../git/models/user';
7676
import { isUserMatch } from '../../git/models/user';
77-
import type { RemoteProviders } from '../../git/remotes/remoteProviders';
7877
import { getRemoteProviderMatcher, loadRemoteProviders } from '../../git/remotes/remoteProviders';
7978
import type { GitSearch, GitSearchResultData, GitSearchResults, SearchQuery } from '../../git/search';
8079
import { getSearchQueryComparisonKey, parseSearchQuery } from '../../git/search';
@@ -2494,13 +2493,10 @@ export class GitHubGitProvider implements GitProvider, Disposable {
24942493
}
24952494

24962495
@log({ args: { 1: false } })
2497-
async getRemotes(
2498-
repoPath: string | undefined,
2499-
options?: { providers?: RemoteProviders; sort?: boolean },
2500-
): Promise<GitRemote[]> {
2496+
async getRemotes(repoPath: string | undefined, _options?: { sort?: boolean }): Promise<GitRemote[]> {
25012497
if (repoPath == null) return [];
25022498

2503-
const providers = options?.providers ?? loadRemoteProviders(configuration.get('remotes', null));
2499+
const providers = loadRemoteProviders(configuration.get('remotes', null));
25042500

25052501
const uri = Uri.parse(repoPath, true);
25062502
const [, owner, repo] = uri.path.split('/', 3);

0 commit comments

Comments
 (0)