'use strict' ; import * as path from 'path' ; import { Uri } from 'vscode' ; import { fileSystemScheme } from './constants' ; import { Strings } from './system' ; const sgUrlPathRegex = /^\/(.+?)\/(.+?)@([0-9a-f]{40})\/-\/raw(.*)$/ ; type RepoInfo = [ string , string , string | undefined ] ; export function fromRemoteHubUri ( uri ) { const [ , owner , repo , ... rest ] = uri . path . split ( '/' ) ; return [ owner , repo , rest . join ( '/' ) ] ; } export function joinPath ( uri , pathFragment ) { return uri . with ( { path : Strings . normalizePath ( path . join ( uri . path || '/' , pathFragment ) ) } ) ; } export function fromSourcegraphUri ( uri ) { const match = sgUrlPathRegex . exec ( uri . path ) ; if ( match == null ) throw new Error ( 'Invalid Uri' ) ; const [ , authority , repo , , path ] = match ; return uri . with ( { scheme : fileSystemScheme , authority : authority , path : `/${ repo }${ path }` } ) ; } export function toSourcegraphUri ( uri , rev ? , root = false ) { const [ owner , repo , path ] = fromRemoteHubUri ( uri ) ; return uri . with ( { scheme : 'https' , authority : 'sourcegraph.com' , path : `${ uri . authority }/${ owner }/${ repo }@${ rev }/-/raw/${ root ? '' : path }` , query : '' , fragment : '' } ) ; }