mirror of
https://github.com/tpope/vim-fugitive.git
synced 2026-05-24 17:28:37 +08:00
Refactor and expose detection helpers
This commit is contained in:
+25
-25
@@ -107,34 +107,34 @@ let s:abstract_prototype = {}
|
|||||||
" }}}1
|
" }}}1
|
||||||
" Initialization {{{1
|
" Initialization {{{1
|
||||||
|
|
||||||
function! s:is_git_dir(path) abort
|
function! fugitive#is_git_dir(path) abort
|
||||||
let path = a:path . '/'
|
let path = a:path . '/'
|
||||||
return isdirectory(path.'objects') && isdirectory(path.'refs') && filereadable(path.'HEAD') && filereadable(path.'config')
|
return isdirectory(path.'objects') && isdirectory(path.'refs') && getfsize(path.'HEAD') > 10
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! s:extract_git_dir(path) abort
|
function! fugitive#extract_git_dir(path) abort
|
||||||
let path = s:shellslash(a:path)
|
if s:shellslash(a:path) =~? '^fugitive://.*//'
|
||||||
if path =~? '^fugitive://.*//'
|
return matchstr(a:path,'fugitive://\zs.\{-\}\ze//')
|
||||||
return matchstr(path,'fugitive://\zs.\{-\}\ze//')
|
|
||||||
endif
|
endif
|
||||||
let fn = fnamemodify(path,':s?[\/]$??')
|
let root = s:shellslash(simplify(fnamemodify(a:path,':p:s?[\/]$??')))
|
||||||
let ofn = ""
|
let previous = ""
|
||||||
let nfn = fn
|
while root !=# previous
|
||||||
while fn !=# ofn
|
let dir = s:sub(root, '[\/]$', '') . '/.git'
|
||||||
let embedded = s:sub(fn, '[\/]$', '') . '/.git'
|
let type = getftype(dir)
|
||||||
if s:is_git_dir(embedded)
|
if type ==# 'dir' && fugitive#is_git_dir(dir)
|
||||||
let full = s:sub(fnamemodify(fn . '/.git', ':p'),'\W$','')
|
return dir
|
||||||
return getftype(full) ==# 'link' ? resolve(full) : simplify(full)
|
elseif type ==# 'link' && fugitive#is_git_dir(dir)
|
||||||
elseif filereadable(embedded)
|
return resolve(dir)
|
||||||
let line = readfile(embedded,1)[0]
|
elseif type !=# ''
|
||||||
if line =~# '^gitdir: '
|
let line = readfile(dir, 1)[0]
|
||||||
|
if line =~# '^gitdir: ' && fugitive#is_git_dir(line[8:-1])
|
||||||
return line[8:-1]
|
return line[8:-1]
|
||||||
endif
|
endif
|
||||||
elseif s:is_git_dir(fn)
|
elseif fugitive#is_git_dir(root)
|
||||||
return s:sub(simplify(fnamemodify(fn,':p')),'\W$','')
|
return root
|
||||||
endif
|
endif
|
||||||
let ofn = fn
|
let previous = root
|
||||||
let fn = fnamemodify(ofn,':h')
|
let root = fnamemodify(previous,':h')
|
||||||
endwhile
|
endwhile
|
||||||
return ''
|
return ''
|
||||||
endfunction
|
endfunction
|
||||||
@@ -144,7 +144,7 @@ function! s:Detect(path)
|
|||||||
unlet b:git_dir
|
unlet b:git_dir
|
||||||
endif
|
endif
|
||||||
if !exists('b:git_dir')
|
if !exists('b:git_dir')
|
||||||
let dir = s:extract_git_dir(a:path)
|
let dir = fugitive#extract_git_dir(a:path)
|
||||||
if dir != ''
|
if dir != ''
|
||||||
let b:git_dir = dir
|
let b:git_dir = dir
|
||||||
endif
|
endif
|
||||||
@@ -181,7 +181,7 @@ let s:repo_prototype = {}
|
|||||||
let s:repos = {}
|
let s:repos = {}
|
||||||
|
|
||||||
function! s:repo(...) abort
|
function! s:repo(...) abort
|
||||||
let dir = a:0 ? a:1 : (exists('b:git_dir') && b:git_dir !=# '' ? b:git_dir : s:extract_git_dir(expand('%:p')))
|
let dir = a:0 ? a:1 : (exists('b:git_dir') && b:git_dir !=# '' ? b:git_dir : fugitive#extract_git_dir(expand('%:p')))
|
||||||
if dir !=# ''
|
if dir !=# ''
|
||||||
if has_key(s:repos,dir)
|
if has_key(s:repos,dir)
|
||||||
let repo = get(s:repos,dir)
|
let repo = get(s:repos,dir)
|
||||||
@@ -1953,7 +1953,7 @@ endfunction
|
|||||||
|
|
||||||
function! s:FileRead()
|
function! s:FileRead()
|
||||||
try
|
try
|
||||||
let repo = s:repo(s:extract_git_dir(expand('<amatch>')))
|
let repo = s:repo(fugitive#extract_git_dir(expand('<amatch>')))
|
||||||
let path = s:sub(s:sub(matchstr(expand('<amatch>'),'fugitive://.\{-\}//\zs.*'),'/',':'),'^\d:',':&')
|
let path = s:sub(s:sub(matchstr(expand('<amatch>'),'fugitive://.\{-\}//\zs.*'),'/',':'),'^\d:',':&')
|
||||||
let hash = repo.rev_parse(path)
|
let hash = repo.rev_parse(path)
|
||||||
if path =~ '^:'
|
if path =~ '^:'
|
||||||
@@ -2102,7 +2102,7 @@ endfunction
|
|||||||
augroup fugitive_files
|
augroup fugitive_files
|
||||||
autocmd!
|
autocmd!
|
||||||
autocmd BufReadCmd index{,.lock}
|
autocmd BufReadCmd index{,.lock}
|
||||||
\ if s:is_git_dir(expand('<amatch>:p:h')) |
|
\ if fugitive#is_git_dir(expand('<amatch>:p:h')) |
|
||||||
\ exe s:BufReadIndex() |
|
\ exe s:BufReadIndex() |
|
||||||
\ endif
|
\ endif
|
||||||
autocmd FileReadCmd fugitive://**//[0-3]/** exe s:FileRead()
|
autocmd FileReadCmd fugitive://**//[0-3]/** exe s:FileRead()
|
||||||
|
|||||||
Reference in New Issue
Block a user