Replace system() with wrapper to protect quotes in Windows.
[vcscommand.git] / plugin / vcshg.vim
blob6195deb0aa1f2ad50cc6f7cc5daea76a0444bfc9
1 " vim600: set foldmethod=marker:
3 " Mercurial 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 " VCSCommandHGExec
33 "   This variable specifies the mercurial executable.  If not set, it defaults
34 "   to 'hg' executed from the user's executable path.
36 " VCSCommandHGDiffExt
37 "   This variable, if set, sets the external diff program used by Subversion.
39 " VCSCommandHGDiffOpt
40 "   This variable, if set, determines the options passed to the hg 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('VCSCommandHGExec', 'hg'))
57         " HG 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:hgFunctions = {}
68 " Section: Utility functions {{{1
70 " Function: s:Executable() {{{2
71 " Returns the executable used to invoke hg suitable for use in a shell
72 " command.
73 function! s:Executable()
74         return shellescape(VCSCommandGetOption('VCSCommandHGExec', 'hg'))
75 endfunction
77 " Function: s:DoCommand(cmd, cmdName, statusText, options) {{{2
78 " Wrapper to VCSCommandDoCommand to add the name of the HG executable to the
79 " command argument.
80 function! s:DoCommand(cmd, cmdName, statusText, options)
81         if VCSCommandGetVCSType(expand('%')) == 'HG'
82                 let fullCmd = s:Executable() . ' ' . a:cmd
83                 return VCSCommandDoCommand(fullCmd, a:cmdName, a:statusText, a:options)
84         else
85                 throw 'HG VCSCommand plugin called on non-HG item.'
86         endif
87 endfunction
89 " Section: VCS function implementations {{{1
91 " Function: s:hgFunctions.Identify(buffer) {{{2
92 function! s:hgFunctions.Identify(buffer)
93         call s:VCSCommandUtility.system(s:Executable() . ' root')
94         if(v:shell_error)
95                 return 0
96         else
97                 return g:VCSCOMMAND_IDENTIFY_INEXACT
98         endif
99 endfunction
101 " Function: s:hgFunctions.Add() {{{2
102 function! s:hgFunctions.Add(argList)
103         return s:DoCommand(join(['add'] + a:argList, ' '), 'add', join(a:argList, ' '), {})
104 endfunction
106 " Function: s:hgFunctions.Annotate(argList) {{{2
107 function! s:hgFunctions.Annotate(argList)
108         if len(a:argList) == 0
109                 if &filetype == 'HGAnnotate'
110                         " Perform annotation of the version indicated by the current line.
111                         let caption = matchstr(getline('.'),'\v^\s+\zs\d+')
112                         let options = ' -r' . caption
113                 else
114                         let caption = ''
115                         let options = ''
116                 endif
117         elseif len(a:argList) == 1 && a:argList[0] !~ '^-'
118                 let caption = a:argList[0]
119                 let options = ' -r' . caption
120         else
121                 let caption = join(a:argList, ' ')
122                 let options = ' ' . caption
123         endif
125         let resultBuffer = s:DoCommand('blame' . options, 'annotate', caption, {})
126         if resultBuffer > 0
127                 set filetype=HGAnnotate
128         endif
129         return resultBuffer
130 endfunction
132 " Function: s:hgFunctions.Commit(argList) {{{2
133 function! s:hgFunctions.Commit(argList)
134         let resultBuffer = s:DoCommand('commit -l "' . a:argList[0] . '"', 'commit', '', {})
135         if resultBuffer == 0
136                 echomsg 'No commit needed.'
137         endif
138 endfunction
140 " Function: s:hgFunctions.Delete() {{{2
141 function! s:hgFunctions.Delete(argList)
142         return s:DoCommand(join(['remove'] + a:argList, ' '), 'remove', join(a:argList, ' '), {})
143 endfunction
145 " Function: s:hgFunctions.Diff(argList) {{{2
146 function! s:hgFunctions.Diff(argList)
147         if len(a:argList) == 0
148                 let revOptions = []
149                 let caption = ''
150         elseif len(a:argList) <= 2 && match(a:argList, '^-') == -1
151                 let revOptions = ['-r' . join(a:argList, ':')]
152                 let caption = '(' . a:argList[0] . ' : ' . get(a:argList, 1, 'current') . ')'
153         else
154                 " Pass-through
155                 let caption = join(a:argList, ' ')
156                 let revOptions = a:argList
157         endif
159         let hgDiffExt = VCSCommandGetOption('VCSCommandHGDiffExt', '')
160         if hgDiffExt == ''
161                 let diffExt = []
162         else
163                 let diffExt = ['--diff-cmd ' . hgDiffExt]
164         endif
166         let hgDiffOpt = VCSCommandGetOption('VCSCommandHGDiffOpt', '')
167         if hgDiffOpt == ''
168                 let diffOptions = []
169         else
170                 let diffOptions = ['-x -' . hgDiffOpt]
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 hgDiffExt == ''
178                         echomsg 'No differences found'
179                 endif
180         endif
181         return resultBuffer
182 endfunction
184 " Function: s:hgFunctions.Info(argList) {{{2
185 function! s:hgFunctions.Info(argList)
186         return s:DoCommand(join(['log --limit 1'] + a:argList, ' '), 'log', join(a:argList, ' '), {})
187 endfunction
189 " Function: s:hgFunctions.GetBufferInfo() {{{2
190 " Provides version control details for the current file.  Current version
191 " number and current repository version number are required to be returned by
192 " the vcscommand plugin.
193 " Returns: List of results:  [revision, repository, branch]
195 function! s:hgFunctions.GetBufferInfo()
196         let originalBuffer = VCSCommandGetOriginalBuffer(bufnr('%'))
197         let fileName = bufname(originalBuffer)
198         let statusText = s:VCSCommandUtility.system(s:Executable() . ' status -- "' . fileName . '"')
199         if(v:shell_error)
200                 return []
201         endif
203         " File not under HG control.
204         if statusText =~ '^?'
205                 return ['Unknown']
206         endif
208         let parentsText = s:VCSCommandUtility.system(s:Executable() . ' parents -- "' . fileName . '"')
209         let [revision] = matchlist(parentsText, '^changeset:\s\+\(\S\+\)\n')[1]
211         let logText = s:VCSCommandUtility.system(s:Executable() . ' log -- "' . fileName . '"')
212         let [repository] = matchlist(logText, '^changeset:\s\+\(\S\+\)\n')[1]
214         if revision == ''
215                 " Error
216                 return ['Unknown']
217         elseif statusText =~ '^A'
218                 return ['New', 'New']
219         else
220                 return [revision, repository]
221         endif
222 endfunction
224 " Function: s:hgFunctions.Log(argList) {{{2
225 function! s:hgFunctions.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', '-v'] + options), 'log', caption, {})
239         return resultBuffer
240 endfunction
242 " Function: s:hgFunctions.Revert(argList) {{{2
243 function! s:hgFunctions.Revert(argList)
244         return s:DoCommand('revert', 'revert', '', {})
245 endfunction
247 " Function: s:hgFunctions.Review(argList) {{{2
248 function! s:hgFunctions.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 --non-interactive' . versionOption, 'review', versiontag, {})
258         let resultBuffer = s:DoCommand('cat' . versionOption, 'review', versiontag, {})
259         if resultBuffer > 0
260                 let &filetype = getbufvar(b:VCSCommandOriginalBuffer, '&filetype')
261         endif
262         return resultBuffer
263 endfunction
265 " Function: s:hgFunctions.Status(argList) {{{2
266 function! s:hgFunctions.Status(argList)
267         let options = ['-u', '-v']
268         if len(a:argList) == 0
269                 let options = a:argList
270         endif
271         return s:DoCommand(join(['status'] + options, ' '), 'status', join(options, ' '), {})
272 endfunction
274 " Function: s:hgFunctions.Update(argList) {{{2
275 function! s:hgFunctions.Update(argList)
276         return s:DoCommand('update', 'update', '', {})
277 endfunction
279 " Section: Plugin Registration {{{1
280 let s:VCSCommandUtility = VCSCommandRegisterModule('HG', expand('<sfile>'), s:hgFunctions, [])
282 let &cpo = s:save_cpo