aboutsummaryrefslogtreecommitdiff
path: root/plugin/fugitive-gitea.vim
blob: 162c139072951e6028c09dfb3271d9193e873916 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
function! s:function(name) abort
  return function(substitute(a:name,'^s:',matchstr(expand('<sfile>'), '<SNR>\d\+_'),''))
endfunction

function! s:giteaserver_url(opts, ...) abort
  if a:0 || type(a:opts) != type({})
    return ''
  endif

  let domains = g:fugitive_gitea_domains
  let domain_patterns = []
 
  for domain in domains
    call add(domain_patterns, escape(split(domain, '://')[-1], '.'))
  endfor

  let domain_pattern = join(domain_patterns, '\|')
  let repo = matchstr(a:opts.remote,'^\%(https\=://\|git://\|\(ssh://\)\=git@\)\%(.\{-\}@\)\=\zs\('.domain_pattern.'\)[/:].\{-\}\ze\%(\.git\)\=$')
  if repo ==# ''
    return ''
  endif


  if index(domains, 'http://' . matchstr(repo, '^[^:/]*')) >= 0
    let root = 'http://'.repo
  else
    let root = 'https://'.repo
  endif

  let path = substitute(a:opts.path, '^/', '', '')
  if path =~# '^\.git/refs/heads/'
    return root . '/commits/'.path[16:-1]
  elseif path =~# '^\.git/refs/tags/'
    return root . '/src/'.path[15:-1]
  elseif path =~# '.git/\%(config$\|hooks\>\)'
    return root . '/admin'
  elseif path =~# '^\.git\>'
    return root
  endif
  if a:opts.commit =~# '^\d\=$'
    let commit = a:opts.repo.rev_parse('HEAD')
  else
    let commit = a:opts.commit
  endif
  if get(a:opts, 'type', '') ==# 'tree' || a:opts.path =~# '/$'
    return ''
  elseif get(a:opts, 'type', '') ==# 'blob' || a:opts.path =~# '[^/]$'
    let url = root . '/src/commit/'.commit.'/'.path
    if get(a:opts, 'line1')
      let url .= '\#L' . a:opts.line1
      if get(a:opts, 'line2') != get(a:opts, 'line1')
        let url .= '-L' . a:opts.line2
      endif
    endif
  else
    let url = root . '/commit/' . commit
  endif
  return url
endfunction

if !exists('g:fugitive_browse_handlers')
  let g:fugitive_browse_handlers = []
endif

call insert(g:fugitive_browse_handlers, s:function('s:giteaserver_url'))