1 " vim600: set foldmethod=marker:
3 " git extension for VCSCommand.
5 " Maintainer: Bob Hiestand <bob.hiestand@gmail.com>
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
27 " Section: Documentation {{{1
29 " Options documentation: {{{2
32 " This variable specifies the git executable. If not set, it defaults to
33 " 'git' executed from the user's executable path.
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.
40 " Section: Plugin header {{{1
42 if exists('VCSCommandDisableAll')
47 echohl WarningMsg|echomsg 'VCSCommand requires at least VIM 7.0'|echohl None
51 runtime plugin/vcscommand.vim
53 if !executable(VCSCommandGetOption('VCSCommandGitExec', 'git'))
54 " git is not installed
61 " Section: Variable initialization {{{1
63 let s:gitFunctions = {}
65 " Section: Utility functions {{{1
67 " Function: s:Executable() {{{2
68 " Returns the executable used to invoke git suitable for use in a shell
70 function! s:Executable()
71 return shellescape(VCSCommandGetOption('VCSCommandGitExec', 'git'))
74 " Function: s:DoCommand(cmd, cmdName, statusText, options) {{{2
75 " Wrapper to VCSCommandDoCommand to add the name of the git executable to the
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)
82 throw 'git VCSCommand plugin called on non-git item.'
86 " Section: VCS function implementations {{{1
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)))
94 call s:VCSCommandUtility.system(s:Executable() . ' rev-parse --is-inside-work-tree')
98 return g:VCSCOMMAND_IDENTIFY_INEXACT
101 call VCSCommandChdir(oldCwd)
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, ' '), {})
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\+')
119 elseif len(a:argList) == 1 && a:argList[0] !~ '^-'
120 let options = a:argList[0]
122 let options = join(a:argList, ' ')
125 return s:DoCommand('blame ' . options, 'annotate', options, {})
128 " Function: s:gitFunctions.Commit(argList) {{{2
129 function! s:gitFunctions.Commit(argList)
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.'
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, {})
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', '')
153 let diffOptions = [gitDiffOpt]
162 return s:DoCommand(join(['diff'] + diffOptions + a:argList), 'diff', join(a:argList), {})
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
170 " Returns: List of results: [revision, repository, branch]
172 function! s:gitFunctions.GetBufferInfo()
173 let oldCwd = VCSCommandChangeToCurrentFileDir(resolve(bufname('%')))
175 let branch = substitute(s:VCSCommandUtility.system(s:Executable() . ' symbolic-ref -q HEAD'), '\n$', '', '')
177 let branch = 'DETACHED'
179 let branch = substitute(branch, '^refs/heads/', '', '')
184 for method in split(VCSCommandGetOption('VCSCommandGitDescribeArgList', (',tags,all,always')), ',', 1)
186 let method = ' --' . method
188 let tag = substitute(s:VCSCommandUtility.system(s:Executable() . ' describe' . method), '\n$', '', '')
197 call VCSCommandChdir(oldCwd)
201 " Function: s:gitFunctions.Log() {{{2
202 function! s:gitFunctions.Log(argList)
203 return s:DoCommand(join(['log'] + a:argList), 'log', join(a:argList, ' '), {})
206 " Function: s:gitFunctions.Revert(argList) {{{2
207 function! s:gitFunctions.Revert(argList)
208 return s:DoCommand('checkout', 'revert', '', {})
211 " Function: s:gitFunctions.Review(argList) {{{2
212 function! s:gitFunctions.Review(argList)
213 if len(a:argList) == 0
214 let revision = 'HEAD'
216 let revision = a:argList[0]
219 let oldCwd = VCSCommandChangeToCurrentFileDir(resolve(bufname(VCSCommandGetOriginalBuffer('%'))))
221 let prefix = s:VCSCommandUtility.system(s:Executable() . ' rev-parse --show-prefix')
223 call VCSCommandChdir(oldCwd)
226 let prefix = substitute(prefix, '\n$', '', '')
227 let blob = '"' . revision . ':' . prefix . '<VCSCOMMANDFILE>"'
228 return s:DoCommand('show ' . blob, 'review', revision, {})
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})
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."
241 " Annotate setting {{{2
242 let s:gitFunctions.AnnotateSplitRegex = ') '
244 " Section: Plugin Registration {{{1
245 let s:VCSCommandUtility = VCSCommandRegisterModule('git', expand('<sfile>'), s:gitFunctions, [])
247 let &cpo = s:save_cpo