host info updates
[distro-setup] / .vim / plugin / vcsgit.vim
1 " vim600: set foldmethod=marker:
2 "
3 " git extension for VCSCommand.
4 "
5 " Maintainer: Bob Hiestand <bob.hiestand@gmail.com>
6 " License:
7 " Copyright (c) Bob Hiestand
8 "
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:
15 "
16 " The above copyright notice and this permission notice shall be included in
17 " all copies or substantial portions of the Software.
18 "
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.
26 "
27 " Section: Documentation {{{1
28 "
29 " Options documentation: {{{2
30 "
31 " VCSCommandGitExec
32 " This variable specifies the git executable. If not set, it defaults to
33 " 'git' executed from the user's executable path.
34 "
35 " VCSCommandGitDiffOpt
36 " This variable, if set, determines the default options passed to the
37 " VCSDiff command. If any options (starting with '-') are passed to the
38 " command, this variable is not used.
39
40 " Section: Plugin header {{{1
41
42 if exists('VCSCommandDisableAll')
43 finish
44 endif
45
46 if v:version < 700
47 echohl WarningMsg|echomsg 'VCSCommand requires at least VIM 7.0'|echohl None
48 finish
49 endif
50
51 runtime plugin/vcscommand.vim
52
53 if !executable(VCSCommandGetOption('VCSCommandGitExec', 'git'))
54 " git is not installed
55 finish
56 endif
57
58 let s:save_cpo=&cpo
59 set cpo&vim
60
61 " Section: Variable initialization {{{1
62
63 let s:gitFunctions = {}
64
65 " Section: Utility functions {{{1
66
67 " Function: s:Executable() {{{2
68 " Returns the executable used to invoke git suitable for use in a shell
69 " command.
70 function! s:Executable()
71 return shellescape(VCSCommandGetOption('VCSCommandGitExec', 'git'))
72 endfunction
73
74 " Function: s:DoCommand(cmd, cmdName, statusText, options) {{{2
75 " Wrapper to VCSCommandDoCommand to add the name of the git executable to the
76 " command argument.
77 function! s:DoCommand(cmd, cmdName, statusText, options)
78 if VCSCommandGetVCSType(expand('%')) == 'git'
79 let fullCmd = s:Executable() . ' ' . a:cmd
80 return VCSCommandDoCommand(fullCmd, a:cmdName, a:statusText, a:options)
81 else
82 throw 'git VCSCommand plugin called on non-git item.'
83 endif
84 endfunction
85
86 " Section: VCS function implementations {{{1
87
88 " Function: s:gitFunctions.Identify(buffer) {{{2
89 " This function only returns an inexact match due to the detection method used
90 " by git, which simply traverses the directory structure upward.
91 function! s:gitFunctions.Identify(buffer)
92 let oldCwd = VCSCommandChangeToCurrentFileDir(resolve(bufname(a:buffer)))
93 try
94 call s:VCSCommandUtility.system(s:Executable() . ' rev-parse --is-inside-work-tree')
95 if(v:shell_error)
96 return 0
97 else
98 return g:VCSCOMMAND_IDENTIFY_INEXACT
99 endif
100 finally
101 call VCSCommandChdir(oldCwd)
102 endtry
103 endfunction
104
105 " Function: s:gitFunctions.Add(argList) {{{2
106 function! s:gitFunctions.Add(argList)
107 return s:DoCommand(join(['add'] + ['-v'] + a:argList, ' '), 'add', join(a:argList, ' '), {})
108 endfunction
109
110 " Function: s:gitFunctions.Annotate(argList) {{{2
111 function! s:gitFunctions.Annotate(argList)
112 if len(a:argList) == 0
113 if &filetype == 'gitannotate'
114 " Perform annotation of the version indicated by the current line.
115 let options = matchstr(getline('.'),'^\x\+')
116 else
117 let options = ''
118 endif
119 elseif len(a:argList) == 1 && a:argList[0] !~ '^-'
120 let options = a:argList[0]
121 else
122 let options = join(a:argList, ' ')
123 endif
124
125 return s:DoCommand('blame ' . options, 'annotate', options, {})
126 endfunction
127
128 " Function: s:gitFunctions.Commit(argList) {{{2
129 function! s:gitFunctions.Commit(argList)
130 try
131 return s:DoCommand('commit -F "' . a:argList[0] . '"', 'commit', '', {})
132 catch /\m^Version control command failed.*nothing\%( added\)\? to commit/
133 echomsg 'No commit needed.'
134 endtry
135 endfunction
136
137 " Function: s:gitFunctions.Delete() {{{2
138 " All options are passed through.
139 function! s:gitFunctions.Delete(argList)
140 let options = a:argList
141 let caption = join(a:argList, ' ')
142 return s:DoCommand(join(['rm'] + options, ' '), 'delete', caption, {})
143 endfunction
144
145 " Function: s:gitFunctions.Diff(argList) {{{2
146 " Pass-through call to git-diff. If no options (starting with '-') are found,
147 " then the options in the 'VCSCommandGitDiffOpt' variable are added.
148 function! s:gitFunctions.Diff(argList)
149 let gitDiffOpt = VCSCommandGetOption('VCSCommandGitDiffOpt', '')
150 if gitDiffOpt == ''
151 let diffOptions = []
152 else
153 let diffOptions = [gitDiffOpt]
154 for arg in a:argList
155 if arg =~ '^-'
156 let diffOptions = []
157 break
158 endif
159 endfor
160 endif
161
162 return s:DoCommand(join(['diff'] + diffOptions + a:argList), 'diff', join(a:argList), {})
163 endfunction
164
165 " Function: s:gitFunctions.GetBufferInfo() {{{2
166 " Provides version control details for the current file. Current version
167 " number and current repository version number are required to be returned by
168 " the vcscommand plugin. This CVS extension adds branch name to the return
169 " list as well.
170 " Returns: List of results: [revision, repository, branch]
171
172 function! s:gitFunctions.GetBufferInfo()
173 let oldCwd = VCSCommandChangeToCurrentFileDir(resolve(bufname('%')))
174 try
175 let branch = substitute(s:VCSCommandUtility.system(s:Executable() . ' symbolic-ref -q HEAD'), '\n$', '', '')
176 if v:shell_error
177 let branch = 'DETACHED'
178 else
179 let branch = substitute(branch, '^refs/heads/', '', '')
180 endif
181
182 let info = [branch]
183
184 for method in split(VCSCommandGetOption('VCSCommandGitDescribeArgList', (',tags,all,always')), ',', 1)
185 if method != ''
186 let method = ' --' . method
187 endif
188 let tag = substitute(s:VCSCommandUtility.system(s:Executable() . ' describe' . method), '\n$', '', '')
189 if !v:shell_error
190 call add(info, tag)
191 break
192 endif
193 endfor
194
195 return info
196 finally
197 call VCSCommandChdir(oldCwd)
198 endtry
199 endfunction
200
201 " Function: s:gitFunctions.Log() {{{2
202 function! s:gitFunctions.Log(argList)
203 return s:DoCommand(join(['log'] + a:argList), 'log', join(a:argList, ' '), {})
204 endfunction
205
206 " Function: s:gitFunctions.Revert(argList) {{{2
207 function! s:gitFunctions.Revert(argList)
208 return s:DoCommand('checkout', 'revert', '', {})
209 endfunction
210
211 " Function: s:gitFunctions.Review(argList) {{{2
212 function! s:gitFunctions.Review(argList)
213 if len(a:argList) == 0
214 let revision = 'HEAD'
215 else
216 let revision = a:argList[0]
217 endif
218
219 let oldCwd = VCSCommandChangeToCurrentFileDir(resolve(bufname(VCSCommandGetOriginalBuffer('%'))))
220 try
221 let prefix = s:VCSCommandUtility.system(s:Executable() . ' rev-parse --show-prefix')
222 finally
223 call VCSCommandChdir(oldCwd)
224 endtry
225
226 let prefix = substitute(prefix, '\n$', '', '')
227 let blob = '"' . revision . ':' . prefix . '<VCSCOMMANDFILE>"'
228 return s:DoCommand('show ' . blob, 'review', revision, {})
229 endfunction
230
231 " Function: s:gitFunctions.Status(argList) {{{2
232 function! s:gitFunctions.Status(argList)
233 return s:DoCommand(join(['status'] + a:argList), 'status', join(a:argList), {'allowNonZeroExit': 1})
234 endfunction
235
236 " Function: s:gitFunctions.Update(argList) {{{2
237 function! s:gitFunctions.Update(argList)
238 throw "This command is not implemented for git because file-by-file update doesn't make much sense in that context. If you have an idea for what it should do, please let me know."
239 endfunction
240
241 " Annotate setting {{{2
242 let s:gitFunctions.AnnotateSplitRegex = ') '
243
244 " Section: Plugin Registration {{{1
245 let s:VCSCommandUtility = VCSCommandRegisterModule('git', expand('<sfile>'), s:gitFunctions, [])
246
247 let &cpo = s:save_cpo