]> Sergey Matveev's repositories - zsh-autoenv.git/commitdiff
Streamline cwd behavior while sourcing
authorDaniel Hahler <git@thequod.de>
Fri, 21 Nov 2014 21:33:00 +0000 (22:33 +0100)
committerDaniel Hahler <git@thequod.de>
Mon, 24 Nov 2014 19:13:18 +0000 (20:13 +0100)
autoenv.zsh
tests/cwd.t

index 317217f43a048ac6870696cabb8b33b610a6e824..19891b688eed8bf5f1b49dc5dd03152c2240654c 100644 (file)
@@ -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
index 2c5838a2be75a3f880c3b6c96db6b94157e7a19a..d6b39028a8a738aae20c81de23fd12770675adbe 100644 (file)
@@ -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