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
13 return get(b:, 'git_dir', '')
14 elseif type(a:1) == type(0)
15 return getbufvar(a:1, 'git_dir')
16 elseif type(a:1) == type('')
17 return substitute(s:Slash(a:1), '/$', '', '')
23 function! FugitiveCommonDir(...) abort
24 let dir = FugitiveGitDir(a:0 ? a:1 : -1)
28 return fugitive#CommonDir(dir)
31 function! FugitiveWorkTree(...) abort
32 return FugitiveTreeForGitDir(FugitiveGitDir(a:0 ? a:1 : -1))
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 function! FugitiveRoute(...) abort
47 return fugitive#Route(a:0 ? a:1 : bufnr(''), FugitiveGitDir(a:0 > 1 ? a:2 : -1))
50 function! FugitivePath(...) abort
52 return fugitive#Path(a:1, a:2, FugitiveGitDir(a:0 > 2 ? a:3 : -1))
54 return FugitiveReal(a:0 ? a:1 : @%)
58 function! FugitiveParse(...) abort
59 let path = s:Slash(a:0 ? a:1 : @%)
60 let vals = matchlist(path, '\c^fugitive:\%(//\)\=\(.\{-\}\)\%(//\|::\)\(\x\{40\}\|[0-3]\)\(/.*\)\=$')
62 return [(vals[2] =~# '^.$' ? ':' : '') . vals[2] . substitute(vals[3], '^/', ':', ''), vals[1]]
64 let v:errmsg = 'fugitive: invalid Fugitive URL ' . path
68 function! FugitiveConfig(key, ...) abort
69 return fugitive#Config(a:key, FugitiveGitDir(a:0 ? a:1 : -1))
72 function! FugitiveRemoteUrl(...) abort
73 return fugitive#RemoteUrl(a:0 ? a:1 : '', FugitiveGitDir(a:0 > 1 ? a:2 : -1))
76 function! FugitiveHead(...) abort
77 let dir = FugitiveGitDir(a:0 > 1 ? a:2 : -1)
81 return fugitive#Head(a:0 ? a:1 : 0, dir)
84 function! FugitiveStatusline(...) abort
85 if !exists('b:git_dir')
88 return fugitive#Statusline()
91 function! FugitiveIsGitDir(path) abort
92 let path = substitute(a:path, '[\/]$', '', '') . '/'
93 return getfsize(path.'HEAD') > 10 && (
94 \ isdirectory(path.'objects') && isdirectory(path.'refs') ||
95 \ getftype(path.'commondir') ==# 'file')
98 let s:worktree_for_dir = {}
99 let s:dir_for_worktree = {}
100 function! FugitiveTreeForGitDir(path) abort
103 return len(dir) ==# 5 ? '/' : dir[0:-6]
105 if !has_key(s:worktree_for_dir, dir)
106 let s:worktree_for_dir[dir] = ''
107 let config_file = dir . '/config'
108 if filereadable(config_file)
109 let config = readfile(config_file,'',10)
110 call filter(config,'v:val =~# "^\\s*worktree *="')
112 let worktree = matchstr(config[0], '= *\zs.*')
114 elseif filereadable(dir . '/gitdir')
115 let worktree = fnamemodify(readfile(dir . '/gitdir')[0], ':h')
120 if exists('worktree')
121 let s:worktree_for_dir[dir] = worktree
122 let s:dir_for_worktree[s:worktree_for_dir[dir]] = dir
125 if s:worktree_for_dir[dir] =~# '^\.'
126 return simplify(dir . '/' . s:worktree_for_dir[dir])
128 return s:worktree_for_dir[dir]
132 function! FugitiveExtractGitDir(path) abort
133 let path = s:Slash(a:path)
134 if path =~# '^fugitive:'
135 return matchstr(path, '\C^fugitive:\%(//\)\=\zs.\{-\}\ze\%(//\|::\|$\)')
136 elseif isdirectory(path)
137 let path = fnamemodify(path, ':p:s?/$??')
139 let path = fnamemodify(path, ':p:h:s?/$??')
141 let pre = substitute(matchstr(path, '^\a\a\+\ze:'), '^.', '\u&', '')
142 if len(pre) && exists('*' . pre . 'Real')
143 let path = s:Slash({pre}Real(path))
145 let root = resolve(path)
147 silent! exe haslocaldir() ? 'lcd .' : 'cd .'
150 while root !=# previous
151 if root =~# '\v^//%([^/]+/?)?$'
154 if index(split($GIT_CEILING_DIRECTORIES, ':'), root) >= 0
157 if root ==# $GIT_WORK_TREE && FugitiveIsGitDir($GIT_DIR)
158 return simplify(fnamemodify($GIT_DIR, ':p:s?[\/]$??'))
160 if FugitiveIsGitDir($GIT_DIR)
161 call FugitiveWorkTree(simplify(fnamemodify($GIT_DIR, ':p:s?[\/]$??')))
162 if has_key(s:dir_for_worktree, root)
163 return s:dir_for_worktree[root]
166 let dir = substitute(root, '[\/]$', '', '') . '/.git'
167 let type = getftype(dir)
168 if type ==# 'dir' && FugitiveIsGitDir(dir)
170 elseif type ==# 'link' && FugitiveIsGitDir(dir)
172 elseif type !=# '' && filereadable(dir)
173 let line = get(readfile(dir, '', 1), 0, '')
174 if line =~# '^gitdir: \.' && FugitiveIsGitDir(root.'/'.line[8:-1])
175 return simplify(root.'/'.line[8:-1])
176 elseif line =~# '^gitdir: ' && FugitiveIsGitDir(line[8:-1])
179 elseif FugitiveIsGitDir(root)
183 let root = fnamemodify(root, ':h')
188 function! FugitiveDetect(path) abort
189 if exists('b:git_dir') && b:git_dir =~# '^$\|/$\|^fugitive:'
192 if !exists('b:git_dir')
193 let dir = FugitiveExtractGitDir(a:path)
198 if exists('b:git_dir')
199 return fugitive#Init()
203 function! FugitiveFind(...) abort
204 return call('FugitiveRoute', a:000)
207 function! FugitiveGenerate(...) abort
208 return call('FugitiveRoute', a:000)
211 function! s:Slash(path) abort
212 if exists('+shellslash')
213 return tr(a:path, '\', '/')
219 function! s:ProjectionistDetect() abort
220 let file = s:Slash(get(g:, 'projectionist_file', ''))
221 let dir = FugitiveExtractGitDir(file)
222 let base = matchstr(file, '^fugitive://.\{-\}//\x\+')
224 let base = FugitiveTreeForGitDir(dir)
227 if exists('+shellslash') && !&shellslash
228 let base = tr(base, '/', '\')
230 call projectionist#append(base, FugitiveCommonDir(dir) . '/info/projections.json')
237 autocmd BufNewFile,BufReadPost * call FugitiveDetect(expand('<amatch>:p'))
238 autocmd FileType netrw call FugitiveDetect(fnamemodify(get(b:, 'netrw_curdir', expand('<amatch>')), ':p'))
239 autocmd User NERDTreeInit,NERDTreeNewRoot
240 \ if exists('b:NERDTree.root.path.str') |
241 \ call FugitiveDetect(b:NERDTree.root.path.str()) |
243 autocmd VimEnter * if empty(expand('<amatch>'))|call FugitiveDetect(getcwd())|endif
244 autocmd CmdWinEnter * call FugitiveDetect(expand('#:p'))
247 \ if exists('b:git_dir') |
248 \ call fugitive#MapJumps() |
249 \ call fugitive#MapCfile() |
251 autocmd FileType gitcommit
252 \ if exists('b:git_dir') |
253 \ call fugitive#MapCfile('fugitive#StatusCfile()') |
255 autocmd FileType gitrebase
256 \ let &l:include = '^\%(pick\|squash\|edit\|reword\|fixup\|drop\|[pserfd]\)\>' |
257 \ if exists('b:git_dir') |
258 \ let &l:includeexpr = 'v:fname =~# ''^\x\{4,40\}$'' ? FugitiveRoute(v:fname) : ' .
259 \ (len(&l:includeexpr) ? &l:includeexpr : 'v:fname') |
261 \ let b:undo_ftplugin = get(b:, 'undo_ftplugin', 'exe') . '|setl inex= inc='
263 autocmd BufReadCmd index{,.lock}
264 \ if FugitiveIsGitDir(expand('<amatch>:p:h')) |
265 \ let b:git_dir = s:Slash(expand('<amatch>:p:h')) |
266 \ exe fugitive#BufReadStatus() |
267 \ elseif filereadable(expand('<amatch>')) |
271 autocmd BufReadCmd fugitive://*//* exe fugitive#BufReadCmd()
272 autocmd BufWriteCmd fugitive://*//[0-3]/* exe fugitive#BufWriteCmd()
273 autocmd FileReadCmd fugitive://*//* exe fugitive#FileReadCmd()
274 autocmd FileWriteCmd fugitive://*//[0-3]/* exe fugitive#FileWriteCmd()
275 if exists('##SourceCmd')
276 autocmd SourceCmd fugitive://*//* nested exe fugitive#SourceCmd()
279 autocmd User Flags call Hoist('buffer', function('FugitiveStatusline'))
281 autocmd User ProjectionistDetect call s:ProjectionistDetect()