🐛 fix(fish): autoenv now searches on the current directory even if is not a repo
This commit is contained in:
parent
0f9543225e
commit
eb93918961
1 changed files with 18 additions and 15 deletions
|
@ -3,28 +3,31 @@
|
||||||
# * Instead of overriding cd, we detect directory change. This allows the script to work
|
# * Instead of overriding cd, we detect directory change. This allows the script to work
|
||||||
# for other means of cd, such as z.
|
# for other means of cd, such as z.
|
||||||
# * Update syntax to work with new versions of fish.
|
# * Update syntax to work with new versions of fish.
|
||||||
# * Handle virtualenvs that are not located in the root of a git directory.
|
|
||||||
|
|
||||||
function __auto_source_venv --on-variable PWD --description "Activate/Deactivate virtualenv on directory change"
|
function __auto_source_venv --on-variable PWD --description "Activate/Deactivate virtualenv on directory change"
|
||||||
status --is-command-substitution; and return
|
status --is-command-substitution; and return
|
||||||
|
|
||||||
# Check if we are inside a git directory
|
# Searched directories are the current directory, and the root of the current git repo if applicable
|
||||||
|
set dirs (pwd)
|
||||||
if git rev-parse --show-toplevel &>/dev/null
|
if git rev-parse --show-toplevel &>/dev/null
|
||||||
set gitdir (realpath (git rev-parse --show-toplevel))
|
set -a dirs (realpath (git rev-parse --show-toplevel))
|
||||||
set cwd (pwd -P)
|
end
|
||||||
# While we are still inside the git directory, find the closest
|
|
||||||
# virtualenv starting from the current directory.
|
# Scan directories for a fish-compatible virtual environment
|
||||||
while string match "$gitdir*" "$cwd" &>/dev/null
|
set VENV_DIR_NAMES env .env venv .venv
|
||||||
if test -e "$cwd/.venv/bin/activate.fish"
|
for venv_dir in $dirs/$VENV_DIR_NAMES
|
||||||
source "$cwd/.venv/bin/activate.fish" &>/dev/null
|
if test -e "$venv_dir/bin/activate.fish"
|
||||||
return
|
break
|
||||||
else
|
|
||||||
set cwd (path dirname "$cwd")
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
# If virtualenv activated but we are not in a git directory, deactivate.
|
|
||||||
if test -n "$VIRTUAL_ENV"
|
# Activate venv if it was found and not activated before
|
||||||
|
if test "$VIRTUAL_ENV" != "$venv_dir" -a -e "$venv_dir/bin/activate.fish"
|
||||||
|
source $venv_dir/bin/activate.fish
|
||||||
|
# Deactivate venv if it is activated but the directory doesn't exist
|
||||||
|
else if not test -z "$VIRTUAL_ENV" -o -e "$venv_dir"
|
||||||
deactivate
|
deactivate
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
__auto_source_venv
|
||||||
|
|
Loading…
Add table
Reference in a new issue