Make cloning safer

Now env vars are set after shell rc files are sourced. And the clone
request cannot specify the cmdline to execute.
This commit is contained in:
Kovid Goyal
2022-04-17 07:49:58 +05:30
parent 38e1d32065
commit 291f9e9a5e
6 changed files with 69 additions and 98 deletions

View File

@@ -32,16 +32,6 @@ builtin emulate -L zsh -o no_warn_create_global -o no_aliases
# 2: none of the above.
builtin typeset -gi _ksi_state
if [ -n "$KITTY_IS_CLONE_LAUNCH" ]; then
# store some vars before the rc files have a chance to change them
builtin typeset -gA _ksi_pre_rc
_ksi_pre_rc[path]="$PATH"
_ksi_pre_rc[conda_default_env]="$CONDA_DEFAULT_ENV"
_ksi_pre_rc[python_venv]="$VIRTUAL_ENV"
_ksi_pre_rc[is_clone_launch]="$KITTY_IS_CLONE_LAUNCH"
builtin unset KITTY_IS_CLONE_LAUNCH
fi
# Attempt to create a writable file descriptor to the TTY so that we can print
# to the TTY later even when STDOUT is redirected. This code is fairly subtle.
#
@@ -359,19 +349,21 @@ _ksi_deferred_init() {
precmd_functions=(${precmd_functions:#_ksi_deferred_init})
fi
if [ -n "${_ksi_pre_rc[is_clone_launch]}" ]; then
builtin export PATH="${_ksi_pre_rc[path]}"
if [ -n "${KITTY_IS_CLONE_LAUNCH}" ]; then
builtin local orig_conda_env="$CONDA_DEFAULT_ENV"
builtin eval "${KITTY_IS_CLONE_LAUNCH}"
builtin hash -r 2> /dev/null 1> /dev/null
if [ -n "${_ksi_pre_rc[python_venv]}" -a -r "${_ksi_pre_rc[python_venv]}/bin/activate" ]; then
builtin local venv="${VIRTUAL_ENV}/bin/activate"
if [ -n "${VIRTUAL_ENV}" -a -r "$venv" ]; then
builtin unset VIRTUAL_ENV
. "${_ksi_pre_rc[python_venv]}/bin/activate"
elif [[ -n "${_ksi_pre_rc[conda_default_env]}" && (( $+commands[conda] )) && "${_ksi_pre_rc[conda_default_env]}" != "$CONDA_DEFAULT_ENV" ]]; then
conda activate "${_ksi_pre_rc[conda_default_env]}"
. "$venv"
elif [[ -n "${CONDA_DEFAULT_ENV}" && (( $+commands[conda] )) && "${CONDA_DEFAULT_ENV}" != "$orig_conda_env" ]]; then
conda activate "${CONDA_DEFAULT_ENV}"
fi
# Ensure PATH has no duplicate entries
typeset -U path
fi
builtin unset _ksi_pre_rc
builtin unset KITTY_IS_CLONE_LAUNCH
# Unfunction _ksi_deferred_init to save memory. Don't unfunction
# kitty-integration though because decent public functions aren't supposed to
@@ -380,7 +372,7 @@ _ksi_deferred_init() {
}
clone-in-kitty() {
builtin local data="pid=$$,cwd=$(builtin printf "%s" "$PWD" | builtin command base64)"
builtin local data="shell=zsh,pid=$$,cwd=$(builtin printf "%s" "$PWD" | builtin command base64)"
while :; do
case "$1" in
"") break;;