import { Repository } from './git.d' ; import { IGitCommandExecutor } from '..' ; import { GitOriginType } from '.' ; import { captureTelemetry } from '../../common/telemetry' ; export class GitRemoteService { constructor ( private readonly repo , private readonly gitCmdExecutor ) { } private get currentBranch ( ) : string { return this . repo . state . HEAD ! . name || '' ; } public async getBranchesConfiguredForPullForRemote ( remoteName ) { const gitShowRemoteOutput = await this . gitCmdExecutor . exec ( this . repo . rootUri . fsPath , ... [ 'remote' , 'show' , remoteName , '-n' ] ) ; const lines = gitShowRemoteOutput . split ( /\r?\n/g ) . map ( line => line . trim ( ) ) . filter ( line => line . length > 0 ) ; const startLineIndex = lines . findIndex ( line => line . startsWith ( 'Local branches configured for' ) ) ; const endLineIndex = lines . findIndex ( line => line . startsWith ( 'Local ref configured for' ) ) ; if ( startLineIndex === - 1 || endLineIndex == - 1 ) { return [ ] ; } if ( startLineIndex > endLineIndex ) { return [ ] ; } return lines . slice ( startLineIndex + 1 , endLineIndex ) . map ( line => line . split ( ' ' ) [ 0 ] ) ; } public async getOriginType ( url ? ) { if ( ! url ) { url = await this . getOriginUrl ( ) ; } if ( url . indexOf ( 'github.com' ) > 0 ) { return GitOriginType . github ; } else if ( url . indexOf ( 'bitbucket' ) > 0 ) { return GitOriginType . bitbucket ; } else if ( url . indexOf ( 'visualstudio' ) > 0 ) { return GitOriginType . vsts ; } return undefined ; } @ captureTelemetry ( ) public async getOriginUrl ( branchName ? ) { branchName = branchName || this . currentBranch ; const branch = await this . repo . getBranch ( branchName ) ; if ( branch . upstream ) { const remoteIndex = this . repo . state . remotes . findIndex ( x => x . name === branch . upstream ! . remote ) ; return this . repo . state . remotes [ remoteIndex ] . fetchUrl || '' ; } return '' ; } }