-
-
Notifications
You must be signed in to change notification settings - Fork 430
Cache hook state to skip redundant pyenv sh-activate calls #523
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,13 @@ | |
| set -e | ||
| [ -n "$PYENV_DEBUG" ] && set -x | ||
|
|
||
| # Detect stat format for mtime: GNU uses -c %Y, BSD uses -f %m | ||
| if stat -c %Y / >/dev/null 2>&1; then | ||
| _stat_fmt="-c %Y" | ||
| else | ||
| _stat_fmt="-f %m" | ||
| fi | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Quite an elegant solution! The comment is very on point, too! (conveys information required for the task that is not in the code itself) |
||
| resolve_link() { | ||
| $(type -p greadlink readlink | head -1) "$1" | ||
| } | ||
|
|
@@ -106,11 +113,35 @@ fish ) | |
| cat <<EOS | ||
| function _pyenv_virtualenv_hook --on-event fish_prompt; | ||
| set -l ret \$status | ||
| if test -n "\$PYENV_VERSION" \\ | ||
| -a "\$PYENV_VERSION" = "\$_PYENV_VH_VERSION" \\ | ||
| -a "\$VIRTUAL_ENV" = "\$_PYENV_VH_VENV" | ||
| return \$ret | ||
| end | ||
| set -l d "\$PWD" | ||
| set -l paths | ||
| while true | ||
| set paths \$paths "\$d" "\$d/.python-version" | ||
| test "\$d" = "/"; and break | ||
| set d (string replace -r '/[^/]*\$' '' -- "\$d") | ||
| test -z "\$d"; and set d "/" | ||
| end | ||
| set paths \$paths "\$PYENV_ROOT/version" | ||
| if test "\$PWD" = "\$_PYENV_VH_PWD" \\ | ||
| -a "\$PYENV_VERSION" = "\$_PYENV_VH_VERSION" \\ | ||
| -a "\$VIRTUAL_ENV" = "\$_PYENV_VH_VENV" \\ | ||
| -a "(stat ${_stat_fmt} \$paths 2>/dev/null)" = "\$_PYENV_VH_MTIMES" | ||
| return \$ret | ||
| end | ||
| if [ -n "\$VIRTUAL_ENV" ] | ||
| pyenv activate --quiet; or pyenv deactivate --quiet; or true | ||
| else | ||
| pyenv activate --quiet; or true | ||
| end | ||
| set -g _PYENV_VH_PWD "\$PWD" | ||
| set -g _PYENV_VH_VERSION "\$PYENV_VERSION" | ||
| set -g _PYENV_VH_VENV "\$VIRTUAL_ENV" | ||
| set -g _PYENV_VH_MTIMES (stat ${_stat_fmt} \$paths 2>/dev/null) | ||
| return \$ret | ||
| end | ||
| EOS | ||
|
|
@@ -130,11 +161,34 @@ esac | |
| if [[ "$shell" != "fish" ]]; then | ||
| cat <<EOS | ||
| local ret=\$? | ||
| if [ -n "\${PYENV_VERSION-}" ] \\ | ||
| && [ "\${PYENV_VERSION-}" = "\${_PYENV_VH_VERSION-}" ] \\ | ||
| && [ "\${VIRTUAL_ENV-}" = "\${_PYENV_VH_VENV-}" ]; then | ||
| return \$ret | ||
| fi | ||
| local _pvh_d="\${PWD}" _pvh_paths="" | ||
| while :; do | ||
| _pvh_paths="\${_pvh_paths} \${_pvh_d} \${_pvh_d}/.python-version" | ||
| [ "\${_pvh_d}" = "/" ] && break | ||
| _pvh_d="\${_pvh_d%/*}" | ||
| [ -z "\${_pvh_d}" ] && _pvh_d="/" | ||
|
Comment on lines
+171
to
+174
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to construct Besides, it's also sufficient to:
This will |
||
| done | ||
| _pvh_paths="\${_pvh_paths} \${PYENV_ROOT}/version" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Like the above, the global file only needs checking if no local version is active. Same possible caveats apply. |
||
| if [ "\${PWD}" = "\${_PYENV_VH_PWD-}" ] \\ | ||
| && [ "\${PYENV_VERSION-}" = "\${_PYENV_VH_VERSION-}" ] \\ | ||
| && [ "\${VIRTUAL_ENV-}" = "\${_PYENV_VH_VENV-}" ] \\ | ||
|
Comment on lines
+178
to
+179
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We already checked |
||
| && [ "\$(stat ${_stat_fmt} \${_pvh_paths} 2>/dev/null)" = "\${_PYENV_VH_MTIMES-}" ]; then | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| return \$ret | ||
| fi | ||
| if [ -n "\${VIRTUAL_ENV-}" ]; then | ||
| eval "\$(pyenv sh-activate --quiet || pyenv sh-deactivate --quiet || true)" || true | ||
| else | ||
| eval "\$(pyenv sh-activate --quiet || true)" || true | ||
| fi | ||
| _PYENV_VH_PWD="\${PWD}" | ||
| _PYENV_VH_VERSION="\${PYENV_VERSION-}" | ||
| _PYENV_VH_VENV="\${VIRTUAL_ENV-}" | ||
| _PYENV_VH_MTIMES="\$(stat ${_stat_fmt} \${_pvh_paths} 2>/dev/null)" | ||
| return \$ret | ||
| }; | ||
| EOS | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As my analysis in the summary shows, we'll have to allow for
pyenv version-namehooks in the cache check.I reckon it's reasonable to check for hooks presence only once during
initand thus require the user to restart their sessions if they make drastic changes to their Pyenv installation like installing a new plugin.