From ee14d5fa590f412e2f58f68157b4faaed036d7f2 Mon Sep 17 00:00:00 2001 From: Pavel Puchkin Date: Fri, 22 Jun 2012 20:50:08 +1100 Subject: [PATCH 1/6] Add git_get_root function which returns root of current repo --- lib/git.zsh | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/git.zsh b/lib/git.zsh index fb4ad8ca6..3db83b6c2 100644 --- a/lib/git.zsh +++ b/lib/git.zsh @@ -4,6 +4,10 @@ function git_prompt_info() { echo "$ZSH_THEME_GIT_PROMPT_PREFIX${ref#refs/heads/}$(parse_git_dirty)$ZSH_THEME_GIT_PROMPT_SUFFIX" } +# returns root of current repo +function git_get_root() { + echo "$(git rev-parse --show-toplevel 2> /dev/null)" +} # Checks if working tree is dirty parse_git_dirty() { @@ -71,7 +75,7 @@ git_prompt_status() { #compare the provided version of git to the version installed and on path #prints 1 if input version <= installed version -#prints -1 otherwise +#prints -1 otherwise function git_compare_version() { local INPUT_GIT_VERSION=$1; local INSTALLED_GIT_VERSION From e13736091cadeaddcecf79cb611caea7992bf35a Mon Sep 17 00:00:00 2001 From: Pavel Puchkin Date: Fri, 22 Jun 2012 21:00:40 +1100 Subject: [PATCH 2/6] Add `toroot` function to fast changing workdir to repo's root If we are not in repo, just call `cd` with no arguments (which in most cases move us to home dir). Maybe will be better to do nothing otherwise? --- plugins/git/git.plugin.zsh | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/plugins/git/git.plugin.zsh b/plugins/git/git.plugin.zsh index d3d3f702a..8b53147f8 100644 --- a/plugins/git/git.plugin.zsh +++ b/plugins/git/git.plugin.zsh @@ -60,6 +60,11 @@ function current_repository() { echo $(git remote -v | cut -d':' -f 2) } +# change workdir to repo's root (or do `cd` with no arguments if not in a repo) +function toroot() { + builtin cd "`git_get_root`" +} + # these aliases take advantage of the previous function alias ggpull='git pull origin $(current_branch)' compdef ggpull=git From e950d0bb2f67856703ad2ee477cc74a2ea5b5fbf Mon Sep 17 00:00:00 2001 From: Pavel Puchkin Date: Fri, 22 Jun 2012 21:23:42 +1100 Subject: [PATCH 3/6] Virtualenvwrapper plugin now use `git_get_root` - it will fix #1180 (since we don't use `readlink`) - unset some variables used only by plugin - `workon_cwd` is private function, so it was prefixed with '_' - indentation --- .../virtualenvwrapper.plugin.zsh | 45 ++++++++++--------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/plugins/virtualenvwrapper/virtualenvwrapper.plugin.zsh b/plugins/virtualenvwrapper/virtualenvwrapper.plugin.zsh index afdad1bea..e7100db9a 100644 --- a/plugins/virtualenvwrapper/virtualenvwrapper.plugin.zsh +++ b/plugins/virtualenvwrapper/virtualenvwrapper.plugin.zsh @@ -8,33 +8,32 @@ for wrapsource in "/usr/local/bin/virtualenvwrapper.sh" "/etc/bash_completion.d/ # Automatically activate Git projects' virtual environments based on the # directory name of the project. Virtual environment name can be overridden # by placing a .venv file in the project root with a virtualenv name in it - function workon_cwd { - # Check that this is a Git repo - GIT_DIR=`git rev-parse --git-dir 2> /dev/null` - if (( $? == 0 )); then - # Find the repo root and check for virtualenv name override - GIT_DIR=`readlink -f $GIT_DIR` - PROJECT_ROOT=`dirname "$GIT_DIR"` - ENV_NAME=`basename "$PROJECT_ROOT"` - if [[ -f "$PROJECT_ROOT/.venv" ]]; then - ENV_NAME=`cat "$PROJECT_ROOT/.venv"` - fi - # Activate the environment only if it is not already active - if [[ "$VIRTUAL_ENV" != "$WORKON_HOME/$ENV_NAME" ]]; then - if [[ -e "$WORKON_HOME/$ENV_NAME/bin/activate" ]]; then - workon "$ENV_NAME" && export CD_VIRTUAL_ENV="$ENV_NAME" - fi - fi - elif [ $CD_VIRTUAL_ENV ]; then - # We've just left the repo, deactivate the environment - # Note: this only happens if the virtualenv was activated automatically - deactivate && unset CD_VIRTUAL_ENV + function _workon_cwd { + # Check that this is a Git repo and get its root + REPO_ROOT=`git_get_root` + if [[ -n "$REPO_ROOT" ]]; then + ENV_NAME=`basename "$REPO_ROOT"` + if [[ -f "$REPO_ROOT/.venv" ]]; then + ENV_NAME=`cat "$REPO_ROOT/.venv"` fi + # Activate the environment only if it is not already active + if [[ "$VIRTUAL_ENV" != "$WORKON_HOME/$ENV_NAME" ]]; then + if [[ -e "$WORKON_HOME/$ENV_NAME/bin/activate" ]]; then + workon "$ENV_NAME" && export CD_VIRTUAL_ENV="$ENV_NAME" + fi + fi + elif [ $CD_VIRTUAL_ENV ]; then + # We've just left the repo, deactivate the environment + # Note: this only happens if the virtualenv was activated automatically + deactivate && unset CD_VIRTUAL_ENV + fi + unset REPO_ROOT + unset ENV_NAME } # New cd function that does the virtualenv magic function cd { - builtin cd "$@" && workon_cwd + builtin cd "$@" && _workon_cwd } fi @@ -45,3 +44,5 @@ done if [ $WRAPPER_FOUND -eq 0 ] ; then print "zsh virtualenvwrapper plugin: Couldn't activate virtualenvwrapper. Please run \`pip install virtualenvwrapper\`." fi + +unset WRAPPER_FOUND From 1684a2319a9d4312e711c044263dee15f9860254 Mon Sep 17 00:00:00 2001 From: Pavel Puchkin Date: Fri, 22 Jun 2012 23:32:29 +1100 Subject: [PATCH 4/6] Bugfix - plugin wont deactivate venv in some cases ...when changing directory directly from git repo to another repo, which have not associated virtualenv --- .../virtualenvwrapper/virtualenvwrapper.plugin.zsh | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/plugins/virtualenvwrapper/virtualenvwrapper.plugin.zsh b/plugins/virtualenvwrapper/virtualenvwrapper.plugin.zsh index e7100db9a..1b214fb5f 100644 --- a/plugins/virtualenvwrapper/virtualenvwrapper.plugin.zsh +++ b/plugins/virtualenvwrapper/virtualenvwrapper.plugin.zsh @@ -20,17 +20,25 @@ for wrapsource in "/usr/local/bin/virtualenvwrapper.sh" "/etc/bash_completion.d/ if [[ "$VIRTUAL_ENV" != "$WORKON_HOME/$ENV_NAME" ]]; then if [[ -e "$WORKON_HOME/$ENV_NAME/bin/activate" ]]; then workon "$ENV_NAME" && export CD_VIRTUAL_ENV="$ENV_NAME" + else + _deactivate fi fi - elif [ $CD_VIRTUAL_ENV ]; then + else # We've just left the repo, deactivate the environment # Note: this only happens if the virtualenv was activated automatically - deactivate && unset CD_VIRTUAL_ENV + _deactivate fi unset REPO_ROOT unset ENV_NAME } + function _deactivate() { + if [[ -n $CD_VIRTUAL_ENV ]]; then + deactivate && unset CD_VIRTUAL_ENV + fi + } + # New cd function that does the virtualenv magic function cd { builtin cd "$@" && _workon_cwd From 6347656b6fef9d592232dcb0019be42b14f052a0 Mon Sep 17 00:00:00 2001 From: Pavel Puchkin Date: Sun, 24 Jun 2012 10:47:31 +1100 Subject: [PATCH 5/6] Some fixes about local variables --- .../virtualenvwrapper.plugin.zsh | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/plugins/virtualenvwrapper/virtualenvwrapper.plugin.zsh b/plugins/virtualenvwrapper/virtualenvwrapper.plugin.zsh index 1b214fb5f..81072a8ac 100644 --- a/plugins/virtualenvwrapper/virtualenvwrapper.plugin.zsh +++ b/plugins/virtualenvwrapper/virtualenvwrapper.plugin.zsh @@ -10,16 +10,16 @@ for wrapsource in "/usr/local/bin/virtualenvwrapper.sh" "/etc/bash_completion.d/ # by placing a .venv file in the project root with a virtualenv name in it function _workon_cwd { # Check that this is a Git repo and get its root - REPO_ROOT=`git_get_root` - if [[ -n "$REPO_ROOT" ]]; then - ENV_NAME=`basename "$REPO_ROOT"` - if [[ -f "$REPO_ROOT/.venv" ]]; then - ENV_NAME=`cat "$REPO_ROOT/.venv"` + local repo_root=`git_get_root` + if [[ -n "$repo_root" ]]; then + local env_name=`basename "$repo_root"` + if [[ -f "$repo_root/.venv" ]]; then + env_name=`cat "$repo_root/.venv"` fi # Activate the environment only if it is not already active - if [[ "$VIRTUAL_ENV" != "$WORKON_HOME/$ENV_NAME" ]]; then - if [[ -e "$WORKON_HOME/$ENV_NAME/bin/activate" ]]; then - workon "$ENV_NAME" && export CD_VIRTUAL_ENV="$ENV_NAME" + if [[ "$VIRTUAL_ENV" != "$WORKON_HOME/$env_name" ]]; then + if [[ -e "$WORKON_HOME/$env_name/bin/activate" ]]; then + workon "$env_name" && export CD_VIRTUAL_ENV="$env_name" else _deactivate fi @@ -29,8 +29,6 @@ for wrapsource in "/usr/local/bin/virtualenvwrapper.sh" "/etc/bash_completion.d/ # Note: this only happens if the virtualenv was activated automatically _deactivate fi - unset REPO_ROOT - unset ENV_NAME } function _deactivate() { From ac8e14f3edb8df95b1f0bc187876e6f66c87bfe7 Mon Sep 17 00:00:00 2001 From: Pavel Puchkin Date: Thu, 19 Jul 2012 12:47:54 +1100 Subject: [PATCH 6/6] Do not override "cd" function, use "chpwd" hook instead This allow the plugin work even w/o using "cd", just by typing directory's name and hitting Enter --- plugins/virtualenvwrapper/virtualenvwrapper.plugin.zsh | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/plugins/virtualenvwrapper/virtualenvwrapper.plugin.zsh b/plugins/virtualenvwrapper/virtualenvwrapper.plugin.zsh index 3117bd189..bb3f05822 100644 --- a/plugins/virtualenvwrapper/virtualenvwrapper.plugin.zsh +++ b/plugins/virtualenvwrapper/virtualenvwrapper.plugin.zsh @@ -37,10 +37,8 @@ for wrapsource in "/usr/bin/virtualenvwrapper.sh" "/usr/local/bin/virtualenvwrap fi } - # New cd function that does the virtualenv magic - function cd { - builtin cd "$@" && _workon_cwd - } + # Add hook to chpwd function + add-zsh-hook chpwd _workon_cwd fi break