Removed trailing white space in source.
[vcscommand.git] / plugin / vcssvn.vim
blob292a81b5f8338b3985c71fddbcae3eff88ef25c2
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 " Section: Plugin header {{{1
45 if exists('VCSCommandDisableAll')
46         finish
47 endif
49 if v:version < 700
50         echohl WarningMsg|echomsg 'VCSCommand requires at least VIM 7.0'|echohl None
51         finish
52 endif
54 runtime plugin/vcscommand.vim
56 if !executable(VCSCommandGetOption('VCSCommandSVNExec', 'svn'))
57         " SVN is not installed
58         finish
59 endif
61 let s:save_cpo=&cpo
62 set cpo&vim
64 " Section: Variable initialization {{{1
66 let s:svnFunctions = {}
68 " Section: Utility functions {{{1
70 " Function: s:DoCommand(cmd, cmdName, statusText, options) {{{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, options)
74         if VCSCommandGetVCSType(expand('%')) == 'SVN'
75                 let fullCmd = VCSCommandGetOption('VCSCommandSVNExec', 'svn') . ' ' . a:cmd
76                 return VCSCommandDoCommand(fullCmd, a:cmdName, a:statusText, a:options)
77         else
78                 throw 'SVN VCSCommand plugin called on non-SVN item.'
79         endif
80 endfunction
82 " Section: VCS function implementations {{{1
84 " Function: s:svnFunctions.Identify(buffer) {{{2
85 function! s:svnFunctions.Identify(buffer)
86         let fileName = resolve(bufname(a:buffer))
87         if isdirectory(fileName)
88                 let directoryName = fileName
89         else
90                 let directoryName = fnamemodify(fileName, ':h')
91         endif
92         if strlen(directoryName) > 0
93                 let svnDir = directoryName . '/.svn'
94         else
95                 let svnDir = '.svn'
96         endif
97         if isdirectory(svnDir)
98                 return 1
99         else
100                 return 0
101         endif
102 endfunction
104 " Function: s:svnFunctions.Add() {{{2
105 function! s:svnFunctions.Add(argList)
106         return s:DoCommand(join(['add'] + a:argList, ' '), 'add', join(a:argList, ' '), {})
107 endfunction
109 " Function: s:svnFunctions.Annotate(argList) {{{2
110 function! s:svnFunctions.Annotate(argList)
111         if len(a:argList) == 0
112                 if &filetype == 'SVNAnnotate'
113                         " Perform annotation of the version indicated by the current line.
114                         let caption = matchstr(getline('.'),'\v^\s+\zs\d+')
115                         let options = ' -r' . caption
116                 else
117                         let caption = ''
118                         let options = ''
119                 endif
120         elseif len(a:argList) == 1 && a:argList[0] !~ '^-'
121                 let caption = a:argList[0]
122                 let options = ' -r' . caption
123         else
124                 let caption = join(a:argList, ' ')
125                 let options = ' ' . caption
126         endif
128         let resultBuffer = s:DoCommand('blame --non-interactive' . options, 'annotate', caption, {})
129         if resultBuffer > 0
130                 set filetype=SVNAnnotate
131         endif
132         return resultBuffer
133 endfunction
135 " Function: s:svnFunctions.Commit(argList) {{{2
136 function! s:svnFunctions.Commit(argList)
137         let resultBuffer = s:DoCommand('commit --non-interactive -F "' . a:argList[0] . '"', 'commit', '', {})
138         if resultBuffer == 0
139                 echomsg 'No commit needed.'
140         endif
141 endfunction
143 " Function: s:svnFunctions.Delete() {{{2
144 function! s:svnFunctions.Delete(argList)
145         return s:DoCommand(join(['delete --non-interactive'] + a:argList, ' '), 'delete', join(a:argList, ' '), {})
146 endfunction
148 " Function: s:svnFunctions.Diff(argList) {{{2
149 function! s:svnFunctions.Diff(argList)
150         if len(a:argList) == 0
151                 let revOptions = []
152                 let caption = ''
153         elseif len(a:argList) <= 2 && match(a:argList, '^-') == -1
154                 let revOptions = ['-r' . join(a:argList, ':')]
155                 let caption = '(' . a:argList[0] . ' : ' . get(a:argList, 1, 'current') . ')'
156         else
157                 " Pass-through
158                 let caption = join(a:argList, ' ')
159                 let revOptions = a:argList
160         endif
162         let svnDiffExt = VCSCommandGetOption('VCSCommandSVNDiffExt', '')
163         if svnDiffExt == ''
164                 let diffExt = []
165         else
166                 let diffExt = ['--diff-cmd ' . svnDiffExt]
167         endif
169         let svnDiffOpt = VCSCommandGetOption('VCSCommandSVNDiffOpt', '')
170         if svnDiffOpt == ''
171                 let diffOptions = []
172         else
173                 let diffOptions = ['-x -' . svnDiffOpt]
174         endif
176         let resultBuffer = s:DoCommand(join(['diff --non-interactive'] + diffExt + diffOptions + revOptions), 'diff', caption, {})
177         if resultBuffer > 0
178                 set filetype=diff
179         else
180                 if svnDiffExt == ''
181                         echomsg 'No differences found'
182                 endif
183         endif
184         return resultBuffer
185 endfunction
187 " Function: s:svnFunctions.GetBufferInfo() {{{2
188 " Provides version control details for the current file.  Current version
189 " number and current repository version number are required to be returned by
190 " the vcscommand plugin.
191 " Returns: List of results:  [revision, repository, branch]
193 function! s:svnFunctions.GetBufferInfo()
194         let originalBuffer = VCSCommandGetOriginalBuffer(bufnr('%'))
195         let fileName = bufname(originalBuffer)
196         let statusText = system(VCSCommandGetOption('VCSCommandSVNExec', 'svn') . ' status --non-interactive -vu "' . fileName . '"')
197         if(v:shell_error)
198                 return []
199         endif
201         " File not under SVN control.
202         if statusText =~ '^?'
203                 return ['Unknown']
204         endif
206         let [flags, revision, repository] = matchlist(statusText, '^\(.\{8}\)\s\+\(\S\+\)\s\+\(\S\+\)\s\+\(\S\+\)\s')[1:3]
207         if revision == ''
208                 " Error
209                 return ['Unknown']
210         elseif flags =~ '^A'
211                 return ['New', 'New']
212         else
213                 return [revision, repository]
214         endif
215 endfunction
217 " Function: s:svnFunctions.Info(argList) {{{2
218 function! s:svnFunctions.Info(argList)
219         return s:DoCommand(join(['info --non-interactive'] + a:argList, ' '), 'info', join(a:argList, ' '), {})
220 endfunction
222 " Function: s:svnFunctions.Lock(argList) {{{2
223 function! s:svnFunctions.Lock(argList)
224         return s:DoCommand(join(['lock --non-interactive'] + a:argList, ' '), 'lock', join(a:argList, ' '), {})
225 endfunction
227 " Function: s:svnFunctions.Log(argList) {{{2
228 function! s:svnFunctions.Log(argList)
229         if len(a:argList) == 0
230                 let options = []
231                 let caption = ''
232         elseif len(a:argList) <= 2 && match(a:argList, '^-') == -1
233                 let options = ['-r' . join(a:argList, ':')]
234                 let caption = options[0]
235         else
236                 " Pass-through
237                 let options = a:argList
238                 let caption = join(a:argList, ' ')
239         endif
241         let resultBuffer = s:DoCommand(join(['log --non-interactive', '-v'] + options), 'log', caption, {})
242         return resultBuffer
243 endfunction
245 " Function: s:svnFunctions.Revert(argList) {{{2
246 function! s:svnFunctions.Revert(argList)
247         return s:DoCommand('revert', 'revert', '', {})
248 endfunction
250 " Function: s:svnFunctions.Review(argList) {{{2
251 function! s:svnFunctions.Review(argList)
252         if len(a:argList) == 0
253                 let versiontag = '(current)'
254                 let versionOption = ''
255         else
256                 let versiontag = a:argList[0]
257                 let versionOption = ' -r ' . versiontag . ' '
258         endif
260         let resultBuffer = s:DoCommand('cat --non-interactive' . versionOption, 'review', versiontag, {})
261         if resultBuffer > 0
262                 let &filetype = getbufvar(b:VCSCommandOriginalBuffer, '&filetype')
263         endif
264         return resultBuffer
265 endfunction
267 " Function: s:svnFunctions.Status(argList) {{{2
268 function! s:svnFunctions.Status(argList)
269         let options = ['-u', '-v']
270         if len(a:argList) == 0
271                 let options = a:argList
272         endif
273         return s:DoCommand(join(['status --non-interactive'] + options, ' '), 'status', join(options, ' '), {})
274 endfunction
276 " Function: s:svnFunctions.Unlock(argList) {{{2
277 function! s:svnFunctions.Unlock(argList)
278         return s:DoCommand(join(['unlock --non-interactive'] + a:argList, ' '), 'unlock', join(a:argList, ' '), {})
279 endfunction
281 " Function: s:svnFunctions.Update(argList) {{{2
282 function! s:svnFunctions.Update(argList)
283         return s:DoCommand('update --non-interactive', 'update', '', {})
284 endfunction
286 " Annotate setting {{{2
287 let s:svnFunctions.AnnotateSplitRegex = '\s\+\S\+\s\+\S\+ '
289 " Section: Plugin Registration {{{1
290 call VCSCommandRegisterModule('SVN', expand('<sfile>'), s:svnFunctions, [])
292 let &cpo = s:save_cpo