Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend :Gbrowse to support line anchors for commits #1171

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions autoload/fugitive.vim
Original file line number Diff line number Diff line change
Expand Up @@ -7400,8 +7400,8 @@ function! fugitive#BrowseCommand(line1, count, range, bang, mods, arg, ...) abor
endif
endif

let line1 = a:count > 0 && type ==# 'blob' ? a:line1 : 0
let line2 = a:count > 0 && type ==# 'blob' ? a:count : 0
let line1 = a:count > 0 && type ==# 'blob' || type ==# 'commit' ? a:line1 : 0
let line2 = a:count > 0 && type ==# 'blob' || type ==# 'commit' ? a:count : 0
if empty(commit) && path !~# '^\.git/'
if a:count < 0 && !empty(merge)
let commit = merge
Expand Down Expand Up @@ -7465,6 +7465,21 @@ function! fugitive#BrowseCommand(line1, count, range, bang, mods, arg, ...) abor
\ 'line1': line1,
\ 'line2': line2}

if type ==# 'commit' && a:count
let pos1 = s:DiffLines(line1)
let pos2 = s:DiffLines(line2)
if !empty(pos1) && !empty(pos2)
let pattern = ' \(\/dev\/null\|[abciow12]\/\)\zs.*'
let opts.path = matchstr(getline(search('^+++'.pattern,'bnW')), '^+++'.pattern)
let opts.line1 = getline(line1)[0] == '-' ? 0 : pos1.new
let opts.line2 = getline(line2)[0] == '-' ? 0 : pos2.new
let opts.ancestor = {}
let opts.ancestor.path = matchstr(getline(search('^---'.pattern,'bnW')), '^---'.pattern)
let opts.ancestor.line1 = getline(line1)[0] == '+' ? 0 : pos1.old
let opts.ancestor.line2 = getline(line2)[0] == '+' ? 0 : pos2.old
endif
endif

let url = ''
for Handler in get(g:, 'fugitive_browse_handlers', [])
let url = call(Handler, [copy(opts)])
Expand All @@ -7483,6 +7498,23 @@ function! fugitive#BrowseCommand(line1, count, range, bang, mods, arg, ...) abor
endtry
endfunction

function! s:DiffLines(line) abort
try
let offsets = {' ':0,'+':0,'-':0}
let offsets[getline(a:line)[0]] -= 0 " throw if on hunk header
let lnum = a:line - 1
while getline(lnum) !~# '^@@ -\d\+\%(,\d\+\)\= +\d\+'
let offsets[getline(lnum)[0]] += 1
let lnum -= 1
endwhile
return {
\ 'old': offsets['-'] + offsets[' '] + matchstr(getline(lnum), '-\zs\d\+'),
\ 'new': offsets['+'] + offsets[' '] + matchstr(getline(lnum), '+\zs\d\+'),
\}
catch /^Vim\%((\a\+)\)\=:E734/
endtry
endfunction

" Section: Go to file

let s:ref_header = '\%(Merge\|Rebase\|Upstream\|Pull\|Push\)'
Expand Down