virtualenvwrapper: treat git repos as project roots again (instead of requiring a .venv customization directory)
Change error output to more conventional OMZ format, so it's clear the plugin is for oh-my-zsh and not base zsh.
Use `local` variables instead of manual unsetting.
Diff
1diff --git a/plugins/virtualenvwrapper/virtualenvwrapper.plugin.zsh b/plugins/virtualenvwrapper/virtualenvwrapper.plugin.zsh
2index f5f37ff6f573a2f8303ac7498d9b96d46a2b6b80..c40ca86ac8254a5eaa673eb13a58d70c7dc19e75 100644
3--- a/plugins/virtualenvwrapper/virtualenvwrapper.plugin.zsh
4+++ b/plugins/virtualenvwrapper/virtualenvwrapper.plugin.zsh
5@@ -14,32 +14,38 @@ elif [[ -f "/etc/bash_completion.d/virtualenvwrapper" ]]; then
6 source "/etc/bash_completion.d/virtualenvwrapper"
7 }
8 else
9- print "zsh virtualenvwrapper plugin: Cannot find ${virtualenvwrapper}.\n"\
10+ print "[oh-my-zsh] virtualenvwrapper plugin: Cannot find ${virtualenvwrapper}.\n"\
11 "Please install with \`pip install virtualenvwrapper\`" >&2
12 return
13 fi
14 if ! type workon &>/dev/null; then
15- print "zsh virtualenvwrapper plugin: shell function 'workon' not defined.\n"\
16+ print "[oh-my-zsh] virtualenvwrapper plugin: shell function 'workon' not defined.\n"\
17 "Please check ${virtualenvwrapper}" >&2
18 return
19 fi
2021 if [[ "$WORKON_HOME" == "" ]]; then
22- print "\$WORKON_HOME is not defined so ZSH plugin virtualenvwrapper will not work" >&2
23+ print "[oh-my-zsh] \$WORKON_HOME is not defined so plugin virtualenvwrapper will not work" >&2
24 return
25 fi
2627 if [[ ! $DISABLE_VENV_CD -eq 1 ]]; then
28- # Automatically activate Git projects' virtual environments based on the
29+ # Automatically activate Git projects or other customized virtualenvwrapper projects based on the
30 # directory name of the project. Virtual environment name can be overridden
31- # by placing a .venv file in the project root with a virtualenv name in it
32+ # by placing a .venv file in the project root with a virtualenv name in it.
33 function workon_cwd {
34- if [ ! $WORKON_CWD ]; then
35- WORKON_CWD=1
36+ if [[ -z "$WORKON_CWD" ]]; then
37+ local WORKON_CWD=1
38 # Check if this is a Git repo
39+ local GIT_REPO_ROOT=""
40+ local GIT_TOPLEVEL=$(git rev-parse --show-toplevel 2> /dev/null)
41+ if [[ $? == 0 ]]; then
42+ GIT_REPO_ROOT="$GIT_TOPLEVEL"
43+ fi
44 # Get absolute path, resolving symlinks
45- PROJECT_ROOT="${PWD:A}"
46- while [[ "$PROJECT_ROOT" != "/" && ! -e "$PROJECT_ROOT/.venv" ]]; do
47+ local PROJECT_ROOT="${PWD:A}"
48+ while [[ "$PROJECT_ROOT" != "/" && ! -e "$PROJECT_ROOT/.venv" \
49+ && ! -d "$PROJECT_ROOT/.git" && "$PROJECT_ROOT" != "$GIT_REPO_ROOT" ]]; do
50 PROJECT_ROOT="${PROJECT_ROOT:h}"
51 done
52 if [[ "$PROJECT_ROOT" == "/" ]]; then
53@@ -47,7 +53,7 @@ if [[ ! $DISABLE_VENV_CD -eq 1 ]]; then
54 fi
55 # Check for virtualenv name override
56 if [[ -f "$PROJECT_ROOT/.venv" ]]; then
57- ENV_NAME=`cat "$PROJECT_ROOT/.venv"`
58+ ENV_NAME=`cat "$PROJECT_ROOT/.venv"`
59 elif [[ -f "$PROJECT_ROOT/.venv/bin/activate" ]];then
60 ENV_NAME="$PROJECT_ROOT/.venv"
61 elif [[ "$PROJECT_ROOT" != "." ]]; then
62@@ -69,8 +75,6 @@ if [[ ! $DISABLE_VENV_CD -eq 1 ]]; then
63 # Note: this only happens if the virtualenv was activated automatically
64 deactivate && unset CD_VIRTUAL_ENV
65 fi
66- unset PROJECT_ROOT
67- unset WORKON_CWD
68 fi
69 }
70