Support :GBrowse {url} outside of Git repository

This commit is contained in:
Tim Pope
2021-04-04 18:20:36 -04:00
parent 81074db0ee
commit f29c9e5079
2 changed files with 52 additions and 51 deletions

View File

@@ -6051,9 +6051,34 @@ augroup END
" Section: :GBrowse " Section: :GBrowse
function! s:BrowserOpen(url, mods, echo_copy) abort
let url = substitute(a:url, '[ <>\|"]', '\="%".printf("%02X",char2nr(submatch(0)))', 'g')
let mods = s:Mods(a:mods)
if a:echo_copy
if has('clipboard')
let @+ = url
endif
return 'echo '.string(url)
elseif exists(':Browse') == 2
return 'echo '.string(url).'|' . mods . 'Browse '.url
elseif exists(':OpenBrowser') == 2
return 'echo '.string(url).'|' . mods . 'OpenBrowser '.url
else
if !exists('g:loaded_netrw')
runtime! autoload/netrw.vim
endif
if exists('*netrw#BrowseX')
return 'echo '.string(url).'|' . mods . 'call netrw#BrowseX('.string(url).', 0)'
elseif exists('*netrw#NetrwBrowseX')
return 'echo '.string(url).'|' . mods . 'call netrw#NetrwBrowseX('.string(url).', 0)'
else
return 'echoerr ' . string('Netrw not found. Define your own :Browse to use :GBrowse')
endif
endif
endfunction
function! fugitive#BrowseCommand(line1, count, range, bang, mods, arg, args) abort function! fugitive#BrowseCommand(line1, count, range, bang, mods, arg, args) abort
let dir = s:Dir() let dir = s:Dir()
exe s:DirCheck(dir)
try try
let arg = a:arg let arg = a:arg
if arg =~# '^++remote=' if arg =~# '^++remote='
@@ -6085,12 +6110,18 @@ function! fugitive#BrowseCommand(line1, count, range, bang, mods, arg, args) abo
if rev ==# '' if rev ==# ''
let rev = s:DirRev(@%)[1] let rev = s:DirRev(@%)[1]
endif endif
if rev =~# '^:\=$' if rev =~? '^\a\a\+:[\/][\/]' && rev !~? '^fugitive:'
let expanded = s:Relative() let rev = substitute(rev, '\\\@<![#!]\|\\\@<!%\ze\w', '\\&', 'g')
elseif rev =~? '^\a\a\+:[\/][\/]' && rev !~? '^fugitive:' elseif rev ==# ':'
let expanded = s:Expand(substitute(rev, '\\\@<!#\|\\\@<!%\ze\w', '\\&', 'g')) let rev = ''
else endif
let expanded = s:Expand(rev) let expanded = s:Expand(rev)
if expanded =~? '^\a\a\+:[\/][\/]' && expanded !~? '^fugitive:'
return s:BrowserOpen(s:Slash(expanded), a:mods, a:bang)
endif
exe s:DirCheck(dir)
if empty(expanded)
let expanded = s:Relative(':(top)', dir)
endif endif
let cdir = FugitiveVimPath(fugitive#CommonDir(dir)) let cdir = FugitiveVimPath(fugitive#CommonDir(dir))
for subdir in ['tags/', 'heads/', 'remotes/'] for subdir in ['tags/', 'heads/', 'remotes/']
@@ -6112,9 +6143,6 @@ function! fugitive#BrowseCommand(line1, count, range, bang, mods, arg, args) abo
let type = 'blob' let type = 'blob'
endif endif
let path = path[1:-1] let path = path[1:-1]
elseif full =~? '^\a\a\+:[\/][\/]'
let path = s:Slash(full)
let type = 'url'
elseif empty(s:Tree(dir)) elseif empty(s:Tree(dir))
let path = '.git/' . full[strlen(dir)+1:-1] let path = '.git/' . full[strlen(dir)+1:-1]
let type = '' let type = ''
@@ -6243,9 +6271,6 @@ function! fugitive#BrowseCommand(line1, count, range, bang, mods, arg, args) abo
\ 'line1': line1, \ 'line1': line1,
\ 'line2': line2} \ 'line2': line2}
if type ==# 'url'
let url = path
else
let url = '' let url = ''
for Handler in get(g:, 'fugitive_browse_handlers', []) for Handler in get(g:, 'fugitive_browse_handlers', [])
let url = call(Handler, [copy(opts)]) let url = call(Handler, [copy(opts)])
@@ -6253,35 +6278,12 @@ function! fugitive#BrowseCommand(line1, count, range, bang, mods, arg, args) abo
break break
endif endif
endfor endfor
endif
if empty(url) if empty(url)
call s:throw("No GBrowse handler installed for '".raw."'") call s:throw("No GBrowse handler installed for '".raw."'")
endif endif
let url = substitute(url, '[ <>\|"]', '\="%".printf("%02X",char2nr(submatch(0)))', 'g') return s:BrowserOpen(url, a:mods, a:bang)
let mods = s:Mods(a:mods)
if a:bang
if has('clipboard')
let @+ = url
endif
return 'echo '.string(url)
elseif exists(':Browse') == 2
return 'echo '.string(url).'|' . mods . 'Browse '.url
elseif exists(':OpenBrowser') == 2
return 'echo '.string(url).'|' . mods . 'OpenBrowser '.url
else
if !exists('g:loaded_netrw')
runtime! autoload/netrw.vim
endif
if exists('*netrw#BrowseX')
return 'echo '.string(url).'|' . mods . 'call netrw#BrowseX('.string(url).', 0)'
elseif exists('*netrw#NetrwBrowseX')
return 'echo '.string(url).'|' . mods . 'call netrw#NetrwBrowseX('.string(url).', 0)'
else
return 'echoerr ' . string('Netrw not found. Define your own :Browse to use :GBrowse')
endif
endif
catch /^fugitive:/ catch /^fugitive:/
return 'echoerr ' . string(v:exception) return 'echoerr ' . string(v:exception)
endtry endtry

