1 " fugitive.vim - A Git wrapper so awesome, it should be illegal
2 " Maintainer: Tim Pope <http://tpo.pe/>
4 " GetLatestVimScripts: 2975 1 :AutoInstall: fugitive.vim
6 if exists('g:loaded_fugitive')
9 let g:loaded_fugitive = 1
11 function! FugitiveGitDir(...) abort
12 if !a:0 || type(a:1) == type(0) && a:1 < 0
13 if exists('g:fugitive_event')
14 return g:fugitive_event
16 let dir = get(b:, 'git_dir', '')
17 if empty(dir) && (empty(bufname('')) || &buftype =~# '^\%(nofile\|acwrite\|quickfix\|prompt\)$')
18 return FugitiveExtractGitDir(getcwd())
21 elseif type(a:1) == type(0)
22 return getbufvar(a:1, 'git_dir')
23 elseif type(a:1) == type('')
24 return substitute(s:Slash(a:1), '/$', '', '')
30 " FugitiveReal() takes a fugitive:// URL and returns the corresponding path in
31 " the work tree. This may be useful to get a cleaner path for inclusion in
32 " the statusline, for example. Note that the file and its parent directories
33 " are not guaranteed to exist.
35 " This is intended as an abstract API to be used on any "virtual" path. For a
36 " buffer named foo://bar, check for a function named FooReal(), and if it
37 " exists, call FooReal("foo://bar").
38 function! FugitiveReal(...) abort
39 let file = a:0 ? a:1 : @%
40 if file =~# '^\a\a\+:' || a:0 > 1
41 return call('fugitive#Real', [file] + a:000[1:-1])
42 elseif file =~# '^/\|^\a:\|^$'
45 return fnamemodify(file, ':p' . (file =~# '[\/]$' ? '' : ':s?[\/]$??'))
49 " FugitiveFind() takes a Fugitive object and returns the appropriate Vim
50 " buffer name. You can use this to generate Fugitive URLs ("HEAD:README") or
51 " to get the absolute path to a file in the Git dir (".git/HEAD"), the common
52 " dir (".git/config"), or the work tree (":(top)Makefile").
54 " An optional second argument provides the Git dir, or the buffer number of a
55 " buffer with a Git dir. The default is the current buffer.
56 function! FugitiveFind(...) abort
57 return fugitive#Find(a:0 ? a:1 : bufnr(''), FugitiveGitDir(a:0 > 1 ? a:2 : -1))
60 function! FugitivePath(...) abort
62 return fugitive#Path(a:1, a:2, FugitiveGitDir(a:0 > 2 ? a:3 : -1))
64 return FugitiveReal(a:0 ? a:1 : @%)
68 " FugitiveParse() takes a fugitive:// URL and returns a 2 element list
69 " containing the Git dir and an object name ("commit:file"). It's effectively
70 " then inverse of FugitiveFind().
71 function! FugitiveParse(...) abort
72 let path = s:Slash(a:0 ? a:1 : @%)
73 if path !~# '^fugitive:'
76 let vals = matchlist(path, '\c^fugitive:\%(//\)\=\(.\{-\}\)\%(//\|::\)\(\x\{40,\}\|[0-3]\)\(/.*\)\=$')
78 return [(vals[2] =~# '^.$' ? ':' : '') . vals[2] . substitute(vals[3], '^/', ':', ''), vals[1]]
80 let v:errmsg = 'fugitive: invalid Fugitive URL ' . path
84 " FugitivePrepare() constructs a Git command string which can be executed with
85 " functions like system() and commands like :!. Integer arguments will be
86 " treated as buffer numbers, and the appropriate relative path inserted in
89 " If the first argument is a string that looks like a path or an empty string,
90 " it will be used as the Git dir. If it's a buffer number, the Git dir for
91 " that buffer will be used. The default is the current buffer.
92 function! FugitivePrepare(...) abort
93 return call('fugitive#Prepare', a:000)
96 function! FugitiveConfig(...) abort
97 if a:0 == 2 && type(a:2) != type({})
98 return fugitive#Config(a:1, FugitiveGitDir(a:2))
99 elseif a:0 == 1 && a:1 !~# '^[[:alnum:]-]\+\.'
100 return fugitive#Config(FugitiveGitDir(a:1))
102 return call('fugitive#Config', a:000)
106 function! FugitiveRemoteUrl(...) abort
107 return fugitive#RemoteUrl(a:0 ? a:1 : '', FugitiveGitDir(a:0 > 1 ? a:2 : -1))
110 function! FugitiveHead(...) abort
111 let dir = FugitiveGitDir(a:0 > 1 ? a:2 : -1)
115 return fugitive#Head(a:0 ? a:1 : 0, dir)
118 function! FugitiveStatusline(...) abort
119 if !exists('b:git_dir')
122 return fugitive#Statusline()
125 function! FugitiveCommonDir(...) abort
126 let dir = FugitiveGitDir(a:0 ? a:1 : -1)
130 return fugitive#CommonDir(dir)
133 function! FugitiveWorkTree(...) abort
134 return s:Tree(FugitiveGitDir(a:0 ? a:1 : -1))
137 function! FugitiveIsGitDir(path) abort
138 let path = substitute(a:path, '[\/]$', '', '') . '/'
139 return len(a:path) && getfsize(path.'HEAD') > 10 && (
140 \ isdirectory(path.'objects') && isdirectory(path.'refs') ||
141 \ getftype(path.'commondir') ==# 'file')
144 let s:worktree_for_dir = {}
145 let s:dir_for_worktree = {}
146 function! s:Tree(path) abort
149 return len(dir) ==# 5 ? '/' : dir[0:-6]
153 if !has_key(s:worktree_for_dir, dir)
154 let s:worktree_for_dir[dir] = ''
155 let config_file = dir . '/config'
156 if filereadable(config_file)
157 let config = readfile(config_file,'',10)
158 call filter(config,'v:val =~# "^\\s*worktree *="')
160 let worktree = s:Slash(FugitiveVimPath(matchstr(config[0], '= *\zs.*')))
162 elseif filereadable(dir . '/gitdir')
163 let worktree = s:Slash(fnamemodify(FugitiveVimPath(readfile(dir . '/gitdir')[0]), ':h'))
168 if exists('worktree')
169 let s:worktree_for_dir[dir] = worktree
170 let s:dir_for_worktree[s:worktree_for_dir[dir]] = dir
173 if s:worktree_for_dir[dir] =~# '^\.'
174 return simplify(dir . '/' . s:worktree_for_dir[dir])
176 return s:worktree_for_dir[dir]
180 function! FugitiveExtractGitDir(path) abort
181 let path = s:Slash(a:path)
182 if path =~# '^fugitive:'
183 return matchstr(path, '\C^fugitive:\%(//\)\=\zs.\{-\}\ze\%(//\|::\|$\)')
184 elseif isdirectory(path)
185 let path = fnamemodify(path, ':p:s?/$??')
187 let path = fnamemodify(path, ':p:h:s?/$??')
189 let pre = substitute(matchstr(path, '^\a\a\+\ze:'), '^.', '\u&', '')
190 if len(pre) && exists('*' . pre . 'Real')
191 let path = s:Slash({pre}Real(path))
193 let root = resolve(path)
195 silent! exe (haslocaldir() ? 'lcd' : exists(':tcd') && haslocaldir(-1) ? 'tcd' : 'cd') '.'
198 let env_git_dir = len($GIT_DIR) ? s:Slash(simplify(fnamemodify(FugitiveVimPath($GIT_DIR), ':p:s?[\/]$??'))) : ''
199 call s:Tree(env_git_dir)
200 while root !=# previous
201 if root =~# '\v^//%([^/]+/?)?$'
204 if index(split($GIT_CEILING_DIRECTORIES, ':'), root) >= 0
207 if root ==# $GIT_WORK_TREE && FugitiveIsGitDir(env_git_dir)
209 elseif has_key(s:dir_for_worktree, root)
210 return s:dir_for_worktree[root]
212 let dir = substitute(root, '[\/]$', '', '') . '/.git'
213 let type = getftype(dir)
214 if type ==# 'dir' && FugitiveIsGitDir(dir)
216 elseif type ==# 'link' && FugitiveIsGitDir(dir)
218 elseif type !=# '' && filereadable(dir)
219 let line = get(readfile(dir, '', 1), 0, '')
220 let file_dir = s:Slash(FugitiveVimPath(matchstr(line, '^gitdir: \zs.*')))
221 if file_dir !~# '^/\|^\a:' && FugitiveIsGitDir(root . '/' . file_dir)
222 return simplify(root . '/' . file_dir)
223 elseif len(file_dir) && FugitiveIsGitDir(file_dir)
226 elseif FugitiveIsGitDir(root)
230 let root = fnamemodify(root, ':h')
235 function! FugitiveDetect(path) abort
236 if exists('b:git_dir') && b:git_dir =~# '^$\|/$\|^fugitive:'
239 if !exists('b:git_dir')
240 let dir = FugitiveExtractGitDir(a:path)
245 if exists('b:git_dir')
246 return fugitive#Init()
250 function! FugitiveVimPath(path) abort
251 if exists('+shellslash') && !&shellslash
252 return tr(a:path, '/', '\')
258 function! FugitiveGitPath(path) abort
259 return s:Slash(a:path)
262 function! s:Slash(path) abort
263 if exists('+shellslash')
264 return tr(a:path, '\', '/')
270 function! s:ProjectionistDetect() abort
271 let file = s:Slash(get(g:, 'projectionist_file', ''))
272 let dir = FugitiveExtractGitDir(file)
273 let base = matchstr(file, '^fugitive://.\{-\}//\x\+')
275 let base = s:Tree(dir)
278 if exists('+shellslash') && !&shellslash
279 let base = tr(base, '/', '\')
281 let file = FugitiveCommonDir(dir) . '/info/projections.json'
282 if filereadable(file)
283 call projectionist#append(base, file)
288 let g:io_fugitive = {
289 \ 'simplify': function('fugitive#simplify'),
290 \ 'resolve': function('fugitive#resolve'),
291 \ 'getftime': function('fugitive#getftime'),
292 \ 'getfsize': function('fugitive#getfsize'),
293 \ 'getftype': function('fugitive#getftype'),
294 \ 'filereadable': function('fugitive#filereadable'),
295 \ 'filewritable': function('fugitive#filewritable'),
296 \ 'isdirectory': function('fugitive#isdirectory'),
297 \ 'getfperm': function('fugitive#getfperm'),
298 \ 'setfperm': function('fugitive#setfperm'),
299 \ 'readfile': function('fugitive#readfile'),
300 \ 'writefile': function('fugitive#writefile'),
301 \ 'glob': function('fugitive#glob'),
302 \ 'delete': function('fugitive#delete'),
303 \ 'Real': function('FugitiveReal')}
308 autocmd BufNewFile,BufReadPost * call FugitiveDetect(expand('<amatch>:p'))
309 autocmd FileType netrw call FugitiveDetect(fnamemodify(get(b:, 'netrw_curdir', expand('<amatch>')), ':p'))
312 \ if len(FugitiveGitDir()) |
313 \ call fugitive#MapJumps() |
314 \ call fugitive#MapCfile() |
316 autocmd FileType gitcommit
317 \ if len(FugitiveGitDir()) |
318 \ call fugitive#MapCfile('fugitive#MessageCfile()') |
320 autocmd FileType fugitive
321 \ if len(FugitiveGitDir()) |
322 \ call fugitive#MapCfile('fugitive#StatusCfile()') |
324 autocmd FileType gitrebase
325 \ let &l:include = '^\%(pick\|squash\|edit\|reword\|fixup\|drop\|[pserfd]\)\>' |
326 \ if len(FugitiveGitDir()) |
327 \ let &l:includeexpr = 'v:fname =~# ''^\x\{4,\}$'' ? FugitiveFind(v:fname) : ' .
328 \ (len(&l:includeexpr) ? &l:includeexpr : 'v:fname') |
330 \ let b:undo_ftplugin = get(b:, 'undo_ftplugin', 'exe') . '|setl inex= inc='
332 autocmd BufReadCmd index{,.lock}
333 \ if FugitiveIsGitDir(expand('<amatch>:p:h')) |
334 \ let b:git_dir = s:Slash(expand('<amatch>:p:h')) |
335 \ exe fugitive#BufReadStatus() |
336 \ elseif filereadable(expand('<amatch>')) |
337 \ silent doautocmd BufReadPre |
338 \ keepalt read <amatch> |
340 \ silent doautocmd BufReadPost |
342 \ silent doautocmd BufNewFile |
345 autocmd BufReadCmd fugitive://*//* exe fugitive#BufReadCmd() |
346 \ if &path =~# '^\.\%(,\|$\)' |
347 \ let &l:path = substitute(&path, '^\.,\=', '', '') |
349 autocmd BufWriteCmd fugitive://*//[0-3]/* exe fugitive#BufWriteCmd()
350 autocmd FileReadCmd fugitive://*//* exe fugitive#FileReadCmd()
351 autocmd FileWriteCmd fugitive://*//[0-3]/* exe fugitive#FileWriteCmd()
352 if exists('##SourceCmd')
353 autocmd SourceCmd fugitive://*//* nested exe fugitive#SourceCmd()
356 autocmd User Flags call Hoist('buffer', function('FugitiveStatusline'))
358 autocmd User ProjectionistDetect call s:ProjectionistDetect()
361 let s:addr_other = has('patch-8.1.560') ? '-addr=other' : ''
362 let s:addr_tabs = has('patch-7.4.542') ? '-addr=tabs' : ''
363 let s:addr_wins = has('patch-7.4.542') ? '-addr=windows' : ''
365 exe 'command! -bang -nargs=? -range=-1' s:addr_other '-complete=customlist,fugitive#Complete G exe fugitive#Command(<line1>, <count>, +"<range>", <bang>0, "<mods>", <q-args>)'
366 exe 'command! -bang -nargs=? -range=-1' s:addr_other '-complete=customlist,fugitive#Complete Git exe fugitive#Command(<line1>, <count>, +"<range>", <bang>0, "<mods>", <q-args>)'
368 exe 'command! -bang -bar -range=-1' s:addr_other 'Gstatus exe fugitive#Command(<line1>, <count>, +"<range>", <bang>0, "<mods>", <q-args>)'
370 exe 'command! -bang -nargs=? -range=-1' s:addr_other '-complete=customlist,fugitive#CommitComplete Gcommit exe fugitive#Command(<line1>, <count>, +"<range>", <bang>0, "<mods>", "commit " . <q-args>)'
371 exe 'command! -bang -nargs=? -range=-1' s:addr_other '-complete=customlist,fugitive#RevertComplete Grevert exe fugitive#Command(<line1>, <count>, +"<range>", <bang>0, "<mods>", "revert " . <q-args>)'
372 exe 'command! -bang -nargs=? -range=-1' s:addr_other '-complete=customlist,fugitive#MergeComplete Gmerge exe fugitive#Command(<line1>, <count>, +"<range>", <bang>0, "<mods>", "merge " . <q-args>)'
373 exe 'command! -bang -nargs=? -range=-1' s:addr_other '-complete=customlist,fugitive#RebaseComplete Grebase exe fugitive#Command(<line1>, <count>, +"<range>", <bang>0, "<mods>", "rebase " . <q-args>)'
374 exe 'command! -bang -nargs=? -range=-1' s:addr_other '-complete=customlist,fugitive#PullComplete Gpull exe fugitive#Command(<line1>, <count>, +"<range>", <bang>0, "<mods>", "pull " . <q-args>)'
375 exe 'command! -bang -nargs=? -range=-1' s:addr_other '-complete=customlist,fugitive#PushComplete Gpush exe fugitive#Command(<line1>, <count>, +"<range>", <bang>0, "<mods>", "push " . <q-args>)'
376 exe 'command! -bang -nargs=? -range=-1' s:addr_other '-complete=customlist,fugitive#FetchComplete Gfetch exe fugitive#Command(<line1>, <count>, +"<range>", <bang>0, "<mods>", "fetch " . <q-args>)'
377 exe 'command! -bang -nargs=? -range=-1 -complete=customlist,fugitive#BlameComplete Gblame exe fugitive#Command(<line1>, <count>, +"<range>", <bang>0, "<mods>", "blame " . <q-args>)'
379 exe "command! -bar -bang -nargs=? -complete=customlist,fugitive#CdComplete Gcd exe fugitive#Cd(<q-args>, 0)"
380 exe "command! -bar -bang -nargs=? -complete=customlist,fugitive#CdComplete Glcd exe fugitive#Cd(<q-args>, 1)"
382 exe 'command! -bang -nargs=? -range=-1' s:addr_wins '-complete=customlist,fugitive#GrepComplete Ggrep exe fugitive#Command(<line1>, <count>, +"<range>", <bang>0, "<mods>", "grep " . <q-args>)'
383 exe 'command! -bang -nargs=? -range=-1' s:addr_wins '-complete=customlist,fugitive#GrepComplete Gcgrep exe fugitive#Command(<line1>, <count>, +"<range>", <bang>0, "<mods>", "grep " . <q-args>)'
384 exe 'command! -bang -nargs=? -range=-1' s:addr_wins '-complete=customlist,fugitive#GrepComplete Glgrep exe fugitive#Command(0, <count> > 0 ? <count> : 0, +"<range>", <bang>0, "<mods>", "grep " . <q-args>)'
386 exe 'command! -bang -nargs=? -range=-1 -complete=customlist,fugitive#LogComplete Glog :exe fugitive#LogCommand(<line1>,<count>,+"<range>",<bang>0,"<mods>",<q-args>, "")'
387 exe 'command! -bang -nargs=? -range=-1 -complete=customlist,fugitive#LogComplete Gclog :exe fugitive#LogCommand(<line1>,<count>,+"<range>",<bang>0,"<mods>",<q-args>, "c")'
388 exe 'command! -bang -nargs=? -range=-1 -complete=customlist,fugitive#LogComplete Gllog :exe fugitive#LogCommand(<line1>,<count>,+"<range>",<bang>0,"<mods>",<q-args>, "l")'
390 exe 'command! -bar -bang -nargs=* -complete=customlist,fugitive#EditComplete Ge exe fugitive#Open("edit<bang>", 0, "<mods>", <q-args>, [<f-args>])'
391 exe 'command! -bar -bang -nargs=* -complete=customlist,fugitive#EditComplete Gedit exe fugitive#Open("edit<bang>", 0, "<mods>", <q-args>, [<f-args>])'
392 exe 'command! -bar -bang -nargs=* -complete=customlist,fugitive#ReadComplete Gpedit exe fugitive#Open("pedit", <bang>0, "<mods>", <q-args>, [<f-args>])'
393 exe 'command! -bar -bang -nargs=* -range=-1' s:addr_other '-complete=customlist,fugitive#ReadComplete Gsplit exe fugitive#Open((<count> > 0 ? <count> : "").(<count> ? "split" : "edit"), <bang>0, "<mods>", <q-args>, [<f-args>])'
394 exe 'command! -bar -bang -nargs=* -range=-1' s:addr_other '-complete=customlist,fugitive#ReadComplete Gvsplit exe fugitive#Open((<count> > 0 ? <count> : "").(<count> ? "vsplit" : "edit!"), <bang>0, "<mods>", <q-args>, [<f-args>])'
395 exe 'command! -bar -bang -nargs=* -range=-1' s:addr_tabs '-complete=customlist,fugitive#ReadComplete Gtabedit exe fugitive#Open((<count> >= 0 ? <count> : "")."tabedit", <bang>0, "<mods>", <q-args>, [<f-args>])'
397 if exists(':Gr') != 2
398 exe 'command! -bar -bang -nargs=* -range=-1 -complete=customlist,fugitive#ReadComplete Gr exe fugitive#ReadCommand(<line1>, <count>, +"<range>", <bang>0, "<mods>", <q-args>, [<f-args>])'
400 exe 'command! -bar -bang -nargs=* -range=-1 -complete=customlist,fugitive#ReadComplete Gread exe fugitive#ReadCommand(<line1>, <count>, +"<range>", <bang>0, "<mods>", <q-args>, [<f-args>])'
402 exe 'command! -bar -bang -nargs=* -complete=customlist,fugitive#EditComplete Gdiffsplit exe fugitive#Diffsplit(1, <bang>0, "<mods>", <q-args>, [<f-args>])'
403 exe 'command! -bar -bang -nargs=* -complete=customlist,fugitive#EditComplete Ghdiffsplit exe fugitive#Diffsplit(0, <bang>0, "<mods>", <q-args>, [<f-args>])'
404 exe 'command! -bar -bang -nargs=* -complete=customlist,fugitive#EditComplete Gvdiffsplit exe fugitive#Diffsplit(0, <bang>0, "vert <mods>", <q-args>, [<f-args>])'
406 exe 'command! -bar -bang -nargs=* -complete=customlist,fugitive#EditComplete Gw exe fugitive#WriteCommand(<line1>, <count>, +"<range>", <bang>0, "<mods>", <q-args>, [<f-args>])'
407 exe 'command! -bar -bang -nargs=* -complete=customlist,fugitive#EditComplete Gwrite exe fugitive#WriteCommand(<line1>, <count>, +"<range>", <bang>0, "<mods>", <q-args>, [<f-args>])'
408 exe 'command! -bar -bang -nargs=* -complete=customlist,fugitive#EditComplete Gwq exe fugitive#WqCommand( <line1>, <count>, +"<range>", <bang>0, "<mods>", <q-args>, [<f-args>])'
410 exe 'command! -bar -bang -nargs=0 -complete=customlist,fugitive#CompleteObject Gremove exe fugitive#RemoveCommand(<line1>, <count>, +"<range>", <bang>0, "<mods>", <q-args>, [<f-args>])'
411 exe 'command! -bar -bang -nargs=0 -complete=customlist,fugitive#CompleteObject Gdelete exe fugitive#DeleteCommand(<line1>, <count>, +"<range>", <bang>0, "<mods>", <q-args>, [<f-args>])'
412 exe 'command! -bar -bang -nargs=1 -complete=customlist,fugitive#CompleteObject Gmove exe fugitive#MoveCommand( <line1>, <count>, +"<range>", <bang>0, "<mods>", <q-args>, [<f-args>])'
413 exe 'command! -bar -bang -nargs=1 -complete=customlist,fugitive#RenameComplete Grename exe fugitive#RenameCommand(<line1>, <count>, +"<range>", <bang>0, "<mods>", <q-args>, [<f-args>])'
415 exe 'command! -bar -bang -range=-1 -nargs=* -complete=customlist,fugitive#CompleteObject Gbrowse exe fugitive#BrowseCommand(<line1>, <count>, +"<range>", <bang>0, "<mods>", <q-args>, [<f-args>])'
417 if get(g:, 'fugitive_no_maps')
421 let s:nowait = v:version >= 704 ? '<nowait>' : ''
423 function! s:Map(mode, lhs, rhs, ...) abort
424 for mode in split(a:mode, '\zs')
425 let flags = (a:0 ? a:1 : '') . (a:rhs =~# '<Plug>' ? '' : '<script>')
428 let keys = get(g:, mode.'remap', {})
429 if type(keys) == type([])
433 if has_key(keys, head)
434 let head = keys[head]
440 let tail = matchstr(head, '<[^<>]*>$\|.$') . tail
441 let head = substitute(head, '<[^<>]*>$\|.$', '', '')
443 if flags !~# '<unique>' || empty(mapcheck(head.tail, mode))
444 exe mode.'map' s:nowait flags head.tail a:rhs
449 call s:Map('c', '<C-R><C-G>', 'fnameescape(fugitive#Object(@%))', '<expr>')
450 call s:Map('n', 'y<C-G>', ':<C-U>call setreg(v:register, fugitive#Object(@%))<CR>', '<silent>')