Module: EncodingHelper
- Extended by:
- EncodingHelper
- Included in:
- EncodingHelper, Gitlab::Git::Blame, Gitlab::Git::Blob, Gitlab::Git::Commit, Gitlab::Git::Diff, Gitlab::Git::Ref, Gitlab::Git::Tree
- Defined in:
- lib/gitlab_git/encoding_helper.rb
Constant Summary collapse
- ENCODING_CONFIDENCE_THRESHOLD =
This threshold is carefully tweaked to prevent usage of encodings detected by CharlockHolmes with low confidence. If CharlockHolmes confidence is low, we’re better off sticking with utf8 encoding. Reason: git diff can return strings with invalid utf8 byte sequences if it truncates a diff in the middle of a multibyte character. In this case CharlockHolmes will try to guess the encoding and will likely suggest an obscure encoding with low confidence. There is a lot more info with this merge request: gitlab.com/gitlab-org/gitlab_git/merge_requests/77#note_4754193
40
Instance Method Summary collapse
Instance Method Details
#encode!(message) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/gitlab_git/encoding_helper.rb', line 15 def encode!() return nil unless .respond_to? :force_encoding # if message is utf-8 encoding, just return it .force_encoding("UTF-8") return if .valid_encoding? # return message if message type is binary detect = CharlockHolmes::EncodingDetector.detect() return .force_encoding("BINARY") if detect && detect[:type] == :binary # force detected encoding if we have sufficient confidence. if detect && detect[:encoding] && detect[:confidence] > ENCODING_CONFIDENCE_THRESHOLD .force_encoding(detect[:encoding]) end # encode and clean the bad chars .replace clean() rescue encoding = detect ? detect[:encoding] : "unknown" "--broken encoding: #{encoding}" end |
#encode_utf8(message) ⇒ Object
38 39 40 41 42 43 44 45 |
# File 'lib/gitlab_git/encoding_helper.rb', line 38 def encode_utf8() detect = CharlockHolmes::EncodingDetector.detect() if detect CharlockHolmes::Converter.convert(, detect[:encoding], 'UTF-8') else clean() end end |