Follow redirect when determining :Gbrowse url

This is a really contrived feature, but I want to make it possible for
people to have their cake and eat it too when it comes to custom
repository URLs that redirect to GitHub.
This commit is contained in:
Tim Pope
2017-05-29 22:41:33 -04:00
parent 0648abc018
commit be770419fc

View File

@@ -68,6 +68,15 @@ function! s:shellslash(path) abort
endif endif
endfunction endfunction
let s:executables = {}
function! s:executable(binary) abort
if !has_key(s:executables, a:binary)
let s:executables[a:binary] = executable(a:binary)
endif
return s:executables[a:binary]
endfunction
let s:git_versions = {} let s:git_versions = {}
function! s:git_command() abort function! s:git_command() abort
@@ -2228,6 +2237,8 @@ endfunction
call s:command("-bar -bang -range=0 -nargs=* -complete=customlist,s:EditComplete Gbrowse :execute s:Browse(<bang>0,<line1>,<count>,<f-args>)") call s:command("-bar -bang -range=0 -nargs=* -complete=customlist,s:EditComplete Gbrowse :execute s:Browse(<bang>0,<line1>,<count>,<f-args>)")
let s:redirects = {}
function! s:Browse(bang,line1,count,...) abort function! s:Browse(bang,line1,count,...) abort
try try
let validremote = '\.\|\.\=/.*\|[[:alnum:]_-]\+\%(://.\{-\}\)\=' let validremote = '\.\|\.\=/.*\|[[:alnum:]_-]\+\%(://.\{-\}\)\='
@@ -2342,6 +2353,17 @@ function! s:Browse(bang,line1,count,...) abort
let raw = remote let raw = remote
endif endif
if raw =~# '^https\=://' && s:executable('curl')
if !has_key(s:redirects, raw)
let s:redirects[raw] = matchstr(system('curl -I ' .
\ s:shellesc(raw . '/info/refs?service=git-upload-pack')),
\ 'Location: \zs\S\+\ze/info/refs?')
endif
if len(s:redirects[raw])
let raw = s:redirects[raw]
endif
endif
for Handler in g:fugitive_browse_handlers for Handler in g:fugitive_browse_handlers
let url = call(Handler, [{ let url = call(Handler, [{
\ 'repo': s:repo(), \ 'repo': s:repo(),