Support for :Gbrowse

This commit is contained in:
Tim Pope
2015-11-30 22:10:10 -05:00
parent d7801e77ca
commit 540b2b6c4b
3 changed files with 70 additions and 10 deletions

View File

@@ -1,17 +1,15 @@
# rhubarb.vim
If [fugitive.vim][] is the Git, rhubarb.vim is the Hub. Or at least it
could be. One day. Right now it's pretty stupid. I almost named it
chubby.vim, but it just didn't feel right.
If [fugitive.vim][] is the Git, rhubarb.vim is the Hub. Here's the full list
of features:
So far there's only one feature: In Git commit messages, GitHub issues
and collaborators can be omni-completed (`<C-X><C-O>`, see `:help
compl-omni`). This makes inserting those `Closes #123` remarks slightly
easier than copying and pasting from the browser.
* Enables `:Gbrowse` from fugitive.vim to open GitHub URLs. (`:Gbrowse`
currently has native support for this, but it is slated to be removed.)
Maybe I'll extract `:Gbrowse` out of fugitive.vim and put it here
instead. Or maybe I'll add some cool Gist stuff. You never know with
rhubarb.vim.
* In commit messages, GitHub issues, issue URLs, and collaborators can be
omni-completed (`<C-X><C-O>`, see `:help compl-omni`). This makes inserting
those `Closes #123` remarks slightly easier than copying and pasting from
the browser.
[fugitive.vim]: https://github.com/tpope/vim-fugitive

View File

@@ -180,3 +180,57 @@ function! rhubarb#omnifunc(findstart,base) abort
echoerr v:errmsg
endtry
endfunction
" Section: Fugitive :Gbrowse support
function! rhubarb#fugitive_url(opts, ...) abort
if a:0 || type(a:opts) != type({}) || !has_key(a:opts, 'repo')
return ''
endif
let root = s:homepage_for_url(get(a:opts, 'remote'))
if empty(root)
return ''
endif
let path = a:opts.path
if path =~# '^\.git/refs/heads/'
let branch = a:opts.repo.git_chomp('config','branch.'.path[16:-1].'.merge')[11:-1]
if branch ==# ''
return root . '/commits/' . path[16:-1]
else
return root . '/commits/' . branch
endif
elseif path =~# '^\.git/refs/.'
return root . '/commits/' . matchstr(path,'[^/]\+$')
elseif path =~# '.git/\%(config$\|hooks\>\)'
return root . '/admin'
elseif path =~# '^\.git\>'
return root
endif
if a:opts.revision =~# '^[[:alnum:]._-]\+:'
let commit = matchstr(a:opts.revision,'^[^:]*')
elseif a:opts.commit =~# '^\d\=$'
let local = matchstr(a:opts.repo.head_ref(),'\<refs/heads/\zs.*')
let commit = a:opts.repo.git_chomp('config','branch.'.local.'.merge')[11:-1]
if commit ==# ''
let commit = local
endif
else
let commit = a:opts.commit
endif
if a:opts.type == 'tree'
let url = substitute(root . '/tree/' . commit . '/' . path, '/$', '', 'g')
elseif a:opts.type == 'blob'
let url = root . '/blob/' . commit . '/' . path
if get(a:opts, 'line2') && a:opts.line1 == a:opts.line2
let url .= '#L' . a:opts.line1
elseif get(a:opts, 'line2')
let url .= '#L' . a:opts.line1 . '-L' . a:opts.line2
endif
elseif a:opts.type == 'tag'
let commit = matchstr(getline(3),'^tag \zs.*')
let url = root . '/tree/' . commit
else
let url = root . '/commit/' . commit
endif
return url
endfunction

View File

@@ -31,3 +31,11 @@ augroup rhubarb
\ setlocal nolist linebreak filetype=markdown |
\ endif
augroup END
if !exists('g:fugitive_browse_handlers')
let g:fugitive_browse_handlers = []
endif
if index(g:fugitive_browse_handlers, function('rhubarb#fugitive_url')) < 0
call insert(g:fugitive_browse_handlers, function('rhubarb#fugitive_url'))
endif