| 1 | #!/usr/bin/env bash |
| 2 | # Usage: pyenv version-file [<dir>] |
| 3 | # Summary: Detect the file that sets the current pyenv version |
| 4 | set -e |
| 5 | [ -n "$PYENV_DEBUG" ] && set -x |
| 6 | |
| 7 | target_dir="$1" |
| 8 | |
| 9 | find_local_version_file() { |
| 10 | local root="$1" |
| 11 | while ! [[ "$root" =~ ^//[^/]*$ ]]; do |
| 12 | if [ -f "${root}/.python-version" ]; then |
| 13 | echo "${root}/.python-version" |
| 14 | return 0 |
| 15 | fi |
| 16 | [ -n "$root" ] || break |
| 17 | root="${root%/*}" |
| 18 | done |
| 19 | return 1 |
| 20 | } |
| 21 | |
| 22 | if [ -n "$target_dir" ]; then |
| 23 | find_local_version_file "$target_dir" |
| 24 | else |
| 25 | find_local_version_file "$PYENV_DIR" || { |
| 26 | [ "$PYENV_DIR" != "$PWD" ] && find_local_version_file "$PWD" |
| 27 | } || echo "${PYENV_ROOT}/version" |
| 28 | fi |