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 if !exists('g:loaded_VCSCommand')
52 runtime plugin/vcscommand.vim
55 if !executable(VCSCommandGetOption('VCSCommandGitExec', 'git'))
56 " git is not installed
63 " Section: Variable initialization {{{1
65 let s:gitFunctions = {}
67 " Section: Utility functions {{{1
69 " Function: s:Executable() {{{2
70 " Returns the executable used to invoke git suitable for use in a shell
72 function! s:Executable()
73 return VCSCommandGetOption('VCSCommandGitExec', 'git')
76 " Function: s:DoCommand(cmd, cmdName, statusText, options) {{{2
77 " Wrapper to VCSCommandDoCommand to add the name of the git executable to the
79 function! s:DoCommand(cmd, cmdName, statusText, options)
80 if VCSCommandGetVCSType(expand('%')) == 'git'
81 let fullCmd = s:Executable() . ' ' . a:cmd
82 return VCSCommandDoCommand(fullCmd, a:cmdName, a:statusText, a:options)
84 throw 'git VCSCommand plugin called on non-git item.'
88 " Section: VCS function implementations {{{1
90 " Function: s:gitFunctions.Identify(buffer) {{{2
91 " This function only returns an inexact match due to the detection method used
92 " by git, which simply traverses the directory structure upward.
93 function! s:gitFunctions.Identify(buffer)
94 let oldCwd = VCSCommandChangeToCurrentFileDir(resolve(bufname(a:buffer)))
96 call s:VCSCommandUtility.system(s:Executable() . ' rev-parse --is-inside-work-tree')
100 return g:VCSCOMMAND_IDENTIFY_INEXACT
103 call VCSCommandChdir(oldCwd)
107 " Function: s:gitFunctions.Add(argList) {{{2
108 function! s:gitFunctions.Add(argList)
109 return s:DoCommand(join(['add'] + ['-v'] + a:argList, ' '), 'add', join(a:argList, ' '), {})
112 " Function: s:gitFunctions.Annotate(argList) {{{2
113 function! s:gitFunctions.Annotate(argList)
114 if len(a:argList) == 0
115 if &filetype == 'gitannotate'
116 " Perform annotation of the version indicated by the current line.
117 let options = matchstr(getline('.'),'^\x\+')
121 elseif len(a:argList) == 1 && a:argList[0] !~ '^-'
122 let options = a:argList[0]
124 let options = join(a:argList, ' ')
127 return s:DoCommand('blame ' . options, 'annotate', options, {})
130 " Function: s:gitFunctions.Commit(argList) {{{2
131 function! s:gitFunctions.Commit(argList)
133 return s:DoCommand('commit -F "' . a:argList[0] . '"', 'commit', '', {})
134 catch /\m^Version control command failed.*nothing\%( added\)\? to commit/
135 echomsg 'No commit needed.'
139 " Function: s:gitFunctions.Delete() {{{2
140 " All options are passed through.
141 function! s:gitFunctions.Delete(argList)
142 let options = a:argList
143 let caption = join(a:argList, ' ')
144 return s:DoCommand(join(['rm'] + options, ' '), 'delete', caption, {})
147 " Function: s:gitFunctions.Diff(argList) {{{2
148 " Pass-through call to git-diff. If no options (starting with '-') are found,
149 " then the options in the 'VCSCommandGitDiffOpt' variable are added.
150 function! s:gitFunctions.Diff(argList)
151 let gitDiffOpt = VCSCommandGetOption('VCSCommandGitDiffOpt', '')
155 let diffOptions = [gitDiffOpt]
164 return s:DoCommand(join(['diff'] + diffOptions + a:argList), 'diff', join(a:argList), {})
167 " Function: s:gitFunctions.GetBufferInfo() {{{2
168 " Provides version control details for the current file. Current version
169 " number and current repository version number are required to be returned by
170 " the vcscommand plugin. This CVS extension adds branch name to the return
172 " Returns: List of results: [revision, repository, branch]
174 function! s:gitFunctions.GetBufferInfo()
175 let oldCwd = VCSCommandChangeToCurrentFileDir(resolve(bufname('%')))
177 let branch = substitute(s:VCSCommandUtility.system(s:Executable() . ' symbolic-ref -q HEAD'), '\n$', '', '')
179 let branch = 'DETACHED'
181 let branch = substitute(branch, '^refs/heads/', '', '')
186 for method in split(VCSCommandGetOption('VCSCommandGitDescribeArgList', (',tags,all,always')), ',', 1)
188 let method = ' --' . method
190 let tag = substitute(s:VCSCommandUtility.system(s:Executable() . ' describe' . method), '\n$', '', '')
199 call VCSCommandChdir(oldCwd)
203 " Function: s:gitFunctions.Log() {{{2
204 function! s:gitFunctions.Log(argList)
205 return s:DoCommand(join(['log'] + a:argList), 'log', join(a:argList, ' '), {})
208 " Function: s:gitFunctions.Revert(argList) {{{2
209 function! s:gitFunctions.Revert(argList)
210 return s:DoCommand('checkout', 'revert', '', {})
213 " Function: s:gitFunctions.Review(argList) {{{2
214 function! s:gitFunctions.Review(argList)
215 if len(a:argList) == 0
216 let revision = 'HEAD'
218 let revision = a:argList[0]
221 let oldCwd = VCSCommandChangeToCurrentFileDir(resolve(bufname(VCSCommandGetOriginalBuffer('%'))))
223 let prefix = s:VCSCommandUtility.system(s:Executable() . ' rev-parse --show-prefix')
225 call VCSCommandChdir(oldCwd)
228 let prefix = substitute(prefix, '\n$', '', '')
229 let blob = '"' . revision . ':' . prefix . '<VCSCOMMANDFILE>"'
230 return s:DoCommand('show ' . blob, 'review', revision, {})
233 " Function: s:gitFunctions.Status(argList) {{{2
234 function! s:gitFunctions.Status(argList)
235 return s:DoCommand(join(['status'] + a:argList), 'status', join(a:argList), {'allowNonZeroExit': 1})
238 " Function: s:gitFunctions.Update(argList) {{{2
239 function! s:gitFunctions.Update(argList)
240 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."
243 " Annotate setting {{{2
244 let s:gitFunctions.AnnotateSplitRegex = ') '
246 " Section: Plugin Registration {{{1
247 let s:VCSCommandUtility = VCSCommandRegisterModule('git', expand('<sfile>'), s:gitFunctions, [])
249 let &cpo = s:save_cpo