Tweaked buffer info for git buffers.
[vcscommand.git] / plugin / vcsgit.vim
blob003e4a5a00416673a3cfd4e03b7d6b4739d50098
1 " vim600: set foldmethod=marker:
3 " git extension for VCSCommand.
5 " Version:       VCS development
6 " Maintainer:    Bob Hiestand <bob.hiestand@gmail.com>
7 " License:
8 " Copyright (c) 2007 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
26 " IN THE SOFTWARE.
28 " Section: Documentation {{{1
30 " Options documentation: {{{2
32 " VCSCommandGitExec
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 v:version < 700
44         echohl WarningMsg|echomsg 'VCSCommand requires at least VIM 7.0'|echohl None
45         finish
46 endif
48 let s:save_cpo=&cpo
49 set cpo&vim
51 runtime plugin/vcscommand.vim
53 if !executable(VCSCommandGetOption('VCSCommandGitExec', 'git'))
54         " git is not installed
55         finish
56 endif
58 " Section: Variable initialization {{{1
60 let s:gitFunctions = {}
62 " Section: Utility functions {{{1
64 " Function: s:DoCommand(cmd, cmdName, statusText) {{{2
65 " Wrapper to VCSCommandDoCommand to add the name of the git executable to the
66 " command argument.
67 function! s:DoCommand(cmd, cmdName, statusText)
68         if VCSCommandGetVCSType(expand('%')) == 'git'
69                 let fullCmd = VCSCommandGetOption('VCSCommandGitExec', 'git') . ' ' . a:cmd
70                 return VCSCommandDoCommand(fullCmd, a:cmdName, a:statusText)
71         else
72                 throw 'git VCSCommand plugin called on non-git item.'
73         endif
74 endfunction
76 " Section: VCS function implementations {{{1
78 " Function: s:gitFunctions.Identify(buffer) {{{2
79 function! s:gitFunctions.Identify(buffer)
80         let oldCwd = VCSCommandChangeToCurrentFileDir(resolve(bufname(a:buffer)))
81         try
82                 call system(VCSCommandGetOption('VCSCommandGitExec', 'git') . ' rev-parse --is-inside-work-tree')
83                 if(v:shell_error)
84                         return 0
85                 else
86                         return 1
87                 endif
88         finally
89                 call VCSCommandChdir(oldCwd)
90         endtry
91 endfunction
93 " Function: s:gitFunctions.Add(argList) {{{2
94 function! s:gitFunctions.Add(argList)
95         return s:DoCommand(join(['add'] + ['-v'] + a:argList, ' '), 'add', join(a:argList, ' '))
96 endfunction
98 " Function: s:gitFunctions.Annotate(argList) {{{2
99 function! s:gitFunctions.Annotate(argList)
100         if len(a:argList) == 0
101                 if &filetype == 'gitAnnotate'
102                         " Perform annotation of the version indicated by the current line.
103                         let options = matchstr(getline('.'),'^\x\+')
104                 else
105                         let options = ''
106                 endif
107         elseif len(a:argList) == 1 && a:argList[0] !~ '^-'
108                 let options = a:argList[0]
109         else
110                 let options = join(a:argList, ' ')
111         endif
113         let resultBuffer = s:DoCommand('blame ' . options . ' -- ', 'annotate', options) 
114         if resultBuffer > 0
115                 normal 1G
116                 set filetype=gitAnnotate
117         endif
118         return resultBuffer
119 endfunction
121 " Function: s:gitFunctions.Commit(argList) {{{2
122 function! s:gitFunctions.Commit(argList)
123         let resultBuffer = s:DoCommand('commit -F "' . a:argList[0] . '"', 'commit', '')
124         if resultBuffer == 0
125                 echomsg 'No commit needed.'
126         endif
127         return resultBuffer
128 endfunction
130 " Function: s:gitFunctions.Delete() {{{2
131 " All options are passed through.
132 function! s:gitFunctions.Delete(argList)
133         let options = a:argList
134         let caption = join(a:argList, ' ')
135         return s:DoCommand(join(['rm'] + options, ' '), 'delete', caption)
136 endfunction
138 " Function: s:gitFunctions.Diff(argList) {{{2
139 " Pass-through call to git-diff.  If no options (starting with '-') are found,
140 " then the options in the 'VCSCommandGitDiffOpt' variable are added.
141 function! s:gitFunctions.Diff(argList)
142         let gitDiffOpt = VCSCommandGetOption('VCSCommandGitDiffOpt', '')
143         if gitDiffOpt == ''
144                 let diffOptions = []
145         else
146                 let diffOptions = [gitDiffOpt]
147                 for arg in a:argList
148                         if arg =~ '^-'
149                                 let diffOptions = []
150                                 break
151                         endif
152                 endfor
153         endif
155         let resultBuffer = s:DoCommand(join(['diff'] + diffOptions + a:argList), 'diff', join(a:argList))
156         if resultBuffer > 0
157                 set filetype=diff
158         else
159                 echomsg 'No differences found'
160         endif
161         return resultBuffer
162 endfunction
164 " Function: s:gitFunctions.GetBufferInfo() {{{2
165 " Provides version control details for the current file.  Current version
166 " number and current repository version number are required to be returned by
167 " the vcscommand plugin.  This CVS extension adds branch name to the return
168 " list as well.
169 " Returns: List of results:  [revision, repository, branch]
171 function! s:gitFunctions.GetBufferInfo()
172         let oldCwd = VCSCommandChangeToCurrentFileDir(resolve(bufname('%')))
173         try
174                 let branch = substitute(system(VCSCommandGetOption('VCSCommandGitExec', 'git') . ' symbolic-ref -q HEAD'), '\n$', '', '')
175                 if v:shell_error
176                         let branch = 'DETACHED'
177                 else
178                         let branch = substitute(branch, '^refs/heads/', '', '')
179                 endif
181                 let description = substitute(system(VCSCommandGetOption('VCSCommandGitExec', 'git') . ' describe --all'), '\n$', '', '')
183                 return[branch, description]
184         finally
185                 call VCSCommandChdir(oldCwd)
186         endtry
187 endfunction
189 " Function: s:gitFunctions.Log() {{{2
190 function! s:gitFunctions.Log(argList)
191         let resultBuffer=s:DoCommand(join(['log'] + a:argList), 'log', join(a:argList, ' '))
192         if resultBuffer > 0
193                 set filetype=gitlog
194         endif
195         return resultBuffer
196 endfunction
198 " Function: s:gitFunctions.Revert(argList) {{{2
199 function! s:gitFunctions.Revert(argList)
200         return s:DoCommand('checkout', 'revert', '')
201 endfunction
203 " Function: s:gitFunctions.Review(argList) {{{2
204 function! s:gitFunctions.Review(argList)
205         if len(a:argList) == 0
206                 let revision = 'HEAD'
207         else
208                 let revision = a:argList[0]
209         endif
211         let oldCwd = VCSCommandChangeToCurrentFileDir(resolve(bufname('%')))
212         try
213                 let prefix = system(VCSCommandGetOption('VCSCommandGitExec', 'git') . ' rev-parse --show-prefix')
214         finally
215                 call VCSCommandChdir(oldCwd)
216         endtry
218         let prefix = substitute(prefix, '\n$', '', '')
219         let blob = revision . ':' . prefix . '<VCSCOMMANDFILE>' 
220         let resultBuffer = s:DoCommand('show ' . blob, 'review', revision)
221         if resultBuffer > 0
222                 let &filetype=getbufvar(b:VCSCommandOriginalBuffer, '&filetype')
223         endif
224         return resultBuffer
225 endfunction
227 " Function: s:gitFunctions.Status(argList) {{{2
228 function! s:gitFunctions.Status(argList)
229         throw "This command is not implemented for git.  If you have an idea for what it should do, please let me know."
230 endfunction
232 " Function: s:gitFunctions.Update(argList) {{{2
233 function! s:gitFunctions.Update(argList)
234         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."
235 endfunction
238 " Section: Plugin Registration {{{1
239 call VCSCommandRegisterModule('git', expand('<sfile>'), s:gitFunctions, [])
241 let &cpo = s:save_cpo