Separate detection from initialization

This commit is contained in:
Tim Pope
2018-05-29 23:27:53 -04:00
parent 8c43505037
commit 0e2680f9ae

View File

@@ -62,7 +62,7 @@ endfunction
function! s:shellslash(path) abort function! s:shellslash(path) abort
if s:winshell() if s:winshell()
return s:gsub(a:path,'\\','/') return tr(a:path, '\', '/')
else else
return a:path return a:path
endif endif
@@ -151,19 +151,15 @@ endfunction
let s:abstract_prototype = {} let s:abstract_prototype = {}
" Section: Initialization " Section: Detection
function! FugitiveIsGitDir(path) abort function! FugitiveIsGitDir(path) abort
let path = s:sub(a:path, '[\/]$', '') . '/' let path = substitute(a:path, '[\/]$', '', '') . '/'
return getfsize(path.'HEAD') > 10 && ( return getfsize(path.'HEAD') > 10 && (
\ isdirectory(path.'objects') && isdirectory(path.'refs') || \ isdirectory(path.'objects') && isdirectory(path.'refs') ||
\ getftype(path.'commondir') ==# 'file') \ getftype(path.'commondir') ==# 'file')
endfunction endfunction
function! fugitive#is_git_dir(path) abort
return FugitiveIsGitDir(a:path)
endfunction
function! FugitiveExtractGitDir(path) abort function! FugitiveExtractGitDir(path) abort
if s:shellslash(a:path) =~# '^fugitive://.*//' if s:shellslash(a:path) =~# '^fugitive://.*//'
return matchstr(s:shellslash(a:path), '\C^fugitive://\zs.\{-\}\ze//') return matchstr(s:shellslash(a:path), '\C^fugitive://\zs.\{-\}\ze//')
@@ -195,7 +191,7 @@ function! FugitiveExtractGitDir(path) abort
return s:dir_for_worktree[root] return s:dir_for_worktree[root]
endif endif
endif endif
let dir = s:sub(root, '[\/]$', '') . '/.git' let dir = substitute(root, '[\/]$', '', '') . '/.git'
let type = getftype(dir) let type = getftype(dir)
if type ==# 'dir' && FugitiveIsGitDir(dir) if type ==# 'dir' && FugitiveIsGitDir(dir)
return dir return dir
@@ -217,10 +213,6 @@ function! FugitiveExtractGitDir(path) abort
return '' return ''
endfunction endfunction
function! fugitive#extract_git_dir(path) abort
return FugitiveExtractGitDir(a:path)
endfunction
function! FugitiveDetect(path) abort function! FugitiveDetect(path) abort
if exists('b:git_dir') && (b:git_dir ==# '' || b:git_dir =~# '/$') if exists('b:git_dir') && (b:git_dir ==# '' || b:git_dir =~# '/$')
unlet b:git_dir unlet b:git_dir
@@ -235,6 +227,23 @@ function! FugitiveDetect(path) abort
endif endif
endif endif
if exists('b:git_dir') if exists('b:git_dir')
return fugitive#Init()
endif
endfunction
augroup fugitive
autocmd!
autocmd BufNewFile,BufReadPost * call FugitiveDetect(expand('%:p'))
autocmd FileType netrw call FugitiveDetect(fnamemodify(get(b:, 'netrw_curdir', @%), ':p'))
autocmd User NERDTreeInit,NERDTreeNewRoot call FugitiveDetect(b:NERDTree.root.path.str())
autocmd VimEnter * if expand('<amatch>')==''|call FugitiveDetect(getcwd())|endif
autocmd CmdWinEnter * call FugitiveDetect(expand('#:p'))
autocmd BufWinLeave * execute getwinvar(+bufwinnr(+expand('<abuf>')), 'fugitive_leave')
augroup END
" Section: Initialization
function! fugitive#Init() abort
if exists('#User#FugitiveBoot') if exists('#User#FugitiveBoot')
try try
let [save_mls, &modelines] = [&mls, 0] let [save_mls, &modelines] = [&mls, 0]
@@ -266,23 +275,20 @@ function! FugitiveDetect(path) abort
finally finally
let &mls = save_mls let &mls = save_mls
endtry endtry
endif endfunction
function! fugitive#is_git_dir(path) abort
return FugitiveIsGitDir(a:path)
endfunction
function! fugitive#extract_git_dir(path) abort
return FugitiveExtractGitDir(a:path)
endfunction endfunction
function! fugitive#detect(path) abort function! fugitive#detect(path) abort
return FugitiveDetect(a:path) return FugitiveDetect(a:path)
endfunction endfunction
augroup fugitive
autocmd!
autocmd BufNewFile,BufReadPost * call FugitiveDetect(expand('%:p'))
autocmd FileType netrw call FugitiveDetect(fnamemodify(get(b:, 'netrw_curdir', @%), ':p'))
autocmd User NERDTreeInit,NERDTreeNewRoot call FugitiveDetect(b:NERDTree.root.path.str())
autocmd VimEnter * if expand('<amatch>')==''|call FugitiveDetect(getcwd())|endif
autocmd CmdWinEnter * call FugitiveDetect(expand('#:p'))
autocmd BufWinLeave * execute getwinvar(+bufwinnr(+expand('<abuf>')), 'fugitive_leave')
augroup END
" Section: Repository " Section: Repository
let s:repo_prototype = {} let s:repo_prototype = {}