Merge pull request #16734 from zeertzjq/chdir-refactor

refactor: remove some chdir-related unnecessary calls and checks
This commit is contained in:
James McCoy 2021-12-20 14:28:11 -05:00 committed by GitHub
commit 9241c684e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 13 deletions

View File

@ -1082,15 +1082,13 @@ static void f_chdir(typval_T *argvars, typval_T *rettv, FunPtr fptr)
// Return the current directory // Return the current directory
cwd = xmalloc(MAXPATHL); cwd = xmalloc(MAXPATHL);
if (cwd != NULL) { if (os_dirname(cwd, MAXPATHL) != FAIL) {
if (os_dirname(cwd, MAXPATHL) != FAIL) {
#ifdef BACKSLASH_IN_FILENAME #ifdef BACKSLASH_IN_FILENAME
slash_adjust(cwd); slash_adjust(cwd);
#endif #endif
rettv->vval.v_string = vim_strsave(cwd); rettv->vval.v_string = vim_strsave(cwd);
}
xfree(cwd);
} }
xfree(cwd);
if (curwin->w_localdir != NULL) { if (curwin->w_localdir != NULL) {
scope = kCdScopeWindow; scope = kCdScopeWindow;

View File

@ -7816,8 +7816,9 @@ bool changedir_func(char_u *new_dir, CdScope scope)
} }
#endif #endif
if (vim_chdir(new_dir) == 0) { bool dir_differs = new_dir == NULL || pdir == NULL
bool dir_differs = pdir == NULL || pathcmp((char *)pdir, (char *)new_dir, -1) != 0; || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
if (new_dir != NULL && (!dir_differs || vim_chdir(new_dir) == 0)) {
post_chdir(scope, dir_differs); post_chdir(scope, dir_differs);
retval = true; retval = true;
} else { } else {

View File

@ -1667,14 +1667,19 @@ int vim_chdirfile(char_u *fname, CdCause cause)
NameBuff[0] = NUL; NameBuff[0] = NUL;
} }
if (os_chdir(dir) == 0) { if (pathcmp(dir, (char *)NameBuff, -1) == 0) {
if (cause != kCdCauseOther && pathcmp(dir, (char *)NameBuff, -1) != 0) { // nothing to do
do_autocmd_dirchanged(dir, kCdScopeWindow, cause); return OK;
} }
} else {
if (os_chdir(dir) != 0) {
return FAIL; return FAIL;
} }
if (cause != kCdCauseOther) {
do_autocmd_dirchanged(dir, kCdScopeWindow, cause);
}
return OK; return OK;
} }