diff --git a/src/commands/openInRemote.ts b/src/commands/openInRemote.ts index a9647c7..13fbc08 100644 --- a/src/commands/openInRemote.ts +++ b/src/commands/openInRemote.ts @@ -1,7 +1,7 @@ 'use strict'; import { TextEditor, Uri } from 'vscode'; import { GlyphChars } from '../constants'; -import { GitRemote, GitService, RemoteResource, RemoteResourceType } from '../git/gitService'; +import { GitRemote, GitService, RemoteProvider, RemoteResource, RemoteResourceType } from '../git/gitService'; import { Logger } from '../logger'; import { Messages } from '../messages'; import { CommandQuickPickItem, OpenRemoteCommandQuickPickItem, RemotesQuickPick } from '../quickpicks'; @@ -10,7 +10,7 @@ import { ActiveEditorCommand, command, Commands } from './common'; export interface OpenInRemoteCommandArgs { remote?: string; - remotes?: GitRemote[]; + remotes?: GitRemote[]; resource?: RemoteResource; clipboard?: boolean; @@ -36,13 +36,7 @@ export class OpenInRemoteCommand extends ActiveEditorCommand { } try { - let remote: GitRemote | undefined; - if (args.remotes.length > 1) { - remote = args.remotes.find(r => r.default); - } else if (args.remotes.length === 1) { - remote = args.remotes[0]; - } - + const remote = args.remotes.length === 1 ? args.remotes[0] : args.remotes.find(r => r.default); if (remote != null) { this.ensureRemoteBranchName(args); const command = new OpenRemoteCommandQuickPickItem(remote, args.resource, args.clipboard); diff --git a/src/commands/remoteProviders.ts b/src/commands/remoteProviders.ts index 92f0a51..c9a2eab 100644 --- a/src/commands/remoteProviders.ts +++ b/src/commands/remoteProviders.ts @@ -43,7 +43,7 @@ export class ConnectRemoteProviderCommand extends Command { const remotes = await Container.git.getRemotes(args.repoPath); const remote = remotes.find(r => r.name === args.remote); - if (!remote?.provider?.hasApi()) return false; + if (!remote?.provider.hasApi()) return false; const connected = await remote.provider.connect(); if (connected && !remotes.some(r => r.default)) { @@ -95,7 +95,7 @@ export class DisconnectRemoteProviderCommand extends Command { if (args?.repoPath == null || args?.remote == null) return undefined; const remote = (await Container.git.getRemotes(args.repoPath)).find(r => r.name === args.remote); - if (!remote?.provider?.hasApi()) return undefined; + if (!remote?.provider.hasApi()) return undefined; return remote.provider.disconnect(); } diff --git a/src/git/gitService.ts b/src/git/gitService.ts index 211f0f7..5ee5c0d 100644 --- a/src/git/gitService.ts +++ b/src/git/gitService.ts @@ -81,7 +81,7 @@ import { RepositoryChangeEvent } from './git'; import { GitUri } from './gitUri'; -import { RemoteProviderFactory, RemoteProviders, RemoteProviderWithApi } from './remotes/factory'; +import { RemoteProvider, RemoteProviderFactory, RemoteProviders, RemoteProviderWithApi } from './remotes/factory'; import { GitReflogParser, GitShortLogParser } from './parsers/parsers'; import { fsExists, isWindows } from './shell'; import { PullRequest, PullRequestDateFormatting } from './models/models'; @@ -2467,8 +2467,8 @@ export class GitService implements Disposable { @log() async getRemotes( repoPath: string | undefined, - options: { includeAll?: boolean; sort?: boolean } = {} - ): Promise { + options: { sort?: boolean } = {} + ): Promise[]> { if (repoPath === undefined) return []; const repository = await this.getRepository(repoPath); @@ -2476,9 +2476,7 @@ export class GitService implements Disposable { ? repository.getRemotes({ sort: options.sort }) : this.getRemotesCore(repoPath, undefined, { sort: options.sort })); - if (options.includeAll) return remotes; - - return remotes.filter(r => r.provider !== undefined); + return remotes.filter(r => r.provider != null) as GitRemote[]; } async getRemotesCore( diff --git a/src/git/models/remote.ts b/src/git/models/remote.ts index ceb7ebe..7d5119e 100644 --- a/src/git/models/remote.ts +++ b/src/git/models/remote.ts @@ -8,7 +8,9 @@ export enum GitRemoteType { Push = 'push' } -export class GitRemote { +export class GitRemote< + TProvider extends RemoteProvider | undefined = RemoteProvider | RemoteProviderWithApi | undefined +> { static is(remote: any): remote is GitRemote { return remote instanceof GitRemote; } @@ -28,7 +30,7 @@ export class GitRemote { public readonly scheme: string, public readonly domain: string, public readonly path: string, - public readonly provider: RemoteProvider | RemoteProviderWithApi | undefined, + public readonly provider: TProvider, public readonly types: { type: GitRemoteType; url: string }[] ) {} diff --git a/src/quickpicks/remotesQuickPick.ts b/src/quickpicks/remotesQuickPick.ts index 47ff6cf..fb7fcca 100644 --- a/src/quickpicks/remotesQuickPick.ts +++ b/src/quickpicks/remotesQuickPick.ts @@ -7,42 +7,39 @@ import { GitRemote, GitService, GitUri, + RemoteProvider, RemoteResource, RemoteResourceType } from '../git/gitService'; import { CommandQuickPickItem, getQuickPickIgnoreFocusOut } from './commonQuickPicks'; export class OpenRemoteCommandQuickPickItem extends CommandQuickPickItem { - private remote: GitRemote; - private resource: RemoteResource; - - constructor(remote: GitRemote, resource: RemoteResource, public readonly clipboard?: boolean) { + constructor( + private readonly remote: GitRemote, + private readonly resource: RemoteResource, + public readonly clipboard?: boolean + ) { super( { label: clipboard ? `$(link-external) Copy ${getNameFromRemoteResource(resource)} Url to Clipboard from ${ - remote.provider!.name + remote.provider.name }` - : `$(link-external) Open ${getNameFromRemoteResource(resource)} on ${remote.provider!.name}`, - description: `$(repo) ${remote.provider!.path}` + : `$(link-external) Open ${getNameFromRemoteResource(resource)} on ${remote.provider.name}`, + description: `$(repo) ${remote.provider.path}` }, undefined, undefined ); - - this.remote = remote; - this.resource = resource; } execute(): Thenable<{} | undefined> { - if (this.clipboard) return this.remote.provider!.copy(this.resource); - - return this.remote.provider!.open(this.resource); + return this.clipboard ? this.remote.provider.copy(this.resource) : this.remote.provider.open(this.resource); } } export class OpenRemotesCommandQuickPickItem extends CommandQuickPickItem { - constructor(remotes: GitRemote[], resource: RemoteResource, goBackCommand?: CommandQuickPickItem) { + constructor(remotes: GitRemote[], resource: RemoteResource, goBackCommand?: CommandQuickPickItem) { const name = getNameFromRemoteResource(resource); let description; @@ -95,13 +92,7 @@ export class OpenRemotesCommandQuickPickItem extends CommandQuickPickItem { break; } - let remote: GitRemote | undefined; - if (remotes.length > 1) { - remote = remotes.find(r => r.default); - } else if (remotes.length === 1) { - remote = remotes[0]; - } - + let remote = remotes.length === 1 ? remotes[0] : remotes.find(r => r.default); if (remote != null) { const commandArgs: OpenInRemoteCommandArgs = { remotes: remotes, @@ -110,8 +101,8 @@ export class OpenRemotesCommandQuickPickItem extends CommandQuickPickItem { }; super( { - label: `$(link-external) Open ${name} on ${remote.provider!.name}`, - description: `${description} in ${GlyphChars.Space}$(repo) ${remote.provider!.path}` + label: `$(link-external) Open ${name} on ${remote.provider.name}`, + description: `${description} in ${GlyphChars.Space}$(repo) ${remote.provider.path}` }, Commands.OpenInRemote, [undefined, commandArgs] @@ -121,10 +112,11 @@ export class OpenRemotesCommandQuickPickItem extends CommandQuickPickItem { } remote = remotes[0]; - // Use the real provider name if there is only 1 provider - const provider = remotes.every(r => r.provider !== undefined && r.provider.name === remote!.provider!.name) - ? remote.provider!.name - : 'Remote'; + let providerName = remote.provider.name; + // Only use the real provider name if there is only 1 type of provider + if (!remotes.every(r => r.provider.name === providerName)) { + providerName = 'Remote'; + } const commandArgs: OpenInRemoteCommandArgs = { remotes: remotes, @@ -133,7 +125,7 @@ export class OpenRemotesCommandQuickPickItem extends CommandQuickPickItem { }; super( { - label: `$(link-external) Open ${name} on ${provider}${GlyphChars.Ellipsis}`, + label: `$(link-external) Open ${name} on ${providerName}${GlyphChars.Ellipsis}`, description: `${description}` }, Commands.OpenInRemote, @@ -144,7 +136,7 @@ export class OpenRemotesCommandQuickPickItem extends CommandQuickPickItem { export class RemotesQuickPick { static async show( - remotes: GitRemote[], + remotes: GitRemote[], placeHolder: string, resource: RemoteResource, clipboard?: boolean,