906f135f26573d10077e8d54e42c5790f50ce076

Author
Nick Cross <ncross@redhat.com>
Committer
Nick Cross <ncross@redhat.com>
Date

Message

Make variables local to functions to prevent leakage.

Diff

 1diff --git a/autoswitch_virtualenv.plugin.zsh b/autoswitch_virtualenv.plugin.zsh
 2index 174bb60a189e628c0e1705d93227a49bf7c4433d..469d8782ae5fed9e736b062283914345c8b22990 100644
 3--- a/autoswitch_virtualenv.plugin.zsh
 4+++ b/autoswitch_virtualenv.plugin.zsh
 5@@ -38,11 +38,11 @@ function _python_version() {
 6 
 7 
 8 function _maybeworkon() {
 9-    venv_dir="$1"
10-    venv_type="$2"
11-    venv_name="$(basename $venv_dir)"
12+    local venv_dir="$1"
13+    local venv_type="$2"
14+    local venv_name="$(basename $venv_dir)"
15 
16-    DEFAULT_MESSAGE_FORMAT="Switching %venv_type: ${BOLD}${PURPLE}%venv_name${NORMAL} ${GREEN}[๐Ÿ%py_version]${NORMAL}"
17+    local DEFAULT_MESSAGE_FORMAT="Switching %venv_type: ${BOLD}${PURPLE}%venv_name${NORMAL} ${GREEN}[๐Ÿ%py_version]${NORMAL}"
18     if [[ "$LANG" != *".UTF-8" ]]; then
19         # Remove multibyte characters if the terminal does not support utf-8
20         DEFAULT_MESSAGE_FORMAT="${DEFAULT_MESSAGE_FORMAT/๐Ÿ/}"
21@@ -57,9 +57,8 @@ function _maybeworkon() {
22         fi
23 
24         if [ -z "$AUTOSWITCH_SILENT" ]; then
25-            py_version="$(_python_version "$venv_dir/bin/python")"
26-
27-            message="${AUTOSWITCH_MESSAGE_FORMAT:-"$DEFAULT_MESSAGE_FORMAT"}"
28+            local py_version="$(_python_version "$venv_dir/bin/python")"
29+            local message="${AUTOSWITCH_MESSAGE_FORMAT:-"$DEFAULT_MESSAGE_FORMAT"}"
30             message="${message//\%venv_type/$venv_type}"
31             message="${message//\%venv_name/$venv_name}"
32             message="${message//\%py_version/$py_version}"
33@@ -93,10 +92,10 @@ function _check_venv_path()
34 # Automatically switch virtualenv when .venv file detected
35 function check_venv()
36 {
37-    SWITCH_TO=""
38+    local SWITCH_TO=""
39 
40     # Get the .venv file, scanning parent directories
41-    venv_path=$(_check_venv_path "$PWD")
42+    local venv_path=$(_check_venv_path "$PWD")
43     if [[ -n "$venv_path" ]]; then
44 
45         stat --version &> /dev/null
46@@ -143,7 +142,6 @@ function _default_venv()
47         _maybeworkon "$(_virtual_env_dir "$AUTOSWITCH_DEFAULTENV")" "virtualenv"
48     elif [[ -n "$VIRTUAL_ENV" ]]; then
49         deactivate
50-        unset venv_dir venv_name venv_path venv_type
51     fi
52 }
53 
54@@ -152,12 +150,11 @@ function _default_venv()
55 function rmvenv()
56 {
57     if [[ -f ".venv" ]]; then
58-
59-        venv_name="$(<.venv)"
60+        local venv_name="$(<.venv)"
61 
62         # detect if we need to switch virtualenv first
63         if [[ -n "$VIRTUAL_ENV" ]]; then
64-            current_venv="$(basename $VIRTUAL_ENV)"
65+            local current_venv="$(basename $VIRTUAL_ENV)"
66             if [[ "$current_venv" = "$venv_name" ]]; then
67                 _default_venv
68             fi
69@@ -178,7 +175,7 @@ function mkvenv()
70     if [[ -f ".venv" ]]; then
71         printf ".venv file already exists. If this is a mistake use the rmvenv command\n"
72     else
73-        venv_name="$(basename $PWD)"
74+        local venv_name="$(basename $PWD)"
75 
76         printf "Creating ${PURPLE}%s${NONE} virtualenv\n" "$venv_name"
77