Fixed recursion. Git not needed for it to work.
Avoid infinite `cd` loops under certain conditions. Try getting `.venv` from the current directory (not necessarily always using git)
Diff
1diff --git a/plugins/virtualenvwrapper/virtualenvwrapper.plugin.zsh b/plugins/virtualenvwrapper/virtualenvwrapper.plugin.zsh
2index 0ed2565b4fcd1bc2c0ced33cd18ebf71500c65d9..35de508746ad21f31290cbac98861b5b3fcc7c3c 100644
3--- a/plugins/virtualenvwrapper/virtualenvwrapper.plugin.zsh
4+++ b/plugins/virtualenvwrapper/virtualenvwrapper.plugin.zsh
5@@ -8,26 +8,36 @@ if [[ -f "$wrapsource" ]]; then
6 # directory name of the project. Virtual environment name can be overridden
7 # by placing a .venv file in the project root with a virtualenv name in it
8 function workon_cwd {
9- # Check that this is a Git repo
10- PROJECT_ROOT=`git rev-parse --show-toplevel 2> /dev/null`
11- if (( $? == 0 )); then
12+ if [ ! $WORKON_CWD ]; then
13+ WORKON_CWD=1
14+ # Check if this is a Git repo
15+ PROJECT_ROOT=`git rev-parse --show-toplevel 2> /dev/null`
16+ if (( $? != 0 )); then
17+ PROJECT_ROOT="."
18+ fi
19 # Check for virtualenv name override
20- ENV_NAME=`basename "$PROJECT_ROOT"`
21 if [[ -f "$PROJECT_ROOT/.venv" ]]; then
22 ENV_NAME=`cat "$PROJECT_ROOT/.venv"`
23+ elif [[ "$PROJECT_ROOT" != "." ]]; then
24+ ENV_NAME=`basename "$PROJECT_ROOT"`
25+ else
26+ ENV_NAME=""
27 fi
28- # Activate the environment only if it is not already active
29- if [[ "$VIRTUAL_ENV" != "$WORKON_HOME/$ENV_NAME" ]]; then
30- if [[ -e "$WORKON_HOME/$ENV_NAME/bin/activate" ]]; then
31- workon "$ENV_NAME" && export CD_VIRTUAL_ENV="$ENV_NAME"
32+ if [[ "$ENV_NAME" != "" ]]; then
33+ # Activate the environment only if it is not already active
34+ if [[ "$VIRTUAL_ENV" != "$WORKON_HOME/$ENV_NAME" ]]; then
35+ if [[ -e "$WORKON_HOME/$ENV_NAME/bin/activate" ]]; then
36+ workon "$ENV_NAME" && export CD_VIRTUAL_ENV="$ENV_NAME"
37+ fi
38 fi
39+ elif [ $CD_VIRTUAL_ENV ]; then
40+ # We've just left the repo, deactivate the environment
41+ # Note: this only happens if the virtualenv was activated automatically
42+ deactivate && unset CD_VIRTUAL_ENV
43 fi
44- elif [ $CD_VIRTUAL_ENV ]; then
45- # We've just left the repo, deactivate the environment
46- # Note: this only happens if the virtualenv was activated automatically
47- deactivate && unset CD_VIRTUAL_ENV
48+ unset PROJECT_ROOT
49+ unset WORKON_CWD
50 fi
51- unset PROJECT_ROOT
52 }
5354 # New cd function that does the virtualenv magic