handle ignored files in GetBufferInfo
[vcscommand.git] / plugin / vcssvn.vim
blobeabe92e069d103d8897686dd9b9ac17d10c3ef15
1 " vim600: set foldmethod=marker:
3 " SVN extension for VCSCommand.
5 " Maintainer:    Bob Hiestand <bob.hiestand@gmail.com>
6 " License:
7 " Copyright (c) Bob Hiestand
9 " Permission is hereby granted, free of charge, to any person obtaining a copy
10 " of this software and associated documentation files (the "Software"), to
11 " deal in the Software without restriction, including without limitation the
12 " rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
13 " sell copies of the Software, and to permit persons to whom the Software is
14 " furnished to do so, subject to the following conditions:
16 " The above copyright notice and this permission notice shall be included in
17 " all copies or substantial portions of the Software.
19 " THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 " IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 " FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 " AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 " LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 " FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25 " IN THE SOFTWARE.
27 " Section: Documentation {{{1
29 " Options documentation: {{{2
31 " VCSCommandSVNExec
32 "   This variable specifies the SVN executable.  If not set, it defaults to
33 "   'svn' executed from the user's executable path.
35 " VCSCommandSVNDiffExt
36 "   This variable, if set, sets the external diff program used by Subversion.
38 " VCSCommandSVNDiffOpt
39 "   This variable, if set, determines the options passed to the svn diff
40 "   command (such as 'u', 'w', or 'b').
42 " Section: Plugin header {{{1
44 if exists('VCSCommandDisableAll')
45         finish
46 endif
48 if v:version < 700
49         echohl WarningMsg|echomsg 'VCSCommand requires at least VIM 7.0'|echohl None
50         finish
51 endif
53 if !exists('g:loaded_VCSCommand')
54         runtime plugin/vcscommand.vim
55 endif
57 if !executable(VCSCommandGetOption('VCSCommandSVNExec', 'svn'))
58         " SVN is not installed
59         finish
60 endif
62 let s:save_cpo=&cpo
63 set cpo&vim
65 " Section: Variable initialization {{{1
67 let s:svnFunctions = {}
69 " Section: Utility functions {{{1
71 " Function: s:Executable() {{{2
72 " Returns the executable used to invoke git suitable for use in a shell
73 " command.
74 function! s:Executable()
75         return shellescape(VCSCommandGetOption('VCSCommandSVNExec', 'svn'))
76 endfunction
78 " Function: s:DoCommand(cmd, cmdName, statusText, options) {{{2
79 " Wrapper to VCSCommandDoCommand to add the name of the SVN executable to the
80 " command argument.
81 function! s:DoCommand(cmd, cmdName, statusText, options)
82         if VCSCommandGetVCSType(expand('%')) == 'SVN'
83                 let fullCmd = s:Executable() . ' ' . a:cmd
84                 return VCSCommandDoCommand(fullCmd, a:cmdName, a:statusText, a:options)
85         else
86                 throw 'SVN VCSCommand plugin called on non-SVN item.'
87         endif
88 endfunction
90 " Section: VCS function implementations {{{1
92 " Function: s:svnFunctions.Identify(buffer) {{{2
93 function! s:svnFunctions.Identify(buffer)
94         let oldCwd = VCSCommandChangeToCurrentFileDir(resolve(bufname(a:buffer)))
95         try
96                 call s:VCSCommandUtility.system(s:Executable() . ' info .')
97                 if(v:shell_error)
98                         return 0
99                 else
100                         return g:VCSCOMMAND_IDENTIFY_EXACT
101                 endif
102         finally
103                 call VCSCommandChdir(oldCwd)
104         endtry
105 endfunction
107 " Function: s:svnFunctions.Add() {{{2
108 function! s:svnFunctions.Add(argList)
109         return s:DoCommand(join(['add'] + a:argList, ' '), 'add', join(a:argList, ' '), {})
110 endfunction
112 " Function: s:svnFunctions.Annotate(argList) {{{2
113 function! s:svnFunctions.Annotate(argList)
114         if len(a:argList) == 0
115                 if &filetype ==? 'svnannotate'
116                         " Perform annotation of the version indicated by the current line.
117                         let caption = matchstr(getline('.'),'\v^\s+\zs\d+')
118                         let options = ' -r' . caption
119                 else
120                         let caption = ''
121                         let options = ''
122                 endif
123         elseif len(a:argList) == 1 && a:argList[0] !~ '^-'
124                 let caption = a:argList[0]
125                 let options = ' -r' . caption
126         else
127                 let caption = join(a:argList, ' ')
128                 let options = ' ' . caption
129         endif
131         return s:DoCommand('blame --non-interactive' . options, 'annotate', caption, {})
132 endfunction
134 " Function: s:svnFunctions.Commit(argList) {{{2
135 function! s:svnFunctions.Commit(argList)
136         let resultBuffer = s:DoCommand('commit --non-interactive -F "' . a:argList[0] . '"', 'commit', '', {})
137         if resultBuffer == 0
138                 echomsg 'No commit needed.'
139         endif
140 endfunction
142 " Function: s:svnFunctions.Delete() {{{2
143 function! s:svnFunctions.Delete(argList)
144         return s:DoCommand(join(['delete --non-interactive'] + a:argList, ' '), 'delete', join(a:argList, ' '), {})
145 endfunction
147 " Function: s:svnFunctions.Diff(argList) {{{2
148 function! s:svnFunctions.Diff(argList)
149         if len(a:argList) == 0
150                 let revOptions = []
151                 let caption = ''
152         elseif len(a:argList) <= 2 && match(a:argList, '^-') == -1
153                 let revOptions = ['-r' . join(a:argList, ':')]
154                 let caption = '(' . a:argList[0] . ' : ' . get(a:argList, 1, 'current') . ')'
155         else
156                 " Pass-through
157                 let caption = join(a:argList, ' ')
158                 let revOptions = a:argList
159         endif
161         let svnDiffExt = VCSCommandGetOption('VCSCommandSVNDiffExt', '')
162         if svnDiffExt == ''
163                 let diffExt = []
164         else
165                 let diffExt = ['--diff-cmd ' . svnDiffExt]
166         endif
168         let svnDiffOpt = VCSCommandGetOption('VCSCommandSVNDiffOpt', '')
169         if svnDiffOpt == ''
170                 let diffOptions = []
171         else
172                 let diffOptions = ['-x -' . svnDiffOpt]
173         endif
175         return s:DoCommand(join(['diff --non-interactive'] + diffExt + diffOptions + revOptions), 'diff', caption, {})
176 endfunction
178 " Function: s:svnFunctions.GetBufferInfo() {{{2
179 " Provides version control details for the current file.  Current version
180 " number and current repository version number are required to be returned by
181 " the vcscommand plugin.
182 " Returns: List of results:  [revision, repository, branch]
184 function! s:svnFunctions.GetBufferInfo()
185         let originalBuffer = VCSCommandGetOriginalBuffer(bufnr('%'))
186         let fileName = bufname(originalBuffer)
187         let statusText = s:VCSCommandUtility.system(s:Executable() . ' status --non-interactive -v -- "' . fileName . '"')
188         if(v:shell_error)
189                 return []
190         endif
192         " File not under SVN control.
193         if statusText =~ '^?'
194                 return ['Unknown']
195         endif
196         " File explicitly ignored by SVN.
197         if statusText =~ '^I'
198                 return ['Ignored']
199         endif
201         let [flags, revision, repository] = matchlist(statusText, '^\(.\{9}\)\s*\(\d\+\)\s\+\(\d\+\)')[1:3]
202         if revision == ''
203                 " Error
204                 return ['Unknown']
205         elseif flags =~ '^A'
206                 return ['New', 'New']
207         elseif flags =~ '*'
208                 return [revision, repository, '*']
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 --non-interactive'] + 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 --non-interactive'] + 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 options = []
228                 let caption = ''
229         elseif len(a:argList) <= 2 && match(a:argList, '^-') == -1
230                 let options = ['-r' . join(a:argList, ':')]
231                 let caption = options[0]
232         else
233                 " Pass-through
234                 let options = a:argList
235                 let caption = join(a:argList, ' ')
236         endif
238         let resultBuffer = s:DoCommand(join(['log --non-interactive', '-v'] + options), '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         return s:DoCommand('cat --non-interactive' . versionOption, 'review', versiontag, {})
258 endfunction
260 " Function: s:svnFunctions.Status(argList) {{{2
261 function! s:svnFunctions.Status(argList)
262         let options = ['-u', '-v']
263         if len(a:argList) != 0
264                 let options = a:argList
265         endif
266         return s:DoCommand(join(['status --non-interactive'] + options, ' '), 'status', join(options, ' '), {})
267 endfunction
269 " Function: s:svnFunctions.Unlock(argList) {{{2
270 function! s:svnFunctions.Unlock(argList)
271         return s:DoCommand(join(['unlock --non-interactive'] + a:argList, ' '), 'unlock', join(a:argList, ' '), {})
272 endfunction
274 " Function: s:svnFunctions.Update(argList) {{{2
275 function! s:svnFunctions.Update(argList)
276         return s:DoCommand('update --non-interactive', 'update', '', {})
277 endfunction
279 " Annotate setting {{{2
280 let s:svnFunctions.AnnotateSplitRegex = '\s\+\S\+\s\+\S\+ '
282 " Section: Plugin Registration {{{1
283 let s:VCSCommandUtility = VCSCommandRegisterModule('SVN', expand('<sfile>'), s:svnFunctions, [])
285 let &cpo = s:save_cpo