Added -- to differentiate file names in system() calls.
[vcscommand.git] / plugin / vcsbzr.vim
blob58d487dc8e12a3ccb7152741fa2b86b05d80ae18
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:DoCommand(cmd, cmdName, statusText) {{{2
64 " Wrapper to VCSCommandDoCommand to add the name of the BZR executable to the
65 " command argument.
66 function! s:DoCommand(cmd, cmdName, statusText, options)
67   if VCSCommandGetVCSType(expand('%')) == 'BZR'
68     let fullCmd = VCSCommandGetOption('VCSCommandBZRExec', 'bzr') . ' ' . a:cmd
69     return VCSCommandDoCommand(fullCmd, a:cmdName, a:statusText, a:options)
70   else
71     throw 'BZR VCSCommand plugin called on non-BZR item.'
72   endif
73 endfunction
75 " Section: VCS function implementations {{{1
77 " Function: s:bzrFunctions.Identify(buffer) {{{2
78 function! s:bzrFunctions.Identify(buffer)
79   let fileName = resolve(bufname(a:buffer))
80   let statusText = system(VCSCommandGetOption('VCSCommandBZRExec', 'bzr') . ' info -- "' . fileName . '"')
81   if(v:shell_error)
82     return 0
83   else
84     return 1
85   endif
86 endfunction
88 " Function: s:bzrFunctions.Add() {{{2
89 function! s:bzrFunctions.Add(argList)
90   return s:DoCommand(join(['add'] + a:argList, ' '), 'add', join(a:argList, ' '), {})
91 endfunction
93 " Function: s:bzrFunctions.Annotate(argList) {{{2
94 function! s:bzrFunctions.Annotate(argList)
95   if len(a:argList) == 0
96     if &filetype == 'BZRAnnotate'
97       " Perform annotation of the version indicated by the current line.
98       let caption = matchstr(getline('.'),'\v^\s+\zs\d+')
99       let options = ' -r' . caption
100     else
101       let caption = ''
102       let options = ''
103     endif
104   elseif len(a:argList) == 1 && a:argList[0] !~ '^-'
105     let caption = a:argList[0]
106     let options = ' -r' . caption
107   else
108     let caption = join(a:argList, ' ')
109     let options = ' ' . caption
110   endif
112   let resultBuffer = s:DoCommand('blame' . options, 'annotate', caption, {})
113   if resultBuffer > 0
114     normal 1G2dd
115     set filetype=BZRAnnotate
116   endif
117   return resultBuffer
118 endfunction
120 " Function: s:bzrFunctions.Commit(argList) {{{2
121 function! s:bzrFunctions.Commit(argList)
122   let resultBuffer = s:DoCommand('commit -F "' . a:argList[0] . '"', 'commit', '', {})
123   if resultBuffer == 0
124     echomsg 'No commit needed.'
125   endif
126 endfunction
128 " Function: s:bzrFunctions.Delete() {{{2
129 function! s:bzrFunctions.Delete(argList)
130   return s:DoCommand(join(['rm'] + a:argList, ' '), 'rm', join(a:argList, ' '), {})
131 endfunction
133 " Function: s:bzrFunctions.Diff(argList) {{{2
134 function! s:bzrFunctions.Diff(argList)
135   if len(a:argList) == 0
136     let revOptions = []
137     let caption = ''
138   elseif len(a:argList) <= 2 && match(a:argList, '^-') == -1
139     let revOptions = ['-r' . join(a:argList, '..')]
140     let caption = '(' . a:argList[0] . ' : ' . get(a:argList, 1, 'current') . ')'
141   else
142     " Pass-through
143     let caption = join(a:argList, ' ')
144     let revOptions = a:argList
145   endif
147   let resultBuffer = s:DoCommand(join(['diff'] + revOptions), 'diff', caption, {'allowNonZeroExit': 1})
148   if resultBuffer > 0
149     set filetype=diff
150   else
151     echomsg 'No differences found'
152   endif
153   return resultBuffer
154 endfunction
156 " Function: s:bzrFunctions.GetBufferInfo() {{{2
157 " Provides version control details for the current file.  Current version
158 " number and current repository version number are required to be returned by
159 " the vcscommand plugin.
160 " Returns: List of results:  [revision, repository]
162 function! s:bzrFunctions.GetBufferInfo()
163   let originalBuffer = VCSCommandGetOriginalBuffer(bufnr('%'))
164   let fileName = resolve(bufname(originalBuffer))
165   let statusText = system(VCSCommandGetOption('VCSCommandBZRExec', 'bzr') . ' status -S -- "' . fileName . '"')
166   let revision = system(VCSCommandGetOption('VCSCommandBZRExec', 'bzr') . ' revno -- "' . fileName . '"')
167   if(v:shell_error)
168     return []
169   endif
171   " File not under BZR control.
172   if statusText =~ '^?'
173     return ['Unknown']
174   endif
176   let [flags, repository] = matchlist(statusText, '^\(.\{3}\)\s\+\(\S\+\)')[1:2]
177   if revision == ''
178     " Error
179     return ['Unknown']
180   elseif flags =~ '^A'
181     return ['New', 'New']
182   else
183     return [revision, repository]
184   endif
185 endfunction
187 " Function: s:bzrFunctions.Info(argList) {{{2
188 function! s:bzrFunctions.Info(argList)
189   return s:DoCommand(join(['version-info'] + a:argList, ' '), 'version-info', join(a:argList, ' '), {})
190 endfunction
192 " Function: s:bzrFunctions.Lock(argList) {{{2
193 function! s:bzrFunctions.Lock(argList)
194   echomsg 'bzr lock is not necessary'
195 endfunction
197 " Function: s:bzrFunctions.Log() {{{2
198 function! s:bzrFunctions.Log(argList)
199   if len(a:argList) == 0
200     let options = []
201     let caption = ''
202   elseif len(a:argList) <= 2 && match(a:argList, '^-') == -1
203     let options = ['-r' . join(a:argList, ':')]
204     let caption = options[0]
205   else
206     " Pass-through
207     let options = a:argList
208     let caption = join(a:argList, ' ')
209   endif
211   let resultBuffer = s:DoCommand(join(['log', '-v'] + options), 'log', caption, {})
212   return resultBuffer
213 endfunction
215 " Function: s:bzrFunctions.Revert(argList) {{{2
216 function! s:bzrFunctions.Revert(argList)
217   return s:DoCommand('revert', 'revert', '', {})
218 endfunction
220 " Function: s:bzrFunctions.Review(argList) {{{2
221 function! s:bzrFunctions.Review(argList)
222   if len(a:argList) == 0
223     let versiontag = '(current)'
224     let versionOption = ''
225   else
226     let versiontag = a:argList[0]
227     let versionOption = ' -r ' . versiontag . ' '
228   endif
230   let resultBuffer = s:DoCommand('cat' . versionOption, 'review', versiontag, {})
231   if resultBuffer > 0
232     let &filetype=getbufvar(b:VCSCommandOriginalBuffer, '&filetype')
233   endif
234   return resultBuffer
235 endfunction
237 " Function: s:bzrFunctions.Status(argList) {{{2
238 function! s:bzrFunctions.Status(argList)
239   let options = ['-S']
240   if len(a:argList) == 0
241     let options = a:argList
242   endif
243   return s:DoCommand(join(['status'] + options, ' '), 'status', join(options, ' '), {})
244 endfunction
246 " Function: s:bzrFunctions.Unlock(argList) {{{2
247 function! s:bzrFunctions.Unlock(argList)
248   echomsg 'bzr unlock is not necessary'
249 endfunction
250 " Function: s:bzrFunctions.Update(argList) {{{2
251 function! s:bzrFunctions.Update(argList)
252   return s:DoCommand('update', 'update', '', {})
253 endfunction
255 " Annotate setting {{{2
256 let s:bzrFunctions.AnnotateSplitRegex = '^[^|]\+ | '
258 " Section: Plugin Registration {{{1
259 call VCSCommandRegisterModule('BZR', expand('<sfile>'), s:bzrFunctions, [])
261 let &cpo = s:save_cpo