import { raiseError } from 'analytics' ; export function parse ( ) { const { pathname } = window . location ; let [ , userName , repoName , type , ... path ] = unescape ( decodeURIComponent ( pathname ) ) . split ( '/' ) ; return { userName , repoName , branchName : undefined , type , path , } ; } export function parseSHA ( ) { const { type , path } = parse ( ) ; return type === 'blob' || type === 'tree' ? path [ 0 ] : undefined ; } function isCommitPath ( path ) { return isCompleteCommitSHA ( path [ 0 ] ) ; } function isCompleteCommitSHA ( sha ? ) { return typeof sha === 'string' && /^[abcdef0-9]{40}$/i . test ( sha ) ; } export function getCurrentPath ( branchName = '' ) { const { path , type } = parse ( ) ; if ( type === 'blob' || type === 'tree' ) { if ( isCommitPath ( path ) ) { path . shift ( ) ; } else { if ( path [ 0 ] === 'HEAD' ) path . shift ( ) ; else { const splitBranchName = branchName . split ( '/' ) ; while ( splitBranchName . length ) { if ( splitBranchName [ 0 ] === path [ 0 ] || ( splitBranchName . length === 1 && splitBranchName [ 0 ] . startsWith ( path [ 0 ] ) ) ) { splitBranchName . shift ( ) ; path . shift ( ) ; } else { raiseError ( new Error ( `branch name and path prefix not match` ) , { branchName , path : parse ( ) . path , } ) ; return [ ] ; } } } } return path . map ( decodeURIComponent ) ; } return [ ] ; }