From 18ff67751f1ad7e258bcf6be354fb2ea650b3e4c Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Tue, 25 Jan 2022 23:36:24 -0500 Subject: [PATCH] Adds onDid[Open|Close]Repository provider events Ensures known SCM repos are known to GitLens --- src/env/node/git/localGitProvider.ts | 65 ++++++++++++++++++++---------------- src/git/gitProvider.ts | 12 ++++++- src/git/gitProviderService.ts | 15 +++++++++ 3 files changed, 62 insertions(+), 30 deletions(-) diff --git a/src/env/node/git/localGitProvider.ts b/src/env/node/git/localGitProvider.ts index a2d8d36..691a7c9 100644 --- a/src/env/node/git/localGitProvider.ts +++ b/src/env/node/git/localGitProvider.ts @@ -32,7 +32,9 @@ import { GitProviderDescriptor, GitProviderId, PagedResult, + RepositoryCloseEvent, RepositoryInitWatcher, + RepositoryOpenEvent, ScmRepository, } from '../../../git/gitProvider'; import { GitProviderService } from '../../../git/gitProviderService'; @@ -136,6 +138,16 @@ export class LocalGitProvider implements GitProvider, Disposable { return this._onDidChangeRepository.event; } + private _onDidCloseRepository = new EventEmitter(); + get onDidCloseRepository(): Event { + return this._onDidCloseRepository.event; + } + + private _onDidOpenRepository = new EventEmitter(); + get onDidOpenRepository(): Event { + return this._onDidOpenRepository.event; + } + private readonly _branchesCache = new Map>>(); private readonly _contributorsCache = new Map>(); private readonly _mergeStatusCache = new Map(); @@ -146,11 +158,15 @@ export class LocalGitProvider implements GitProvider, Disposable { private readonly _trackedPaths = new PathTrie>(); private readonly _userMapCache = new Map(); + private _disposables: Disposable[] = []; + constructor(private readonly container: Container) { Git.setLocator(this.ensureGit.bind(this)); } - dispose() {} + dispose() { + Disposable.from(...this._disposables).dispose(); + } private get useCaching() { return this.container.config.advanced.caching.enabled; @@ -214,31 +230,22 @@ export class LocalGitProvider implements GitProvider, Disposable { throw new UnableToFindGitError(); } - const scmPromise = this.getScmGitApi(); + const scmGitPromise = this.getScmGitApi(); - async function subscribeToScmOpenCloseRepository( - container: Container, - apiPromise: Promise, - ) { - const gitApi = await apiPromise; - if (gitApi == null) return; + async function subscribeToScmOpenCloseRepository(this: LocalGitProvider) { + const scmGit = await scmGitPromise; + if (scmGit == null) return; - container.context.subscriptions.push( - gitApi.onDidCloseRepository(e => { - const repository = container.git.getCachedRepository(normalizePath(e.rootUri.fsPath)); - if (repository != null) { - repository.closed = true; - } - }), - gitApi.onDidOpenRepository(e => { - const repository = container.git.getCachedRepository(normalizePath(e.rootUri.fsPath)); - if (repository != null) { - repository.closed = false; - } - }), + this._disposables.push( + scmGit.onDidCloseRepository(e => this._onDidCloseRepository.fire({ uri: e.rootUri })), + scmGit.onDidOpenRepository(e => this._onDidOpenRepository.fire({ uri: e.rootUri })), ); + + for (const scmRepository of scmGit.repositories) { + this._onDidOpenRepository.fire({ uri: scmRepository.rootUri }); + } } - void subscribeToScmOpenCloseRepository(this.container, scmPromise); + void subscribeToScmOpenCloseRepository.call(this); const potentialGitPaths = configuration.getAny('git.path') ?? @@ -248,7 +255,7 @@ export class LocalGitProvider implements GitProvider, Disposable { const findGitPromise = findGitPath(potentialGitPaths); // Try to use the same git as the built-in vscode git extension, but don't wait for it if we find something faster - const findGitFromSCMPromise = scmPromise.then(gitApi => { + const findGitFromSCMPromise = scmGitPromise.then(gitApi => { const path = gitApi?.git.path; if (!path) return findGitPromise; @@ -294,15 +301,15 @@ export class LocalGitProvider implements GitProvider, Disposable { async discoverRepositories(uri: Uri): Promise { if (uri.scheme !== DocumentSchemes.File) return []; - const autoRepositoryDetection = - configuration.getAny( - BuiltInGitConfiguration.AutoRepositoryDetection, - ) ?? true; - if (autoRepositoryDetection === false || autoRepositoryDetection === 'openEditors') return []; - try { void (await this.ensureGit()); + const autoRepositoryDetection = + configuration.getAny( + BuiltInGitConfiguration.AutoRepositoryDetection, + ) ?? true; + if (autoRepositoryDetection === false || autoRepositoryDetection === 'openEditors') return []; + const repositories = await this.repositorySearch(workspace.getWorkspaceFolder(uri)!); if (autoRepositoryDetection === true || autoRepositoryDetection === 'subFolders') { for (const repository of repositories) { diff --git a/src/git/gitProvider.ts b/src/git/gitProvider.ts index 608c425..2bff633 100644 --- a/src/git/gitProvider.ts +++ b/src/git/gitProvider.ts @@ -64,8 +64,18 @@ export interface PagedResult { readonly values: NonNullable[]; } -export interface GitProvider { +export interface RepositoryCloseEvent { + readonly uri: Uri; +} + +export interface RepositoryOpenEvent { + readonly uri: Uri; +} + +export interface GitProvider extends Disposable { get onDidChangeRepository(): Event; + get onDidCloseRepository(): Event; + get onDidOpenRepository(): Event; readonly descriptor: GitProviderDescriptor; readonly supportedSchemes: string[]; diff --git a/src/git/gitProviderService.ts b/src/git/gitProviderService.ts index 1f1c99c..bb9ff92 100644 --- a/src/git/gitProviderService.ts +++ b/src/git/gitProviderService.ts @@ -308,6 +308,7 @@ export class GitProviderService implements Disposable { } const disposable = Disposable.from( + provider, ...disposables, provider.onDidChangeRepository(e => { if (e.changed(RepositoryChange.Closed, RepositoryChangeComparisonMode.Any)) { @@ -319,6 +320,20 @@ export class GitProviderService implements Disposable { this._onDidChangeRepository.fire(e); }), + provider.onDidCloseRepository(e => { + const repository = this._repositories.get(e.uri); + if (repository != null) { + repository.closed = true; + } + }), + provider.onDidOpenRepository(e => { + const repository = this._repositories.get(e.uri); + if (repository != null) { + repository.closed = false; + } else { + void this.getOrCreateRepository(e.uri); + } + }), ); this.fireProvidersChanged([provider]);