Make robust the replacement of temp file paths with actual paths.

The previous implementation meant the temp file paths were treated as
regular expressions, which was vulnerable to problems with backslashes
etc.

See #494.
This commit is contained in:
Andy Stewart
2018-03-02 12:44:49 +00:00
parent fa85bd41d9
commit 5db4a3a2a5
3 changed files with 91 additions and 5 deletions

View File

@@ -228,13 +228,28 @@ endfunction
" Replaces references to temp files with the actual file. " Replaces references to temp files with the actual file.
function! s:fix_file_references(filepath, hunk_diff) function! s:fix_file_references(filepath, hunk_diff)
let diff = a:hunk_diff let lines = split(a:hunk_diff, '\n')
for tmp in matchlist(diff, '\vdiff --git ./(\S+) ./(\S+)\n')[1:2]
let diff = substitute(diff, tmp, a:filepath, 'g') let left_prefix = matchstr(lines[2], '[abciow12]').'/'
endfor let right_prefix = matchstr(lines[3], '[abciow12]').'/'
return diff let quote = lines[0][11] == '"' ? '"' : ''
let left_file = quote.left_prefix.a:filepath.quote
let right_file = quote.right_prefix.a:filepath.quote
let lines[0] = 'diff --git '.left_file.' '.right_file
let lines[2] = '--- '.left_file
let lines[3] = '+++ '.right_file
return join(lines, "\n")."\n"
endfunction endfunction
if $TEST
function! gitgutter#hunk#fix_file_references(filepath, hunk_diff)
return s:fix_file_references(a:filepath, a:hunk_diff)
endfunction
endif
function! s:adjust_hunk_summary(hunk_diff) abort function! s:adjust_hunk_summary(hunk_diff) abort
let line_adjustment = s:line_adjustment_for_current_hunk() let line_adjustment = s:line_adjustment_for_current_hunk()

View File

@@ -2,6 +2,8 @@
VIM="/Applications/MacVim.app/Contents/MacOS/Vim -v" VIM="/Applications/MacVim.app/Contents/MacOS/Vim -v"
export TEST=1
$VIM -u NONE -U NONE -N \ $VIM -u NONE -U NONE -N \
--cmd 'set rtp+=../' \ --cmd 'set rtp+=../' \
--cmd 'let g:gitgutter_async=0' \ --cmd 'let g:gitgutter_async=0' \

View File

@@ -494,3 +494,72 @@ function Test_user_autocmd()
call s:trigger_gitgutter() call s:trigger_gitgutter()
call assert_equal(bufnr, s:autocmd_user) call assert_equal(bufnr, s:autocmd_user)
endfunction endfunction
function Test_fix_file_references()
" No special characters
let hunk_diff = join([
\ 'diff --git a/fixture.txt b/fixture.txt',
\ 'index f5c6aff..3fbde56 100644',
\ '--- a/fixture.txt',
\ '+++ b/fixture.txt',
\ '@@ -2,0 +3,1 @@ b',
\ '+x'
\ ], "\n")."\n"
let filepath = 'blah.txt'
let expected = join([
\ 'diff --git a/blah.txt b/blah.txt',
\ 'index f5c6aff..3fbde56 100644',
\ '--- a/blah.txt',
\ '+++ b/blah.txt',
\ '@@ -2,0 +3,1 @@ b',
\ '+x'
\ ], "\n")."\n"
call assert_equal(expected, gitgutter#hunk#fix_file_references(filepath, hunk_diff))
" diff.mnemonicPrefix; spaces in filename
let hunk_diff = join([
\ 'diff --git i/x/cat dog w/x/cat dog',
\ 'index f5c6aff..3fbde56 100644',
\ '--- i/x/cat dog',
\ '+++ w/x/cat dog',
\ '@@ -2,0 +3,1 @@ b',
\ '+x'
\ ], "\n")."\n"
let filepath = 'blah.txt'
let expected = join([
\ 'diff --git i/blah.txt w/blah.txt',
\ 'index f5c6aff..3fbde56 100644',
\ '--- i/blah.txt',
\ '+++ w/blah.txt',
\ '@@ -2,0 +3,1 @@ b',
\ '+x'
\ ], "\n")."\n"
call assert_equal(expected, gitgutter#hunk#fix_file_references(filepath, hunk_diff))
" Backslashes in filename; quotation marks
let hunk_diff = join([
\ 'diff --git "a/C:\\Users\\FOO~1.PAR\\AppData\\Local\\Temp\\nvimJcmSv9\\11.1.vim" "b/C:\\Users\\FOO~1.PAR\\AppData\\Local\\Temp\\nvimJcmSv9\\12.1.vim"',
\ 'index f42aeb0..4930403 100644',
\ '--- "a/C:\\Users\\FOO~1.PAR\\AppData\\Local\\Temp\\nvimJcmSv9\\11.1.vim"',
\ '+++ "b/C:\\Users\\FOO~1.PAR\\AppData\\Local\\Temp\\nvimJcmSv9\\12.1.vim"',
\ '@@ -172,0 +173 @@ stuff',
\ '+x'
\ ], "\n")."\n"
let filepath = 'init.vim'
let expected = join([
\ 'diff --git "a/init.vim" "b/init.vim"',
\ 'index f42aeb0..4930403 100644',
\ '--- "a/init.vim"',
\ '+++ "b/init.vim"',
\ '@@ -172,0 +173 @@ stuff',
\ '+x'
\ ], "\n")."\n"
call assert_equal(expected, gitgutter#hunk#fix_file_references(filepath, hunk_diff))
endfunction