Replace system() with wrapper to protect quotes in Windows.
[vcscommand.git] / plugin / vcsbzr.vim
blobbbbca385cbef051cf5522bdde82608da7bb3179c
1 " vim600: set foldmethod=marker:
3 " BZR extension for VCSCommand.
5 " Version:       VCS development
6 " Maintainer:    Bob Hiestand <bob.hiestand@gmail.com>
7 " License:
8 " Copyright (c) 2009 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 " VCSCommandBZRExec
33 "   This variable specifies the BZR executable.  If not set, it defaults to
34 "   'bzr' executed from the user's executable path.
36 " Section: Plugin header {{{1
38 if exists('VCSCommandDisableAll')
39         finish
40 endif
42 if v:version < 700
43   echohl WarningMsg|echomsg 'VCSCommand requires at least VIM 7.0'|echohl None
44   finish
45 endif
47 runtime plugin/vcscommand.vim
49 if !executable(VCSCommandGetOption('VCSCommandBZRExec', 'bzr'))
50   " BZR is not installed
51   finish
52 endif
54 let s:save_cpo=&cpo
55 set cpo&vim
57 " Section: Variable initialization {{{1
59 let s:bzrFunctions = {}
61 " Section: Utility functions {{{1
63 " Function: s:Executable() {{{2
64 " Returns the executable used to invoke bzr suitable for use in a shell
65 " command.
66 function! s:Executable()
67         return shellescape(VCSCommandGetOption('VCSCommandBZRExec', 'bzr'))
68 endfunction
70 " Function: s:DoCommand(cmd, cmdName, statusText) {{{2
71 " Wrapper to VCSCommandDoCommand to add the name of the BZR executable to the
72 " command argument.
73 function! s:DoCommand(cmd, cmdName, statusText, options)
74   if VCSCommandGetVCSType(expand('%')) == 'BZR'
75     let fullCmd = s:Executable() . ' ' . a:cmd
76     return VCSCommandDoCommand(fullCmd, a:cmdName, a:statusText, a:options)
77   else
78     throw 'BZR VCSCommand plugin called on non-BZR item.'
79   endif
80 endfunction
82 " Section: VCS function implementations {{{1
84 " Function: s:bzrFunctions.Identify(buffer) {{{2
85 function! s:bzrFunctions.Identify(buffer)
86   let fileName = resolve(bufname(a:buffer))
87   let statusText = s:VCSCommandUtility.system(s:Executable() . ' info -- "' . fileName . '"')
88   if(v:shell_error)
89     return 0
90   else
91     return 1
92   endif
93 endfunction
95 " Function: s:bzrFunctions.Add() {{{2
96 function! s:bzrFunctions.Add(argList)
97   return s:DoCommand(join(['add'] + a:argList, ' '), 'add', join(a:argList, ' '), {})
98 endfunction
100 " Function: s:bzrFunctions.Annotate(argList) {{{2
101 function! s:bzrFunctions.Annotate(argList)
102   if len(a:argList) == 0
103     if &filetype == 'BZRAnnotate'
104       " Perform annotation of the version indicated by the current line.
105       let caption = matchstr(getline('.'),'\v^\s+\zs\d+')
106       let options = ' -r' . caption
107     else
108       let caption = ''
109       let options = ''
110     endif
111   elseif len(a:argList) == 1 && a:argList[0] !~ '^-'
112     let caption = a:argList[0]
113     let options = ' -r' . caption
114   else
115     let caption = join(a:argList, ' ')
116     let options = ' ' . caption
117   endif
119   let resultBuffer = s:DoCommand('blame' . options, 'annotate', caption, {})
120   if resultBuffer > 0
121     normal 1G2dd
122     set filetype=BZRAnnotate
123   endif
124   return resultBuffer
125 endfunction
127 " Function: s:bzrFunctions.Commit(argList) {{{2
128 function! s:bzrFunctions.Commit(argList)
129   let resultBuffer = s:DoCommand('commit -F "' . a:argList[0] . '"', 'commit', '', {})
130   if resultBuffer == 0
131     echomsg 'No commit needed.'
132   endif
133 endfunction
135 " Function: s:bzrFunctions.Delete() {{{2
136 function! s:bzrFunctions.Delete(argList)
137   return s:DoCommand(join(['rm'] + a:argList, ' '), 'rm', join(a:argList, ' '), {})
138 endfunction
140 " Function: s:bzrFunctions.Diff(argList) {{{2
141 function! s:bzrFunctions.Diff(argList)
142   if len(a:argList) == 0
143     let revOptions = []
144     let caption = ''
145   elseif len(a:argList) <= 2 && match(a:argList, '^-') == -1
146     let revOptions = ['-r' . join(a:argList, '..')]
147     let caption = '(' . a:argList[0] . ' : ' . get(a:argList, 1, 'current') . ')'
148   else
149     " Pass-through
150     let caption = join(a:argList, ' ')
151     let revOptions = a:argList
152   endif
154   let resultBuffer = s:DoCommand(join(['diff'] + revOptions), 'diff', caption, {'allowNonZeroExit': 1})
155   if resultBuffer > 0
156     set filetype=diff
157   else
158     echomsg 'No differences found'
159   endif
160   return resultBuffer
161 endfunction
163 " Function: s:bzrFunctions.GetBufferInfo() {{{2
164 " Provides version control details for the current file.  Current version
165 " number and current repository version number are required to be returned by
166 " the vcscommand plugin.
167 " Returns: List of results:  [revision, repository]
169 function! s:bzrFunctions.GetBufferInfo()
170   let originalBuffer = VCSCommandGetOriginalBuffer(bufnr('%'))
171   let fileName = resolve(bufname(originalBuffer))
172   let statusText = s:VCSCommandUtility.system(s:Executable() . ' status -S -- "' . fileName . '"')
173   let revision = s:VCSCommandUtility.system(s:Executable() . ' revno -- "' . fileName . '"')
174   if(v:shell_error)
175     return []
176   endif
178   " File not under BZR control.
179   if statusText =~ '^?'
180     return ['Unknown']
181   endif
183   let [flags, repository] = matchlist(statusText, '^\(.\{3}\)\s\+\(\S\+\)')[1:2]
184   if revision == ''
185     " Error
186     return ['Unknown']
187   elseif flags =~ '^A'
188     return ['New', 'New']
189   else
190     return [revision, repository]
191   endif
192 endfunction
194 " Function: s:bzrFunctions.Info(argList) {{{2
195 function! s:bzrFunctions.Info(argList)
196   return s:DoCommand(join(['version-info'] + a:argList, ' '), 'version-info', join(a:argList, ' '), {})
197 endfunction
199 " Function: s:bzrFunctions.Lock(argList) {{{2
200 function! s:bzrFunctions.Lock(argList)
201   echomsg 'bzr lock is not necessary'
202 endfunction
204 " Function: s:bzrFunctions.Log() {{{2
205 function! s:bzrFunctions.Log(argList)
206   if len(a:argList) == 0
207     let options = []
208     let caption = ''
209   elseif len(a:argList) <= 2 && match(a:argList, '^-') == -1
210     let options = ['-r' . join(a:argList, ':')]
211     let caption = options[0]
212   else
213     " Pass-through
214     let options = a:argList
215     let caption = join(a:argList, ' ')
216   endif
218   let resultBuffer = s:DoCommand(join(['log', '-v'] + options), 'log', caption, {})
219   return resultBuffer
220 endfunction
222 " Function: s:bzrFunctions.Revert(argList) {{{2
223 function! s:bzrFunctions.Revert(argList)
224   return s:DoCommand('revert', 'revert', '', {})
225 endfunction
227 " Function: s:bzrFunctions.Review(argList) {{{2
228 function! s:bzrFunctions.Review(argList)
229   if len(a:argList) == 0
230     let versiontag = '(current)'
231     let versionOption = ''
232   else
233     let versiontag = a:argList[0]
234     let versionOption = ' -r ' . versiontag . ' '
235   endif
237   let resultBuffer = s:DoCommand('cat' . versionOption, 'review', versiontag, {})
238   if resultBuffer > 0
239     let &filetype=getbufvar(b:VCSCommandOriginalBuffer, '&filetype')
240   endif
241   return resultBuffer
242 endfunction
244 " Function: s:bzrFunctions.Status(argList) {{{2
245 function! s:bzrFunctions.Status(argList)
246   let options = ['-S']
247   if len(a:argList) == 0
248     let options = a:argList
249   endif
250   return s:DoCommand(join(['status'] + options, ' '), 'status', join(options, ' '), {})
251 endfunction
253 " Function: s:bzrFunctions.Unlock(argList) {{{2
254 function! s:bzrFunctions.Unlock(argList)
255   echomsg 'bzr unlock is not necessary'
256 endfunction
257 " Function: s:bzrFunctions.Update(argList) {{{2
258 function! s:bzrFunctions.Update(argList)
259   return s:DoCommand('update', 'update', '', {})
260 endfunction
262 " Annotate setting {{{2
263 let s:bzrFunctions.AnnotateSplitRegex = '^[^|]\+ | '
265 " Section: Plugin Registration {{{1
266 let s:VCSCommandUtility = VCSCommandRegisterModule('BZR', expand('<sfile>'), s:bzrFunctions, [])
268 let &cpo = s:save_cpo