From 655c50849d56ab36941ada262cab036a62df666c Mon Sep 17 00:00:00 2001 From: Toshi MARUYAMA Date: Fri, 5 Apr 2013 09:56:14 +0000 Subject: [PATCH] Merged r11706 and r11707 from trunk to 2.3-stable (#13644) fix diff error in case of line_left out of range. git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/branches/2.3-stable@11709 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- lib/redmine/unified_diff.rb | 12 ++-- test/fixtures/diffs/issue-13644-1.diff | 7 +++ test/fixtures/diffs/issue-13644-2.diff | 7 +++ test/unit/lib/redmine/unified_diff_test.rb | 64 ++++++++++++++++++++++ 4 files changed, 86 insertions(+), 4 deletions(-) create mode 100644 test/fixtures/diffs/issue-13644-1.diff create mode 100644 test/fixtures/diffs/issue-13644-2.diff diff --git a/lib/redmine/unified_diff.rb b/lib/redmine/unified_diff.rb index 6fbfb80fe..548003220 100644 --- a/lib/redmine/unified_diff.rb +++ b/lib/redmine/unified_diff.rb @@ -199,15 +199,19 @@ module Redmine while starting < max && line_left[starting] == line_right[starting] starting += 1 end - while line_left[starting].ord.between?(128, 191) && starting > 0 - starting -= 1 + if (! "".respond_to?(:force_encoding)) && starting < line_left.size + while line_left[starting].ord.between?(128, 191) && starting > 0 + starting -= 1 + end end ending = -1 while ending >= -(max - starting) && line_left[ending] == line_right[ending] ending -= 1 end - while line_left[ending].ord.between?(128, 191) && ending > -1 - ending -= 1 + if (! "".respond_to?(:force_encoding)) && ending > (-1 * line_left.size) + while line_left[ending].ord.between?(128, 191) && ending > -1 + ending -= 1 + end end unless starting == 0 && ending == -1 [starting, ending] diff --git a/test/fixtures/diffs/issue-13644-1.diff b/test/fixtures/diffs/issue-13644-1.diff new file mode 100644 index 000000000..3c25c97fa --- /dev/null +++ b/test/fixtures/diffs/issue-13644-1.diff @@ -0,0 +1,7 @@ +--- a.txt 2013-04-05 14:19:39.000000000 +0900 ++++ b.txt 2013-04-05 14:19:51.000000000 +0900 +@@ -1,3 +1,3 @@ + aaaa +-日本 ++日本語 + bbbb diff --git a/test/fixtures/diffs/issue-13644-2.diff b/test/fixtures/diffs/issue-13644-2.diff new file mode 100644 index 000000000..3bf142502 --- /dev/null +++ b/test/fixtures/diffs/issue-13644-2.diff @@ -0,0 +1,7 @@ +--- a.txt 2013-04-05 14:19:39.000000000 +0900 ++++ b.txt 2013-04-05 14:19:51.000000000 +0900 +@@ -1,3 +1,3 @@ + aaaa +-日本 ++にっぽん日本 + bbbb diff --git a/test/unit/lib/redmine/unified_diff_test.rb b/test/unit/lib/redmine/unified_diff_test.rb index cbc5cec45..7e1ccbdd9 100644 --- a/test/unit/lib/redmine/unified_diff_test.rb +++ b/test/unit/lib/redmine/unified_diff_test.rb @@ -244,6 +244,70 @@ DIFF end end + def test_offset_range_ascii_1 + raw = <<-DIFF +--- a.txt 2013-04-05 14:19:39.000000000 +0900 ++++ b.txt 2013-04-05 14:19:51.000000000 +0900 +@@ -1,3 +1,3 @@ + aaaa +-abc ++abcd + bbbb +DIFF + diff = Redmine::UnifiedDiff.new(raw, :type => 'sbs') + assert_equal 1, diff.size + assert_equal 3, diff.first.size + assert_equal "abc", diff.first[1].html_line_left + assert_equal "abcd", diff.first[1].html_line_right + end + + def test_offset_range_ascii_2 + raw = <<-DIFF +--- a.txt 2013-04-05 14:19:39.000000000 +0900 ++++ b.txt 2013-04-05 14:19:51.000000000 +0900 +@@ -1,3 +1,3 @@ + aaaa +-abc ++zabc + bbbb +DIFF + diff = Redmine::UnifiedDiff.new(raw, :type => 'sbs') + assert_equal 1, diff.size + assert_equal 3, diff.first.size + assert_equal "abc", diff.first[1].html_line_left + assert_equal "zabc", diff.first[1].html_line_right + end + + def test_offset_range_japanese_1 + ja1 = "\xe6\x97\xa5\xe6\x9c\xac" + ja1.force_encoding('UTF-8') if ja1.respond_to?(:force_encoding) + ja2 = "\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e" + ja2.force_encoding('UTF-8') if ja2.respond_to?(:force_encoding) + with_settings :repositories_encodings => '' do + diff = Redmine::UnifiedDiff.new( + read_diff_fixture('issue-13644-1.diff'), :type => 'sbs') + assert_equal 1, diff.size + assert_equal 3, diff.first.size + assert_equal ja1, diff.first[1].html_line_left + assert_equal ja2, diff.first[1].html_line_right + end + end + + def test_offset_range_japanese_2 + ja1 = "\xe6\x97\xa5\xe6\x9c\xac" + ja1.force_encoding('UTF-8') if ja1.respond_to?(:force_encoding) + ja2 = "\xe3\x81\xab\xe3\x81\xa3\xe3\x81\xbd\xe3\x82\x93\xe6\x97\xa5\xe6\x9c\xac" + ja2.force_encoding('UTF-8') if ja2.respond_to?(:force_encoding) + with_settings :repositories_encodings => '' do + diff = Redmine::UnifiedDiff.new( + read_diff_fixture('issue-13644-2.diff'), :type => 'sbs') + assert_equal 1, diff.size + assert_equal 3, diff.first.size + assert_equal ja1, diff.first[1].html_line_left + assert_equal ja2, diff.first[1].html_line_right + end + end + private def read_diff_fixture(filename)