Change git working directory according to the current buffer (#1290)

* Commits
* BCommits
* GFiles
This commit is contained in:
Frederick Zhang
2022-08-03 23:24:13 +10:00
committed by GitHub
parent dff3ad4b62
commit 2f3e29fc37

View File

@@ -604,7 +604,8 @@ endfunction
" ------------------------------------------------------------------
function! s:get_git_root()
let root = split(system('git rev-parse --show-toplevel'), '\n')[0]
let bufdir = substitute(split(expand('%:p:h'), '[/\\]\.git\([/\\]\|$\)')[0], '^fugitive://', '', '')
let root = systemlist('git -C ' . fzf#shellescape(bufdir) . ' rev-parse --show-toplevel')[0]
return v:shell_error ? '' : root
endfunction
@@ -613,9 +614,10 @@ function! fzf#vim#gitfiles(args, ...)
if empty(root)
return s:warn('Not in git repo')
endif
let prefix = 'git -C ' . fzf#shellescape(root) . ' '
if a:args != '?'
return s:fzf('gfiles', {
\ 'source': 'git ls-files '.a:args.(s:is_win ? '' : ' | uniq'),
\ 'source': prefix . 'ls-files '.a:args.(s:is_win ? '' : ' | uniq'),
\ 'dir': root,
\ 'options': '-m --prompt "GitFiles> "'
\}, a:000)
@@ -628,11 +630,11 @@ function! fzf#vim#gitfiles(args, ...)
let preview = printf(
\ 'bash -c "if [[ {1} =~ M ]]; then %s; else %s {-1}; fi"',
\ executable('delta')
\ ? 'git diff -- {-1} '.bar.' delta --width $FZF_PREVIEW_COLUMNS --file-style=omit '.bar.' sed 1d'
\ : 'git diff --color=always -- {-1} '.bar.' sed 1,4d',
\ ? prefix . 'diff -- {-1} ' . bar . ' delta --width $FZF_PREVIEW_COLUMNS --file-style=omit ' . bar . ' sed 1d'
\ : prefix . 'diff --color=always -- {-1} ' . bar . ' sed 1,4d',
\ s:bin.preview)
let wrapped = fzf#wrap({
\ 'source': 'git -c color.status=always status --short --untracked-files=all',
\ 'source': prefix . '-c color.status=always status --short --untracked-files=all',
\ 'dir': root,
\ 'options': ['--ansi', '--multi', '--nth', '2..,..', '--tiebreak=index', '--prompt', 'GitFiles?> ', '--preview', preview]
\})
@@ -1192,11 +1194,12 @@ function! s:commits(range, buffer_local, args)
return s:warn('Not in git repository')
endif
let source = 'git log '.get(g:, 'fzf_commits_log_options', '--color=always '.fzf#shellescape('--format=%C(auto)%h%d %s %C(green)%cr'))
let current = expand('%')
let prefix = 'git -C ' . fzf#shellescape(s:git_root) . ' '
let source = prefix . 'log '.get(g:, 'fzf_commits_log_options', '--color=always '.fzf#shellescape('--format=%C(auto)%h%d %s %C(green)%cr'))
let current = expand('%:p')
let managed = 0
if !empty(current)
call system('git show '.fzf#shellescape(current).' 2> '.(s:is_win ? 'nul' : '/dev/null'))
call system(prefix . 'show '.fzf#shellescape(current).' 2> '.(s:is_win ? 'nul' : '/dev/null'))
let managed = !v:shell_error
endif
@@ -1231,9 +1234,9 @@ function! s:commits(range, buffer_local, args)
if !s:is_win && &columns > s:wide
let suffix = executable('delta') ? '| delta --width $FZF_PREVIEW_COLUMNS' : ''
let orderfile = tempname()
call writefile([current], orderfile)
call writefile([current[len(s:git_root)+1:]], orderfile)
call extend(options.options,
\ ['--preview', 'echo {} | grep -o "[a-f0-9]\{7,\}" | head -1 | xargs git show -O'.fzf#shellescape(orderfile).' --format=format: --color=always ' . suffix])
\ ['--preview', 'echo {} | grep -o "[a-f0-9]\{7,\}" | head -1 | xargs ' . prefix . 'show -O'.fzf#shellescape(orderfile).' --format=format: --color=always ' . suffix])
endif
return s:fzf(a:buffer_local ? 'bcommits' : 'commits', options, a:args)