Automatic character encoding detection in Mercurial repos.

This commit is contained in:
Kolan Sh 2012-03-24 23:39:33 +04:00 committed by Kolan Sh
parent 9f3239a25a
commit 9ec90a50d4
2 changed files with 41 additions and 3 deletions

View File

@ -9,6 +9,8 @@ gem "builder", "3.0.0"
gem "mime-types" gem "mime-types"
gem "awesome_nested_set", "2.1.6" gem "awesome_nested_set", "2.1.6"
gem "chardet", ">= 0.9.0"
# Optional gem for LDAP authentication # Optional gem for LDAP authentication
group :ldap do group :ldap do
gem "net-ldap", "~> 0.3.1" gem "net-ldap", "~> 0.3.1"

View File

@ -17,6 +17,8 @@
require 'redmine/scm/adapters/abstract_adapter' require 'redmine/scm/adapters/abstract_adapter'
require 'cgi' require 'cgi'
require 'rubygems'
require 'UniversalDetector'
module Redmine module Redmine
module Scm module Scm
@ -255,7 +257,18 @@ module Redmine
diff = [] diff = []
hg *hg_args do |io| hg *hg_args do |io|
io.each_line do |line| io.each_line do |line|
diff << line iconv_line = line
if !line.nil?
detected = UniversalDetector::chardet(line)
enc = detected['encoding']
if !enc.nil?
begin
iconv_line = Iconv.conv('UTF-8', enc, line)
rescue Iconv::Failure => err
end
end
end
diff << iconv_line
end end
end end
diff diff
@ -267,7 +280,18 @@ module Redmine
p = CGI.escape(scm_iconv(@path_encoding, 'UTF-8', path)) p = CGI.escape(scm_iconv(@path_encoding, 'UTF-8', path))
hg 'rhcat', '-r', CGI.escape(hgrev(identifier)), hgtarget(p) do |io| hg 'rhcat', '-r', CGI.escape(hgrev(identifier)), hgtarget(p) do |io|
io.binmode io.binmode
io.read str = io.read
return nil if str.nil?
iconv_str = str
detected = UniversalDetector::chardet(str)
enc = detected['encoding']
if !enc.nil?
begin
iconv_str = Iconv.conv('UTF-8', enc, str)
rescue Iconv::Failure => err
end
end
return iconv_str
end end
rescue HgCommandAborted rescue HgCommandAborted
nil # means not found nil # means not found
@ -282,7 +306,19 @@ module Redmine
next unless line =~ %r{^([^:]+)\s(\d+)\s([0-9a-f]+):\s(.*)$} next unless line =~ %r{^([^:]+)\s(\d+)\s([0-9a-f]+):\s(.*)$}
r = Revision.new(:author => $1.strip, :revision => $2, :scmid => $3, r = Revision.new(:author => $1.strip, :revision => $2, :scmid => $3,
:identifier => $3) :identifier => $3)
blame.add_line($4.rstrip, r) str = $4.rstrip
iconv_str = str
if !str.nil?
detected = UniversalDetector::chardet(str)
enc = detected['encoding']
if !enc.nil?
begin
iconv_str = Iconv.conv('UTF-8', enc, str)
rescue Iconv::Failure => err
end
end
end
blame.add_line(iconv_str, r)
end end
end end
blame blame