diff --git a/src/git/gitProviderService.ts b/src/git/gitProviderService.ts index 1d37d73..a29366c 100644 --- a/src/git/gitProviderService.ts +++ b/src/git/gitProviderService.ts @@ -1846,7 +1846,9 @@ export class GitProviderService implements Disposable { getBestRepository(uri?: TextEditor | Uri, editor?: TextEditor): Repository | undefined; @log({ exit: r => `returned ${r?.path}` }) getBestRepository(editorOrUri?: TextEditor | Uri, editor?: TextEditor): Repository | undefined { - if (this.repositoryCount === 0) return undefined; + const count = this.repositoryCount; + if (count === 0) return undefined; + if (count === 1) return this.highlander; if (editorOrUri != null && editorOrUri instanceof Uri) { const repo = this.getRepository(editorOrUri); @@ -1859,6 +1861,29 @@ export class GitProviderService implements Disposable { return (editor != null ? this.getRepository(editor.document.uri) : undefined) ?? this.highlander; } + getBestRepositoryOrFirst(): Repository | undefined; + getBestRepositoryOrFirst(uri?: Uri): Repository | undefined; + getBestRepositoryOrFirst(editor?: TextEditor): Repository | undefined; + getBestRepositoryOrFirst(uri?: TextEditor | Uri, editor?: TextEditor): Repository | undefined; + @log({ exit: r => `returned ${r?.path}` }) + getBestRepositoryOrFirst(editorOrUri?: TextEditor | Uri, editor?: TextEditor): Repository | undefined { + const count = this.repositoryCount; + if (count === 0) return undefined; + if (count === 1) return first(this._repositories.values()); + + if (editorOrUri != null && editorOrUri instanceof Uri) { + const repo = this.getRepository(editorOrUri); + if (repo != null) return repo; + + editorOrUri = undefined; + } + + editor = editorOrUri ?? editor ?? window.activeTextEditor; + return ( + (editor != null ? this.getRepository(editor.document.uri) : undefined) ?? first(this._repositories.values()) + ); + } + @log({ exit: r => `returned ${r?.path}` }) async getOrOpenRepository(uri: Uri, detectNested?: boolean): Promise { const scope = getLogScope();