Skip to content

Commit 747781c

Browse files
author
Brian Lyles
committed
Handle range-diff commit header lines in s:cfile()
While `range-diff` output doesn't display with any syntax highlighting, it's still in a buffer with a filetype of 'git' and thus fugitive attaches the standard jump mappings. However, those mappings work inconsistently for `range-diff` output due to some accidental matching in `s:cfile()`. For lines like these: 1: aaaaaaa = 1: bbbbbbb My subject 2: aaaaaaa = -: ------- My other subject Depending on the position of the cursor, we sometimes hit the `<cword>` fallback and open the commit. This can happen when the cursor is over a commit hash, or even on one of the `:`. However, for a line like this: -: ------- > 2: aaaaaaa My subject We end up hitting the block intended to catch +/- lines in diff output, and fail to identify a commit. Add a special set of handling for range-diff header lines. In cases where only one commit is present in the line, open that commit automatically regardless of cursor position. For cases where the line represents a different commit on each side, fall back to a `<cword>` approach to allow opening either commit based on cursor position.
1 parent d4877e5 commit 747781c

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

autoload/fugitive.vim

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8135,6 +8135,9 @@ function! s:BranchCfile(result) abort
81358135
endfunction
81368136

81378137
let s:diff_header_pattern = '^diff --git \%("\=[abciow12]/.*\|/dev/null\) \%("\=[abciow12]/.*\|/dev/null\)$'
8138+
let s:rdiff_hash_or_missing = '\(\x\{7,40\}\|-\{7,40\}\)'
8139+
let s:rdiff_side = '\%(-\|\d\+\):\s\+' . s:rdiff_hash_or_missing
8140+
let s:rdiff_header_pattern = '^' . s:rdiff_side . '\s\+[=!<>]\s\+' . s:rdiff_side . '\s\+'
81388141
function! s:cfile() abort
81398142
let temp_state = s:TempState()
81408143
let name = substitute(get(get(temp_state, 'args', []), 0, ''), '\%(^\|-\)\(\l\)', '\u\1', 'g')
@@ -8232,6 +8235,19 @@ function! s:cfile() abort
82328235
let dcmds = ['', 'Gdiffsplit! >' . myhash . '^:' . fnameescape(files[0])]
82338236
endif
82348237

8238+
elseif getline('.') =~# s:rdiff_header_pattern
8239+
let ref = ''
8240+
let matches = matchlist(getline('.'), s:rdiff_header_pattern)
8241+
if matches[1] =~# '^-\+$' && matches[2] =~# '^\x\{7,40\}$'
8242+
let ref = matches[2]
8243+
elseif matches[2] =~# '^-\+$' && matches[1] =~# '^\x\{7,40\}$'
8244+
let ref = matches[1]
8245+
elseif matches[1] =~# '^\x\{7,40\}$' && matches[1] == matches[2]
8246+
let ref = matches[1]
8247+
elseif expand('<cword>') =~# '^\x\{7,40\}$'
8248+
let ref = expand('<cword>')
8249+
endif
8250+
82358251
elseif getline('.') =~# '^[+-]'
82368252
let [header_lnum, old_lnum, new_lnum] = s:HunkPosition(line('.'))
82378253
if new_lnum > 0

0 commit comments

Comments
 (0)