blob: 6767ff719e8a157cfb453af0a7491a8d3e3e5304 [file] [log] [blame]
Bram Moolenaara7241f52008-06-24 20:39:31 +00001" Vim filetype plugin
Bram Moolenaar5c736222010-01-06 20:54:52 +01002" Language: git commit file
3" Maintainer: Tim Pope <vimNOSPAM@tpope.org>
Bram Moolenaar89bcfda2016-08-30 23:26:57 +02004" Last Change: 2016 Aug 29
Bram Moolenaara7241f52008-06-24 20:39:31 +00005
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
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020014setlocal 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<'
Bram Moolenaara7241f52008-06-24 20:39:31 +000018
19if exists("g:no_gitcommit_commands") || v:version < 700
20 finish
21endif
22
23if !exists("b:git_dir")
Bram Moolenaar7a329912010-05-21 12:05:36 +020024 let b:git_dir = expand("%:p:h")
Bram Moolenaara7241f52008-06-24 20:39:31 +000025endif
26
Bram Moolenaara7241f52008-06-24 20:39:31 +000027command! -bang -bar -buffer -complete=custom,s:diffcomplete -nargs=* DiffGitCached :call s:gitdiffcached(<bang>0,b:git_dir,<f-args>)
28
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020029let b:undo_ftplugin = b:undo_ftplugin . "|delc DiffGitCached"
30
Bram Moolenaara7241f52008-06-24 20:39:31 +000031function! s:diffcomplete(A,L,P)
Bram Moolenaar7a329912010-05-21 12:05:36 +020032 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")
Bram Moolenaara7241f52008-06-24 20:39:31 +000040 endif
Bram Moolenaar7a329912010-05-21 12:05:36 +020041 endif
42 return args
Bram Moolenaara7241f52008-06-24 20:39:31 +000043endfunction
44
45function! s:gitdiffcached(bang,gitdir,...)
Bram Moolenaar7a329912010-05-21 12:05:36 +020046 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
Bram Moolenaar53bfca22012-04-13 23:04:47 +020057 call system(git." diff --cached --no-color --no-ext-diff ".extra." > ".(exists("*shellescape") ? shellescape(name) : name))
Bram Moolenaar7a329912010-05-21 12:05:36 +020058 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>)
Bram Moolenaar53bfca22012-04-13 23:04:47 +020062 nnoremap <buffer> <silent> q :q<CR>
Bram Moolenaar7a329912010-05-21 12:05:36 +020063 setlocal buftype=nowrite nobuflisted noswapfile nomodifiable filetype=git
Bram Moolenaara7241f52008-06-24 20:39:31 +000064endfunction