From 3250117918f4d0a8426be12557378033b726777a Mon Sep 17 00:00:00 2001 From: Shannon Skipper Date: Sat, 6 Sep 2014 14:45:32 -0700 Subject: [PATCH 1/2] Sort Ruby versions lexicographically. First normalize each Ruby (by converting dashes and plusses to periods) followed by a tab then the original Ruby version: ```shell sed -e "h" -e 's/[+-]/./g' -e "G" -e 's/\n/ /' ``` For example, `jruby-9000+graal-dev` becomes `jruby.9000.graal.dev\tjruby-9000+graal-dev`. Then dictionary sort the first column and numerically sort the next four columns: ```shell sort -t "." -k "1,1" -k "2,2n" -k "3,3n" -k "4,4n" -k "5,5n" ``` For most non-MRI Rubies, this results in the Ruby name being dictionary sorted and the MAJOR, MINOR, TINY, plus one more column being numerically sorted. Additional numeric sort columns could be added but aren't needed at present. For all existing ruby-build Ruby versions this sort order is correct. Once lexicographically sorted, cut to the original Ruby versions column with a `cut -f 2`. Closes #512. One caveat is that with ruby-build's special case dropping the CRuby `ruby-` prefix (see #543), a future MRI version 10.0+ could miss-sorted. I'm assuming 1.X will be long dead before a 10.0+ comes around but now we've had a JRuby 9000 so who knows. See chruby [#278](https://github.com/postmodern/chruby/pull/278) and [#277](https://github.com/postmodern/chruby/pull/277) for more discussion of related numeric sorting of Ruby versions with sed. --- bin/ruby-build | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/bin/ruby-build b/bin/ruby-build index 3bc6cb2fc7..ce9ffddda0 100755 --- a/bin/ruby-build +++ b/bin/ruby-build @@ -870,7 +870,9 @@ list_definitions() { { for definition in "${RUBY_BUILD_ROOT}/share/ruby-build/"*; do echo "${definition##*/}" done - } | sort + } | sed -e "h" -e 's/[+-]/./g' -e "G" -e 's/\n/ /' | + sort -t "." -k "1,1" -k "2,2n" -k "3,3n" -k "4,4n" -k "5,5n" | + cut -f 2 } From b4691e36db9971db568a8ea5eeaa1c9b514fce90 Mon Sep 17 00:00:00 2001 From: Shannon Skipper Date: Sat, 6 Sep 2014 20:39:02 -0700 Subject: [PATCH 2/2] Limit explicit sorting to four columns. What I thought was an abundance of caution with the extra sort column ended up causing the four-digit JRuby versions to be missorted. The fall-through sorting appears to sort properly for columns beyond the four that are explicitly sorted. --- bin/ruby-build | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/ruby-build b/bin/ruby-build index ce9ffddda0..5a0cb9f62d 100755 --- a/bin/ruby-build +++ b/bin/ruby-build @@ -871,7 +871,7 @@ list_definitions() { echo "${definition##*/}" done } | sed -e "h" -e 's/[+-]/./g' -e "G" -e 's/\n/ /' | - sort -t "." -k "1,1" -k "2,2n" -k "3,3n" -k "4,4n" -k "5,5n" | + LC_ALL=C sort -t "." -k "1,1" -k "2,2n" -k "3,3n" -k "4,4n" | cut -f 2 }