diff --git a/Library/Homebrew/brew.rb b/Library/Homebrew/brew.rb index 2deba9a510..15c1ee4ffb 100644 --- a/Library/Homebrew/brew.rb +++ b/Library/Homebrew/brew.rb @@ -1,5 +1,9 @@ std_trap = trap("INT") { exit! 130 } # no backtrace thanks +# check ruby version before requiring any modules. +RUBY_TWO = RUBY_VERSION.split(".").first.to_i >= 2 +raise "Homebrew must be run under Ruby 2!" unless RUBY_TWO + require "pathname" HOMEBREW_LIBRARY_PATH = Pathname.new(__FILE__).realpath.parent $:.unshift(HOMEBREW_LIBRARY_PATH.to_s) diff --git a/Library/Homebrew/dev-cmd/test-bot.rb b/Library/Homebrew/dev-cmd/test-bot.rb index 509780869d..f2ca5bbf55 100644 --- a/Library/Homebrew/dev-cmd/test-bot.rb +++ b/Library/Homebrew/dev-cmd/test-bot.rb @@ -49,22 +49,12 @@ module Homebrew HOMEBREW_TAP_REGEX = %r{^([\w-]+)/homebrew-([\w-]+)$} - if ruby_has_encoding? - def fix_encoding!(str) - # Assume we are starting from a "mostly" UTF-8 string - str.force_encoding(Encoding::UTF_8) - return str if str.valid_encoding? - str.encode!(Encoding::UTF_16, :invalid => :replace) - str.encode!(Encoding::UTF_8) - end - elsif require "iconv" - def fix_encoding!(str) - Iconv.conv("UTF-8//IGNORE", "UTF-8", str) - end - else - def fix_encoding!(str) - str - end + def fix_encoding!(str) + # Assume we are starting from a "mostly" UTF-8 string + str.force_encoding(Encoding::UTF_8) + return str if str.valid_encoding? + str.encode!(Encoding::UTF_16, :invalid => :replace) + str.encode!(Encoding::UTF_8) end def resolve_test_tap @@ -181,7 +171,7 @@ module Homebrew verbose = ARGV.verbose? # Step may produce arbitrary output and we read it bytewise, so must # buffer it as binary and convert to UTF-8 once complete - output = ruby_has_encoding? ? "".encode!("BINARY") : "" + output = "".encode!("BINARY") working_dir = Pathname.new(@command.first == "git" ? @repository : Dir.pwd) read, write = IO.pipe @@ -1046,19 +1036,8 @@ module Homebrew def sanitize_output_for_xml(output) unless output.empty? # Remove invalid XML CData characters from step output. - if ruby_has_encoding? - # This is the regex for valid XML chars, but only works in Ruby 2.0+ - # /[\x09\x0A\x0D\x20-\uD7FF\uE000-\uFFFD\u{10000}-\u{10FFFF}]/ - # For 1.9 compatibility, use the inverse of that, which stays under \u10000 - # invalid_xml_pat = /[\x00-\x08\x0B\x0C\x0E-\x1F\uD800-\uDFFF\uFFFE\uFFFF]/ - # But Ruby won't allow you to reference surrogates, so we have: - invalid_xml_pat = /[\x00-\x08\x0B\x0C\x0E-\x1F\uFFFE\uFFFF]/ - output = output.gsub(invalid_xml_pat, "\uFFFD") - else - # Invalid XML chars, as far as single-byte chars go - output = output.delete("\x00\x01\x02\x03\x04\x05\x06\x07\x08\x0b\x0c\x0e\x0f" \ - "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f") - end + invalid_xml_pat = /[^\x09\x0A\x0D\x20-\uD7FF\uE000-\uFFFD\u{10000}-\u{10FFFF}]/ + output = output.gsub(invalid_xml_pat, "\uFFFD") # Truncate to 1MB to avoid hitting CI limits if output.bytesize > MAX_STEP_OUTPUT_SIZE diff --git a/Library/Homebrew/extend/fileutils.rb b/Library/Homebrew/extend/fileutils.rb index 86abcee853..2b5dcfef32 100644 --- a/Library/Homebrew/extend/fileutils.rb +++ b/Library/Homebrew/extend/fileutils.rb @@ -65,8 +65,7 @@ module FileUtils Process.gid end begin - # group_id.to_s makes OS X 10.6.7 (ruby-1.8.7-p174) and earlier happy. - chown(nil, group_id.to_s, tmpdir) + chown(nil, group_id, tmpdir) rescue Errno::EPERM opoo "Failed setting group \"#{Etc.getgrgid(group_id).name}\" on #{tmpdir}" end diff --git a/Library/Homebrew/extend/pathname.rb b/Library/Homebrew/extend/pathname.rb index 2f0ea7e03b..c9cc9d3f3c 100644 --- a/Library/Homebrew/extend/pathname.rb +++ b/Library/Homebrew/extend/pathname.rb @@ -445,39 +445,7 @@ class Pathname end end - # We redefine these private methods in order to add the /o modifier to - # the Regexp literals, which forces string interpolation to happen only - # once instead of each time the method is called. This is fixed in 1.9+. - if RUBY_VERSION <= "1.8.7" - # @private - alias_method :old_chop_basename, :chop_basename - - def chop_basename(path) - base = File.basename(path) - if /\A#{Pathname::SEPARATOR_PAT}?\z/o =~ base - return nil - else - return path[0, path.rindex(base)], base - end - end - private :chop_basename - - # @private - alias_method :old_prepend_prefix, :prepend_prefix - - def prepend_prefix(prefix, relpath) - if relpath.empty? - File.dirname(prefix) - elsif /#{SEPARATOR_PAT}/o =~ prefix - prefix = File.dirname(prefix) - prefix = File.join(prefix, "") if File.basename(prefix + "a") != "a" - prefix + relpath - else - prefix + relpath - end - end - private :prepend_prefix - elsif RUBY_VERSION == "2.0.0" + if RUBY_VERSION == "2.0.0" # https://bugs.ruby-lang.org/issues/9915 prepend Module.new { def inspect diff --git a/Library/Homebrew/global.rb b/Library/Homebrew/global.rb index 95aa568b91..8472eba5cb 100644 --- a/Library/Homebrew/global.rb +++ b/Library/Homebrew/global.rb @@ -22,16 +22,8 @@ require "config" HOMEBREW_REPOSITORY.extend(GitRepositoryExtension) -if RbConfig.respond_to?(:ruby) - RUBY_PATH = Pathname.new(RbConfig.ruby) -else - RUBY_PATH = Pathname.new(RbConfig::CONFIG["bindir"]).join( - RbConfig::CONFIG["ruby_install_name"] + RbConfig::CONFIG["EXEEXT"] - ) -end +RUBY_PATH = Pathname.new(RbConfig.ruby) RUBY_BIN = RUBY_PATH.dirname -RUBY_TWO = RUBY_VERSION.split(".").first.to_i >= 2 -raise "Homebrew must be run under Ruby 2!" unless RUBY_TWO HOMEBREW_USER_AGENT_CURL = ENV["HOMEBREW_USER_AGENT_CURL"] HOMEBREW_USER_AGENT_RUBY = "#{ENV["HOMEBREW_USER_AGENT"]} ruby/#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}" diff --git a/Library/Homebrew/utils.rb b/Library/Homebrew/utils.rb index 90469bca92..0a1b5158d5 100644 --- a/Library/Homebrew/utils.rb +++ b/Library/Homebrew/utils.rb @@ -561,11 +561,6 @@ def number_readable(number) numstr end -# True if this version of Ruby supports text encodings in its strings -def ruby_has_encoding? - String.method_defined?(:force_encoding) -end - # Truncates a text string to fit within a byte size constraint, # preserving character encoding validity. The returned string will # be not much longer than the specified max_bytes, though the exact @@ -579,13 +574,8 @@ def truncate_text_to_approximate_size(s, max_bytes, options = {}) glue = "\n[...snip...]\n" max_bytes_in = [max_bytes - glue.bytesize, 1].max - if ruby_has_encoding? - bytes = s.dup.force_encoding("BINARY") - glue_bytes = glue.encode("BINARY") - else - bytes = s - glue_bytes = glue - end + bytes = s.dup.force_encoding("BINARY") + glue_bytes = glue.encode("BINARY") n_front_bytes = (max_bytes_in * front_weight).floor n_back_bytes = max_bytes_in - n_front_bytes if n_front_bytes == 0 @@ -599,10 +589,8 @@ def truncate_text_to_approximate_size(s, max_bytes, options = {}) back = bytes[-n_back_bytes..-1] end out = front + glue_bytes + back - if ruby_has_encoding? - out.force_encoding("UTF-8") - out.encode!("UTF-16", :invalid => :replace) - out.encode!("UTF-8") - end + out.force_encoding("UTF-8") + out.encode!("UTF-16", :invalid => :replace) + out.encode!("UTF-8") out end