Merged /tags/beta14@38 /branches/release/beta15@43.
[vcscommand.git] / plugin / vcssvn.vim
blob3c202b4f9edd50ca29e133b529c862920345d03b
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 " Options documentation: {{{2
32 " VCSCommandSVNExec
33 "   This variable specifies the SVN executable.  If not set, it defaults to
34 "   'svn' executed from the user's executable path.
36 " VCSCommandSVNDiffExt
37 "   This variable, if set, sets the external diff program used by Subversion.
39 " VCSCommandSVNDiffOpt
40 "   This variable, if set, determines the options passed to the svn diff
41 "   command (such as 'u', 'w', or 'b').
43 if v:version < 700
44   finish
45 endif
47 runtime plugin/vcscommand.vim
49 call system(VCSCommandGetOption('VCSCommandSVNExec', 'svn') . ' --version')
50 if v:shell_error
51   " SVN is not installed
52   finish
53 endif
55 " Section: Variable initialization {{{1
57 let s:svnFunctions = {}
59 " Section: Utility functions {{{1
61 " Function: s:DoCommand(cmd, cmdName, statusText) {{{2
62 " Wrapper to VCSCommandDoCommand to add the name of the SVN executable to the
63 " command argument.
64 function! s:DoCommand(cmd, cmdName, statusText)
65   if VCSCommandGetVCSType(expand('%')) == 'SVN'
66     let fullCmd = VCSCommandGetOption('VCSCommandSVNExec', 'svn') . ' ' . a:cmd
67     return VCSCommandDoCommand(fullCmd, a:cmdName, a:statusText)
68   else
69     throw 'SVN VCSCommand plugin called on non-SVN item.'
70   endif
71 endfunction
73 " Section: VCS function implementations {{{1
75 " Function: s:svnFunctions.Identify(buffer) {{{2
76 function! s:svnFunctions.Identify(buffer)
77   let fileName = resolve(bufname(a:buffer))
78   if isdirectory(fileName)
79     let directoryName = fileName
80   else
81     let directoryName = fnamemodify(fileName, ':h')
82   endif
83   if strlen(directoryName) > 0
84     let svnDir = directoryName . '/.svn'
85   else
86     let svnDir = '.svn'
87   endif
88   if isdirectory(svnDir)
89     return 1
90   else
91     return 0
92   endif
93 endfunction
95 " Function: s:svnFunctions.Add() {{{2
96 function! s:svnFunctions.Add(argList)
97   return s:DoCommand(join(['add'] + a:argList, ' '), 'add', join(a:argList, ' '))
98 endfunction
100 " Function: s:svnFunctions.Annotate(argList) {{{2
101 function! s:svnFunctions.Annotate(argList)
102   if len(a:argList) == 0
103     if &filetype == 'SVNAnnotate'
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     set filetype=SVNAnnotate
122   endif
123   return resultBuffer
124 endfunction
126 " Function: s:svnFunctions.Commit(argList) {{{2
127 function! s:svnFunctions.Commit(argList)
128   let resultBuffer = s:DoCommand('commit -F "' . a:argList[0] . '"', 'commit', '')
129   if resultBuffer == 0
130     echomsg 'No commit needed.'
131   endif
132 endfunction
134 " Function: s:svnFunctions.Delete() {{{2
135 function! s:svnFunctions.Delete(argList)
136   return s:DoCommand(join(['delete'] + a:argList, ' '), 'delete', join(a:argList, ' '))
137 endfunction
139 " Function: s:svnFunctions.Diff(argList) {{{2
140 function! s:svnFunctions.Diff(argList)
141   if len(a:argList) == 0
142     let revOptions = [] 
143     let caption = ''
144   elseif len(a:argList) <= 2 && a:argList[0] !~ '^-'
145     if len(a:argList) == 1
146       let revOptions = ['-r' . a:argList[0]]
147       let caption = '(' . a:argList[0] . ' : current)'
148     elseif len(a:argList) == 2
149       let revOptions = ['-r' . a:argList[0] . ':' . a:argList[1]]
150       let caption = '(' . a:argList[0] . ' : ' . a:argList[1] . ')'
151     endif
152   else
153     " Pass-through
154     let caption = join(a:argList, ' ')
155     let revOptions = a:argList
156   endif
158   let svnDiffExt = VCSCommandGetOption('VCSCommandSVNDiffExt', '')
159   if svnDiffExt == ''
160     let diffExt = []
161   else
162     let diffExt = ['--diff-cmd ' . svnDiffExt]
163   endif
165   let svnDiffOpt = VCSCommandGetOption('VCSCommandSVNDiffOpt', '')
167   if svnDiffOpt == ''
168     let diffOptions = []
169   else
170     let diffOptions = ['-x -' . svnDiffOpt]
171   endif
173   let resultBuffer = s:DoCommand(join(['diff'] + diffExt + diffOptions + revOptions), 'diff', caption)
174   if resultBuffer > 0
175     set filetype=diff
176   else
177     if svnDiffExt == ''
178       echomsg 'No differences found'
179     endif
180   endif
181   return resultBuffer
182 endfunction
184 " Function: s:svnFunctions.GetBufferInfo() {{{2
185 " Provides version control details for the current file.  Current version
186 " number and current repository version number are required to be returned by
187 " the vcscommand plugin.
188 " Returns: List of results:  [revision, repository, branch]
190 function! s:svnFunctions.GetBufferInfo()
191   let originalBuffer = VCSCommandGetOriginalBuffer(bufnr('%'))
192   let fileName = bufname(originalBuffer)
193   let statusText = system(VCSCommandGetOption('VCSCommandSVNExec', 'svn') . ' status -vu "' . fileName . '"')
194   if(v:shell_error)
195     return []
196   endif
198   " File not under SVN control.
199   if statusText =~ '^?'
200     return ['Unknown']
201   endif
203   let [flags, revision, repository] = matchlist(statusText, '^\(.\{8}\)\s\+\(\S\+\)\s\+\(\S\+\)\s\+\(\S\+\)\s')[1:3]
204   if revision == ''
205     " Error
206     return ['Unknown']
207   elseif flags =~ '^A'
208     return ['New', 'New']
209   else
210     return [revision, repository]
211   endif
212 endfunction
214 " Function: s:svnFunctions.Info(argList) {{{2
215 function! s:svnFunctions.Info(argList)
216   return s:DoCommand(join(['info'] + a:argList, ' '), 'info', join(a:argList, ' '))
217 endfunction
219 " Function: s:svnFunctions.Lock(argList) {{{2
220 function! s:svnFunctions.Lock(argList)
221   return s:DoCommand(join(['lock'] + a:argList, ' '), 'lock', join(a:argList, ' '))
222 endfunction
224 " Function: s:svnFunctions.Log(argList) {{{2
225 function! s:svnFunctions.Log(argList)
226   if len(a:argList) == 0
227     let versionOption = ''
228     let caption = ''
229   elseif len(a:argList) == 1 && a:argList[0] !~ "^-"
230     let versionOption = ' -r' . a:argList[0]
231     let caption = a:argList[0]
232   else
233     " Multiple options, or the option starts with '-'
234     let caption = join(a:argList, ' ')
235     let versionOption = ' ' . caption
236   endif
238   let resultBuffer = s:DoCommand('log -v' . versionOption, 'log', caption)
239   return resultBuffer
240 endfunction
242 " Function: s:svnFunctions.Revert(argList) {{{2
243 function! s:svnFunctions.Revert(argList)
244   return s:DoCommand('revert', 'revert', '')
245 endfunction
247 " Function: s:svnFunctions.Review(argList) {{{2
248 function! s:svnFunctions.Review(argList)
249   if len(a:argList) == 0
250     let versiontag = '(current)'
251     let versionOption = ''
252   else
253     let versiontag = a:argList[0]
254     let versionOption = ' -r ' . versiontag . ' '
255   endif
257   let resultBuffer = s:DoCommand('cat' . versionOption, 'review', versiontag)
258   if resultBuffer > 0
259     let &filetype = getbufvar(b:VCSCommandOriginalBuffer, '&filetype')
260   endif
261   return resultBuffer
262 endfunction
264 " Function: s:svnFunctions.Status(argList) {{{2
265 function! s:svnFunctions.Status(argList)
266   let options = ['-u', '-v']
267   if len(a:argList) == 0
268     let options = a:argList
269   endif
270   return s:DoCommand(join(['status'] + options, ' '), 'status', join(options, ' '))
271 endfunction
273 " Function: s:svnFunctions.Unlock(argList) {{{2
274 function! s:svnFunctions.Unlock(argList)
275   return s:DoCommand(join(['unlock'] + a:argList, ' '), 'unlock', join(a:argList, ' '))
276 endfunction
277 " Function: s:svnFunctions.Update(argList) {{{2
278 function! s:svnFunctions.Update(argList)
279   return s:DoCommand('update', 'update', '')
280 endfunction
282 " Section: Plugin Registration {{{1
283 call VCSCommandRegisterModule('SVN', expand('<sfile>'), s:svnFunctions, [])