blob: 94d635c5dd4d60747c5cd8c41015038798866617 [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 Moolenaar7a329912010-05-21 12:05:36 +02004" Last Change: 2010 May 21
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
14if &textwidth == 0
15 " make sure that log messages play nice with git-log on standard terminals
16 setlocal textwidth=72
17 if !exists("b:undo_ftplugin")
Bram Moolenaar7a329912010-05-21 12:05:36 +020018 let b:undo_ftplugin = ""
Bram Moolenaara7241f52008-06-24 20:39:31 +000019 endif
20 let b:undo_ftplugin = b:undo_ftplugin . "|setl tw<"
21endif
22
23if exists("g:no_gitcommit_commands") || v:version < 700
24 finish
25endif
26
27if !exists("b:git_dir")
Bram Moolenaar7a329912010-05-21 12:05:36 +020028 let b:git_dir = expand("%:p:h")
Bram Moolenaara7241f52008-06-24 20:39:31 +000029endif
30
31" Automatically diffing can be done with:
32" autocmd FileType gitcommit DiffGitCached | wincmd p
33command! -bang -bar -buffer -complete=custom,s:diffcomplete -nargs=* DiffGitCached :call s:gitdiffcached(<bang>0,b:git_dir,<f-args>)
34
35function! s:diffcomplete(A,L,P)
Bram Moolenaar7a329912010-05-21 12:05:36 +020036 let args = ""
37 if a:P <= match(a:L." -- "," -- ")+3
38 let args = args . "-p\n--stat\n--shortstat\n--summary\n--patch-with-stat\n--no-renames\n-B\n-M\n-C\n"
39 end
40 if exists("b:git_dir") && a:A !~ '^-'
41 let tree = fnamemodify(b:git_dir,':h')
42 if strpart(getcwd(),0,strlen(tree)) == tree
43 let args = args."\n".system("git diff --cached --name-only")
Bram Moolenaara7241f52008-06-24 20:39:31 +000044 endif
Bram Moolenaar7a329912010-05-21 12:05:36 +020045 endif
46 return args
Bram Moolenaara7241f52008-06-24 20:39:31 +000047endfunction
48
49function! s:gitdiffcached(bang,gitdir,...)
Bram Moolenaar7a329912010-05-21 12:05:36 +020050 let tree = fnamemodify(a:gitdir,':h')
51 let name = tempname()
52 let git = "git"
53 if strpart(getcwd(),0,strlen(tree)) != tree
54 let git .= " --git-dir=".(exists("*shellescape") ? shellescape(a:gitdir) : '"'.a:gitdir.'"')
55 endif
56 if a:0
57 let extra = join(map(copy(a:000),exists("*shellescape") ? 'shellescape(v:val)' : "'\"'.v:val.'\"'"))
58 else
59 let extra = "-p --stat=".&columns
60 endif
61 call system(git." diff --cached --no-color ".extra." > ".(exists("*shellescape") ? shellescape(name) : name))
62 exe "pedit ".(exists("*fnameescape") ? fnameescape(name) : name)
63 wincmd P
64 let b:git_dir = a:gitdir
65 command! -bang -bar -buffer -complete=custom,s:diffcomplete -nargs=* DiffGitCached :call s:gitdiffcached(<bang>0,b:git_dir,<f-args>)
66 nnoremap <silent> q :q<CR>
67 setlocal buftype=nowrite nobuflisted noswapfile nomodifiable filetype=git
Bram Moolenaara7241f52008-06-24 20:39:31 +000068endfunction