Skip to content

Commit

Permalink
Git: add support for worktree
Browse files Browse the repository at this point in the history
  • Loading branch information
hattya committed Apr 21, 2024
1 parent d25e551 commit 0d98b8d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
16 changes: 11 additions & 5 deletions autoload/vcs_info/git.vim
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
" File: autoload/vcs_info/git.vim
" Author: Akinori Hattori <hattya@gmail.com>
" Last Change: 2020-10-30
" Last Change: 2024-04-21
" License: MIT License

let s:save_cpo = &cpo
Expand All @@ -17,20 +17,26 @@ function! vcs_info#git#detect(path) abort
return git_dir
endif
elseif ft ==# 'file'
let rel = vcs_info#readfile(git_dir)
let i = matchend(rel, '^gitdir:\s*')
let data = vcs_info#readfile(git_dir)
let i = matchend(data, '^gitdir:\s*')
if i != -1
return simplify(s:FP.join(a:path, vcs_info#from_slash(rel[i :])))
let git_dir = vcs_info#from_slash(data[i :])
return s:FP.is_absolute(git_dir) ? git_dir : simplify(s:FP.join(a:path, git_dir))
endif
endif
return ''
endfunction

function! vcs_info#git#get(git_dir) abort
let sep = escape(s:FP.separator(), '\')
if a:git_dir =~# sep . '\.git' . sep . 'worktrees' . sep
let root = s:FP.dirname(vcs_info#from_slash(vcs_info#readfile(s:FP.join(a:git_dir, 'gitdir'))))
else
let root = substitute(a:git_dir, '\v' . sep . '\.git%(' . sep . 'modules)=', '', '')
endif
let info = {
\ 'vcs': 'Git',
\ 'root': substitute(a:git_dir, '\v' . sep . '\.git%(' . sep . 'modules)=', '', ''),
\ 'root': root,
\ 'dir': a:git_dir,
\ 'head': '',
\ 'action': '',
Expand Down
16 changes: 16 additions & 0 deletions test/git.vimspec
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,22 @@ Describe Git barckend
\ })
End

It detects a repository (worktree)
call mkdir(Join(git_dir, 'worktrees', 'next'), 'p')
call writefile(['ref: refs/heads/next'], Join(git_dir, 'worktrees', 'next', 'HEAD'))
call writefile([Join(path, 'next', '.git')], Join(git_dir, 'worktrees', 'next', 'gitdir'))
call mkdir(Join(path, 'next'))
call writefile(['gitdir: ' . Join(git_dir, 'worktrees', 'next')], Join(path, 'next', '.git'))
edit `=Join(path, 'next', 'file')`
Assert Equals(vcs_info#get() , {
\ 'vcs': vcs,
\ 'root': Join(path, 'next'),
\ 'dir': Join(git_dir, 'worktrees', 'next'),
\ 'head': 'next',
\ 'action': '',
\ })
End

It detects that it is in the 'detached HEAD' state
call writefile([hash], Join(git_dir, 'HEAD'))
Assert Equals(vcs_info#get(), {
Expand Down

0 comments on commit 0d98b8d

Please sign in to comment.