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! s:shellslash(path) abort
12 if &shell =~? 'cmd' || exists('+shellslash') && !&shellslash
13 return tr(a:path, '\', '/')
19 function! FugitiveGitDir(...) abort
21 return get(b:, 'git_dir', '')
22 elseif type(a:1) == type(0)
23 return getbufvar(a:1, 'git_dir')
24 elseif type(a:1) == type('')
25 return substitute(s:shellslash(a:1), '/$', '', '')
31 function! FugitiveIsGitDir(path) abort
32 let path = substitute(a:path, '[\/]$', '', '') . '/'
33 return getfsize(path.'HEAD') > 10 && (
34 \ isdirectory(path.'objects') && isdirectory(path.'refs') ||
35 \ getftype(path.'commondir') ==# 'file')
38 let s:worktree_for_dir = {}
39 let s:dir_for_worktree = {}
40 function! FugitiveWorkTree(...) abort
41 let dir = substitute(s:shellslash(a:0 ? a:1 : get(b:, 'git_dir', '')), '/$', '', '')
43 return len(dir) ==# 5 ? '/' : dir[0:-6]
45 if !has_key(s:worktree_for_dir, dir)
46 let s:worktree_for_dir[dir] = ''
47 let config_file = dir . '/config'
48 if filereadable(config_file)
49 let config = readfile(config_file,'',10)
50 call filter(config,'v:val =~# "^\\s*worktree *="')
52 let worktree = matchstr(config[0], '= *\zs.*')
54 elseif filereadable(dir . '/gitdir')
55 let worktree = fnamemodify(readfile(dir . '/gitdir')[0], ':h')
61 let s:worktree_for_dir[dir] = worktree
62 let s:dir_for_worktree[s:worktree_for_dir[dir]] = dir
65 if s:worktree_for_dir[dir] =~# '^\.'
66 return simplify(dir . '/' . s:worktree_for_dir[dir])
68 return s:worktree_for_dir[dir]
72 function! FugitiveReal(...) abort
73 let file = a:0 ? a:1 : @%
74 if file =~? '^fugitive:' || a:0 > 1
75 return call('fugitive#Real', [file] + a:000[1:-1])
76 elseif file =~# '^/\|^\a\+:\|^$'
79 return fnamemodify(file, ':p' . (file =~# '[\/]$' ? '' : ':s?[\/]$??'))
83 function! FugitivePath(...) abort
85 return fugitive#Path(a:1, a:2, FugitiveGitDir(a:0 > 2 ? a:3 : -1))
87 return FugitiveReal(a:0 ? a:1 : @%)
91 function! FugitiveGenerate(...) abort
92 if a:0 && s:shellslash(a:0) =~# '^\%(\a\a\+:\)\=\%(a:\)\=/\|^[~$]'
95 return fugitive#repo(FugitiveGitDir(a:0 > 1 ? a:2 : -1)).translate(a:0 ? a:1 : '', 1)
98 function! FugitiveParse(...) abort
99 let path = s:shellslash(a:0 ? a:1 : @%)
100 let vals = matchlist(path, '\c^fugitive:\%(//\)\=\(.\{-\}\)\%(//\|::\)\(\x\{40\}\|[0-3]\)\(/.*\)\=$')
102 return [(vals[2] =~# '^.$' ? ':' : '') . vals[2] . substitute(vals[3], '^/', ':', ''), vals[1]]
104 let v:errmsg = 'fugitive: invalid Fugitive URL ' . path
108 function! FugitiveConfig(key, ...) abort
109 return fugitive#Config(a:key, FugitiveGitDir(a:0 ? a:1 : -1))
112 function! FugitiveRemoteUrl(...) abort
113 return fugitive#RemoteUrl(a:0 ? a:1 : '', FugitiveGitDir(a:0 > 1 ? a:2 : -1))
116 function! FugitiveHead(...) abort
117 let dir = FugitiveGitDir(a:0 > 1 ? a:2 : -1)
121 return fugitive#repo(dir).head(a:0 ? a:1 : 0)
124 function! FugitiveStatusline(...) abort
125 if !exists('b:git_dir')
128 return fugitive#Statusline()
131 function! FugitiveTreeForGitDir(path) abort
132 return FugitiveWorkTree(a:path)
135 function! FugitiveExtractGitDir(path) abort
136 let path = s:shellslash(a:path)
137 if path =~# '^fugitive:'
138 return matchstr(path, '\C^fugitive:\%(//\)\=\zs.\{-\}\ze\%(//\|::\|$\)')
139 elseif isdirectory(path)
140 let path = fnamemodify(path, ':p:s?/$??')
142 let path = fnamemodify(path, ':p:h:s?/$??')
144 let pre = substitute(matchstr(path, '^\a\a\+\ze:'), '^.', '\u&', '')
145 if len(pre) && exists('*' . pre . 'Real')
146 let path = s:shellslash({pre}Real(path))
148 let root = resolve(path)
150 silent! exe haslocaldir() ? 'lcd .' : 'cd .'
153 while root !=# previous
154 if root =~# '\v^//%([^/]+/?)?$'
157 if index(split($GIT_CEILING_DIRECTORIES, ':'), root) >= 0
160 if root ==# $GIT_WORK_TREE && FugitiveIsGitDir($GIT_DIR)
161 return simplify(fnamemodify($GIT_DIR, ':p:s?[\/]$??'))
163 if FugitiveIsGitDir($GIT_DIR)
164 call FugitiveWorkTree(simplify(fnamemodify($GIT_DIR, ':p:s?[\/]$??')))
165 if has_key(s:dir_for_worktree, root)
166 return s:dir_for_worktree[root]
169 let dir = substitute(root, '[\/]$', '', '') . '/.git'
170 let type = getftype(dir)
171 if type ==# 'dir' && FugitiveIsGitDir(dir)
173 elseif type ==# 'link' && FugitiveIsGitDir(dir)
175 elseif type !=# '' && filereadable(dir)
176 let line = get(readfile(dir, '', 1), 0, '')
177 if line =~# '^gitdir: \.' && FugitiveIsGitDir(root.'/'.line[8:-1])
178 return simplify(root.'/'.line[8:-1])
179 elseif line =~# '^gitdir: ' && FugitiveIsGitDir(line[8:-1])
182 elseif FugitiveIsGitDir(root)
186 let root = fnamemodify(root, ':h')
191 function! FugitiveDetect(path) abort
192 if exists('b:git_dir') && b:git_dir =~# '^$\|/$\|^fugitive:'
195 if !exists('b:git_dir')
196 let dir = FugitiveExtractGitDir(a:path)
201 if exists('b:git_dir')
202 return fugitive#Init()
209 autocmd BufNewFile,BufReadPost * call FugitiveDetect(expand('<amatch>:p'))
210 autocmd FileType netrw call FugitiveDetect(fnamemodify(get(b:, 'netrw_curdir', expand('<amatch>')), ':p'))
211 autocmd User NERDTreeInit,NERDTreeNewRoot
212 \ if exists('b:NERDTree.root.path.str') |
213 \ call FugitiveDetect(b:NERDTree.root.path.str()) |
215 autocmd VimEnter * if empty(expand('<amatch>'))|call FugitiveDetect(getcwd())|endif
216 autocmd CmdWinEnter * call FugitiveDetect(expand('#:p'))
219 \ if exists('b:git_dir') |
220 \ call fugitive#MapJumps() |
221 \ call fugitive#MapCfile() |
223 autocmd FileType gitcommit
224 \ if exists('b:git_dir') |
225 \ call fugitive#MapCfile('fugitive#StatusCfile()') |
227 autocmd FileType gitrebase
228 \ let &l:include = '^\%(pick\|squash\|edit\|reword\|fixup\|drop\|[pserfd]\)\>' |
229 \ if exists('b:git_dir') |
230 \ let &l:includeexpr = 'v:fname =~# ''^\x\{4,40\}$'' ? FugitiveGenerate(v:fname) : ' .
231 \ (len(&l:includeexpr) ? &l:includeexpr : 'v:fname') |
233 \ let b:undo_ftplugin = get(b:, 'undo_ftplugin', 'exe') . '|setl inex= inc='
235 autocmd BufReadCmd index{,.lock}
236 \ if FugitiveIsGitDir(expand('<amatch>:p:h')) |
237 \ let b:git_dir = s:shellslash(expand('<amatch>:p:h')) |
238 \ exe fugitive#BufReadStatus() |
239 \ elseif filereadable(expand('<amatch>')) |
243 autocmd BufReadCmd fugitive://*//* exe fugitive#BufReadCmd()
244 autocmd BufWriteCmd fugitive://*//[0-3]/* exe fugitive#BufWriteCmd()
245 autocmd FileReadCmd fugitive://*//* exe fugitive#FileReadCmd()
246 autocmd FileWriteCmd fugitive://*//[0-3]/* exe fugitive#FileWriteCmd()
247 if exists('##SourceCmd')
248 autocmd SourceCmd fugitive://*//* nested exe fugitive#SourceCmd()
251 autocmd User Flags call Hoist('buffer', function('FugitiveStatusline'))