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 let dir = get(b:, 'git_dir', '')
14 if empty(dir) && (empty(bufname('')) || &buftype =~# '^\%(nofile\|acwrite\|quickfix\|prompt\)$')
15 return FugitiveExtractGitDir(getcwd())
18 elseif type(a:1) == type(0)
19 return getbufvar(a:1, 'git_dir')
20 elseif type(a:1) == type('')
21 return substitute(s:Slash(a:1), '/$', '', '')
27 " FugitiveReal() takes a fugitive:// URL and returns the corresponding path in
28 " the work tree. This may be useful to get a cleaner path for inclusion in
29 " the statusline, for example. Note that the file and its parent directories
30 " are not guaranteed to exist.
32 " This is intended as an abstract API to be used on any "virtual" path. For a
33 " buffer named foo://bar, check for a function named FooReal(), and if it
34 " exists, call FooReal("foo://bar").
35 function! FugitiveReal(...) abort
36 let file = a:0 ? a:1 : @%
37 if file =~# '^\a\a\+:' || a:0 > 1
38 return call('fugitive#Real', [file] + a:000[1:-1])
39 elseif file =~# '^/\|^\a:\|^$'
42 return fnamemodify(file, ':p' . (file =~# '[\/]$' ? '' : ':s?[\/]$??'))
46 " FugitiveFind() takes a Fugitive object and returns the appropriate Vim
47 " buffer name. You can use this to generate Fugitive URLs ("HEAD:README") or
48 " to get the absolute path to a file in the Git dir (".git/HEAD"), the common
49 " dir (".git/config"), or the work tree (":(top)Makefile").
51 " An optional second argument provides the Git dir, or the buffer number of a
52 " buffer with a Git dir. The default is the current buffer.
53 function! FugitiveFind(...) abort
54 return fugitive#Find(a:0 ? a:1 : bufnr(''), FugitiveGitDir(a:0 > 1 ? a:2 : -1))
57 function! FugitivePath(...) abort
59 return fugitive#Path(a:1, a:2, FugitiveGitDir(a:0 > 2 ? a:3 : -1))
61 return FugitiveReal(a:0 ? a:1 : @%)
65 " FugitiveParse() takes a fugitive:// URL and returns a 2 element list
66 " containing the Git dir and an object name ("commit:file"). It's effectively
67 " then inverse of FugitiveFind().
68 function! FugitiveParse(...) abort
69 let path = s:Slash(a:0 ? a:1 : @%)
70 if path !~# '^fugitive:'
73 let vals = matchlist(path, '\c^fugitive:\%(//\)\=\(.\{-\}\)\%(//\|::\)\(\x\{40,\}\|[0-3]\)\(/.*\)\=$')
75 return [(vals[2] =~# '^.$' ? ':' : '') . vals[2] . substitute(vals[3], '^/', ':', ''), vals[1]]
77 let v:errmsg = 'fugitive: invalid Fugitive URL ' . path
81 " FugitivePrepare() constructs a Git command string which can be executed with
82 " functions like system() and commands like :!. Integer arguments will be
83 " treated as buffer numbers, and the appropriate relative path inserted in
86 " If the first argument is a string that looks like a path or an empty string,
87 " it will be used as the Git dir. If it's a buffer number, the Git dir for
88 " that buffer will be used. The default is the current buffer.
89 function! FugitivePrepare(...) abort
90 return call('fugitive#Prepare', a:000)
93 function! FugitiveConfig(...) abort
94 if a:0 == 2 && type(a:2) != type({})
95 return fugitive#Config(a:1, FugitiveGitDir(a:2))
96 elseif a:0 == 1 && a:1 !~# '^[[:alnum:]-]\+\.'
97 return fugitive#Config(FugitiveGitDir(a:1))
99 return call('fugitive#Config', a:000)
103 function! FugitiveRemoteUrl(...) abort
104 return fugitive#RemoteUrl(a:0 ? a:1 : '', FugitiveGitDir(a:0 > 1 ? a:2 : -1))
107 function! FugitiveHead(...) abort
108 let dir = FugitiveGitDir(a:0 > 1 ? a:2 : -1)
112 return fugitive#Head(a:0 ? a:1 : 0, dir)
115 function! FugitiveStatusline(...) abort
116 if !exists('b:git_dir')
119 return fugitive#Statusline()
122 function! FugitiveCommonDir(...) abort
123 let dir = FugitiveGitDir(a:0 ? a:1 : -1)
127 return fugitive#CommonDir(dir)
130 function! FugitiveWorkTree(...) abort
131 return s:Tree(FugitiveGitDir(a:0 ? a:1 : -1))
134 function! FugitiveIsGitDir(path) abort
135 let path = substitute(a:path, '[\/]$', '', '') . '/'
136 return len(a:path) && getfsize(path.'HEAD') > 10 && (
137 \ isdirectory(path.'objects') && isdirectory(path.'refs') ||
138 \ getftype(path.'commondir') ==# 'file')
141 let s:worktree_for_dir = {}
142 let s:dir_for_worktree = {}
143 function! s:Tree(path) abort
146 return len(dir) ==# 5 ? '/' : dir[0:-6]
150 if !has_key(s:worktree_for_dir, dir)
151 let s:worktree_for_dir[dir] = ''
152 let config_file = dir . '/config'
153 if filereadable(config_file)
154 let config = readfile(config_file,'',10)
155 call filter(config,'v:val =~# "^\\s*worktree *="')
157 let worktree = s:Slash(FugitiveVimPath(matchstr(config[0], '= *\zs.*')))
159 elseif filereadable(dir . '/gitdir')
160 let worktree = s:Slash(fnamemodify(FugitiveVimPath(readfile(dir . '/gitdir')[0]), ':h'))
165 if exists('worktree')
166 let s:worktree_for_dir[dir] = worktree
167 let s:dir_for_worktree[s:worktree_for_dir[dir]] = dir
170 if s:worktree_for_dir[dir] =~# '^\.'
171 return simplify(dir . '/' . s:worktree_for_dir[dir])
173 return s:worktree_for_dir[dir]
177 function! FugitiveExtractGitDir(path) abort
178 let path = s:Slash(a:path)
179 if path =~# '^fugitive:'
180 return matchstr(path, '\C^fugitive:\%(//\)\=\zs.\{-\}\ze\%(//\|::\|$\)')
181 elseif isdirectory(path)
182 let path = fnamemodify(path, ':p:s?/$??')
184 let path = fnamemodify(path, ':p:h:s?/$??')
186 let pre = substitute(matchstr(path, '^\a\a\+\ze:'), '^.', '\u&', '')
187 if len(pre) && exists('*' . pre . 'Real')
188 let path = s:Slash({pre}Real(path))
190 let root = resolve(path)
192 silent! exe (haslocaldir() ? 'lcd' : exists(':tcd') && haslocaldir(-1) ? 'tcd' : 'cd') '.'
195 let env_git_dir = len($GIT_DIR) ? s:Slash(simplify(fnamemodify(FugitiveVimPath($GIT_DIR), ':p:s?[\/]$??'))) : ''
196 call s:Tree(env_git_dir)
197 while root !=# previous
198 if root =~# '\v^//%([^/]+/?)?$'
201 if index(split($GIT_CEILING_DIRECTORIES, ':'), root) >= 0
204 if root ==# $GIT_WORK_TREE && FugitiveIsGitDir(env_git_dir)
206 elseif has_key(s:dir_for_worktree, root)
207 return s:dir_for_worktree[root]
209 let dir = substitute(root, '[\/]$', '', '') . '/.git'
210 let type = getftype(dir)
211 if type ==# 'dir' && FugitiveIsGitDir(dir)
213 elseif type ==# 'link' && FugitiveIsGitDir(dir)
215 elseif type !=# '' && filereadable(dir)
216 let line = get(readfile(dir, '', 1), 0, '')
217 let file_dir = s:Slash(FugitiveVimPath(matchstr(line, '^gitdir: \zs.*')))
218 if file_dir !~# '^/\|^\a:' && FugitiveIsGitDir(root . '/' . file_dir)
219 return simplify(root . '/' . file_dir)
220 elseif len(file_dir) && FugitiveIsGitDir(file_dir)
223 elseif FugitiveIsGitDir(root)
227 let root = fnamemodify(root, ':h')
232 function! FugitiveDetect(path) abort
233 if exists('b:git_dir') && b:git_dir =~# '^$\|/$\|^fugitive:'
236 if !exists('b:git_dir')
237 let dir = FugitiveExtractGitDir(a:path)
242 if exists('b:git_dir')
243 return fugitive#Init()
247 function! FugitiveVimPath(path) abort
248 if exists('+shellslash') && !&shellslash
249 return tr(a:path, '/', '\')
255 function! FugitiveGitPath(path) abort
256 return s:Slash(a:path)
259 function! s:Slash(path) abort
260 if exists('+shellslash')
261 return tr(a:path, '\', '/')
267 function! s:ProjectionistDetect() abort
268 let file = s:Slash(get(g:, 'projectionist_file', ''))
269 let dir = FugitiveExtractGitDir(file)
270 let base = matchstr(file, '^fugitive://.\{-\}//\x\+')
272 let base = s:Tree(dir)
275 if exists('+shellslash') && !&shellslash
276 let base = tr(base, '/', '\')
278 let file = FugitiveCommonDir(dir) . '/info/projections.json'
279 if filereadable(file)
280 call projectionist#append(base, file)
285 let g:io_fugitive = {
286 \ 'simplify': function('fugitive#simplify'),
287 \ 'resolve': function('fugitive#resolve'),
288 \ 'getftime': function('fugitive#getftime'),
289 \ 'getfsize': function('fugitive#getfsize'),
290 \ 'getftype': function('fugitive#getftype'),
291 \ 'filereadable': function('fugitive#filereadable'),
292 \ 'filewritable': function('fugitive#filewritable'),
293 \ 'isdirectory': function('fugitive#isdirectory'),
294 \ 'getfperm': function('fugitive#getfperm'),
295 \ 'setfperm': function('fugitive#setfperm'),
296 \ 'readfile': function('fugitive#readfile'),
297 \ 'writefile': function('fugitive#writefile'),
298 \ 'glob': function('fugitive#glob'),
299 \ 'delete': function('fugitive#delete'),
300 \ 'Real': function('FugitiveReal')}
305 autocmd BufNewFile,BufReadPost * call FugitiveDetect(expand('<amatch>:p'))
306 autocmd FileType netrw call FugitiveDetect(fnamemodify(get(b:, 'netrw_curdir', expand('<amatch>')), ':p'))
309 \ if len(FugitiveGitDir()) |
310 \ call fugitive#MapJumps() |
311 \ call fugitive#MapCfile() |
313 autocmd FileType gitcommit
314 \ if len(FugitiveGitDir()) |
315 \ call fugitive#MapCfile('fugitive#MessageCfile()') |
317 autocmd FileType fugitive
318 \ if len(FugitiveGitDir()) |
319 \ call fugitive#MapCfile('fugitive#StatusCfile()') |
321 autocmd FileType gitrebase
322 \ let &l:include = '^\%(pick\|squash\|edit\|reword\|fixup\|drop\|[pserfd]\)\>' |
323 \ if len(FugitiveGitDir()) |
324 \ let &l:includeexpr = 'v:fname =~# ''^\x\{4,\}$'' ? FugitiveFind(v:fname) : ' .
325 \ (len(&l:includeexpr) ? &l:includeexpr : 'v:fname') |
327 \ let b:undo_ftplugin = get(b:, 'undo_ftplugin', 'exe') . '|setl inex= inc='
329 autocmd BufReadCmd index{,.lock}
330 \ if FugitiveIsGitDir(expand('<amatch>:p:h')) |
331 \ let b:git_dir = s:Slash(expand('<amatch>:p:h')) |
332 \ exe fugitive#BufReadStatus() |
333 \ elseif filereadable(expand('<amatch>')) |
334 \ silent doautocmd BufReadPre |
335 \ keepalt read <amatch> |
337 \ silent doautocmd BufReadPost |
339 \ silent doautocmd BufNewFile |
342 autocmd BufReadCmd fugitive://*//* exe fugitive#BufReadCmd() |
343 \ if &path =~# '^\.\%(,\|$\)' |
344 \ let &l:path = substitute(&path, '^\.,\=', '', '') |
346 autocmd BufWriteCmd fugitive://*//[0-3]/* exe fugitive#BufWriteCmd()
347 autocmd FileReadCmd fugitive://*//* exe fugitive#FileReadCmd()
348 autocmd FileWriteCmd fugitive://*//[0-3]/* exe fugitive#FileWriteCmd()
349 if exists('##SourceCmd')
350 autocmd SourceCmd fugitive://*//* nested exe fugitive#SourceCmd()
353 autocmd User Flags call Hoist('buffer', function('FugitiveStatusline'))
355 autocmd User ProjectionistDetect call s:ProjectionistDetect()
358 let s:addr_other = has('patch-8.1.560') ? '-addr=other' : ''
359 let s:addr_tabs = has('patch-7.4.542') ? '-addr=tabs' : ''
360 let s:addr_wins = has('patch-7.4.542') ? '-addr=windows' : ''
361 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>)'
362 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>)'
364 exe "command! -bar -bang -nargs=? -complete=customlist,fugitive#CdComplete Gcd exe fugitive#Cd(<q-args>, 0)"
365 exe "command! -bar -bang -nargs=? -complete=customlist,fugitive#CdComplete Glcd exe fugitive#Cd(<q-args>, 1)"
367 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>)'
368 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>)'
369 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>)'
371 exe 'command! -bang -nargs=? -range=-1' s:addr_other '-complete=customlist,fugitive#LogComplete Glog :exe fugitive#LogCommand(<line1>,<count>,+"<range>",<bang>0,"<mods>",<q-args>, "")'
372 exe 'command! -bang -nargs=? -range=-1' s:addr_other '-complete=customlist,fugitive#LogComplete Gclog :exe fugitive#LogCommand(<line1>,<count>,+"<range>",<bang>0,"<mods>",<q-args>, "c")'
373 exe 'command! -bang -nargs=? -range=-1' s:addr_other '-complete=customlist,fugitive#LogComplete Gllog :exe fugitive#LogCommand(<line1>,<count>,+"<range>",<bang>0,"<mods>",<q-args>, "l")'
375 exe 'command! -bar -bang -nargs=* -complete=customlist,fugitive#CompleteObject Ge exe fugitive#Open("edit<bang>", 0, "<mods>", <q-args>, [<f-args>])'
376 exe 'command! -bar -bang -nargs=* -complete=customlist,fugitive#CompleteObject Gedit exe fugitive#Open("edit<bang>", 0, "<mods>", <q-args>, [<f-args>])'
377 exe 'command! -bar -bang -nargs=* -complete=customlist,fugitive#ReadComplete Gpedit exe fugitive#Open("pedit", <bang>0, "<mods>", <q-args>, [<f-args>])'
378 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>])'
379 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>])'
380 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>])'
382 if exists(':Gr') != 2
383 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>])'
385 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>])'
387 exe 'command! -bar -bang -nargs=* -complete=customlist,fugitive#CompleteObject Gdiffsplit exe fugitive#Diffsplit(1, <bang>0, "<mods>", <q-args>, [<f-args>])'
388 exe 'command! -bar -bang -nargs=* -complete=customlist,fugitive#CompleteObject Ghdiffsplit exe fugitive#Diffsplit(0, <bang>0, "<mods>", <q-args>, [<f-args>])'
389 exe 'command! -bar -bang -nargs=* -complete=customlist,fugitive#CompleteObject Gvdiffsplit exe fugitive#Diffsplit(0, <bang>0, "vert <mods>", <q-args>, [<f-args>])'
391 exe 'command! -bar -bang -nargs=* -complete=customlist,fugitive#CompleteObject Gw exe fugitive#WriteCommand(<line1>, <count>, +"<range>", <bang>0, "<mods>", <q-args>, [<f-args>])'
392 exe 'command! -bar -bang -nargs=* -complete=customlist,fugitive#CompleteObject Gwrite exe fugitive#WriteCommand(<line1>, <count>, +"<range>", <bang>0, "<mods>", <q-args>, [<f-args>])'
393 exe 'command! -bar -bang -nargs=* -complete=customlist,fugitive#CompleteObject Gwq exe fugitive#WqCommand( <line1>, <count>, +"<range>", <bang>0, "<mods>", <q-args>, [<f-args>])'
395 exe 'command! -bar -bang -nargs=0 -complete=customlist,fugitive#CompleteObject Gremove exe fugitive#RemoveCommand(<line1>, <count>, +"<range>", <bang>0, "<mods>", <q-args>, [<f-args>])'
396 exe 'command! -bar -bang -nargs=0 -complete=customlist,fugitive#CompleteObject Gdelete exe fugitive#DeleteCommand(<line1>, <count>, +"<range>", <bang>0, "<mods>", <q-args>, [<f-args>])'
397 exe 'command! -bar -bang -nargs=1 -complete=customlist,fugitive#CompleteObject Gmove exe fugitive#MoveCommand( <line1>, <count>, +"<range>", <bang>0, "<mods>", <q-args>, [<f-args>])'
398 exe 'command! -bar -bang -nargs=1 -complete=customlist,fugitive#RenameComplete Grename exe fugitive#RenameCommand(<line1>, <count>, +"<range>", <bang>0, "<mods>", <q-args>, [<f-args>])'
400 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>])'
402 if get(g:, 'fugitive_no_maps')
406 let s:nowait = v:version >= 704 ? '<nowait>' : ''
408 function! s:Map(mode, lhs, rhs, ...) abort
409 for mode in split(a:mode, '\zs')
410 let flags = (a:0 ? a:1 : '') . (a:rhs =~# '<Plug>' ? '' : '<script>')
413 let keys = get(g:, mode.'remap', {})
414 if type(keys) == type([])
418 if has_key(keys, head)
419 let head = keys[head]
425 let tail = matchstr(head, '<[^<>]*>$\|.$') . tail
426 let head = substitute(head, '<[^<>]*>$\|.$', '', '')
428 if flags !~# '<unique>' || empty(mapcheck(head.tail, mode))
429 exe mode.'map' s:nowait flags head.tail a:rhs
434 call s:Map('c', '<C-R><C-G>', 'fnameescape(fugitive#Object(@%))', '<expr>')
435 call s:Map('n', 'y<C-G>', ':<C-U>call setreg(v:register, fugitive#Object(@%))<CR>', '<silent>')