blob: 9b1998acaa7e96fa1cc664ae73e3fe302ae94d50 [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 Moolenaarc08ee742019-12-05 22:47:25 +01004" Last Change: 2019 Dec 05
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
Bram Moolenaarc08ee742019-12-05 22:47:25 +010016setlocal 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<'
Bram Moolenaara7241f52008-06-24 20:39:31 +000020
21if exists("g:no_gitcommit_commands") || v:version < 700
22 finish
23endif
24
25if !exists("b:git_dir")
Bram Moolenaar7a329912010-05-21 12:05:36 +020026 let b:git_dir = expand("%:p:h")
Bram Moolenaara7241f52008-06-24 20:39:31 +000027endif
28
Bram Moolenaara7241f52008-06-24 20:39:31 +000029command! -bang -bar -buffer -complete=custom,s:diffcomplete -nargs=* DiffGitCached :call s:gitdiffcached(<bang>0,b:git_dir,<f-args>)
30
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020031let b:undo_ftplugin = b:undo_ftplugin . "|delc DiffGitCached"
32
Bram Moolenaara7241f52008-06-24 20:39:31 +000033function! s:diffcomplete(A,L,P)
Bram Moolenaar7a329912010-05-21 12:05:36 +020034 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")
Bram Moolenaara7241f52008-06-24 20:39:31 +000042 endif
Bram Moolenaar7a329912010-05-21 12:05:36 +020043 endif
44 return args
Bram Moolenaara7241f52008-06-24 20:39:31 +000045endfunction
46
47function! s:gitdiffcached(bang,gitdir,...)
Bram Moolenaar7a329912010-05-21 12:05:36 +020048 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
Bram Moolenaar53bfca22012-04-13 23:04:47 +020059 call system(git." diff --cached --no-color --no-ext-diff ".extra." > ".(exists("*shellescape") ? shellescape(name) : name))
Bram Moolenaar7a329912010-05-21 12:05:36 +020060 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>)
Bram Moolenaar53bfca22012-04-13 23:04:47 +020064 nnoremap <buffer> <silent> q :q<CR>
Bram Moolenaar7a329912010-05-21 12:05:36 +020065 setlocal buftype=nowrite nobuflisted noswapfile nomodifiable filetype=git
Bram Moolenaara7241f52008-06-24 20:39:31 +000066endfunction