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! FugitiveRoute(...) abort
99 return call('FugitiveGenerate', a:000)
102 function! FugitiveParse(...) abort
103 let path = s:shellslash(a:0 ? a:1 : @%)
104 let vals = matchlist(path, '\c^fugitive:\%(//\)\=\(.\{-\}\)\%(//\|::\)\(\x\{40\}\|[0-3]\)\(/.*\)\=$')
106 return [(vals[2] =~# '^.$' ? ':' : '') . vals[2] . substitute(vals[3], '^/', ':', ''), vals[1]]
108 let v:errmsg = 'fugitive: invalid Fugitive URL ' . path
112 function! FugitiveConfig(key, ...) abort
113 return fugitive#Config(a:key, FugitiveGitDir(a:0 ? a:1 : -1))
116 function! FugitiveRemoteUrl(...) abort
117 return fugitive#RemoteUrl(a:0 ? a:1 : '', FugitiveGitDir(a:0 > 1 ? a:2 : -1))
120 function! FugitiveHead(...) abort
121 let dir = FugitiveGitDir(a:0 > 1 ? a:2 : -1)
125 return fugitive#repo(dir).head(a:0 ? a:1 : 0)
128 function! FugitiveStatusline(...) abort
129 if !exists('b:git_dir')
132 return fugitive#Statusline()
135 function! FugitiveTreeForGitDir(path) abort
136 return FugitiveWorkTree(a:path)
139 function! FugitiveExtractGitDir(path) abort
140 let path = s:shellslash(a:path)
141 if path =~# '^fugitive:'
142 return matchstr(path, '\C^fugitive:\%(//\)\=\zs.\{-\}\ze\%(//\|::\|$\)')
143 elseif isdirectory(path)
144 let path = fnamemodify(path, ':p:s?/$??')
146 let path = fnamemodify(path, ':p:h:s?/$??')
148 let pre = substitute(matchstr(path, '^\a\a\+\ze:'), '^.', '\u&', '')
149 if len(pre) && exists('*' . pre . 'Real')
150 let path = s:shellslash({pre}Real(path))
152 let root = resolve(path)
154 silent! exe haslocaldir() ? 'lcd .' : 'cd .'
157 while root !=# previous
158 if root =~# '\v^//%([^/]+/?)?$'
161 if index(split($GIT_CEILING_DIRECTORIES, ':'), root) >= 0
164 if root ==# $GIT_WORK_TREE && FugitiveIsGitDir($GIT_DIR)
165 return simplify(fnamemodify($GIT_DIR, ':p:s?[\/]$??'))
167 if FugitiveIsGitDir($GIT_DIR)
168 call FugitiveWorkTree(simplify(fnamemodify($GIT_DIR, ':p:s?[\/]$??')))
169 if has_key(s:dir_for_worktree, root)
170 return s:dir_for_worktree[root]
173 let dir = substitute(root, '[\/]$', '', '') . '/.git'
174 let type = getftype(dir)
175 if type ==# 'dir' && FugitiveIsGitDir(dir)
177 elseif type ==# 'link' && FugitiveIsGitDir(dir)
179 elseif type !=# '' && filereadable(dir)
180 let line = get(readfile(dir, '', 1), 0, '')
181 if line =~# '^gitdir: \.' && FugitiveIsGitDir(root.'/'.line[8:-1])
182 return simplify(root.'/'.line[8:-1])
183 elseif line =~# '^gitdir: ' && FugitiveIsGitDir(line[8:-1])
186 elseif FugitiveIsGitDir(root)
190 let root = fnamemodify(root, ':h')
195 function! FugitiveDetect(path) abort
196 if exists('b:git_dir') && b:git_dir =~# '^$\|/$\|^fugitive:'
199 if !exists('b:git_dir')
200 let dir = FugitiveExtractGitDir(a:path)
205 if exists('b:git_dir')
206 return fugitive#Init()
213 autocmd BufNewFile,BufReadPost * call FugitiveDetect(expand('<amatch>:p'))
214 autocmd FileType netrw call FugitiveDetect(fnamemodify(get(b:, 'netrw_curdir', expand('<amatch>')), ':p'))
215 autocmd User NERDTreeInit,NERDTreeNewRoot
216 \ if exists('b:NERDTree.root.path.str') |
217 \ call FugitiveDetect(b:NERDTree.root.path.str()) |
219 autocmd VimEnter * if empty(expand('<amatch>'))|call FugitiveDetect(getcwd())|endif
220 autocmd CmdWinEnter * call FugitiveDetect(expand('#:p'))
223 \ if exists('b:git_dir') |
224 \ call fugitive#MapJumps() |
225 \ call fugitive#MapCfile() |
227 autocmd FileType gitcommit
228 \ if exists('b:git_dir') |
229 \ call fugitive#MapCfile('fugitive#StatusCfile()') |
231 autocmd FileType gitrebase
232 \ let &l:include = '^\%(pick\|squash\|edit\|reword\|fixup\|drop\|[pserfd]\)\>' |
233 \ if exists('b:git_dir') |
234 \ let &l:includeexpr = 'v:fname =~# ''^\x\{4,40\}$'' ? FugitiveGenerate(v:fname) : ' .
235 \ (len(&l:includeexpr) ? &l:includeexpr : 'v:fname') |
237 \ let b:undo_ftplugin = get(b:, 'undo_ftplugin', 'exe') . '|setl inex= inc='
239 autocmd BufReadCmd index{,.lock}
240 \ if FugitiveIsGitDir(expand('<amatch>:p:h')) |
241 \ let b:git_dir = s:shellslash(expand('<amatch>:p:h')) |
242 \ exe fugitive#BufReadStatus() |
243 \ elseif filereadable(expand('<amatch>')) |
247 autocmd BufReadCmd fugitive://*//* exe fugitive#BufReadCmd()
248 autocmd BufWriteCmd fugitive://*//[0-3]/* exe fugitive#BufWriteCmd()
249 autocmd FileReadCmd fugitive://*//* exe fugitive#FileReadCmd()
250 autocmd FileWriteCmd fugitive://*//[0-3]/* exe fugitive#FileWriteCmd()
251 if exists('##SourceCmd')
252 autocmd SourceCmd fugitive://*//* nested exe fugitive#SourceCmd()
255 autocmd User Flags call Hoist('buffer', function('FugitiveStatusline'))