1
0
mirror of https://github.com/Tarrasch/zsh-autoenv.git synced 2024-11-25 16:31:00 +02:00

varstash: add support for exported variables in subshells

This commit is contained in:
Daniel Hahler 2015-04-23 15:11:40 +02:00
parent fe3d479b08
commit c4e388980e
2 changed files with 75 additions and 7 deletions

View File

@ -188,7 +188,7 @@ function stash() {
elif [[ $vartype == $pattern" -x"* ]]; then elif [[ $vartype == $pattern" -x"* ]]; then
# variable is exported # variable is exported
if [[ -z $already_stashed ]]; then if [[ -z $already_stashed ]]; then
eval "__varstash_export__$stash_name=\"\$$stash_which\"" eval "export __varstash_export__$stash_name=\"\$$stash_which\""
fi fi
if [[ $stash_which != $stash_expression && -z $_stashing_alias_assign ]]; then if [[ $stash_which != $stash_expression && -z $_stashing_alias_assign ]]; then
eval "export $stash_which=\"$stash_value\"" eval "export $stash_which=\"$stash_value\""
@ -214,7 +214,7 @@ function stash() {
# (eval):1: command not found: __varstash_nostash___tmp__home_dolszewski_src_smartcd_RANDOM_VARIABLE=1 # (eval):1: command not found: __varstash_nostash___tmp__home_dolszewski_src_smartcd_RANDOM_VARIABLE=1
# fixed in zsh commit 724fd07a67f, version 4.3.14 # fixed in zsh commit 724fd07a67f, version 4.3.14
if [[ -z $already_stashed ]]; then if [[ -z $already_stashed ]]; then
eval "__varstash_nostash__$stash_name=1" eval "export __varstash_nostash__$stash_name=1"
fi fi
# In the case of a previously unset variable that we're assigning too, export it # In the case of a previously unset variable that we're assigning too, export it
@ -228,6 +228,14 @@ function stash() {
done done
} }
function get_autostash_array_name() {
local autostash_name=$(_mangle_var AUTOSTASH)
# Create a scalar variable linked to an array (for exporting).
local autostash_array_name=${(L)autostash_name}
typeset -xT $autostash_name $autostash_array_name
ret=$autostash_array_name
}
function autostash() { function autostash() {
local run_from_autostash=1 local run_from_autostash=1
while [[ -n $1 ]]; do while [[ -n $1 ]]; do
@ -239,9 +247,9 @@ function autostash() {
local already_stashed= local already_stashed=
stash "$1" stash "$1"
if [[ -z $already_stashed ]]; then if [[ -z $already_stashed ]]; then
local autostash_name=$(_mangle_var AUTOSTASH) local ret varname=${1%%'='*}
local varname=${1%%'='*} get_autostash_array_name
apush $autostash_name "$varname" apush $ret "$varname"
fi fi
shift shift
unset -v _stashing_alias_assign unset -v _stashing_alias_assign
@ -322,7 +330,9 @@ function unstash() {
function autounstash() { function autounstash() {
# If there is anything in (mangled) variable AUTOSTASH, then unstash it # If there is anything in (mangled) variable AUTOSTASH, then unstash it
local autounstash_name=$(_mangle_var AUTOSTASH) local ret
get_autostash_array_name
local autounstash_name=$ret
if (( $(alen $autounstash_name) > 0 )); then if (( $(alen $autounstash_name) > 0 )); then
local run_from_autounstash=1 local run_from_autounstash=1
while (( $(alen $autounstash_name) > 0 )); do while (( $(alen $autounstash_name) > 0 )); do
@ -341,4 +351,4 @@ function _mangle_var() {
echo "_tmp_${mangle_var_where}_${mangled_name}" echo "_tmp_${mangle_var_where}_${mangled_name}"
} }
# vim: filetype=sh autoindent expandtab shiftwidth=4 softtabstop=4 # vim: filetype=zsh autoindent expandtab shiftwidth=4 softtabstop=4

58
tests/varstash_export.t Normal file
View File

@ -0,0 +1,58 @@
Test varstash with exported variables in subshell.
$ source $TESTDIR/setup.sh || return 1
Setup test environment.
$ mkdir sub
$ cd sub
$ echo 'echo ENTER; autostash MYVAR=changed; autostash MYEXPORT=changed_export' > $AUTOENV_FILE_ENTER
$ echo 'echo LEAVE; autounstash' > $AUTOENV_FILE_LEAVE
Manually create auth file
$ test_autoenv_auth_env_files
Set environment variable.
$ MYVAR=orig
$ export MYEXPORT=orig_export
Activating the env stashes it and applies a new value.
$ cd .
ENTER
$ echo $MYVAR
changed
$ echo $MYEXPORT
changed_export
The variable is not available in a subshell, only the exported one.
$ $SHELL -c 'echo ${MYVAR:-empty}; echo $MYEXPORT'
empty
changed_export
Activate autoenv in the subshell.
$ $SHELL -c 'source $TEST_AUTOENV_PLUGIN_FILE; echo ${MYVAR}; echo $MYEXPORT'
ENTER
changed
changed_export
"autounstash" should handle the exported variables.
$ $SHELL -c 'source $TEST_AUTOENV_PLUGIN_FILE; cd ..; echo ${MYVAR:-empty}; echo $MYEXPORT'
ENTER
LEAVE
empty
orig_export
#
# Exiting the subshell should restore.
#
# $ pwd
# */varstash_export.t (glob)
# $ echo $MYVAR
# changed
# $ echo $MYEXPORT
# changed_export