blob: bfc5b33f285932a9568ece616579cbfe50da62ba [file] [log] [blame]
Bram Moolenaara7241f52008-06-24 20:39:31 +00001" Vim filetype plugin
2" Language: git config file
3" Maintainer: Tim Pope <vimNOSPAM@tpope.info>
4" Last Change: 2008 Jun 04
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
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")
18 let b:undo_ftplugin = ""
19 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")
28 let b:git_dir = expand("%:p:h")
29endif
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)
36 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")
44 endif
45 endif
46 return args
47endfunction
48
49function! s:gitdiffcached(bang,gitdir,...)
50 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
68endfunction