handle &sel of 'exclusive' in split annotation
[vcscommand.git] / plugin / vcsgit.vim
blobb440565f94170b40ed5f121faa533dba71eab34d
1 " vim600: set foldmethod=marker:
3 " git extension for VCSCommand.
5 " Maintainer:    Bob Hiestand <bob.hiestand@gmail.com>
6 " License:
7 " Copyright (c) Bob Hiestand
9 " Permission is hereby granted, free of charge, to any person obtaining a copy
10 " of this software and associated documentation files (the "Software"), to
11 " deal in the Software without restriction, including without limitation the
12 " rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
13 " sell copies of the Software, and to permit persons to whom the Software is
14 " furnished to do so, subject to the following conditions:
16 " The above copyright notice and this permission notice shall be included in
17 " all copies or substantial portions of the Software.
19 " THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 " IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 " FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 " AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 " LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 " FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25 " IN THE SOFTWARE.
27 " Section: Documentation {{{1
29 " Options documentation: {{{2
31 " VCSCommandGitExec
32 "   This variable specifies the git executable.  If not set, it defaults to
33 "   'git' executed from the user's executable path.
35 " VCSCommandGitDiffOpt
36 "   This variable, if set, determines the default options passed to the
37 "   VCSDiff command.  If any options (starting with '-') are passed to the
38 "   command, this variable is not used.
40 " Section: Plugin header {{{1
42 if exists('VCSCommandDisableAll')
43         finish
44 endif
46 if v:version < 700
47         echohl WarningMsg|echomsg 'VCSCommand requires at least VIM 7.0'|echohl None
48         finish
49 endif
51 runtime plugin/vcscommand.vim
53 if !executable(VCSCommandGetOption('VCSCommandGitExec', 'git'))
54         " git is not installed
55         finish
56 endif
58 let s:save_cpo=&cpo
59 set cpo&vim
61 " Section: Variable initialization {{{1
63 let s:gitFunctions = {}
65 " Section: Utility functions {{{1
67 " Function: s:Executable() {{{2
68 " Returns the executable used to invoke git suitable for use in a shell
69 " command.
70 function! s:Executable()
71         return VCSCommandGetOption('VCSCommandGitExec', 'git')
72 endfunction
74 " Function: s:DoCommand(cmd, cmdName, statusText, options) {{{2
75 " Wrapper to VCSCommandDoCommand to add the name of the git executable to the
76 " command argument.
77 function! s:DoCommand(cmd, cmdName, statusText, options)
78         if VCSCommandGetVCSType(expand('%')) == 'git'
79                 let fullCmd = s:Executable() . ' ' . a:cmd
80                 return VCSCommandDoCommand(fullCmd, a:cmdName, a:statusText, a:options)
81         else
82                 throw 'git VCSCommand plugin called on non-git item.'
83         endif
84 endfunction
86 " Section: VCS function implementations {{{1
88 " Function: s:gitFunctions.Identify(buffer) {{{2
89 " This function only returns an inexact match due to the detection method used
90 " by git, which simply traverses the directory structure upward.
91 function! s:gitFunctions.Identify(buffer)
92         let oldCwd = VCSCommandChangeToCurrentFileDir(resolve(bufname(a:buffer)))
93         try
94                 call s:VCSCommandUtility.system(s:Executable() . ' rev-parse --is-inside-work-tree')
95                 if(v:shell_error)
96                         return 0
97                 else
98                         return g:VCSCOMMAND_IDENTIFY_INEXACT
99                 endif
100         finally
101                 call VCSCommandChdir(oldCwd)
102         endtry
103 endfunction
105 " Function: s:gitFunctions.Add(argList) {{{2
106 function! s:gitFunctions.Add(argList)
107         return s:DoCommand(join(['add'] + ['-v'] + a:argList, ' '), 'add', join(a:argList, ' '), {})
108 endfunction
110 " Function: s:gitFunctions.Annotate(argList) {{{2
111 function! s:gitFunctions.Annotate(argList)
112         if len(a:argList) == 0
113                 if &filetype == 'gitannotate'
114                         " Perform annotation of the version indicated by the current line.
115                         let options = matchstr(getline('.'),'^\x\+')
116                 else
117                         let options = ''
118                 endif
119         elseif len(a:argList) == 1 && a:argList[0] !~ '^-'
120                 let options = a:argList[0]
121         else
122                 let options = join(a:argList, ' ')
123         endif
125         return s:DoCommand('blame ' . options, 'annotate', options, {})
126 endfunction
128 " Function: s:gitFunctions.Commit(argList) {{{2
129 function! s:gitFunctions.Commit(argList)
130         try
131                 return s:DoCommand('commit -F "' . a:argList[0] . '"', 'commit', '', {})
132         catch /\m^Version control command failed.*nothing\%( added\)\? to commit/
133                 echomsg 'No commit needed.'
134         endtry
135 endfunction
137 " Function: s:gitFunctions.Delete() {{{2
138 " All options are passed through.
139 function! s:gitFunctions.Delete(argList)
140         let options = a:argList
141         let caption = join(a:argList, ' ')
142         return s:DoCommand(join(['rm'] + options, ' '), 'delete', caption, {})
143 endfunction
145 " Function: s:gitFunctions.Diff(argList) {{{2
146 " Pass-through call to git-diff.  If no options (starting with '-') are found,
147 " then the options in the 'VCSCommandGitDiffOpt' variable are added.
148 function! s:gitFunctions.Diff(argList)
149         let gitDiffOpt = VCSCommandGetOption('VCSCommandGitDiffOpt', '')
150         if gitDiffOpt == ''
151                 let diffOptions = []
152         else
153                 let diffOptions = [gitDiffOpt]
154                 for arg in a:argList
155                         if arg =~ '^-'
156                                 let diffOptions = []
157                                 break
158                         endif
159                 endfor
160         endif
162         return s:DoCommand(join(['diff'] + diffOptions + a:argList), 'diff', join(a:argList), {})
163 endfunction
165 " Function: s:gitFunctions.GetBufferInfo() {{{2
166 " Provides version control details for the current file.  Current version
167 " number and current repository version number are required to be returned by
168 " the vcscommand plugin.  This CVS extension adds branch name to the return
169 " list as well.
170 " Returns: List of results:  [revision, repository, branch]
172 function! s:gitFunctions.GetBufferInfo()
173         let oldCwd = VCSCommandChangeToCurrentFileDir(resolve(bufname('%')))
174         try
175                 let branch = substitute(s:VCSCommandUtility.system(s:Executable() . ' symbolic-ref -q HEAD'), '\n$', '', '')
176                 if v:shell_error
177                         let branch = 'DETACHED'
178                 else
179                         let branch = substitute(branch, '^refs/heads/', '', '')
180                 endif
182                 let info = [branch]
184                 for method in split(VCSCommandGetOption('VCSCommandGitDescribeArgList', (',tags,all,always')), ',', 1)
185                         if method != ''
186                                 let method = ' --' . method
187                         endif
188                         let tag = substitute(s:VCSCommandUtility.system(s:Executable() . ' describe' . method), '\n$', '', '')
189                         if !v:shell_error
190                                 call add(info, tag)
191                                 break
192                         endif
193                 endfor
195                 return info
196         finally
197                 call VCSCommandChdir(oldCwd)
198         endtry
199 endfunction
201 " Function: s:gitFunctions.Log() {{{2
202 function! s:gitFunctions.Log(argList)
203         return s:DoCommand(join(['log'] + a:argList), 'log', join(a:argList, ' '), {})
204 endfunction
206 " Function: s:gitFunctions.Revert(argList) {{{2
207 function! s:gitFunctions.Revert(argList)
208         return s:DoCommand('checkout', 'revert', '', {})
209 endfunction
211 " Function: s:gitFunctions.Review(argList) {{{2
212 function! s:gitFunctions.Review(argList)
213         if len(a:argList) == 0
214                 let revision = 'HEAD'
215         else
216                 let revision = a:argList[0]
217         endif
219         let oldCwd = VCSCommandChangeToCurrentFileDir(resolve(bufname(VCSCommandGetOriginalBuffer('%'))))
220         try
221                 let prefix = s:VCSCommandUtility.system(s:Executable() . ' rev-parse --show-prefix')
222         finally
223                 call VCSCommandChdir(oldCwd)
224         endtry
226         let prefix = substitute(prefix, '\n$', '', '')
227         let blob = '"' . revision . ':' . prefix . '<VCSCOMMANDFILE>"'
228         return s:DoCommand('show ' . blob, 'review', revision, {})
229 endfunction
231 " Function: s:gitFunctions.Status(argList) {{{2
232 function! s:gitFunctions.Status(argList)
233         return s:DoCommand(join(['status'] + a:argList), 'status', join(a:argList), {'allowNonZeroExit': 1})
234 endfunction
236 " Function: s:gitFunctions.Update(argList) {{{2
237 function! s:gitFunctions.Update(argList)
238         throw "This command is not implemented for git because file-by-file update doesn't make much sense in that context.  If you have an idea for what it should do, please let me know."
239 endfunction
241 " Annotate setting {{{2
242 let s:gitFunctions.AnnotateSplitRegex = ') '
244 " Section: Plugin Registration {{{1
245 let s:VCSCommandUtility = VCSCommandRegisterModule('git', expand('<sfile>'), s:gitFunctions, [])
247 let &cpo = s:save_cpo