Skip to content

Commit

Permalink
Only pass revision argument to diff command when required
Browse files Browse the repository at this point in the history
Move common diff generation code to local script function.
  • Loading branch information
vhda committed Dec 7, 2016
1 parent bc5bf54 commit dffaaaa
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 26 deletions.
59 changes: 34 additions & 25 deletions autoload/quickfixsigns/vcsdiff.vim
Expand Up @@ -32,17 +32,17 @@ if !exists('g:quickfixsigns#vcsdiff#vcs')
" cmd ... command templates that generate a unified diff file.
" "%s" is replaced with the filename.
" dir ... the directory name
" revision ... The default revision/branch
" rev_arg ... argument to selection revision to diff against
" Currently supported vcs: git, hg, svn, bzr
"
" Users can also use g:quickfixsigns#vcsdiff#vcs_{vcs_type} for
" configuration.
" :read: let g:quickfixsigns#vcsdiff#vcs = {...} "{{{2
let g:quickfixsigns#vcsdiff#vcs = {
\ 'git': {'cmd': 'git diff --no-ext-diff -U0 %s %s -- %s', 'dir': '.git', 'revision': 'HEAD'}
\ , 'hg': {'cmd': 'hg diff -U0 -r %s %s %s', 'dir': '.hg', 'revision': '-1'}
\ , 'svn': {'cmd': 'svn diff --diff-cmd diff --extensions -U0 --revision %s %s %s', 'dir': '.svn', 'revision': 'BASE'}
\ , 'bzr': {'cmd': 'bzr diff --diff-options=-U0 -r %s %s %s', 'dir': '.bzr', 'revision': 'last:1'}
\ 'git': {'cmd': 'git diff --no-ext-diff -U0 %s %s -- %s', 'dir': '.git', 'rev_arg': ''}
\ , 'hg': {'cmd': 'hg diff -U0 %s %s %s', 'dir': '.hg', 'rev_arg': '-r'}
\ , 'svn': {'cmd': 'svn diff --diff-cmd diff --extensions -U0 %s %s %s', 'dir': '.svn', 'rev_arg': '--revision'}
\ , 'bzr': {'cmd': 'bzr diff --diff-options=-U0 %s %s %s', 'dir': '.bzr', 'rev_arg': '-r'}
\ }
endif

Expand Down Expand Up @@ -302,17 +302,8 @@ function! quickfixsigns#vcsdiff#GetList0(filename) "{{{3
" TLogVAR a:filename, vcs_type
" Ignore files that are not readable
if !empty(s:Config(vcs_type)) && filereadable(a:filename)
let cmdt = s:Config(vcs_type).cmd
let dir = fnamemodify(a:filename, ':h')
let file = fnamemodify(a:filename, ':t')
let rev = s:GetParam('quickfixsigns#vcsdiff#revision', vcs_type, s:Config(vcs_type).revision)
let extra = s:GetParam('quickfixsigns#vcsdiff#extra_args', vcs_type, '')
let cmds = join([
\ printf("%s %s", g:quickfixsigns#vcsdiff#cd, shellescape(dir)),
\ printf(cmdt, rev, extra, shellescape(file))
\ ], g:quickfixsigns#vcsdiff#cmd_separator)
" TLogVAR cmds
let diff = system(cmds)
let diff = s:Diff(a:filename, vcs_type)
" TLogVAR diff
let bufnr = bufnr('%')
let bufdiff = exists('b:quickfixsigns_vcsdiff') ? b:quickfixsigns_vcsdiff : ''
Expand Down Expand Up @@ -425,17 +416,8 @@ function! quickfixsigns#vcsdiff#GetList1(filename) "{{{3
" TLogVAR a:filename, vcs_type
" Ignore files that are not readable
if !empty(s:Config(vcs_type)) && filereadable(a:filename)
let cmdt = s:Config(vcs_type).cmd
let rev = s:GetParam('quickfixsigns#vcsdiff#revision', vcs_type, s:Config(vcs_type).revision)
let extra = s:GetParam('quickfixsigns#vcsdiff#extra_args', vcs_type, '')
let dir = fnamemodify(a:filename, ':h')
let file = fnamemodify(a:filename, ':t')
let cmds = join([
\ printf("%s %s", g:quickfixsigns#vcsdiff#cd, shellescape(dir)),
\ printf(cmdt, extra, rev, shellescape(file))
\ ], g:quickfixsigns#vcsdiff#cmd_separator)
" TLogVAR cmds
let diff = system(cmds)
let diff = s:Diff(a:filename, vcs_type)
" TLogVAR diff
let bufnr = bufnr('%')
let bufdiff = exists('b:quickfixsigns_vcsdiff') ? b:quickfixsigns_vcsdiff : ''
Expand Down Expand Up @@ -573,6 +555,33 @@ function! s:GetParam(name, type, default) abort "{{{3
endf


function! s:Diff(filename, vcs_type) "{{{3
let cmdt = s:Config(a:vcs_type).cmd
let rev_arg = s:GetParam('quickfixsigns#vcsdiff#rev_arg', a:vcs_type, s:Config(a:vcs_type).rev_arg)
let extra = s:GetParam('quickfixsigns#vcsdiff#extra_args', a:vcs_type, '')
let dir = fnamemodify(a:filename, ':h')
let file = fnamemodify(a:filename, ':t')

" Create command string
let cmds = printf("%s %s", g:quickfixsigns#vcsdiff#cd, shellescape(dir))
if exists('g:quickfixsigns#vcsdiff#revision') && !empty('g:quickfixsigns#vcsdiff#revision')
let revision = g:quickfixsigns#vcsdiff#revision
let cmds = join([
\ cmds,
\ printf(cmdt, extra, printf("%s %s", rev_arg, revision), shellescape(file))
\ ], g:quickfixsigns#vcsdiff#cmd_separator)
else
let cmds = join([
\ cmds,
\ printf(cmdt, extra, "", shellescape(file))
\ ], g:quickfixsigns#vcsdiff#cmd_separator)
endif

" Return command result
return system(cmds)
endf


if exists(':TStatusregister1')
TStatusregister1 --event=BufRead,BufWritePost vcs quickfixsigns#vcsdiff#GetHunkSummaryAsString()
endif
Expand Down
2 changes: 1 addition & 1 deletion doc/quickfixsigns.txt
Expand Up @@ -437,7 +437,7 @@ g:quickfixsigns#vcsdiff#vcs (default: {...})
cmd ... command templates that generate a unified diff file.
"%s" is replaced with the filename.
dir ... the directory name
revision ... The default revision/branch
rev_arg ... argument to selection revision to diff against
Currently supported vcs: git, hg, svn, bzr

Users can also use g:quickfixsigns#vcsdiff#vcs_{vcs_type} for
Expand Down

0 comments on commit dffaaaa

Please sign in to comment.