Mark git identification as inexact.
[vcscommand.git] / plugin / vcsgit.vim
blobfc72c246a87ddb0f3d3dc86f1bdf0394b801015f
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 runtime plugin/vcscommand.vim
50 if !executable(VCSCommandGetOption('VCSCommandGitExec', 'git'))
51         " git is not installed
52         finish
53 endif
55 let s:save_cpo=&cpo
56 set cpo&vim
58 " Section: Variable initialization {{{1
60 let s:gitFunctions = {}
62 " Section: Utility functions {{{1
64 " Function: s:DoCommand(cmd, cmdName, statusText, options) {{{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, options)
68         if VCSCommandGetVCSType(expand('%')) == 'git'
69                 let fullCmd = VCSCommandGetOption('VCSCommandGitExec', 'git',) . ' ' . a:cmd
70                 return VCSCommandDoCommand(fullCmd, a:cmdName, a:statusText, a:options)
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 " This function only returns an inexact match due to the detection method used
80 " by git, which simply traverses the directory structure upward.
81 function! s:gitFunctions.Identify(buffer)
82         let oldCwd = VCSCommandChangeToCurrentFileDir(resolve(bufname(a:buffer)))
83         try
84                 call system(VCSCommandGetOption('VCSCommandGitExec', 'git') . ' rev-parse --is-inside-work-tree')
85                 if(v:shell_error)
86                         return 0
87                 else
88                         return g:VCSCOMMAND_IDENTIFY_INEXACT
89                 endif
90         finally
91                 call VCSCommandChdir(oldCwd)
92         endtry
93 endfunction
95 " Function: s:gitFunctions.Add(argList) {{{2
96 function! s:gitFunctions.Add(argList)
97         return s:DoCommand(join(['add'] + ['-v'] + a:argList, ' '), 'add', join(a:argList, ' '), {})
98 endfunction
100 " Function: s:gitFunctions.Annotate(argList) {{{2
101 function! s:gitFunctions.Annotate(argList)
102         if len(a:argList) == 0
103                 if &filetype == 'gitAnnotate'
104                         " Perform annotation of the version indicated by the current line.
105                         let options = matchstr(getline('.'),'^\x\+')
106                 else
107                         let options = ''
108                 endif
109         elseif len(a:argList) == 1 && a:argList[0] !~ '^-'
110                 let options = a:argList[0]
111         else
112                 let options = join(a:argList, ' ')
113         endif
115         let resultBuffer = s:DoCommand('blame ' . options . ' -- ', 'annotate', options, {})
116         if resultBuffer > 0
117                 normal 1G
118                 set filetype=gitAnnotate
119         endif
120         return resultBuffer
121 endfunction
123 " Function: s:gitFunctions.Commit(argList) {{{2
124 function! s:gitFunctions.Commit(argList)
125         let resultBuffer = s:DoCommand('commit -F "' . a:argList[0] . '"', 'commit', '', {})
126         if resultBuffer == 0
127                 echomsg 'No commit needed.'
128         endif
129         return resultBuffer
130 endfunction
132 " Function: s:gitFunctions.Delete() {{{2
133 " All options are passed through.
134 function! s:gitFunctions.Delete(argList)
135         let options = a:argList
136         let caption = join(a:argList, ' ')
137         return s:DoCommand(join(['rm'] + options, ' '), 'delete', caption, {})
138 endfunction
140 " Function: s:gitFunctions.Diff(argList) {{{2
141 " Pass-through call to git-diff.  If no options (starting with '-') are found,
142 " then the options in the 'VCSCommandGitDiffOpt' variable are added.
143 function! s:gitFunctions.Diff(argList)
144         let gitDiffOpt = VCSCommandGetOption('VCSCommandGitDiffOpt', '')
145         if gitDiffOpt == ''
146                 let diffOptions = []
147         else
148                 let diffOptions = [gitDiffOpt]
149                 for arg in a:argList
150                         if arg =~ '^-'
151                                 let diffOptions = []
152                                 break
153                         endif
154                 endfor
155         endif
157         let resultBuffer = s:DoCommand(join(['diff'] + diffOptions + a:argList), 'diff', join(a:argList), {})
158         if resultBuffer > 0
159                 set filetype=diff
160         else
161                 echomsg 'No differences found'
162         endif
163         return resultBuffer
164 endfunction
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
170 " list as well.
171 " Returns: List of results:  [revision, repository, branch]
173 function! s:gitFunctions.GetBufferInfo()
174         let oldCwd = VCSCommandChangeToCurrentFileDir(resolve(bufname('%')))
175         try
176                 let branch = substitute(system(VCSCommandGetOption('VCSCommandGitExec', 'git') . ' symbolic-ref -q HEAD'), '\n$', '', '')
177                 if v:shell_error
178                         let branch = 'DETACHED'
179                 else
180                         let branch = substitute(branch, '^refs/heads/', '', '')
181                 endif
183                 let description = substitute(system(VCSCommandGetOption('VCSCommandGitExec', 'git') . ' describe --all'), '\n$', '', '')
185                 return[branch, description]
186         finally
187                 call VCSCommandChdir(oldCwd)
188         endtry
189 endfunction
191 " Function: s:gitFunctions.Log() {{{2
192 function! s:gitFunctions.Log(argList)
193         let resultBuffer=s:DoCommand(join(['log'] + a:argList), 'log', join(a:argList, ' '), {})
194         if resultBuffer > 0
195                 set filetype=gitlog
196         endif
197         return resultBuffer
198 endfunction
200 " Function: s:gitFunctions.Revert(argList) {{{2
201 function! s:gitFunctions.Revert(argList)
202         return s:DoCommand('checkout', 'revert', '', {})
203 endfunction
205 " Function: s:gitFunctions.Review(argList) {{{2
206 function! s:gitFunctions.Review(argList)
207         if len(a:argList) == 0
208                 let revision = 'HEAD'
209         else
210                 let revision = a:argList[0]
211         endif
213         let oldCwd = VCSCommandChangeToCurrentFileDir(resolve(bufname('%')))
214         try
215                 let prefix = system(VCSCommandGetOption('VCSCommandGitExec', 'git') . ' rev-parse --show-prefix')
216         finally
217                 call VCSCommandChdir(oldCwd)
218         endtry
220         let prefix = substitute(prefix, '\n$', '', '')
221         let blob = revision . ':' . prefix . '<VCSCOMMANDFILE>' 
222         let resultBuffer = s:DoCommand('show ' . blob, 'review', revision, {})
223         if resultBuffer > 0
224                 let &filetype=getbufvar(b:VCSCommandOriginalBuffer, '&filetype')
225         endif
226         return resultBuffer
227 endfunction
229 " Function: s:gitFunctions.Status(argList) {{{2
230 function! s:gitFunctions.Status(argList)
231         return s:DoCommand(join(['status'] + a:argList), 'log', join(a:argList), {'allowNonZeroExit': 1})
232 endfunction
234 " Function: s:gitFunctions.Update(argList) {{{2
235 function! s:gitFunctions.Update(argList)
236         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."
237 endfunction
240 " Section: Plugin Registration {{{1
241 call VCSCommandRegisterModule('git', expand('<sfile>'), s:gitFunctions, [])
243 let &cpo = s:save_cpo