Replace system() with wrapper to protect quotes in Windows.
[vcscommand.git] / plugin / vcsgit.vim
blob255e9ab770605f8db7baa2581e7cae14b08ac58c
1 " vim600: set foldmethod=marker:
3 " git extension for VCSCommand.
5 " Version:       VCS development
6 " Maintainer:    Bob Hiestand <bob.hiestand@gmail.com>
7 " License:
8 " Copyright (c) 2008 Bob Hiestand
10 " Permission is hereby granted, free of charge, to any person obtaining a copy
11 " of this software and associated documentation files (the "Software"), to
12 " deal in the Software without restriction, including without limitation the
13 " rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
14 " sell copies of the Software, and to permit persons to whom the Software is
15 " furnished to do so, subject to the following conditions:
17 " The above copyright notice and this permission notice shall be included in
18 " all copies or substantial portions of the Software.
20 " THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 " IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 " FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 " AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 " LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 " FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
26 " IN THE SOFTWARE.
28 " Section: Documentation {{{1
30 " Options documentation: {{{2
32 " VCSCommandGitExec
33 "   This variable specifies the git executable.  If not set, it defaults to
34 "   'git' executed from the user's executable path.
36 " VCSCommandGitDiffOpt
37 "   This variable, if set, determines the default options passed to the
38 "   VCSDiff command.  If any options (starting with '-') are passed to the
39 "   command, this variable is not used.
41 " Section: Plugin header {{{1
43 if exists('VCSCommandDisableAll')
44         finish
45 endif
47 if v:version < 700
48         echohl WarningMsg|echomsg 'VCSCommand requires at least VIM 7.0'|echohl None
49         finish
50 endif
52 runtime plugin/vcscommand.vim
54 if !executable(VCSCommandGetOption('VCSCommandGitExec', 'git'))
55         " git is not installed
56         finish
57 endif
59 let s:save_cpo=&cpo
60 set cpo&vim
62 " Section: Variable initialization {{{1
64 let s:gitFunctions = {}
66 " Section: Utility functions {{{1
68 " Function: s:Executable() {{{2
69 " Returns the executable used to invoke git suitable for use in a shell
70 " command.
71 function! s:Executable()
72         return shellescape(VCSCommandGetOption('VCSCommandGitExec', 'git'))
73 endfunction
75 " Function: s:DoCommand(cmd, cmdName, statusText, options) {{{2
76 " Wrapper to VCSCommandDoCommand to add the name of the git executable to the
77 " command argument.
78 function! s:DoCommand(cmd, cmdName, statusText, options)
79         if VCSCommandGetVCSType(expand('%')) == 'git'
80                 let fullCmd = s:Executable() . ' ' . a:cmd
81                 return VCSCommandDoCommand(fullCmd, a:cmdName, a:statusText, a:options)
82         else
83                 throw 'git VCSCommand plugin called on non-git item.'
84         endif
85 endfunction
87 " Section: VCS function implementations {{{1
89 " Function: s:gitFunctions.Identify(buffer) {{{2
90 " This function only returns an inexact match due to the detection method used
91 " by git, which simply traverses the directory structure upward.
92 function! s:gitFunctions.Identify(buffer)
93         let oldCwd = VCSCommandChangeToCurrentFileDir(resolve(bufname(a:buffer)))
94         try
95                 call s:VCSCommandUtility.system(s:Executable() . ' rev-parse --is-inside-work-tree')
96                 if(v:shell_error)
97                         return 0
98                 else
99                         return g:VCSCOMMAND_IDENTIFY_INEXACT
100                 endif
101         finally
102                 call VCSCommandChdir(oldCwd)
103         endtry
104 endfunction
106 " Function: s:gitFunctions.Add(argList) {{{2
107 function! s:gitFunctions.Add(argList)
108         return s:DoCommand(join(['add'] + ['-v'] + a:argList, ' '), 'add', join(a:argList, ' '), {})
109 endfunction
111 " Function: s:gitFunctions.Annotate(argList) {{{2
112 function! s:gitFunctions.Annotate(argList)
113         if len(a:argList) == 0
114                 if &filetype == 'gitAnnotate'
115                         " Perform annotation of the version indicated by the current line.
116                         let options = matchstr(getline('.'),'^\x\+')
117                 else
118                         let options = ''
119                 endif
120         elseif len(a:argList) == 1 && a:argList[0] !~ '^-'
121                 let options = a:argList[0]
122         else
123                 let options = join(a:argList, ' ')
124         endif
126         let resultBuffer = s:DoCommand('blame ' . options, 'annotate', options, {})
127         if resultBuffer > 0
128                 normal 1G
129                 set filetype=gitAnnotate
130         endif
131         return resultBuffer
132 endfunction
134 " Function: s:gitFunctions.Commit(argList) {{{2
135 function! s:gitFunctions.Commit(argList)
136         let resultBuffer = s:DoCommand('commit -F "' . a:argList[0] . '"', 'commit', '', {})
137         if resultBuffer == 0
138                 echomsg 'No commit needed.'
139         endif
140         return resultBuffer
141 endfunction
143 " Function: s:gitFunctions.Delete() {{{2
144 " All options are passed through.
145 function! s:gitFunctions.Delete(argList)
146         let options = a:argList
147         let caption = join(a:argList, ' ')
148         return s:DoCommand(join(['rm'] + options, ' '), 'delete', caption, {})
149 endfunction
151 " Function: s:gitFunctions.Diff(argList) {{{2
152 " Pass-through call to git-diff.  If no options (starting with '-') are found,
153 " then the options in the 'VCSCommandGitDiffOpt' variable are added.
154 function! s:gitFunctions.Diff(argList)
155         let gitDiffOpt = VCSCommandGetOption('VCSCommandGitDiffOpt', '')
156         if gitDiffOpt == ''
157                 let diffOptions = []
158         else
159                 let diffOptions = [gitDiffOpt]
160                 for arg in a:argList
161                         if arg =~ '^-'
162                                 let diffOptions = []
163                                 break
164                         endif
165                 endfor
166         endif
168         let resultBuffer = s:DoCommand(join(['diff'] + diffOptions + a:argList), 'diff', join(a:argList), {})
169         if resultBuffer > 0
170                 set filetype=diff
171         else
172                 echomsg 'No differences found'
173         endif
174         return resultBuffer
175 endfunction
177 " Function: s:gitFunctions.GetBufferInfo() {{{2
178 " Provides version control details for the current file.  Current version
179 " number and current repository version number are required to be returned by
180 " the vcscommand plugin.  This CVS extension adds branch name to the return
181 " list as well.
182 " Returns: List of results:  [revision, repository, branch]
184 function! s:gitFunctions.GetBufferInfo()
185         let oldCwd = VCSCommandChangeToCurrentFileDir(resolve(bufname('%')))
186         try
187                 let branch = substitute(s:VCSCommandUtility.system(s:Executable() . ' symbolic-ref -q HEAD'), '\n$', '', '')
188                 if v:shell_error
189                         let branch = 'DETACHED'
190                 else
191                         let branch = substitute(branch, '^refs/heads/', '', '')
192                 endif
194                 let info = [branch]
196                 for method in split(VCSCommandGetOption('VCSCommandGitDescribeArgList', (',tags,all,always')), ',', 1)
197                         if method != ''
198                                 let method = ' --' . method
199                         endif
200                         let tag = substitute(s:VCSCommandUtility.system(s:Executable() . ' describe' . method), '\n$', '', '')
201                         if !v:shell_error
202                                 call add(info, tag)
203                                 break
204                         endif
205                 endfor
207                 return info
208         finally
209                 call VCSCommandChdir(oldCwd)
210         endtry
211 endfunction
213 " Function: s:gitFunctions.Log() {{{2
214 function! s:gitFunctions.Log(argList)
215         let resultBuffer=s:DoCommand(join(['log'] + a:argList), 'log', join(a:argList, ' '), {})
216         if resultBuffer > 0
217                 set filetype=gitlog
218         endif
219         return resultBuffer
220 endfunction
222 " Function: s:gitFunctions.Revert(argList) {{{2
223 function! s:gitFunctions.Revert(argList)
224         return s:DoCommand('checkout', 'revert', '', {})
225 endfunction
227 " Function: s:gitFunctions.Review(argList) {{{2
228 function! s:gitFunctions.Review(argList)
229         if len(a:argList) == 0
230                 let revision = 'HEAD'
231         else
232                 let revision = a:argList[0]
233         endif
235         let oldCwd = VCSCommandChangeToCurrentFileDir(resolve(bufname(VCSCommandGetOriginalBuffer('%'))))
236         try
237                 let prefix = s:VCSCommandUtility.system(s:Executable() . ' rev-parse --show-prefix')
238         finally
239                 call VCSCommandChdir(oldCwd)
240         endtry
242         let prefix = substitute(prefix, '\n$', '', '')
243         let blob = '"' . revision . ':' . prefix . '<VCSCOMMANDFILE>"'
244         let resultBuffer = s:DoCommand('show ' . blob, 'review', revision, {})
245         if resultBuffer > 0
246                 let &filetype=getbufvar(b:VCSCommandOriginalBuffer, '&filetype')
247         endif
248         return resultBuffer
249 endfunction
251 " Function: s:gitFunctions.Status(argList) {{{2
252 function! s:gitFunctions.Status(argList)
253         return s:DoCommand(join(['status'] + a:argList), 'status', join(a:argList), {'allowNonZeroExit': 1})
254 endfunction
256 " Function: s:gitFunctions.Update(argList) {{{2
257 function! s:gitFunctions.Update(argList)
258         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."
259 endfunction
261 " Annotate setting {{{2
262 let s:gitFunctions.AnnotateSplitRegex = ') '
264 " Section: Plugin Registration {{{1
265 let s:VCSCommandUtility = VCSCommandRegisterModule('git', expand('<sfile>'), s:gitFunctions, [])
267 let &cpo = s:save_cpo