2019-04-23 10:23:33 -04:00
|
|
|
#!/usr/bin/env bash
|
2016-03-02 01:39:52 +00:00
|
|
|
# Usage: pyenv version-file [<dir>]
|
2013-01-18 17:41:41 +09:00
|
|
|
# Summary: Detect the file that sets the current pyenv version
|
2012-08-31 15:23:41 +09:00
|
|
|
set -e
|
|
|
|
|
[ -n "$PYENV_DEBUG" ] && set -x
|
|
|
|
|
|
2016-03-02 01:39:52 +00:00
|
|
|
target_dir="$1"
|
|
|
|
|
|
2013-01-18 17:55:46 +09:00
|
|
|
find_local_version_file() {
|
2014-01-06 16:37:21 +09:00
|
|
|
local root="$1"
|
2026-08-08 20:35:35 +03:00
|
|
|
# Nonexistent paths are UB as of this writing.
|
|
|
|
|
# Relative ones cause a failure but absolute ones don't
|
|
|
|
|
[[ $root != /* ]] && root=$(CDPATH= cd -- "$root" && pwd)
|
2026-08-16 18:51:28 -04:00
|
|
|
# Original Rbenv code supports UNC notation for Cygwin/MinGW
|
2026-08-10 12:55:19 +03:00
|
|
|
# (https://github.com/rbenv/rbenv/pull/529)
|
|
|
|
|
# POSIX.1-2024 still allows to treat //<name> in implementation-specific manner
|
|
|
|
|
# (https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/V1_chap04.html#tag_04_16)
|
|
|
|
|
# even though few UNIX variants do that
|
|
|
|
|
local unc; [[ $root =~ ^//[^/] && ! / -ef // ]] && unc=1
|
2026-08-10 12:56:57 +03:00
|
|
|
root="${root%/}"
|
2026-08-10 12:55:19 +03:00
|
|
|
# when testing root, $root is ""
|
|
|
|
|
while true; do
|
|
|
|
|
# don't test //.python-version if // is special
|
|
|
|
|
# as it's pointless and possibly very slow
|
|
|
|
|
# if it e.g. leads to a network search
|
|
|
|
|
[[ $unc && $root == / ]] && break
|
|
|
|
|
|
|
|
|
|
if [[ -f $root/.python-version ]]; then
|
|
|
|
|
echo "$root/.python-version"
|
2016-03-02 01:39:52 +00:00
|
|
|
return 0
|
2013-01-18 17:55:46 +09:00
|
|
|
fi
|
2026-08-10 12:55:19 +03:00
|
|
|
[[ -n $root ]] || break
|
2013-01-18 17:55:46 +09:00
|
|
|
root="${root%/*}"
|
|
|
|
|
done
|
2016-03-02 01:39:52 +00:00
|
|
|
return 1
|
2013-01-18 17:55:46 +09:00
|
|
|
}
|
|
|
|
|
|
2016-03-02 01:39:52 +00:00
|
|
|
if [ -n "$target_dir" ]; then
|
|
|
|
|
find_local_version_file "$target_dir"
|
2012-08-31 15:23:41 +09:00
|
|
|
else
|
2026-08-25 22:43:59 +03:00
|
|
|
# In tests, PYENV_DIR is not set, ultimately leading to "cd: null directory" in OpenSUSE
|
2026-08-29 08:01:30 -04:00
|
|
|
# While most `cd` implementations treat an empty argument the same as missing, that's still wrong
|
2026-08-25 22:43:59 +03:00
|
|
|
find_local_version_file "${PYENV_DIR:=$PWD}" || {
|
2016-03-02 01:39:52 +00:00
|
|
|
[ "$PYENV_DIR" != "$PWD" ] && find_local_version_file "$PWD"
|
|
|
|
|
} || echo "${PYENV_ROOT}/version"
|
2012-08-31 15:23:41 +09:00
|
|
|
fi
|