diff --git a/libexec/rbenv-versions b/libexec/rbenv-versions index d20b4556..28c3cdc3 100755 --- a/libexec/rbenv-versions +++ b/libexec/rbenv-versions @@ -86,21 +86,36 @@ print_version() { num_versions=$((num_versions + 1)) } +sort_versions() { + sed 'h; s/[+-]/./g; s/.p\([[:digit:]]\)/.z.\1/; s/$/.z/; G; s/\n/ /' | \ + LC_ALL=C sort -t. -k 1,1 -k 2,2n -k 3,3n -k 4,4n -k 5,5n | awk '{print $2}' +} + # Include "system" in the non-bare output, if it exists if [ -n "$include_system" ] && RBENV_VERSION=system rbenv-which ruby >/dev/null 2>&1; then print_version system fi shopt -s nullglob + +versions=($( for path in "$versions_dir"/*; do if [ -d "$path" ]; then if [ -n "$skip_aliases" ] && [ -L "$path" ]; then target="$(realpath "$path")" [ "${target%/*}" != "$versions_dir" ] || continue fi - print_version "${path##*/}" + echo "${path##*/}" fi done +)) + +sorted_versions=($(printf "%s\n" ${versions[@]} | sort_versions)) + +for version in ${sorted_versions[@]}; do + print_version $version +done + shopt -u nullglob if [ "$num_versions" -eq 0 ] && [ -n "$include_system" ]; then diff --git a/test/versions.bats b/test/versions.bats index 2c88ee2f..71d98871 100644 --- a/test/versions.bats +++ b/test/versions.bats @@ -57,14 +57,16 @@ OUT stub_system_ruby create_version "1.8.7" create_version "1.9.3" - create_version "2.0.0" + create_version "2.2.10" + create_version "2.2.3" run rbenv-versions assert_success assert_output <