Commit 80ddd2c09d9017f49976436982e2b9be5084fc4c

Authored by Dmitriy Zaporozhets
1 parent 3706a974

Better encoding handling. Updated grit

@@ -14,7 +14,7 @@ GIT @@ -14,7 +14,7 @@ GIT
14 14
15 GIT 15 GIT
16 remote: https://github.com/gitlabhq/grit.git 16 remote: https://github.com/gitlabhq/grit.git
17 - revision: ff015074ef35bd94cba943f9c0f98e161ab5851c 17 + revision: 3fc864f3c637e06e2fa7a81f6b48a5df58a9bc5b
18 specs: 18 specs:
19 grit (2.4.1) 19 grit (2.4.1)
20 diff-lcs (~> 1.1) 20 diff-lcs (~> 1.1)
@@ -148,7 +148,7 @@ GEM @@ -148,7 +148,7 @@ GEM
148 mime-types (~> 1.16) 148 mime-types (~> 1.16)
149 treetop (~> 1.4.8) 149 treetop (~> 1.4.8)
150 method_source (0.7.0) 150 method_source (0.7.0)
151 - mime-types (1.17.2) 151 + mime-types (1.18)
152 modularity (0.6.1) 152 modularity (0.6.1)
153 multi_json (1.0.4) 153 multi_json (1.0.4)
154 multi_xml (0.4.1) 154 multi_xml (0.4.1)
app/controllers/commits_controller.rb
  1 +require 'benchmark'
1 require "base64" 2 require "base64"
2 3
3 class CommitsController < ApplicationController 4 class CommitsController < ApplicationController
config/initializers/gitlabhq/20_grit_ext.rb
@@ -17,13 +17,35 @@ Grit::GitRuby::Internal::RawObject.class_eval do @@ -17,13 +17,35 @@ Grit::GitRuby::Internal::RawObject.class_eval do
17 end 17 end
18 18
19 private 19 private
  20 +
20 def transcoding(content) 21 def transcoding(content)
21 content ||= "" 22 content ||= ""
22 - detection = CharlockHolmes::EncodingDetector.detect(content)  
23 - if hash = detection  
24 - content = CharlockHolmes::Converter.convert(content, hash[:encoding], 'UTF-8') if hash[:encoding] 23 + hash = CharlockHolmes::EncodingDetector.detect(content)
  24 +
  25 + if hash
  26 + return content if hash[:type] == :binary
  27 +
  28 + if hash[:encoding] == "UTF-8"
  29 + content = if hash[:confidence] < 100
  30 + content
  31 + else
  32 + content.force_encoding("UTF-8")
  33 + end
  34 +
  35 + return content
  36 + end
  37 +
  38 + CharlockHolmes::Converter.convert(content, hash[:encoding], 'UTF-8') if hash[:encoding]
  39 + else
  40 + content.force_encoding("UTF-8")
  41 + end
  42 + end
  43 +
  44 + def z_binary?(string)
  45 + string.each_byte do |x|
  46 + x.nonzero? or return true
25 end 47 end
26 - content 48 + false
27 end 49 end
28 end 50 end
29 51