blob: e8619004bf8dc66c8db85fef714912074cb6ea64 [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 Moolenaar543b7ef2013-06-01 14:50:56 +02004" Last Change: 2013 May 30
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 Moolenaar543b7ef2013-06-01 14:50:56 +020014setlocal nomodeline tabstop=8 formatoptions-=croq formatoptions+=tl
Bram Moolenaar53bfca22012-04-13 23:04:47 +020015
Bram Moolenaar543b7ef2013-06-01 14:50:56 +020016let b:undo_ftplugin = 'setl modeline< tabstop< formatoptions<'
Bram Moolenaar53bfca22012-04-13 23:04:47 +020017
Bram Moolenaara7241f52008-06-24 20:39:31 +000018if &textwidth == 0
19 " make sure that log messages play nice with git-log on standard terminals
20 setlocal textwidth=72
Bram Moolenaar53bfca22012-04-13 23:04:47 +020021 let b:undo_ftplugin .= "|setl tw<"
Bram Moolenaara7241f52008-06-24 20:39:31 +000022endif
23
24if exists("g:no_gitcommit_commands") || v:version < 700
25 finish
26endif
27
28if !exists("b:git_dir")
Bram Moolenaar7a329912010-05-21 12:05:36 +020029 let b:git_dir = expand("%:p:h")
Bram Moolenaara7241f52008-06-24 20:39:31 +000030endif
31
Bram Moolenaara7241f52008-06-24 20:39:31 +000032command! -bang -bar -buffer -complete=custom,s:diffcomplete -nargs=* DiffGitCached :call s:gitdiffcached(<bang>0,b:git_dir,<f-args>)
33
34function! s:diffcomplete(A,L,P)
Bram Moolenaar7a329912010-05-21 12:05:36 +020035 let args = ""
36 if a:P <= match(a:L." -- "," -- ")+3
37 let args = args . "-p\n--stat\n--shortstat\n--summary\n--patch-with-stat\n--no-renames\n-B\n-M\n-C\n"
38 end
39 if exists("b:git_dir") && a:A !~ '^-'
40 let tree = fnamemodify(b:git_dir,':h')
41 if strpart(getcwd(),0,strlen(tree)) == tree
42 let args = args."\n".system("git diff --cached --name-only")
Bram Moolenaara7241f52008-06-24 20:39:31 +000043 endif
Bram Moolenaar7a329912010-05-21 12:05:36 +020044 endif
45 return args
Bram Moolenaara7241f52008-06-24 20:39:31 +000046endfunction
47
48function! s:gitdiffcached(bang,gitdir,...)
Bram Moolenaar7a329912010-05-21 12:05:36 +020049 let tree = fnamemodify(a:gitdir,':h')
50 let name = tempname()
51 let git = "git"
52 if strpart(getcwd(),0,strlen(tree)) != tree
53 let git .= " --git-dir=".(exists("*shellescape") ? shellescape(a:gitdir) : '"'.a:gitdir.'"')
54 endif
55 if a:0
56 let extra = join(map(copy(a:000),exists("*shellescape") ? 'shellescape(v:val)' : "'\"'.v:val.'\"'"))
57 else
58 let extra = "-p --stat=".&columns
59 endif
Bram Moolenaar53bfca22012-04-13 23:04:47 +020060 call system(git." diff --cached --no-color --no-ext-diff ".extra." > ".(exists("*shellescape") ? shellescape(name) : name))
Bram Moolenaar7a329912010-05-21 12:05:36 +020061 exe "pedit ".(exists("*fnameescape") ? fnameescape(name) : name)
62 wincmd P
63 let b:git_dir = a:gitdir
64 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 +020065 nnoremap <buffer> <silent> q :q<CR>
Bram Moolenaar7a329912010-05-21 12:05:36 +020066 setlocal buftype=nowrite nobuflisted noswapfile nomodifiable filetype=git
Bram Moolenaara7241f52008-06-24 20:39:31 +000067endfunction