OSDN Git Service

Better encoding handling. Updated grit
authorDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Wed, 4 Apr 2012 22:51:49 +0000 (01:51 +0300)
committerDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Wed, 4 Apr 2012 22:51:49 +0000 (01:51 +0300)
Gemfile.lock
app/controllers/commits_controller.rb
config/initializers/gitlabhq/20_grit_ext.rb

index 46831d3..3808242 100644 (file)
@@ -14,7 +14,7 @@ GIT
 
 GIT
   remote: https://github.com/gitlabhq/grit.git
-  revision: ff015074ef35bd94cba943f9c0f98e161ab5851c
+  revision: 3fc864f3c637e06e2fa7a81f6b48a5df58a9bc5b
   specs:
     grit (2.4.1)
       diff-lcs (~> 1.1)
@@ -148,7 +148,7 @@ GEM
       mime-types (~> 1.16)
       treetop (~> 1.4.8)
     method_source (0.7.0)
-    mime-types (1.17.2)
+    mime-types (1.18)
     modularity (0.6.1)
     multi_json (1.0.4)
     multi_xml (0.4.1)
index 486e198..3ee2e7a 100644 (file)
@@ -1,3 +1,4 @@
+require 'benchmark'
 require "base64"
 
 class CommitsController < ApplicationController
index a8c97b1..9d4b226 100644 (file)
@@ -17,13 +17,35 @@ Grit::GitRuby::Internal::RawObject.class_eval do
   end
 
   private
+
   def transcoding(content)
     content ||= ""
-    detection = CharlockHolmes::EncodingDetector.detect(content)
-    if hash = detection
-     content = CharlockHolmes::Converter.convert(content, hash[:encoding], 'UTF-8') if hash[:encoding]
+    hash = CharlockHolmes::EncodingDetector.detect(content)
+
+    if hash 
+      return content if hash[:type] == :binary
+
+      if hash[:encoding] == "UTF-8"
+        content = if hash[:confidence] < 100
+                    content
+                  else
+                    content.force_encoding("UTF-8")
+                  end
+
+        return content
+      end
+
+      CharlockHolmes::Converter.convert(content, hash[:encoding], 'UTF-8') if hash[:encoding]
+    else 
+      content.force_encoding("UTF-8")
+    end
+  end
+
+  def z_binary?(string)
+    string.each_byte do |x|
+      x.nonzero? or return true
     end
-    content
+    false
   end
 end