From dbfb8fe51948701997d4d03dd02d63c61d3dfff0 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Fri, 21 Nov 2014 22:33:00 +0100 Subject: [PATCH] Streamline cwd behavior while sourcing --- autoenv.zsh | 14 +++++++++++--- tests/cwd.t | 12 ++++++------ 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/autoenv.zsh b/autoenv.zsh index 317217f..19891b6 100644 --- a/autoenv.zsh +++ b/autoenv.zsh @@ -96,7 +96,9 @@ _dotenv_this_dir=${0:A:h} _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 @@ _dotenv_source() { # 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 @@ _dotenv_chpwd_handler() { 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 @@ _dotenv_chpwd_handler() { # 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 2c5838a..d6b3902 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 @@ Setup env actions / output. $ 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 @@ Manually create auth files. 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 -- 2.44.0