From 476940099d8d2986de18caffa83eb697ee4632a1 Mon Sep 17 00:00:00 2001 From: Maksim Odnoletkov Date: Fri, 11 Jan 2019 22:36:55 +0000 Subject: [PATCH] Support line anchors for commit URLs Anchor format is: #diff-[[L|R][-[L|R]]] With L line number is relative to the '---' side, and R to the '+++' side of the diff. Path of the '+++' side used when it exists, otherwise path of the '---' side (for removals). Closes https://github.com/tpope/vim-rhubarb/issues/36 --- autoload/rhubarb.vim | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/autoload/rhubarb.vim b/autoload/rhubarb.vim index 26f52bc..f9af973 100644 --- a/autoload/rhubarb.vim +++ b/autoload/rhubarb.vim @@ -269,7 +269,24 @@ function! rhubarb#FugitiveUrl(...) abort else let commit = opts.commit endif - if get(opts, 'type', '') ==# 'tree' || opts.path =~# '/$' + if get(opts, 'type', '') ==# 'commit' + let url = root . '/commit/' . commit + if has_key(opts, 'ancestor') + let url .= '#diff-' . sha256(len(opts.path) > 0 ? opts.path : opts.ancestor.path) + let lines = [] + if get(opts, 'line1') > 0 + let lines += ['R' . opts.line1] + elseif get(opts.ancestor, 'line1') > 0 + let lines += ['L' . opts.ancestor.line1] + endif + if get(opts, 'line2') > 0 + let lines += ['R' . opts.line2] + elseif get(opts.ancestor, 'line2') > 0 + let lines += ['L' . opts.ancestor.line2] + endif + let url .= join(uniq(lines), '-') + endif + elseif get(opts, 'type', '') ==# 'tree' || opts.path =~# '/$' let url = substitute(root . '/tree/' . commit . '/' . path, '/$', '', 'g') elseif get(opts, 'type', '') ==# 'blob' || opts.path =~# '[^/]$' let escaped_commit = substitute(commit, '#', '%23', 'g')