autoenv.zsh | 14 +++++++++++--- tests/cwd.t | 12 ++++++------ diff --git a/autoenv.zsh b/autoenv.zsh index 317217f43a048ac6870696cabb8b33b610a6e824..19891b688eed8bf5f1b49dc5dd03152c2240654c 100644 --- a/autoenv.zsh +++ b/autoenv.zsh @@ -96,7 +96,9 @@ _dotenv_source() { local env_file=$1 _dotenv_event=$2 - _dotenv_cwd=$3 + _dotenv_envfile_dir=$3 + _dotenv_from_dir=$_dotenv_chpwd_prev_dir + _dotenv_to_dir=$PWD # Source varstash library once. if [[ $_dotenv_sourced_varstash == 0 ]]; then @@ -108,13 +110,14 @@ fi # Change to directory of env file, source it and cd back. local new_dir=$PWD - builtin cd -q $_dotenv_cwd + builtin cd -q $_dotenv_envfile_dir source $env_file builtin cd -q $new_dir - unset _dotenv_event _dotenv_cwd + unset _dotenv_event _dotenv_from_dir } +_dotenv_chpwd_prev_dir=$PWD _dotenv_chpwd_handler() { local env_file="$PWD/$DOTENV_FILE_ENTER" @@ -140,11 +143,13 @@ m=((../)#${DOTENV_FILE_ENTER}(N)) if (( $#m )); then env_file=${${m[1]}:A} else + _dotenv_chpwd_prev_dir=$PWD return fi fi if ! _dotenv_check_authorized_env_file $env_file; then + _dotenv_chpwd_prev_dir=$PWD return fi @@ -152,12 +157,15 @@ # Load the env file only once: check if $env_file's parent # is in $_dotenv_stack_entered. local env_file_dir=${env_file:A:h} if (( ${+_dotenv_stack_entered[(r)${env_file_dir}]} )); then + _dotenv_chpwd_prev_dir=$PWD return fi _dotenv_stack_entered+=(${env_file_dir}) _dotenv_source $env_file enter $PWD + + _dotenv_chpwd_prev_dir=$PWD } autoload -U add-zsh-hook diff --git a/tests/cwd.t b/tests/cwd.t index 2c5838a2be75a3f880c3b6c96db6b94157e7a19a..d6b39028a8a738aae20c81de23fd12770675adbe 100644 --- a/tests/cwd.t +++ b/tests/cwd.t @@ -1,4 +1,4 @@ -Test $PWD and $_dotenv_cwd. +Test $PWD, $_dotenv_from_dir and _dotenv_to_dir. $ source $TESTDIR/setup.sh @@ -7,8 +7,8 @@ $ DOTENV_LOOK_UPWARDS=1 $ mkdir -p sub/sub2 $ cd sub - $ echo 'echo ENTERED: cwd:${PWD:t} ${_dotenv_cwd:t}' >> .env - $ echo 'echo LEFT: cwd:${PWD:t} ${_dotenv_cwd:t}' >> .env.leave + $ echo 'echo ENTERED: PWD:${PWD:t} from:${_dotenv_from_dir:t} to:${_dotenv_to_dir:t}' > .env + $ echo 'echo LEFT: PWD:${PWD:t} from:${_dotenv_from_dir:t} to:${_dotenv_to_dir:t}' > .env.leave Manually create auth files. @@ -17,10 +17,10 @@ The actual tests. $ cd . - ENTERED: cwd:sub sub + ENTERED: PWD:sub from:sub to:sub $ cd .. - LEFT: cwd:sub cwd.t + LEFT: PWD:sub from:sub to:cwd.t $ cd sub/sub2 - ENTERED: cwd:sub sub2 + ENTERED: PWD:sub2 from:cwd.t to:sub2