vcsgit: Quote the blob name for VCSReview to handle paths with spaces.
[vcscommand.git] / plugin / vcsgit.vim
blob6508806efa000cc1391d5885a8e34fdf4a9b11bb
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 v:version < 700
44         echohl WarningMsg|echomsg 'VCSCommand requires at least VIM 7.0'|echohl None
45         finish
46 endif
48 runtime plugin/vcscommand.vim
50 if !executable(VCSCommandGetOption('VCSCommandGitExec', 'git'))
51         " git is not installed
52         finish
53 endif
55 let s:save_cpo=&cpo
56 set cpo&vim
58 " Section: Variable initialization {{{1
60 let s:gitFunctions = {}
62 " Section: Utility functions {{{1
64 " Function: s:DoCommand(cmd, cmdName, statusText, options) {{{2
65 " Wrapper to VCSCommandDoCommand to add the name of the git executable to the
66 " command argument.
67 function! s:DoCommand(cmd, cmdName, statusText, options)
68         if VCSCommandGetVCSType(expand('%')) == 'git'
69                 let fullCmd = VCSCommandGetOption('VCSCommandGitExec', 'git',) . ' ' . a:cmd
70                 return VCSCommandDoCommand(fullCmd, a:cmdName, a:statusText, a:options)
71         else
72                 throw 'git VCSCommand plugin called on non-git item.'
73         endif
74 endfunction
76 " Section: VCS function implementations {{{1
78 " Function: s:gitFunctions.Identify(buffer) {{{2
79 " This function only returns an inexact match due to the detection method used
80 " by git, which simply traverses the directory structure upward.
81 function! s:gitFunctions.Identify(buffer)
82         let oldCwd = VCSCommandChangeToCurrentFileDir(resolve(bufname(a:buffer)))
83         try
84                 call system(VCSCommandGetOption('VCSCommandGitExec', 'git') . ' rev-parse --is-inside-work-tree')
85                 if(v:shell_error)
86                         return 0
87                 else
88                         return g:VCSCOMMAND_IDENTIFY_INEXACT
89                 endif
90         finally
91                 call VCSCommandChdir(oldCwd)
92         endtry
93 endfunction
95 " Function: s:gitFunctions.Add(argList) {{{2
96 function! s:gitFunctions.Add(argList)
97         return s:DoCommand(join(['add'] + ['-v'] + a:argList, ' '), 'add', join(a:argList, ' '), {})
98 endfunction
100 " Function: s:gitFunctions.Annotate(argList) {{{2
101 function! s:gitFunctions.Annotate(argList)
102         if len(a:argList) == 0
103                 if &filetype == 'gitAnnotate'
104                         " Perform annotation of the version indicated by the current line.
105                         let options = matchstr(getline('.'),'^\x\+')
106                 else
107                         let options = ''
108                 endif
109         elseif len(a:argList) == 1 && a:argList[0] !~ '^-'
110                 let options = a:argList[0]
111         else
112                 let options = join(a:argList, ' ')
113         endif
115         let resultBuffer = s:DoCommand('blame ' . options . ' -- ', 'annotate', options, {})
116         if resultBuffer > 0
117                 normal 1G
118                 set filetype=gitAnnotate
119         endif
120         return resultBuffer
121 endfunction
123 " Function: s:gitFunctions.Commit(argList) {{{2
124 function! s:gitFunctions.Commit(argList)
125         let resultBuffer = s:DoCommand('commit -F "' . a:argList[0] . '"', 'commit', '', {})
126         if resultBuffer == 0
127                 echomsg 'No commit needed.'
128         endif
129         return resultBuffer
130 endfunction
132 " Function: s:gitFunctions.Delete() {{{2
133 " All options are passed through.
134 function! s:gitFunctions.Delete(argList)
135         let options = a:argList
136         let caption = join(a:argList, ' ')
137         return s:DoCommand(join(['rm'] + options, ' '), 'delete', caption, {})
138 endfunction
140 " Function: s:gitFunctions.Diff(argList) {{{2
141 " Pass-through call to git-diff.  If no options (starting with '-') are found,
142 " then the options in the 'VCSCommandGitDiffOpt' variable are added.
143 function! s:gitFunctions.Diff(argList)
144         let gitDiffOpt = VCSCommandGetOption('VCSCommandGitDiffOpt', '')
145         if gitDiffOpt == ''
146                 let diffOptions = []
147         else
148                 let diffOptions = [gitDiffOpt]
149                 for arg in a:argList
150                         if arg =~ '^-'
151                                 let diffOptions = []
152                                 break
153                         endif
154                 endfor
155         endif
157         let resultBuffer = s:DoCommand(join(['diff'] + diffOptions + a:argList), 'diff', join(a:argList), {})
158         if resultBuffer > 0
159                 set filetype=diff
160         else
161                 echomsg 'No differences found'
162         endif
163         return resultBuffer
164 endfunction
166 " Function: s:gitFunctions.GetBufferInfo() {{{2
167 " Provides version control details for the current file.  Current version
168 " number and current repository version number are required to be returned by
169 " the vcscommand plugin.  This CVS extension adds branch name to the return
170 " list as well.
171 " Returns: List of results:  [revision, repository, branch]
173 function! s:gitFunctions.GetBufferInfo()
174         let oldCwd = VCSCommandChangeToCurrentFileDir(resolve(bufname('%')))
175         try
176                 let branch = substitute(system(VCSCommandGetOption('VCSCommandGitExec', 'git') . ' symbolic-ref -q HEAD'), '\n$', '', '')
177                 if v:shell_error
178                         let branch = 'DETACHED'
179                 else
180                         let branch = substitute(branch, '^refs/heads/', '', '')
181                 endif
183                 let info = [branch]
185                 for method in split(VCSCommandGetOption('VCSCommandGitDescribeArgList', (',tags,all,always')), ',', 1)
186                         if method != ''
187                                 let method = ' --' . method
188                         endif
189                         let tag = substitute(system(VCSCommandGetOption('VCSCommandGitExec', 'git') . ' describe' . method), '\n$', '', '')
190                         if !v:shell_error
191                                 call add(info, tag)
192                                 break
193                         endif
194                 endfor
196                 return info
197         finally
198                 call VCSCommandChdir(oldCwd)
199         endtry
200 endfunction
202 " Function: s:gitFunctions.Log() {{{2
203 function! s:gitFunctions.Log(argList)
204         let resultBuffer=s:DoCommand(join(['log'] + a:argList), 'log', join(a:argList, ' '), {})
205         if resultBuffer > 0
206                 set filetype=gitlog
207         endif
208         return resultBuffer
209 endfunction
211 " Function: s:gitFunctions.Revert(argList) {{{2
212 function! s:gitFunctions.Revert(argList)
213         return s:DoCommand('checkout', 'revert', '', {})
214 endfunction
216 " Function: s:gitFunctions.Review(argList) {{{2
217 function! s:gitFunctions.Review(argList)
218         if len(a:argList) == 0
219                 let revision = 'HEAD'
220         else
221                 let revision = a:argList[0]
222         endif
224         let oldCwd = VCSCommandChangeToCurrentFileDir(resolve(bufname(VCSCommandGetOriginalBuffer('%'))))
225         try
226                 let prefix = system(VCSCommandGetOption('VCSCommandGitExec', 'git') . ' rev-parse --show-prefix')
227         finally
228                 call VCSCommandChdir(oldCwd)
229         endtry
231         let prefix = substitute(prefix, '\n$', '', '')
232         let blob = '"' . revision . ':' . prefix . '<VCSCOMMANDFILE>"'
233         let resultBuffer = s:DoCommand('show ' . blob, 'review', revision, {})
234         if resultBuffer > 0
235                 let &filetype=getbufvar(b:VCSCommandOriginalBuffer, '&filetype')
236         endif
237         return resultBuffer
238 endfunction
240 " Function: s:gitFunctions.Status(argList) {{{2
241 function! s:gitFunctions.Status(argList)
242         return s:DoCommand(join(['status'] + a:argList), 'status', join(a:argList), {'allowNonZeroExit': 1})
243 endfunction
245 " Function: s:gitFunctions.Update(argList) {{{2
246 function! s:gitFunctions.Update(argList)
247         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."
248 endfunction
251 " Section: Plugin Registration {{{1
252 call VCSCommandRegisterModule('git', expand('<sfile>'), s:gitFunctions, [])
254 let &cpo = s:save_cpo