Skip to content

Commit

Permalink
Avoid breaking tab-local directory settings
Browse files Browse the repository at this point in the history
Neovim has support for tab-local settings, additionally to global and
window-local.

When a tab is set with `tcd`, if the directory is changed with `cd`, the
tab-local setting is discarded.

This patch fixes the current bug by setting checking the granularity of current
setting prior to changing directory.
  • Loading branch information
hkupty committed Oct 25, 2016
1 parent 78bde21 commit c746c5e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
8 changes: 4 additions & 4 deletions autoload/magit/git.vim
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ endfunction
function! magit#git#is_work_tree(path)
let dir = getcwd()
try
call magit#utils#lcd(a:path)
call magit#utils#chdir(a:path)
let top_dir=magit#utils#strip(
\ system(g:magit_git_cmd . " rev-parse --show-toplevel")) . "/"
if ( v:shell_error != 0 )
return ''
endif
return top_dir
finally
call magit#utils#lcd(dir)
call magit#utils#chdir(dir)
endtry
endfunction

Expand All @@ -68,7 +68,7 @@ endfunction
function! magit#git#set_top_dir(path)
let dir = getcwd()
try
call magit#utils#lcd(a:path)
call magit#utils#chdir(a:path)
let top_dir=magit#utils#strip(
\ system(g:magit_git_cmd . " rev-parse --show-toplevel")) . "/"
if ( v:shell_error != 0 )
Expand All @@ -81,7 +81,7 @@ function! magit#git#set_top_dir(path)
let b:magit_top_dir=top_dir
let b:magit_git_dir=git_dir
finally
call magit#utils#lcd(dir)
call magit#utils#chdir(dir)
endtry
endfunction

Expand Down
4 changes: 2 additions & 2 deletions autoload/magit/state.vim
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ function! magit#state#update() dict

let dir = getcwd()
try
call magit#utils#lcd(magit#git#top_dir())
call magit#utils#chdir(magit#git#top_dir())
call magit#utils#refresh_submodule_list()
for [mode, diff_dict_mode] in items(self.dict)
let status_list = magit#git#get_status()
Expand All @@ -289,7 +289,7 @@ function! magit#state#update() dict
endfor
endfor
finally
call magit#utils#lcd(dir)
call magit#utils#chdir(dir)
endtry

" remove files that have changed their mode or been committed/deleted/discarded...
Expand Down
25 changes: 15 additions & 10 deletions autoload/magit/utils.vim
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,17 @@ function! magit#utils#is_submodule(dirname)
return ( index(s:submodule_list, a:dirname) != -1 )
endfunction

" s:magit_cd_cmd: plugin variable to choose lcd/cd command, 'lcd' if exists,
" 'cd' otherwise
let s:magit_cd_cmd = exists('*haslocaldir') && haslocaldir() ? 'lcd ' : 'cd '
" magit#utils#lcd: helper function to lcd. use cd if lcd doesn't exists
function! magit#utils#lcd(dir)
execute s:magit_cd_cmd . fnameescape(a:dir)
" magit#utils#chdir will change the directory respecting
" local/tab-local/global directory settings.
function! magit#utils#chdir(dir)
" This is a dirty hack to fix tcd breakages on neovim.
" Future work should be based on nvim API and has a `if has('nvim')` check.
if has('nvim')
let chdir = haslocaldir() ? 'lcd' : haslocaldir(-1, 0) ? 'tcd' : 'cd'
else
let chdir = exists('*haslocaldir') && haslocaldir() ? 'lcd' : 'cd'
endif
execute chdir fnameescape(a:dir)
endfunction

" magit#utils#clear_undo: this function clear local undo history.
Expand Down Expand Up @@ -55,7 +60,7 @@ endfunction
function! magit#utils#system(...)
let dir = getcwd()
try
call magit#utils#lcd(magit#git#top_dir())
call magit#utils#chdir(magit#git#top_dir())
" List as system() input is since v7.4.247, it is safe to check
" systemlist, which is sine v7.4.248
if exists('*systemlist')
Expand All @@ -75,7 +80,7 @@ function! magit#utils#system(...)
endif
endif
finally
call magit#utils#lcd(dir)
call magit#utils#chdir(dir)
endtry
endfunction

Expand All @@ -88,15 +93,15 @@ endfunction
function! magit#utils#systemlist(...)
let dir = getcwd()
try
call magit#utils#lcd(magit#git#top_dir())
call magit#utils#chdir(magit#git#top_dir())
" systemlist since v7.4.248
if exists('*systemlist')
return call('systemlist', a:000)
else
return split(call('magit#utils#system', a:000), '\n')
endif
finally
call magit#utils#lcd(dir)
call magit#utils#chdir(dir)
endtry
endfunction

Expand Down

0 comments on commit c746c5e

Please sign in to comment.