mirror of
https://github.com/Tarrasch/zsh-autoenv.git
synced 2024-11-25 16:31:00 +02:00
Streamline cwd behavior while sourcing
This commit is contained in:
parent
67e8030770
commit
dbfb8fe519
14
autoenv.zsh
14
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
|
||||
|
12
tests/cwd.t
12
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
|
||||
|
Loading…
Reference in New Issue
Block a user