Skip to content

Commit

Permalink
Change lcd/cd to lcd/tcd/cd based change directory workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
hkupty committed Oct 20, 2016
1 parent e24e40e commit 1ebfaf4
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 18 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 @@ -273,7 +273,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 @@ -288,7 +288,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
53 changes: 41 additions & 12 deletions autoload/magit/utils.vim
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,43 @@ 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 . 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 . ' ' . a:dir
endfunction

" magit#utils#pushd_root goes to git root directory, saving current directory
" prior to changing the cwd.
function! magit#utils#pushd_root()
if has('nvim')
let cwd = haslocaldir(-1, 0) ? getcwd(-1, 0) : getcwd()
else
let cwd = getcwd()
endif

let root = magit#git#top_dir()

if root !=? cwd
call magit#utils#chdir(root)
let g:_vimagit_cwd = cwd
endif
endfunction


" magit#utils#popd_cwd returns to previous directory
function! magit#utils#popd_cwd()
if exists('g:_vimagit_cwd')
call magit#utils#chdir(g:_vimagit_cwd)
unlet g:_vimagit_cwd
endif
endfunction

" magit#utils#clear_undo: this function clear local undo history.
Expand Down Expand Up @@ -53,9 +84,8 @@ endfunction
" param[in] ...: command + optional args
" return: command output as a string
function! magit#utils#system(...)
let dir = getcwd()
try
execute s:magit_cd_cmd . magit#git#top_dir()
call magit#utils#pushd_root()
" 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 +105,7 @@ function! magit#utils#system(...)
endif
endif
finally
execute s:magit_cd_cmd . dir
call magit#utils#popd_cwd()
endtry
endfunction

Expand All @@ -86,17 +116,16 @@ endfunction
" param[in] ...: command + optional args to execute, args can be List or String
" return: command output as a list
function! magit#utils#systemlist(...)
let dir = getcwd()
try
execute s:magit_cd_cmd . magit#git#top_dir()
call magit#utils#pushd_root()
" 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
execute s:magit_cd_cmd . dir
call magit#utils#popd_cwd()
endtry
endfunction

Expand Down

0 comments on commit 1ebfaf4

Please sign in to comment.