1" Vim filetype plugin
2" Language:	git commit file
3" Maintainer:	Tim Pope <[email protected]>
4" Last Change:	2016 Aug 29
5
6" Only do this when not done yet for this buffer
7if (exists("b:did_ftplugin"))
8  finish
9endif
10
11runtime! ftplugin/git.vim
12let b:did_ftplugin = 1
13
14setlocal comments=:# commentstring=#\ %s
15setlocal nomodeline tabstop=8 formatoptions+=tl textwidth=72
16setlocal formatoptions-=c formatoptions-=r formatoptions-=o formatoptions-=q
17let b:undo_ftplugin = 'setl modeline< tabstop< formatoptions< tw< com< cms<'
18
19if exists("g:no_gitcommit_commands") || v:version < 700
20  finish
21endif
22
23if !exists("b:git_dir")
24  let b:git_dir = expand("%:p:h")
25endif
26
27command! -bang -bar -buffer -complete=custom,s:diffcomplete -nargs=* DiffGitCached :call s:gitdiffcached(<bang>0,b:git_dir,<f-args>)
28
29let b:undo_ftplugin = b:undo_ftplugin . "|delc DiffGitCached"
30
31function! s:diffcomplete(A,L,P)
32  let args = ""
33  if a:P <= match(a:L." -- "," -- ")+3
34    let args = args . "-p\n--stat\n--shortstat\n--summary\n--patch-with-stat\n--no-renames\n-B\n-M\n-C\n"
35  end
36  if exists("b:git_dir") && a:A !~ '^-'
37    let tree = fnamemodify(b:git_dir,':h')
38    if strpart(getcwd(),0,strlen(tree)) == tree
39      let args = args."\n".system("git diff --cached --name-only")
40    endif
41  endif
42  return args
43endfunction
44
45function! s:gitdiffcached(bang,gitdir,...)
46  let tree = fnamemodify(a:gitdir,':h')
47  let name = tempname()
48  let git = "git"
49  if strpart(getcwd(),0,strlen(tree)) != tree
50    let git .= " --git-dir=".(exists("*shellescape") ? shellescape(a:gitdir) : '"'.a:gitdir.'"')
51  endif
52  if a:0
53    let extra = join(map(copy(a:000),exists("*shellescape") ? 'shellescape(v:val)' : "'\"'.v:val.'\"'"))
54  else
55    let extra = "-p --stat=".&columns
56  endif
57  call system(git." diff --cached --no-color --no-ext-diff ".extra." > ".(exists("*shellescape") ? shellescape(name) : name))
58  exe "pedit ".(exists("*fnameescape") ? fnameescape(name) : name)
59  wincmd P
60  let b:git_dir = a:gitdir
61  command! -bang -bar -buffer -complete=custom,s:diffcomplete -nargs=* DiffGitCached :call s:gitdiffcached(<bang>0,b:git_dir,<f-args>)
62  nnoremap <buffer> <silent> q :q<CR>
63  setlocal buftype=nowrite nobuflisted noswapfile nomodifiable filetype=git
64endfunction
65