Sha256: 776fb66635a3ee0d486c57bf67e5e1883c8dfbd09e4580815b89349bee5f9955

Contents?: true

Size: 1.09 KB

Versions: 14

Compression:

Stored size: 1.09 KB

Contents

require "charlock_holmes"
require "grit_ext/actor"
require "grit_ext/blob"
require "grit_ext/commit"
require "grit_ext/tree"
require "grit_ext/diff"
require "grit_ext/version"

module GritExt
  extend self

  def encode!(message)
    return nil unless message.respond_to? :force_encoding

    # if message is utf-8 encoding, just return it
    message.force_encoding("UTF-8")
    return message if message.valid_encoding?

    # return message if message type is binary
    detect = CharlockHolmes::EncodingDetector.detect(message)
    return message.force_encoding("BINARY") if detect && detect[:type] == :binary

    # encoding message to detect encoding
    if detect && detect[:encoding]
      message.force_encoding(detect[:encoding])
    end

    # encode and clean the bad chars
    message.replace clean(message)
  rescue
    encoding = detect ? detect[:encoding] : "unknown"
    "--broken encoding: #{encoding}"
  end

  def clean(message)
    message.encode("UTF-16BE", :undef => :replace, :invalid => :replace, :replace => "")
           .encode("UTF-8")
           .gsub("\0".encode("UTF-8"), "")
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
gitlab-grit-2.8.3 lib/grit_ext.rb
gitlab-grit-2.8.2 lib/grit_ext.rb
gitlab-grit-2.8.1 lib/grit_ext.rb
gitlab-grit-2.8.0 lib/grit_ext.rb
gitlab-grit-2.7.3 lib/grit_ext.rb
gitlab-grit-2.6.12 lib/grit_ext.rb
gitlab-grit-2.7.2 lib/grit_ext.rb
gitlab-grit-2.6.11 lib/grit_ext.rb
gitlab-grit-2.7.1 lib/grit_ext.rb
gitlab-grit-2.6.10 lib/grit_ext.rb
gitlab-grit-2.7.0 lib/grit_ext.rb
gitlab-grit-2.6.9 lib/grit_ext.rb
gitlab-grit-2.6.8 lib/grit_ext.rb
gitlab-grit-2.6.7 lib/grit_ext.rb