From a81049d52bebe85418efe42a1674b74611ffd6cc Mon Sep 17 00:00:00 2001
From: Toshi MARUYAMA <marutosijp2@yahoo.co.jp>
Date: Tue, 22 Feb 2011 13:39:37 +0000
Subject: [PATCH] scm: for log in Ruby 1.9, replace invalid UTF-8 to '?'
 instead of removing.

Refer r3466 #4773.

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@4926 e93f8b46-1217-0410-a6f0-8f06a7374b81
---
 app/models/changeset.rb     | 20 ++++++++++++++------
 test/unit/changeset_test.rb |  9 +++++++--
 2 files changed, 21 insertions(+), 8 deletions(-)

diff --git a/app/models/changeset.rb b/app/models/changeset.rb
index d2e4a53f8..01220a21f 100644
--- a/app/models/changeset.rb
+++ b/app/models/changeset.rb
@@ -253,12 +253,20 @@ class Changeset < ActiveRecord::Base
         # do nothing here
       end
     end
-    # removes invalid UTF8 sequences
-    begin
-      Iconv.conv('UTF-8//IGNORE', 'UTF-8', str + '  ')[0..-3]
-    rescue Iconv::InvalidEncoding
-      # "UTF-8//IGNORE" is not supported on some OS
-      str
+    if str.respond_to?(:force_encoding)
+      str.force_encoding('UTF-8')
+      if ! str.valid_encoding?
+        str = str.encode("US-ASCII", :invalid => :replace,
+              :undef => :replace, :replace => '?').encode("UTF-8")
+      end
+    else
+      # removes invalid UTF8 sequences
+      begin
+        str = Iconv.conv('UTF-8//IGNORE', 'UTF-8', str + '  ')[0..-3]
+      rescue Iconv::InvalidEncoding
+        # "UTF-8//IGNORE" is not supported on some OS
+      end
     end
+    str
   end
 end
diff --git a/test/unit/changeset_test.rb b/test/unit/changeset_test.rb
index 508294fea..a0aaff78d 100644
--- a/test/unit/changeset_test.rb
+++ b/test/unit/changeset_test.rb
@@ -216,8 +216,13 @@ class ChangesetTest < ActiveSupport::TestCase
   def test_invalid_utf8_sequences_in_comments_should_be_stripped
     with_settings :commit_logs_encoding => 'UTF-8' do
       c = Changeset.new
-      c.comments = File.read("#{RAILS_ROOT}/test/fixtures/encoding/iso-8859-1.txt")
-      assert_equal "Texte encod en ISO-8859-1.", c.comments
+      str = File.read("#{RAILS_ROOT}/test/fixtures/encoding/iso-8859-1.txt")
+      c.comments = str
+      if str.respond_to?(:force_encoding)
+        assert_equal "Texte encod? en ISO-8859-1.", c.comments
+      else
+        assert_equal "Texte encod en ISO-8859-1.", c.comments
+      end
     end
   end
 
-- 
GitLab