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
if s:winshell()
return s:gsub(a:path,'\\','/')
return tr(a:path, '\', '/')
else
return a:path
endif
@@ -151,19 +151,15 @@ endfunction
let s:abstract_prototype = {}
" Section: Initialization
" Section: Detection
function! FugitiveIsGitDir(path) abort
let path = s:sub(a:path, '[\/]$', '') . '/'
let path = substitute(a:path, '[\/]$', '', '') . '/'
return getfsize(path.'HEAD') > 10 && (
\ isdirectory(path.'objects') && isdirectory(path.'refs') ||
\ getftype(path.'commondir') ==# 'file')
endfunction
function! fugitive#is_git_dir(path) abort
return FugitiveIsGitDir(a:path)
endfunction
function! FugitiveExtractGitDir(path) abort
if s:shellslash(a:path) =~# '^fugitive://.*//'
return matchstr(s:shellslash(a:path), '\C^fugitive://\zs.\{-\}\ze//')
@@ -195,7 +191,7 @@ function! FugitiveExtractGitDir(path) abort
return s:dir_for_worktree[root]
endif
endif
let dir = s:sub(root, '[\/]$', '') . '/.git'
let dir = substitute(root, '[\/]$', '', '') . '/.git'
let type = getftype(dir)
if type ==# 'dir' && FugitiveIsGitDir(dir)
return dir
@@ -217,10 +213,6 @@ function! FugitiveExtractGitDir(path) abort
return ''
endfunction
function! fugitive#extract_git_dir(path) abort
return FugitiveExtractGitDir(a:path)
endfunction
function! FugitiveDetect(path) abort
if exists('b:git_dir') && (b:git_dir ==# '' || b:git_dir =~# '/$')
unlet b:git_dir
@@ -235,6 +227,23 @@ function! FugitiveDetect(path) abort
endif
endif
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')
try
let [save_mls, &modelines] = [&mls, 0]
@@ -266,23 +275,20 @@ function! FugitiveDetect(path) abort
finally
let &mls = save_mls
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
function! fugitive#detect(path) abort
return FugitiveDetect(a:path)
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
let s:repo_prototype = {}