-
Notifications
You must be signed in to change notification settings - Fork 49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Avoid breaking current directory on neovim #95
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
if exists(':tcd') | ||
let chdir = haslocaldir() ? 'lcd' : haslocaldir(-1, 0) ? 'tcd' : 'cd' | ||
else | ||
let chdir = exists('*haslocaldir') && haslocaldir() ? 'lcd' : 'cd' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can externalize both checks, but that would only make a difference if we define a function for each case. Otherwise we're swapping a strong check (i.e. By conditioning the implementation like that, we end up with an unmaintainable version of it: if has('nvim')
function! magit#utils#chdir(dir)
let chdir = haslocaldir() ? 'lcd' : haslocaldir(-1, 0) ? 'tcd' : 'cd'
exec chdir a:dir
endfunction
elseif exists('*haslocaldir')
function! magit#utils#chdir(dir)
let chdir = haslocaldir() ? 'lcd' : 'cd'
exec chdir a:dir
endfunction
else
function! magit#utils#chdir(dir)
cd a:dir
endfunction
endif There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you think it is unmaintainable? I think this is a good solution, even better than my previous solution of storing the chdir command in a variable. I do prefer this solution. Maybe this is a micro optimization, but if vimagit wins two if statements during runtime, I prefer that. Several points here:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With this solution, we end up with 3 functions to maintain, so we have three places to add Also,this is a premature optimization; we're trading readability and maintainability for theoretical performance gains, which never proved to be actually necessary. Plus, if we scale the amount of time spent on operations, changing the directory setting of nvim/vim is way faster than making a system call to interact with git, for example, so even if vimagit was extremely slow, this optimization would not perceivable results. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, I won't fight against this, in the end this is only a micro optim. One day, I hope changing directory won't be necessary... I already tried to play around with GIT_WORK_TREE and GIT_DIR environment variables, but it was a dead end, I can't remember why. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would prefer to not mix two solutions based on third party presence. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I realised my comment was stupid. Still, you can use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I already tried these. I try to remember why I did not went to the end... Maybe because these options appeared lately in git. I'll check that, but it could worth a try to support both:
|
||
endif | ||
execute chdir fnameescape(a:dir) | ||
endfunction | ||
|
||
" magit#utils#clear_undo: this function clear local undo history. | ||
|
@@ -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') | ||
|
@@ -75,7 +80,7 @@ function! magit#utils#system(...) | |
endif | ||
endif | ||
finally | ||
call magit#utils#lcd(dir) | ||
call magit#utils#chdir(dir) | ||
endtry | ||
endfunction | ||
|
||
|
@@ -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 | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
:tcd
has been introduced right after nvim 0.1.3 (I did not check, it should be officially present since nvim 0.1.4).Before
:tcd
introduction,haslocaldir(-1, 0)
will return an error:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will do. I think nvim allows checking for version with
has('nvim-0.1.4')
. If not I'll probably be able toexists('*tcd')
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer exists() solution .
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I already implemented using
exists
. Github doesn't update diffs properly now... :/