mirror of
https://github.com/junegunn/fzf.vim.git
synced 2025-12-07 21:24:40 +08:00
Change git working directory according to the current buffer (#1290)
* Commits * BCommits * GFiles
This commit is contained in:
@@ -604,7 +604,8 @@ endfunction
|
|||||||
" ------------------------------------------------------------------
|
" ------------------------------------------------------------------
|
||||||
|
|
||||||
function! s:get_git_root()
|
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
|
return v:shell_error ? '' : root
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
@@ -613,9 +614,10 @@ function! fzf#vim#gitfiles(args, ...)
|
|||||||
if empty(root)
|
if empty(root)
|
||||||
return s:warn('Not in git repo')
|
return s:warn('Not in git repo')
|
||||||
endif
|
endif
|
||||||
|
let prefix = 'git -C ' . fzf#shellescape(root) . ' '
|
||||||
if a:args != '?'
|
if a:args != '?'
|
||||||
return s:fzf('gfiles', {
|
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,
|
\ 'dir': root,
|
||||||
\ 'options': '-m --prompt "GitFiles> "'
|
\ 'options': '-m --prompt "GitFiles> "'
|
||||||
\}, a:000)
|
\}, a:000)
|
||||||
@@ -628,11 +630,11 @@ function! fzf#vim#gitfiles(args, ...)
|
|||||||
let preview = printf(
|
let preview = printf(
|
||||||
\ 'bash -c "if [[ {1} =~ M ]]; then %s; else %s {-1}; fi"',
|
\ 'bash -c "if [[ {1} =~ M ]]; then %s; else %s {-1}; fi"',
|
||||||
\ executable('delta')
|
\ executable('delta')
|
||||||
\ ? 'git diff -- {-1} '.bar.' delta --width $FZF_PREVIEW_COLUMNS --file-style=omit '.bar.' sed 1d'
|
\ ? prefix . '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 --color=always -- {-1} ' . bar . ' sed 1,4d',
|
||||||
\ s:bin.preview)
|
\ s:bin.preview)
|
||||||
let wrapped = fzf#wrap({
|
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,
|
\ 'dir': root,
|
||||||
\ 'options': ['--ansi', '--multi', '--nth', '2..,..', '--tiebreak=index', '--prompt', 'GitFiles?> ', '--preview', preview]
|
\ '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')
|
return s:warn('Not in git repository')
|
||||||
endif
|
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 prefix = 'git -C ' . fzf#shellescape(s:git_root) . ' '
|
||||||
let current = expand('%')
|
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
|
let managed = 0
|
||||||
if !empty(current)
|
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
|
let managed = !v:shell_error
|
||||||
endif
|
endif
|
||||||
|
|
||||||
@@ -1231,9 +1234,9 @@ function! s:commits(range, buffer_local, args)
|
|||||||
if !s:is_win && &columns > s:wide
|
if !s:is_win && &columns > s:wide
|
||||||
let suffix = executable('delta') ? '| delta --width $FZF_PREVIEW_COLUMNS' : ''
|
let suffix = executable('delta') ? '| delta --width $FZF_PREVIEW_COLUMNS' : ''
|
||||||
let orderfile = tempname()
|
let orderfile = tempname()
|
||||||
call writefile([current], orderfile)
|
call writefile([current[len(s:git_root)+1:]], orderfile)
|
||||||
call extend(options.options,
|
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
|
endif
|
||||||
|
|
||||||
return s:fzf(a:buffer_local ? 'bcommits' : 'commits', options, a:args)
|
return s:fzf(a:buffer_local ? 'bcommits' : 'commits', options, a:args)
|
||||||
|
|||||||
Reference in New Issue
Block a user