From f23052f158cac1b2508374e0480679bf4b229c12 Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Tue, 24 May 2022 02:07:51 -0400 Subject: [PATCH] Improves findRepository flow --- src/git/gitProviderService.ts | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/src/git/gitProviderService.ts b/src/git/gitProviderService.ts index 04cbd78..80f561d 100644 --- a/src/git/gitProviderService.ts +++ b/src/git/gitProviderService.ts @@ -4,6 +4,7 @@ import { Disposable, Event, EventEmitter, + FileType, ProgressLocation, Range, TextDocument, @@ -38,7 +39,7 @@ import { groupByFilterMap, groupByMap } from '../system/array'; import { gate } from '../system/decorators/gate'; import { debug, log } from '../system/decorators/log'; import { count, filter, first, flatMap, map, some } from '../system/iterable'; -import { dirname, getBestPath, getScheme, isAbsolute, maybeUri, normalizePath } from '../system/path'; +import { getBestPath, getScheme, isAbsolute, maybeUri, normalizePath } from '../system/path'; import { cancellable, fastestSettled, isPromise, PromiseCancelledError } from '../system/promise'; import { VisitedPathsTrie } from '../system/trie'; import { @@ -1824,14 +1825,26 @@ export class GitProviderService implements Disposable { async getOrOpenRepository(uri: Uri, detectNested?: boolean): Promise { const cc = Logger.getCorrelationContext(); - const folderPath = dirname(getBestPath(uri)); - const repository = this.getRepository(uri); + const path = getBestPath(uri); + let repository: Repository | undefined; + repository = this.getRepository(uri); + + let isDirectory: boolean | undefined; detectNested = detectNested ?? configuration.get('detectNestedRepositories'); if (!detectNested) { if (repository != null) return repository; - } else if (this._visitedPaths.has(folderPath)) { + } else if (this._visitedPaths.has(path)) { return repository; + } else { + const stats = await workspace.fs.stat(uri); + // If the uri isn't a directory, go up one level + if ((stats.type & FileType.Directory) !== FileType.Directory) { + uri = Uri.joinPath(uri, '..'); + if (this._visitedPaths.has(getBestPath(uri))) return repository; + } + + isDirectory = true; } const key = asRepoComparisonKey(uri); @@ -1839,17 +1852,20 @@ export class GitProviderService implements Disposable { if (promise == null) { async function findRepository(this: GitProviderService): Promise { const { provider } = this.getProvider(uri); - const repoUri = await provider.findRepositoryUri(uri); + const repoUri = await provider.findRepositoryUri(uri, isDirectory); - this._visitedPaths.set(folderPath); + this._visitedPaths.set(path); if (repoUri == null) return undefined; - let repository = this._repositories.get(repoUri); - if (repository != null) return repository; + let root: Repository | undefined; + if (this._repositories.count !== 0) { + repository = this._repositories.get(repoUri); + if (repository != null) return repository; - // If this new repo is inside one of our known roots and we we don't already know about, add it - const root = this._repositories.getClosest(provider.getAbsoluteUri(uri, repoUri)); + // If this new repo is inside one of our known roots and we we don't already know about, add it + root = this._repositories.getClosest(provider.getAbsoluteUri(uri, repoUri)); + } const autoRepositoryDetection = configuration.getAny(