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! FugitiveFind(...) abort
47 return fugitive#Find(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 if path !~# '^fugitive:'
63 let vals = matchlist(path, '\c^fugitive:\%(//\)\=\(.\{-\}\)\%(//\|::\)\(\x\{40\}\|[0-3]\)\(/.*\)\=$')
65 return [(vals[2] =~# '^.$' ? ':' : '') . vals[2] . substitute(vals[3], '^/', ':', ''), vals[1]]
67 let v:errmsg = 'fugitive: invalid Fugitive URL ' . path
71 function! FugitivePrepare(...) abort
72 return call('fugitive#Prepare', a:000)
75 function! FugitiveConfig(key, ...) abort
76 return fugitive#Config(a:key, FugitiveGitDir(a:0 ? a:1 : -1))
79 function! FugitiveRemoteUrl(...) abort
80 return fugitive#RemoteUrl(a:0 ? a:1 : '', FugitiveGitDir(a:0 > 1 ? a:2 : -1))
83 function! FugitiveHead(...) abort
84 let dir = FugitiveGitDir(a:0 > 1 ? a:2 : -1)
88 return fugitive#Head(a:0 ? a:1 : 0, dir)
91 function! FugitiveStatusline(...) abort
92 if !exists('b:git_dir')
95 return fugitive#Statusline()
98 function! FugitiveIsGitDir(path) abort
99 let path = substitute(a:path, '[\/]$', '', '') . '/'
100 return getfsize(path.'HEAD') > 10 && (
101 \ isdirectory(path.'objects') && isdirectory(path.'refs') ||
102 \ getftype(path.'commondir') ==# 'file')
105 let s:worktree_for_dir = {}
106 let s:dir_for_worktree = {}
107 function! FugitiveTreeForGitDir(path) abort
110 return len(dir) ==# 5 ? '/' : dir[0:-6]
114 if !has_key(s:worktree_for_dir, dir)
115 let s:worktree_for_dir[dir] = ''
116 let config_file = dir . '/config'
117 if filereadable(config_file)
118 let config = readfile(config_file,'',10)
119 call filter(config,'v:val =~# "^\\s*worktree *="')
121 let worktree = matchstr(config[0], '= *\zs.*')
123 elseif filereadable(dir . '/gitdir')
124 let worktree = fnamemodify(readfile(dir . '/gitdir')[0], ':h')
129 if exists('worktree')
130 let s:worktree_for_dir[dir] = worktree
131 let s:dir_for_worktree[s:worktree_for_dir[dir]] = dir
134 if s:worktree_for_dir[dir] =~# '^\.'
135 return simplify(dir . '/' . s:worktree_for_dir[dir])
137 return s:worktree_for_dir[dir]
141 function! FugitiveExtractGitDir(path) abort
142 let path = s:Slash(a:path)
143 if path =~# '^fugitive:'
144 return matchstr(path, '\C^fugitive:\%(//\)\=\zs.\{-\}\ze\%(//\|::\|$\)')
145 elseif isdirectory(path)
146 let path = fnamemodify(path, ':p:s?/$??')
148 let path = fnamemodify(path, ':p:h:s?/$??')
150 let pre = substitute(matchstr(path, '^\a\a\+\ze:'), '^.', '\u&', '')
151 if len(pre) && exists('*' . pre . 'Real')
152 let path = s:Slash({pre}Real(path))
154 let root = resolve(path)
156 silent! exe haslocaldir() ? 'lcd .' : 'cd .'
159 while root !=# previous
160 if root =~# '\v^//%([^/]+/?)?$'
163 if index(split($GIT_CEILING_DIRECTORIES, ':'), root) >= 0
166 if root ==# $GIT_WORK_TREE && FugitiveIsGitDir($GIT_DIR)
167 return simplify(fnamemodify($GIT_DIR, ':p:s?[\/]$??'))
169 if FugitiveIsGitDir($GIT_DIR)
170 call FugitiveWorkTree(simplify(fnamemodify($GIT_DIR, ':p:s?[\/]$??')))
171 if has_key(s:dir_for_worktree, root)
172 return s:dir_for_worktree[root]
175 let dir = substitute(root, '[\/]$', '', '') . '/.git'
176 let type = getftype(dir)
177 if type ==# 'dir' && FugitiveIsGitDir(dir)
179 elseif type ==# 'link' && FugitiveIsGitDir(dir)
181 elseif type !=# '' && filereadable(dir)
182 let line = get(readfile(dir, '', 1), 0, '')
183 if line =~# '^gitdir: \.' && FugitiveIsGitDir(root.'/'.line[8:-1])
184 return simplify(root.'/'.line[8:-1])
185 elseif line =~# '^gitdir: ' && FugitiveIsGitDir(line[8:-1])
188 elseif FugitiveIsGitDir(root)
192 let root = fnamemodify(root, ':h')
197 function! FugitiveDetect(path) abort
198 if exists('b:git_dir') && b:git_dir =~# '^$\|/$\|^fugitive:'
201 if !exists('b:git_dir')
202 let dir = FugitiveExtractGitDir(a:path)
207 if exists('b:git_dir')
208 return fugitive#Init()
212 function! s:Slash(path) abort
213 if exists('+shellslash')
214 return tr(a:path, '\', '/')
220 function! s:ProjectionistDetect() abort
221 let file = s:Slash(get(g:, 'projectionist_file', ''))
222 let dir = FugitiveExtractGitDir(file)
223 let base = matchstr(file, '^fugitive://.\{-\}//\x\+')
225 let base = FugitiveTreeForGitDir(dir)
228 if exists('+shellslash') && !&shellslash
229 let base = tr(base, '/', '\')
231 call projectionist#append(base, FugitiveCommonDir(dir) . '/info/projections.json')
238 autocmd BufNewFile,BufReadPost * call FugitiveDetect(expand('<amatch>:p'))
239 autocmd FileType netrw call FugitiveDetect(fnamemodify(get(b:, 'netrw_curdir', expand('<amatch>')), ':p'))
240 autocmd User NERDTreeInit,NERDTreeNewRoot
241 \ if exists('b:NERDTree.root.path.str') |
242 \ call FugitiveDetect(b:NERDTree.root.path.str()) |
244 autocmd VimEnter * if empty(expand('<amatch>'))|call FugitiveDetect(getcwd())|endif
245 autocmd CmdWinEnter * call FugitiveDetect(expand('#:p'))
248 \ if exists('b:git_dir') |
249 \ call fugitive#MapJumps() |
250 \ call fugitive#MapCfile() |
252 autocmd FileType gitcommit
253 \ if exists('b:git_dir') |
254 \ call fugitive#MapCfile('fugitive#StatusCfile()') |
256 autocmd FileType gitrebase
257 \ let &l:include = '^\%(pick\|squash\|edit\|reword\|fixup\|drop\|[pserfd]\)\>' |
258 \ if exists('b:git_dir') |
259 \ let &l:includeexpr = 'v:fname =~# ''^\x\{4,40\}$'' ? FugitiveFind(v:fname) : ' .
260 \ (len(&l:includeexpr) ? &l:includeexpr : 'v:fname') |
262 \ let b:undo_ftplugin = get(b:, 'undo_ftplugin', 'exe') . '|setl inex= inc='
264 autocmd BufReadCmd index{,.lock}
265 \ if FugitiveIsGitDir(expand('<amatch>:p:h')) |
266 \ let b:git_dir = s:Slash(expand('<amatch>:p:h')) |
267 \ exe fugitive#BufReadStatus() |
268 \ elseif filereadable(expand('<amatch>')) |
272 autocmd BufReadCmd fugitive://*//* exe fugitive#BufReadCmd()
273 autocmd BufWriteCmd fugitive://*//[0-3]/* exe fugitive#BufWriteCmd()
274 autocmd FileReadCmd fugitive://*//* exe fugitive#FileReadCmd()
275 autocmd FileWriteCmd fugitive://*//[0-3]/* exe fugitive#FileWriteCmd()
276 if exists('##SourceCmd')
277 autocmd SourceCmd fugitive://*//* nested exe fugitive#SourceCmd()
280 autocmd User Flags call Hoist('buffer', function('FugitiveStatusline'))
282 autocmd User ProjectionistDetect call s:ProjectionistDetect()