File: rbenv-version-file

package info (click to toggle)
rbenv 1.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 400 kB
  • sloc: sh: 1,259; ruby: 40; makefile: 15
file content (34 lines) | stat: -rwxr-xr-x 889 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/env bash
# Usage: rbenv version-file [<dir>]
# Summary: Detect the file that sets the current rbenv version
#
# Detects and prints the location of a `.ruby-version` file that sets the
# version for the current working directory. If no file found, this prints
# the location of the global version file, even if that file does
# not exist.

set -e
[ -n "$RBENV_DEBUG" ] && set -x

target_dir="$1"

find_local_version_file() {
  local root="$1"
  while ! [[ "$root" =~ ^//[^/]*$ ]]; do
    if [ -s "${root}/.ruby-version" ]; then
      echo "${root}/.ruby-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 "$RBENV_DIR" || {
    [ "$RBENV_DIR" != "$PWD" ] && find_local_version_file "$PWD"
  } || echo "${RBENV_ROOT}/version"
fi