r8@localhost: bob | 2007-02-08 11:02:13 -0600
[vcscommand.git] / plugin / vcssvn.vim
blob655cfe7e3c3dbcae54175f7099b0606f439b1c31
1 " vim600: set foldmethod=marker:
3 " SVN extension for VCSCommand.
5 " Version:       VCS development
6 " Maintainer:    Bob Hiestand <bob.hiestand@gmail.com>
7 " License:
8 " Copyright (c) 2007 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 " Command documentation {{{2
32 " The following command only applies to files under SVN source control.
34 " SVNInfo          Performs "svn info" on the current file.
36 " Mapping documentation: {{{2
38 " By default, a mapping is defined for each command.  User-provided mappings
39 " can be used instead by mapping to <Plug>CommandName, for instance:
41 " nnoremap ,si <Plug>SVNInfo
43 " The default mappings are as follow:
45 "   <Leader>si SVNInfo
47 " Options documentation: {{{2
49 " VCSCommandSVNExec
50 "   This variable specifies the SVN executable.  If not set, it defaults to
51 "   'svn' executed from the user's executable path.
53 " VCSCommandSVNDiffExt
54 "   This variable, if set, sets the external diff program used by Subversion.
56 " VCSCommandSVNDiffOpt
57 "   This variable, if set, determines the options passed to the svn diff
58 "   command (such as 'u', 'w', or 'b').
60 if v:version < 700
61   finish
62 endif
64 " Section: Variable initialization {{{1
66 let s:svnFunctions = {}
68 " Section: Utility functions {{{1
70 " Function: s:DoCommand(cmd, cmdName, statusText) {{{2
71 " Wrapper to VCSCommandDoCommand to add the name of the SVN executable to the
72 " command argument.
73 function! s:DoCommand(cmd, cmdName, statusText)
74   try
75     if VCSCommandGetVCSType(expand('%')) == 'SVN'
76       let fullCmd = VCSCommandGetOption('VCSCommandSVNExec', 'svn') . ' ' . a:cmd
77       return VCSCommandDoCommand(fullCmd, a:cmdName, a:statusText)
78     else
79       throw 'No suitable plugin'
80     endif
81   catch /No suitable plugin/
82     echohl WarningMsg|echomsg 'Cannot apply SVN commands to this file.'|echohl None
83   endtry
84 endfunction
86 " Section: VCS function implementations {{{1
88 " Function: s:svnFunctions.Identify(buffer) {{{2
89 function! s:svnFunctions.Identify(buffer)
90   let fileName = resolve(bufname(a:buffer))
91   if isdirectory(fileName)
92     let directory = fileName
93   else
94     let directory = fnamemodify(fileName, ':h')
95   endif
96   if strlen(directory) > 0
97     let svnDir = directory . '/.svn'
98   else
99     let svnDir = '.svn'
100   endif
101   if isdirectory(svnDir)
102     return 1
103   else
104     return 0
105   endif
106 endfunction
108 " Function: s:svnFunctions.Add() {{{2
109 function! s:svnFunctions.Add(argList)
110   return s:DoCommand(join(['add'] + a:argList, ' '), 'add', join(a:argList, ' '))
111 endfunction
113 " Function: s:svnFunctions.Annotate(argList) {{{2
114 function! s:svnFunctions.Annotate(argList)
115   if len(a:argList) == 0
116     if &filetype == 'SVNAnnotate'
117       " Perform annotation of the version indicated by the current line.
118       let revision = matchstr(getline('.'),'\v^\s+\zs\d+')
119     else
120       let revision=VCSCommandGetRevision()
121       if revision == ''
122         throw 'Unable to obtain version information.'
123       elseif revision == 'Unknown'
124         throw 'File not under source control'
125       elseif revision == 'New'
126         throw 'No annotatation available for new file.'
127       endif
128     endif
129   else
130     let revision=a:argList[0]
131   endif
133   let resultBuffer=s:DoCommand('blame -r' . revision, 'annotate', revision) 
134   if resultBuffer > 0
135     set filetype=SVNAnnotate
136   endif
137   return resultBuffer
138 endfunction
140 " Function: s:svnFunctions.Commit(argList) {{{2
141 function! s:svnFunctions.Commit(argList)
142   let resultBuffer = s:DoCommand('commit -F "' . a:argList[0] . '"', 'commit', '')
143   if resultBuffer == 0
144     echomsg 'No commit needed.'
145   endif
146 endfunction
148 " Function: s:svnFunctions.Delete() {{{2
149 function! s:svnFunctions.Delete(argList)
150   return s:DoCommand(join(['delete'] + a:argList, ' '), 'delete', join(a:argList, ' '))
151 endfunction
153 " Function: s:svnFunctions.Diff(argList) {{{2
154 function! s:svnFunctions.Diff(argList)
155   if len(a:argList) == 1
156     let revOptions = ' -r' . a:argList[0]
157     let caption = '(' . a:argList[0] . ' : current)'
158   elseif len(a:argList) == 2
159     let revOptions = ' -r' . a:argList[0] . ':' . a:argList[1]
160     let caption = '(' . a:argList[0] . ' : ' . a:argList[1] . ')'
161   else
162     let revOptions = ''
163     let caption = ''
164   endif
166   let svndiffext = VCSCommandGetOption('VCSCommandSVNDiffExt', '')
167   if svndiffext == ''
168     let diffextstring = ''
169   else
170     let diffextstring = ' --diff-cmd ' . svndiffext . ' '
171   endif
173   let svndiffopt = VCSCommandGetOption('VCSCommandSVNDiffOpt', '')
175   if svndiffopt == ''
176     let diffoptionstring = ''
177   else
178     let diffoptionstring = ' -x -' . svndiffopt . ' '
179   endif
181   let resultBuffer = s:DoCommand('diff' . diffextstring . diffoptionstring . revOptions , 'diff', caption)
182   if resultBuffer > 0
183     set filetype=diff
184   else
185     if svndiffext == ''
186       echomsg 'No differences found'
187     endif
188   endif
189   return resultBuffer
190 endfunction
192 " Function: s:svnFunctions.GetBufferInfo() {{{2
193 " Provides version control details for the current file.  Current version
194 " number and current repository version number are required to be returned by
195 " the vcscommand plugin.
196 " Returns: List of results:  [revision, repository, branch]
198 function! s:svnFunctions.GetBufferInfo()
199   let originalBuffer=VCSCommandGetOriginalBuffer(bufnr('%'))
200   let fileName=bufname(originalBuffer)
201   let realFileName = fnamemodify(resolve(fileName), ':t')
202   if !filereadable(fileName)
203     return ['Unknown']
204   endif
205   let oldCwd=VCSCommandChangeToCurrentFileDir(fileName)
206   try
207     let statusText=system(VCSCommandGetOption('VCSCommandSVNExec', 'svn') . ' status -vu "' . realFileName . '"')
208     if(v:shell_error)
209       return []
210     endif
212     " File not under SVN control.
213     if statusText =~ '^?'
214       return ['Unknown']
215     endif
217     let [flags, revision, repository] = matchlist(statusText, '^\(.\{8}\)\s\+\(\S\+\)\s\+\(\S\+\)\s\+\(\S\+\)\s')[1:3]
218     if revision == ''
219       " Error
220       return ['Unknown']
221     elseif flags =~ '^A'
222       return ['New', 'New']
223     else
224       return [revision, repository]
225     endif
226   finally
227     execute 'cd' escape(oldCwd, ' ')
228   endtry
229 endfunction
231 " Function: s:svnFunctions.Lock(argList) {{{2
232 function! s:svnFunctions.Lock(argList)
233   return s:DoCommand(join(['lock'] + a:argList, ' '), 'lock', join(a:argList, ' '))
234 endfunction
236 " Function: s:svnFunctions.Log() {{{2
237 function! s:svnFunctions.Log(argList)
238   if len(a:argList) == 0
239     let versionOption = ''
240     let caption = ''
241   elseif len(a:argList) == 1 && a:argList[0] !~ "^-"
242     let versionOption=' -r' . a:argList[0]
243     let caption = a:argList[0]
244   else
245     " Multiple options, or the option starts with '-'
246     let caption = join(a:argList, ' ')
247     let versionOption = ' ' . caption
248   endif
250   let resultBuffer=s:DoCommand('log -v' . versionOption, 'log', caption)
251   return resultBuffer
252 endfunction
254 " Function: s:svnFunctions.Revert(argList) {{{2
255 function! s:svnFunctions.Revert(argList)
256   return s:DoCommand('revert', 'revert', '')
257 endfunction
259 " Function: s:svnFunctions.Review(argList) {{{2
260 function! s:svnFunctions.Review(argList)
261   if len(a:argList) == 0
262     let versiontag = '(current)'
263     let versionOption = ''
264   else
265     let versiontag = a:argList[0]
266     let versionOption = ' -r ' . versiontag . ' '
267   endif
269   let resultBuffer = s:DoCommand('cat' . versionOption, 'review', versiontag)
270   if resultBuffer > 0
271     let &filetype=getbufvar(b:VCSCommandOriginalBuffer, '&filetype')
272   endif
273   return resultBuffer
274 endfunction
276 " Function: s:svnFunctions.Status(argList) {{{2
277 function! s:svnFunctions.Status(argList)
278   if len(a:argList) == 0
279     let a:argList = ['-u', '-v']
280   endif
281   return s:DoCommand(join(['status -u -v'] + a:argList, ' '), 'status', join(a:argList, ' '))
282 endfunction
284 " Function: s:svnFunctions.Unlock(argList) {{{2
285 function! s:svnFunctions.Unlock(argList)
286   return s:DoCommand(join(['unlock'] + a:argList, ' '), 'unlock', join(a:argList, ' '))
287 endfunction
288 " Function: s:svnFunctions.Update(argList) {{{2
289 function! s:svnFunctions.Update(argList)
290   return s:DoCommand('update', 'update', '')
291 endfunction
293 " Section: SVN-specific functions {{{1
295 " Function: s:SVNInfo() {{{2
296 function! s:SVNInfo(argList)
297   return s:DoCommand(join(['info'] + a:argList, ' '), 'svninfo', join(a:argList, ' '))
298 endfunction
300 " Section: Command definitions {{{1
301 " Section: Primary commands {{{2
302 com! -nargs=* SVNInfo call s:SVNInfo([<f-args>])
304 " Section: Plugin command mappings {{{1
306 let s:svnExtensionMappings = {}
307 let mappingInfo = [['SVNInfo', 'SVNInfo', 'ci']]
308 for [pluginName, commandText, shortCut] in mappingInfo
309   execute 'nnoremap <silent> <Plug>' . pluginName . ' :' . commandText . '<CR>'
310   if !hasmapto('<Plug>' . pluginName)
311     let s:svnExtensionMappings[shortCut] = commandText
312   endif
313 endfor
315 " Section: Menu items {{{1
316 amenu <silent> &Plugin.VCS.SVN.&Info       <Plug>SVNInfo
318 " Section: Plugin Registration {{{1
319 " If the vcscommand.vim plugin hasn't loaded, delay registration until it
320 " loads.
321 if exists('g:loaded_VCSCommand')
322   call VCSCommandRegisterModule('SVN', expand('<sfile>'), s:svnFunctions, s:svnExtensionMappings)
323 else
324   augroup VCSCommand
325     au User VCSLoadExtensions call VCSCommandRegisterModule('SVN', expand('<sfile>'), s:svnFunctions, s:svnExtensionMappings)
326   augroup END
327 endif