├── .gitignore ├── README.md ├── doc └── vim-fubitive.txt └── plugin └── fubitive.vim /.gitignore: -------------------------------------------------------------------------------- 1 | /doc/tags 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | fubitive.vim 2 | ============ 3 | 4 | Extend [fugitive.vim](https://github.com/tpope/vim-fugitive) to support 5 | Bitbucket URLs in `:Gbrowse`. 6 | 7 | ## Configuration 8 | 9 | The default domain when searching remotes is `bitbucket.org`. To make this 10 | plugin work with a Bitbucket instance under a different domain, simply add the 11 | following to your `.vimrc` (taking care to escape special characters): 12 | 13 | ```vim 14 | let g:fubitive_domain_pattern = 'code\.example\.com' 15 | ``` 16 | 17 | For Bitbucket instances that are not installed in the root of the domain, for 18 | example under `code.example.com/bitbucket/`, add the following line to 19 | your `.vimrc`. 20 | 21 | ```vim 22 | let g:fubitive_domain_pattern = 'code\.example\.com' 23 | let g:fubitive_domain_context_path = 'bitbucket' 24 | ``` 25 | 26 | By default, fubitive will assume `https://` when building URLs. 27 | To change this, set the `g:fubitive_default_protocol` variable: 28 | 29 | ```vim 30 | let g:fubitive_default_protocol = 'http://' 31 | ``` 32 | -------------------------------------------------------------------------------- /doc/vim-fubitive.txt: -------------------------------------------------------------------------------- 1 | vim-fubitive.txt 2 | 3 | ================================================================================ 4 | CONTENTS *vim-fubitive-contents* 5 | 6 | 1. fubitive.vim........................................|vim-fubitive-fubitive.vim| 7 | 1.1. Configuration................................|vim-fubitive-configuration| 8 | 9 | ================================================================================ 10 | FUBITIVE.VIM *vim-fubitive-fubitive.vim* 11 | 12 | Extend fugitive.vim (https://github.com/tpope/vim-fugitive) to support 13 | Bitbucket URLs in `:Gbrowse`. 14 | 15 | -------------------------------------------------------------------------------- 16 | CONFIGURATION *vim-fubitive-configuration* 17 | 18 | The default domain when searching remotes is `bitbucket.org`. To make this 19 | plugin work with a Bitbucket instance under a different domain, simply add the 20 | following to your `.vimrc` (taking care to escape special characters): 21 | > 22 | let g:fubitive_domain_pattern = 'code\.example\.com' 23 | < 24 | 25 | vim:tw=78:ts=8:ft=help:norl: 26 | -------------------------------------------------------------------------------- /plugin/fubitive.vim: -------------------------------------------------------------------------------- 1 | if exists( 'g:fubitive_loaded' ) 2 | finish 3 | endif 4 | let g:fubitive_loaded = 1 5 | 6 | function! s:function(name) abort 7 | return function(substitute(a:name,'^s:',matchstr(expand(''), '\d\+_'),'')) 8 | endfunction 9 | 10 | function! s:default_protocol() 11 | return exists('g:fubitive_default_protocol') 12 | \ ? g:fubitive_default_protocol 13 | \ : 'https://' 14 | endfunction 15 | 16 | function! s:domain_context_path() 17 | return exists('g:fubitive_domain_context_path') 18 | \ ? '/' . g:fubitive_domain_context_path 19 | \ : '' 20 | endfunction 21 | 22 | function! s:bitbucket_url(opts, ...) abort 23 | if a:0 || type(a:opts) != type({}) 24 | return '' 25 | endif 26 | let path = substitute(a:opts.path, '^/', '', '') 27 | let domain_pattern = exists('g:fubitive_domain_pattern') ? g:fubitive_domain_pattern : 'bitbucket\.org' 28 | let domains = exists('g:fugitive_bitbucket_domains') ? g:fugitive_bitbucket_domains : [] 29 | for domain in domains 30 | let domain_pattern .= '\|' . escape(split(domain, '://')[-1], '.') 31 | endfor 32 | let repo = matchstr(a:opts.remote,'^\%(https\=://\|git://\|\(ssh://\)\=git@\)\%(.\{-\}@\)\=\zs\('.domain_pattern.'\)[/:].\{-\}\ze\%(\.git\)\=$') 33 | let domain = matchstr(a:opts.remote,'^\%(https\=://\|git://\|\(ssh://\)\=git@\)\%(.\{-\}@\)\=\zs\('.domain_pattern.'\)\ze[/:].\{-\}\%(\.git\)\=$') 34 | if repo ==# '' 35 | return '' 36 | endif 37 | let is_cloud = domain =~? 'bitbucket\.org' 38 | if !is_cloud 39 | let project = matchstr(repo, '\zs\([^/]*\)\ze/[^/]*$') 40 | let repo = matchstr(repo, '/\zs\([^/]*\)$') 41 | endif 42 | let protocol = matchstr(a:opts.remote,'^\(https\=://\)') 43 | if empty(protocol) 44 | let protocol = s:default_protocol() 45 | endif 46 | let root = protocol . (is_cloud 47 | \ ? substitute(repo, ':', '/', '') 48 | \ : domain . s:domain_context_path() . '/projects/' . project . '/repos/' . repo) 49 | if path =~# '^\.git/refs/heads/' 50 | return root . '/commits/' . path[16:-1] 51 | elseif path =~# '^\.git/refs/tags/' 52 | return root . '/src/' .path[15:-1] 53 | elseif path =~# '.git/\%(config$\|hooks\>\)' 54 | return root . '/admin' 55 | elseif path =~# '^\.git\>' 56 | return root 57 | endif 58 | if a:opts.commit =~# '^\d\=$' 59 | let commit = a:opts.repo.rev_parse('HEAD') 60 | else 61 | let commit = a:opts.commit 62 | endif 63 | if get(a:opts, 'type', '') ==# 'tree' || a:opts.path =~# '/$' 64 | let url = is_cloud 65 | \ ? substitute(root . '/src/' . commit . '/' . path, '/$', '', '') 66 | \ : substitute(root . '/browse/' . path . '?at=' . commit, '/$', '', '') 67 | elseif get(a:opts, 'type', '') ==# 'blob' || a:opts.path =~# '[^/]$' 68 | let objSpec = split(fugitive#Object(), ':') 69 | let commit = len(objSpec) == 2 ? objSpec[0] : fugitive#RevParse('HEAD') 70 | let url = is_cloud 71 | \ ? root . '/src/' . commit . '/' . path 72 | \ : root . '/browse/' . path . '?at=' . commit 73 | if get(a:opts, 'line1') 74 | let url .= is_cloud 75 | \ ? '#' . fnamemodify(path, ':t') . '-' . a:opts.line1 76 | \ : '#' . a:opts.line1 77 | if get(a:opts, 'line2') 78 | let url .= (is_cloud ? ':' : '-') . a:opts.line2 79 | endif 80 | endif 81 | else 82 | let url = root . '/commits/' . commit 83 | endif 84 | return url 85 | endfunction 86 | 87 | if !exists('g:fugitive_browse_handlers') 88 | let g:fugitive_browse_handlers = [] 89 | endif 90 | 91 | call insert(g:fugitive_browse_handlers, s:function('s:bitbucket_url')) 92 | --------------------------------------------------------------------------------