View File

@@ -212,9 +212,6 @@ that are part of Git repositories).
*:GBrowse* *:GBrowse*
:GBrowse Open the current file, blob, tree, commit, or tag :GBrowse Open the current file, blob, tree, commit, or tag
in your browser at the upstream hosting provider. in your browser at the upstream hosting provider.
If a range is given, it is appropriately appended to
the URL as an anchor.
Upstream providers can be added by installing an Upstream providers can be added by installing an
appropriate Vim plugin. For example, GitHub can be appropriate Vim plugin. For example, GitHub can be
supported by installing rhubarb.vim, available at supported by installing rhubarb.vim, available at
@@ -222,16 +219,18 @@ that are part of Git repositories).
:GBrowse {object} Like :GBrowse, but for a given |fugitive-object|. :GBrowse {object} Like :GBrowse, but for a given |fugitive-object|.
:GBrowse [...]@{remote} Force using the given remote rather than the remote
for the current branch. The remote is used to
determine which upstream repository to link to.
:{range}GBrowse [args] Appends an anchor to the URL that emphasizes the :{range}GBrowse [args] Appends an anchor to the URL that emphasizes the
selected lines. This also forces the URL to include a selected lines. This also forces the URL to include a
commit rather than a branch name so it remains valid commit rather than a branch name so it remains valid
if the file changes. You can give a range of "0" to if the file changes. You can give a range of "0" to
force this behavior without including an anchor. force this behavior without including an anchor.
:GBrowse [...]@{remote} Force using the given remote rather than the remote
for the current branch. The remote is used to
determine which upstream repository to link to.
:GBrowse {url} Open an arbitrary URL in your browser.
:[range]GBrowse! [args] Like :GBrowse, but put the URL on the clipboard rather :[range]GBrowse! [args] Like :GBrowse, but put the URL on the clipboard rather
than opening it. than opening it.