Commit 46cbe5418947ab58c919432b9013252ada6a3bc3

Authored by Saito
1 parent 75fa0632

fix the issue on github #157.

directly force_encoding is wrong,
must detect the file string's encoding.
then force_encoding the string to it's encoding.
last convert it to utf-8.
app/helpers/commits_helper.rb
1 module CommitsHelper 1 module CommitsHelper
  2 + include Utils::CharEncode
2 def diff_line(line, line_new = 0, line_old = 0) 3 def diff_line(line, line_new = 0, line_old = 0)
3 full_line = html_escape(line.gsub(/\n/, '')) 4 full_line = html_escape(line.gsub(/\n/, ''))
4 color = if line[0] == "+" 5 color = if line[0] == "+"
app/models/commit.rb
1 class Commit 1 class Commit
  2 + include Utils::CharEncode
  3 +
2 attr_accessor :commit 4 attr_accessor :commit
3 attr_accessor :head 5 attr_accessor :head
4 6
@@ -20,7 +22,7 @@ class Commit @@ -20,7 +22,7 @@ class Commit
20 end 22 end
21 23
22 def safe_message 24 def safe_message
23 - message.force_encoding(Encoding::UTF_8) 25 + encode(message)
24 end 26 end
25 27
26 def created_at 28 def created_at
@@ -28,10 +30,10 @@ class Commit @@ -28,10 +30,10 @@ class Commit
28 end 30 end
29 31
30 def author_email 32 def author_email
31 - author.email.force_encoding(Encoding::UTF_8) 33 + encode(author.email)
32 end 34 end
33 35
34 def author_name 36 def author_name
35 - author.name.force_encoding(Encoding::UTF_8) 37 + encode(author.name)
36 end 38 end
37 end 39 end
app/views/commits/_text_file.html.haml
@@ -2,7 +2,7 @@ @@ -2,7 +2,7 @@
2 - line_new = 0 2 - line_new = 0
3 - lines_arr = diff.diff.lines.to_a 3 - lines_arr = diff.diff.lines.to_a
4 - lines_arr.each do |line| 4 - lines_arr.each do |line|
5 - - line.force_encoding(Encoding::UTF_8) 5 + - encode(line)
6 - next if line.match(/^--- \/dev\/null/) 6 - next if line.match(/^--- \/dev\/null/)
7 - next if line.match(/^--- a/) 7 - next if line.match(/^--- a/)
8 - next if line.match(/^\+\+\+ b/) 8 - next if line.match(/^\+\+\+ b/)
lib/graph_commit.rb
1 require "grit" 1 require "grit"
2 2
3 class GraphCommit 3 class GraphCommit
  4 + include Utils::CharEncode
4 attr_accessor :time, :space 5 attr_accessor :time, :space
5 attr_accessor :refs 6 attr_accessor :refs
6 7
@@ -65,7 +66,7 @@ class GraphCommit @@ -65,7 +66,7 @@ class GraphCommit
65 # @param [GraphCommit] the commit object. 66 # @param [GraphCommit] the commit object.
66 # @param [Hash<String,GraphCommit>] map of commits 67 # @param [Hash<String,GraphCommit>] map of commits
67 # 68 #
68 - # @return [Fixnum] max space used. 69 + # @return [Fixnum] max space used.
69 def self.mark_chain(mark, commit, map) 70 def self.mark_chain(mark, commit, map)
70 commit.space = mark if commit.space == 0 71 commit.space = mark if commit.space == 0
71 m1 = mark - 1 72 m1 = mark - 1
@@ -96,13 +97,13 @@ class GraphCommit @@ -96,13 +97,13 @@ class GraphCommit
96 h[:parents] = self.parents.collect do |p| 97 h[:parents] = self.parents.collect do |p|
97 [p.id,0,0] 98 [p.id,0,0]
98 end 99 end
99 - h[:author] = author.name.force_encoding("UTF-8") 100 + h[:author] = encode(author.name)
100 h[:time] = time 101 h[:time] = time
101 h[:space] = space 102 h[:space] = space
102 h[:refs] = refs.collect{|r|r.name}.join(" ") unless refs.nil? 103 h[:refs] = refs.collect{|r|r.name}.join(" ") unless refs.nil?
103 h[:id] = sha 104 h[:id] = sha
104 h[:date] = date 105 h[:date] = date
105 - h[:message] = message.force_encoding("UTF-8") 106 + h[:message] = encode(message)
106 h[:login] = author.email 107 h[:login] = author.email
107 h 108 h
108 end 109 end
@@ -16,9 +16,20 @@ module Utils @@ -16,9 +16,20 @@ module Utils
16 end 16 end
17 end 17 end
18 18
  19 + module CharEncode
  20 + def encode(string)
  21 + cd = CharDet.detect(string)
  22 + if cd.confidence > 0.6
  23 + string.force_encoding(cd.encoding)
  24 + end
  25 + string.encode("utf-8", :undef => :replace, :replace => "?", :invalid => :replace)
  26 + end
  27 + end
  28 +
19 module Colorize 29 module Colorize
  30 + include CharEncode
20 def colorize 31 def colorize
21 - system_colorize(data, name) 32 + system_colorize(encode(data), name)
22 end 33 end
23 34
24 def system_colorize(data, file_name) 35 def system_colorize(data, file_name)