handle ignored files in GetBufferInfo
[vcscommand.git] / plugin / vcsgit.vim
bloba893d158cf89308144968eb4b061f492f5295b8d
1 " vim600: set foldmethod=marker:
3 " git extension for VCSCommand.
5 " Maintainer:    Bob Hiestand <bob.hiestand@gmail.com>
6 " License:
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
25 " IN THE SOFTWARE.
27 " Section: Documentation {{{1
29 " Options documentation: {{{2
31 " VCSCommandGitExec
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')
43         finish
44 endif
46 if v:version < 700
47         echohl WarningMsg|echomsg 'VCSCommand requires at least VIM 7.0'|echohl None
48         finish
49 endif
51 if !exists('g:loaded_VCSCommand')
52         runtime plugin/vcscommand.vim
53 endif
55 if !executable(VCSCommandGetOption('VCSCommandGitExec', 'git'))
56         " git is not installed
57         finish
58 endif
60 let s:save_cpo=&cpo
61 set cpo&vim
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
71 " command.
72 function! s:Executable()
73         return shellescape(VCSCommandGetOption('VCSCommandGitExec', 'git'))
74 endfunction
76 " Function: s:DoCommand(cmd, cmdName, statusText, options) {{{2
77 " Wrapper to VCSCommandDoCommand to add the name of the git executable to the
78 " command argument.
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)
83         else
84                 throw 'git VCSCommand plugin called on non-git item.'
85         endif
86 endfunction
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)))
95         try
96                 call s:VCSCommandUtility.system(s:Executable() . ' rev-parse --is-inside-work-tree')
97                 if(v:shell_error)
98                         return 0
99                 else
100                         return g:VCSCOMMAND_IDENTIFY_INEXACT
101                 endif
102         finally
103                 call VCSCommandChdir(oldCwd)
104         endtry
105 endfunction
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, ' '), {})
110 endfunction
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\+')
118                 else
119                         let options = ''
120                 endif
121         elseif len(a:argList) == 1 && a:argList[0] !~ '^-'
122                 let options = a:argList[0]
123         else
124                 let options = join(a:argList, ' ')
125         endif
127         return s:DoCommand('blame ' . options, 'annotate', options, {})
128 endfunction
130 " Function: s:gitFunctions.Commit(argList) {{{2
131 function! s:gitFunctions.Commit(argList)
132         try
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.'
136         endtry
137 endfunction
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, {})
145 endfunction
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', '')
152         if gitDiffOpt == ''
153                 let diffOptions = []
154         else
155                 let diffOptions = [gitDiffOpt]
156                 for arg in a:argList
157                         if arg =~ '^-'
158                                 let diffOptions = []
159                                 break
160                         endif
161                 endfor
162         endif
164         return s:DoCommand(join(['diff'] + diffOptions + a:argList), 'diff', join(a:argList), {})
165 endfunction
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
171 " list as well.
172 " Returns: List of results:  [revision, repository, branch]
174 function! s:gitFunctions.GetBufferInfo()
175         let oldCwd = VCSCommandChangeToCurrentFileDir(resolve(bufname('%')))
176         try
177                 let branch = substitute(s:VCSCommandUtility.system(s:Executable() . ' symbolic-ref -q HEAD'), '\n$', '', '')
178                 if v:shell_error
179                         let branch = 'DETACHED'
180                 else
181                         let branch = substitute(branch, '^refs/heads/', '', '')
182                 endif
184                 let info = [branch]
186                 for method in split(VCSCommandGetOption('VCSCommandGitDescribeArgList', (',tags,all,always')), ',', 1)
187                         if method != ''
188                                 let method = ' --' . method
189                         endif
190                         let tag = substitute(s:VCSCommandUtility.system(s:Executable() . ' describe' . method), '\n$', '', '')
191                         if !v:shell_error
192                                 call add(info, tag)
193                                 break
194                         endif
195                 endfor
197                 return info
198         finally
199                 call VCSCommandChdir(oldCwd)
200         endtry
201 endfunction
203 " Function: s:gitFunctions.Log() {{{2
204 function! s:gitFunctions.Log(argList)
205         return s:DoCommand(join(['log'] + a:argList), 'log', join(a:argList, ' '), {})
206 endfunction
208 " Function: s:gitFunctions.Revert(argList) {{{2
209 function! s:gitFunctions.Revert(argList)
210         return s:DoCommand('checkout', 'revert', '', {})
211 endfunction
213 " Function: s:gitFunctions.Review(argList) {{{2
214 function! s:gitFunctions.Review(argList)
215         if len(a:argList) == 0
216                 let revision = 'HEAD'
217         else
218                 let revision = a:argList[0]
219         endif
221         let oldCwd = VCSCommandChangeToCurrentFileDir(resolve(bufname(VCSCommandGetOriginalBuffer('%'))))
222         try
223                 let prefix = s:VCSCommandUtility.system(s:Executable() . ' rev-parse --show-prefix')
224         finally
225                 call VCSCommandChdir(oldCwd)
226         endtry
228         let prefix = substitute(prefix, '\n$', '', '')
229         let blob = '"' . revision . ':' . prefix . '<VCSCOMMANDFILE>"'
230         return s:DoCommand('show ' . blob, 'review', revision, {})
231 endfunction
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})
236 endfunction
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."
241 endfunction
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