mirror of
https://github.com/pyenv/pyenv.git
synced 2026-09-11 08:06:16 -04:00
find_local_version_file reduces its target with ${root%/*}, but that leaves a slash-less relative argument such as "." unchanged, leading to an infinite loop
Signed-off-by: 付典 <fudianchn@gmail.com>
32 lines
829 B
Bash
Executable file
32 lines
829 B
Bash
Executable file
#!/usr/bin/env bash
|
|
# Usage: pyenv version-file [<dir>]
|
|
# Summary: Detect the file that sets the current pyenv version
|
|
set -e
|
|
[ -n "$PYENV_DEBUG" ] && set -x
|
|
|
|
target_dir="$1"
|
|
|
|
find_local_version_file() {
|
|
local root="$1"
|
|
# Nonexistent paths are UB as of this writing.
|
|
# Relative ones cause a failure but absolute ones don't
|
|
[[ $root != /* ]] && root=$(CDPATH= cd -- "$root" && pwd)
|
|
while ! [[ "$root" =~ ^//[^/]*$ ]]; do
|
|
if [ -f "${root}/.python-version" ]; then
|
|
echo "${root}/.python-version"
|
|
return 0
|
|
fi
|
|
[ -n "$root" ] || break
|
|
root="${root%/*}"
|
|
done
|
|
return 1
|
|
}
|
|
|
|
if [ -n "$target_dir" ]; then
|
|
find_local_version_file "$target_dir"
|
|
else
|
|
find_local_version_file "$PYENV_DIR" || {
|
|
[ "$PYENV_DIR" != "$PWD" ] && find_local_version_file "$PWD"
|
|
} || echo "${PYENV_ROOT}/version"
|
|
fi
|