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