1 " vim600: set foldmethod=marker:
3 " git extension for VCSCommand.
5 " Version: VCS development
6 " Maintainer: Bob Hiestand <bob.hiestand@gmail.com>
8 " Copyright (c) 2008 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
28 " Section: Documentation {{{1
30 " Options documentation: {{{2
33 " This variable specifies the git executable. If not set, it defaults to
34 " 'git' executed from the user's executable path.
36 " VCSCommandGitDiffOpt
37 " This variable, if set, determines the default options passed to the
38 " VCSDiff command. If any options (starting with '-') are passed to the
39 " command, this variable is not used.
41 " Section: Plugin header {{{1
43 if exists('VCSCommandDisableAll')
48 echohl WarningMsg|echomsg 'VCSCommand requires at least VIM 7.0'|echohl None
52 runtime plugin/vcscommand.vim
54 if !executable(VCSCommandGetOption('VCSCommandGitExec', 'git'))
55 " git is not installed
62 " Section: Variable initialization {{{1
64 let s:gitFunctions = {}
66 " Section: Utility functions {{{1
68 " Function: s:Executable() {{{2
69 " Returns the executable used to invoke git suitable for use in a shell
71 function! s:Executable()
72 return shellescape(VCSCommandGetOption('VCSCommandGitExec', 'git'))
75 " Function: s:DoCommand(cmd, cmdName, statusText, options) {{{2
76 " Wrapper to VCSCommandDoCommand to add the name of the git executable to the
78 function! s:DoCommand(cmd, cmdName, statusText, options)
79 if VCSCommandGetVCSType(expand('%')) == 'git'
80 let fullCmd = s:Executable() . ' ' . a:cmd
81 return VCSCommandDoCommand(fullCmd, a:cmdName, a:statusText, a:options)
83 throw 'git VCSCommand plugin called on non-git item.'
87 " Section: VCS function implementations {{{1
89 " Function: s:gitFunctions.Identify(buffer) {{{2
90 " This function only returns an inexact match due to the detection method used
91 " by git, which simply traverses the directory structure upward.
92 function! s:gitFunctions.Identify(buffer)
93 let oldCwd = VCSCommandChangeToCurrentFileDir(resolve(bufname(a:buffer)))
95 call s:VCSCommandUtility.system(s:Executable() . ' rev-parse --is-inside-work-tree')
99 return g:VCSCOMMAND_IDENTIFY_INEXACT
102 call VCSCommandChdir(oldCwd)
106 " Function: s:gitFunctions.Add(argList) {{{2
107 function! s:gitFunctions.Add(argList)
108 return s:DoCommand(join(['add'] + ['-v'] + a:argList, ' '), 'add', join(a:argList, ' '), {})
111 " Function: s:gitFunctions.Annotate(argList) {{{2
112 function! s:gitFunctions.Annotate(argList)
113 if len(a:argList) == 0
114 if &filetype == 'gitannotate'
115 " Perform annotation of the version indicated by the current line.
116 let options = matchstr(getline('.'),'^\x\+')
120 elseif len(a:argList) == 1 && a:argList[0] !~ '^-'
121 let options = a:argList[0]
123 let options = join(a:argList, ' ')
126 return s:DoCommand('blame ' . options, 'annotate', options, {})
129 " Function: s:gitFunctions.Commit(argList) {{{2
130 function! s:gitFunctions.Commit(argList)
131 let resultBuffer = s:DoCommand('commit -F "' . a:argList[0] . '"', 'commit', '', {})
133 echomsg 'No commit needed.'
138 " Function: s:gitFunctions.Delete() {{{2
139 " All options are passed through.
140 function! s:gitFunctions.Delete(argList)
141 let options = a:argList
142 let caption = join(a:argList, ' ')
143 return s:DoCommand(join(['rm'] + options, ' '), 'delete', caption, {})
146 " Function: s:gitFunctions.Diff(argList) {{{2
147 " Pass-through call to git-diff. If no options (starting with '-') are found,
148 " then the options in the 'VCSCommandGitDiffOpt' variable are added.
149 function! s:gitFunctions.Diff(argList)
150 let gitDiffOpt = VCSCommandGetOption('VCSCommandGitDiffOpt', '')
154 let diffOptions = [gitDiffOpt]
163 return s:DoCommand(join(['diff'] + diffOptions + a:argList), 'diff', join(a:argList), {})
166 " Function: s:gitFunctions.GetBufferInfo() {{{2
167 " Provides version control details for the current file. Current version
168 " number and current repository version number are required to be returned by
169 " the vcscommand plugin. This CVS extension adds branch name to the return
171 " Returns: List of results: [revision, repository, branch]
173 function! s:gitFunctions.GetBufferInfo()
174 let oldCwd = VCSCommandChangeToCurrentFileDir(resolve(bufname('%')))
176 let branch = substitute(s:VCSCommandUtility.system(s:Executable() . ' symbolic-ref -q HEAD'), '\n$', '', '')
178 let branch = 'DETACHED'
180 let branch = substitute(branch, '^refs/heads/', '', '')
185 for method in split(VCSCommandGetOption('VCSCommandGitDescribeArgList', (',tags,all,always')), ',', 1)
187 let method = ' --' . method
189 let tag = substitute(s:VCSCommandUtility.system(s:Executable() . ' describe' . method), '\n$', '', '')
198 call VCSCommandChdir(oldCwd)
202 " Function: s:gitFunctions.Log() {{{2
203 function! s:gitFunctions.Log(argList)
204 return s:DoCommand(join(['log'] + a:argList), 'log', join(a:argList, ' '), {})
207 " Function: s:gitFunctions.Revert(argList) {{{2
208 function! s:gitFunctions.Revert(argList)
209 return s:DoCommand('checkout', 'revert', '', {})
212 " Function: s:gitFunctions.Review(argList) {{{2
213 function! s:gitFunctions.Review(argList)
214 if len(a:argList) == 0
215 let revision = 'HEAD'
217 let revision = a:argList[0]
220 let oldCwd = VCSCommandChangeToCurrentFileDir(resolve(bufname(VCSCommandGetOriginalBuffer('%'))))
222 let prefix = s:VCSCommandUtility.system(s:Executable() . ' rev-parse --show-prefix')
224 call VCSCommandChdir(oldCwd)
227 let prefix = substitute(prefix, '\n$', '', '')
228 let blob = '"' . revision . ':' . prefix . '<VCSCOMMANDFILE>"'
229 return s:DoCommand('show ' . blob, 'review', revision, {})
232 " Function: s:gitFunctions.Status(argList) {{{2
233 function! s:gitFunctions.Status(argList)
234 return s:DoCommand(join(['status'] + a:argList), 'status', join(a:argList), {'allowNonZeroExit': 1})
237 " Function: s:gitFunctions.Update(argList) {{{2
238 function! s:gitFunctions.Update(argList)
239 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."
242 " Annotate setting {{{2
243 let s:gitFunctions.AnnotateSplitRegex = ') '
245 " Section: Plugin Registration {{{1
246 let s:VCSCommandUtility = VCSCommandRegisterModule('git', expand('<sfile>'), s:gitFunctions, [])
248 let &cpo = s:save_cpo