Skip to content

Commit a5e1f7b

Browse files
committed
Extract get diff cmd method
1 parent 513c333 commit a5e1f7b

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

autoload/project/git.vim

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -427,18 +427,25 @@ function! s:ShowDiffOnChangelist()
427427
endtry
428428
endfunction
429429

430+
function! s:GetDiffCmd(file)
431+
if s:IsStagedFile(a:file)
432+
let cmd = 'git diff --staged -- "'.a:file.'"'
433+
elseif s:IsUntrackedFile(a:file)
434+
let cmd = 'git diff --no-index -- /dev/null "'.a:file.'"'
435+
else
436+
let cmd = 'git diff -- "'.a:file.'"'
437+
endif
438+
return cmd
439+
endfunction
440+
430441
function! s:AddChangeDetails(file)
431442
let diff_file = s:TryGetDiffFile(a:file)
432443
if !empty(diff_file)
433444
let cmd = 'cat '.diff_file
434445
elseif isdirectory(s:GetAbsolutePath(a:file))
435446
let cmd = 'ls -F '.a:file
436-
elseif s:IsStagedFile(a:file)
437-
let cmd = 'git diff --staged -- "'.a:file.'"'
438-
elseif s:IsUntrackedFile(a:file)
439-
let cmd = 'git diff --no-index -- /dev/null "'.a:file.'"'
440447
else
441-
let cmd = 'git diff -- "'.a:file.'"'
448+
let cmd = s:GetDiffCmd(a:file)
442449
endif
443450
let buf_nr = s:GetBufnr(s:diff_buffer)
444451
call s:RunJob(cmd, 'VimProjectAddChangeDetails', buf_nr)
@@ -817,7 +824,16 @@ function! s:ShelfFile() range
817824

818825
for file in files
819826
" TODO: fix file under a directory abc/def/name.txt
820-
let cmd = 'git diff "'.file.'" > '.folder.'/'.file.'.diff'
827+
let segments = split(file, '/\|\\')
828+
let diff_cmd = s:GetDiffCmd(file)
829+
if len(segments) == 1
830+
let cmd = diff_cmd.' > '.folder.'/'.file.'.diff'
831+
else
832+
let directory = segments[:-2]
833+
let filename = segments[-1]
834+
let diff_file = join(directory, '_').'@'.filename
835+
let cmd = diff_cmd.' > '.folder.'/'.diff_file.'.diff'
836+
endif
821837
call project#RunShellCmd(cmd)
822838
if v:shell_error
823839
return

0 commit comments

Comments
 (0)