From 5dd115802529f377b5dfe3622203797c59d2e13f Mon Sep 17 00:00:00 2001 From: "Jeong, Heon" Date: Thu, 24 May 2012 10:22:09 +0900 Subject: [PATCH 1/3] Use grit with force_encoding('ascii-8bit') --- lib/gollum/committer.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/gollum/committer.rb b/lib/gollum/committer.rb index e1911bdf..3d6e3427 100644 --- a/lib/gollum/committer.rb +++ b/lib/gollum/committer.rb @@ -104,7 +104,7 @@ module Gollum end end - index.add(fullpath, @wiki.normalize(data)) + index.add(fullpath.force_encoding('ascii-8bit'), @wiki.normalize(data)) end # Update the given file in the repository's working directory if there @@ -131,9 +131,9 @@ module Gollum Dir.chdir(::File.join(@wiki.repo.path, '..')) do if file_path_scheduled_for_deletion?(index.tree, path) - @wiki.repo.git.rm({'f' => true}, '--', path) + @wiki.repo.git.rm({'f' => true}, '--', path.force_encoding('ascii-8bit')) else - @wiki.repo.git.checkout({}, 'HEAD', '--', path) + @wiki.repo.git.checkout({}, 'HEAD', '--', path.force_encoding('ascii-8bit')) end end end @@ -212,6 +212,7 @@ module Gollum # Proxies methods t def method_missing(name, *args) + args.map! { |item| item.force_encoding('ascii-8bit') } index.send(name, *args) end end From 150a019b8d440329dd3bbb6a6e65999ab348bbf3 Mon Sep 17 00:00:00 2001 From: Neon Date: Thu, 24 May 2012 22:13:41 +0900 Subject: [PATCH 2/3] Added test cases to test Unicode handling issue. these test cases will fail on non-unicode testing environment --- test/test_unicode.rb | 110 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 test/test_unicode.rb diff --git a/test/test_unicode.rb b/test/test_unicode.rb new file mode 100644 index 00000000..b24bba86 --- /dev/null +++ b/test/test_unicode.rb @@ -0,0 +1,110 @@ +# ~*~ encoding: utf-8 ~*~ +require File.expand_path(File.join(File.dirname(__FILE__), "helper")) + +context "Unicode Support" do + setup do + @path = cloned_testpath("examples/revert.git") + @wiki = Gollum::Wiki.new(@path) + end + + teardown do + FileUtils.rm_rf(@path) + end + + test "create and read non-latin page" do + @wiki.write_page("한글 test", :markdown, "# 한글") + + page = @wiki.page("한글 test") + assert_equal Gollum::Page, page.class + assert_equal "# 한글", page.raw_data.force_encoding('utf-8') + end + + test "unicode with existing format rules" do + @wiki.write_page("한글 test", :markdown, "# 한글") + assert_equal @wiki.page("한글 test").path, @wiki.page("한글-test").path + end +end + +context "Frontend Unicode support" do + include Rack::Test::Methods + + setup do + @path = cloned_testpath("examples/revert.git") + @wiki = Gollum::Wiki.new(@path) + Precious::App.set(:gollum_path, @path) + Precious::App.set(:wiki_options, {}) + end + + teardown do + FileUtils.rm_rf(@path) + end + + test "creates korean page" do + post "/create", :content => 'english text', :page => "한글", + :format => 'markdown', :message => 'def' + follow_redirect! + assert last_response.ok? + + page = @wiki.page('한글') + assert_equal 'english text', page.raw_data + assert_equal 'def', page.version.message + end + + test "creates korean page which contains korean content" do + post "/create", :content => '한글 text', :page => "한글", + :format => 'markdown', :message => 'def' + follow_redirect! + assert last_response.ok? + + page = @wiki.page('한글') + assert_equal '한글 text', page.raw_data.force_encoding('utf-8') + assert_equal 'def', page.version.message + end + + test "heavy use 1" do + post "/create", :content => '한글 text'.force_encoding('ascii-8bit'), :page => "PG", + :format => 'markdown', :message => 'def' + follow_redirect! + assert last_response.ok? + + @wiki.update_page(@wiki.page('PG'), nil, nil, '다른 text', {}) + page = @wiki.page('PG') + assert_equal '다른 text', page.raw_data.force_encoding('utf-8') + + post '/edit/PG', :content => '바뀐 text'.force_encoding('ascii-8bit'), :message => 'ghi' + follow_redirect! + assert last_response.ok? + + @wiki = Gollum::Wiki.new(@path) + page = @wiki.page('PG') + assert_equal '바뀐 text', page.raw_data.force_encoding('utf-8') + assert_equal 'ghi', page.version.message + end + + test "heavy use 2" do + post "/create", :content => '한글 text', :page => "한글", + :format => 'markdown', :message => 'def' + follow_redirect! + assert last_response.ok? + + @wiki.update_page(@wiki.page('한글'), nil, nil, '다른 text', {}) + @wiki = Gollum::Wiki.new(@path) + page = @wiki.page('한글') + assert_equal '다른 text', page.raw_data.force_encoding('utf-8') + + post '/edit/' + CGI.escape('한글'), :content => '바뀐 text', + :format => 'markdown', :message => 'ghi' + follow_redirect! + assert last_response.ok? + + @wiki = Gollum::Wiki.new(@path) + page = @wiki.page('한글') + assert_equal '바뀐 text', page.raw_data.force_encoding('utf-8') + assert_equal 'ghi', page.version.message + end + + def app + Precious::App + end +end + From eaa7a61f71271b35af1abda282f0485bddd622a7 Mon Sep 17 00:00:00 2001 From: blmarket Date: Thu, 31 May 2012 21:23:58 +0900 Subject: [PATCH 3/3] Fixed 1.8.7 compatible issue --- lib/gollum/committer.rb | 12 ++++++++---- test/test_unicode.rb | 20 ++++++++++++-------- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/lib/gollum/committer.rb b/lib/gollum/committer.rb index 3d6e3427..ada599c9 100644 --- a/lib/gollum/committer.rb +++ b/lib/gollum/committer.rb @@ -104,7 +104,9 @@ module Gollum end end - index.add(fullpath.force_encoding('ascii-8bit'), @wiki.normalize(data)) + fullpath = fullpath.force_encoding('ascii-8bit') if fullpath.respond_to?(:force_encoding) + + index.add(fullpath, @wiki.normalize(data)) end # Update the given file in the repository's working directory if there @@ -129,11 +131,13 @@ module Gollum ::File.join(dir, @wiki.page_file_name(name, format)) end + path = path.force_encoding('ascii-8bit') if path.respond_to?(:force_encoding) + Dir.chdir(::File.join(@wiki.repo.path, '..')) do if file_path_scheduled_for_deletion?(index.tree, path) - @wiki.repo.git.rm({'f' => true}, '--', path.force_encoding('ascii-8bit')) + @wiki.repo.git.rm({'f' => true}, '--', path) else - @wiki.repo.git.checkout({}, 'HEAD', '--', path.force_encoding('ascii-8bit')) + @wiki.repo.git.checkout({}, 'HEAD', '--', path) end end end @@ -212,7 +216,7 @@ module Gollum # Proxies methods t def method_missing(name, *args) - args.map! { |item| item.force_encoding('ascii-8bit') } + args.map! { |item| item.respond_to?(:force_encoding) ? item.force_encoding('ascii-8bit') : item } index.send(name, *args) end end diff --git a/test/test_unicode.rb b/test/test_unicode.rb index b24bba86..2fe209d7 100644 --- a/test/test_unicode.rb +++ b/test/test_unicode.rb @@ -1,6 +1,10 @@ # ~*~ encoding: utf-8 ~*~ require File.expand_path(File.join(File.dirname(__FILE__), "helper")) +def utf8(str) + str.respond_to?(:force_encoding) ? str.force_encoding('utf-8') : str +end + context "Unicode Support" do setup do @path = cloned_testpath("examples/revert.git") @@ -16,7 +20,7 @@ context "Unicode Support" do page = @wiki.page("한글 test") assert_equal Gollum::Page, page.class - assert_equal "# 한글", page.raw_data.force_encoding('utf-8') + assert_equal "# 한글", utf8(page.raw_data) end test "unicode with existing format rules" do @@ -57,27 +61,27 @@ context "Frontend Unicode support" do assert last_response.ok? page = @wiki.page('한글') - assert_equal '한글 text', page.raw_data.force_encoding('utf-8') + assert_equal '한글 text', utf8(page.raw_data) assert_equal 'def', page.version.message end test "heavy use 1" do - post "/create", :content => '한글 text'.force_encoding('ascii-8bit'), :page => "PG", + post "/create", :content => '한글 text', :page => "PG", :format => 'markdown', :message => 'def' follow_redirect! assert last_response.ok? @wiki.update_page(@wiki.page('PG'), nil, nil, '다른 text', {}) page = @wiki.page('PG') - assert_equal '다른 text', page.raw_data.force_encoding('utf-8') + assert_equal '다른 text', utf8(page.raw_data) - post '/edit/PG', :content => '바뀐 text'.force_encoding('ascii-8bit'), :message => 'ghi' + post '/edit/PG', :content => '바뀐 text', :message => 'ghi' follow_redirect! assert last_response.ok? @wiki = Gollum::Wiki.new(@path) page = @wiki.page('PG') - assert_equal '바뀐 text', page.raw_data.force_encoding('utf-8') + assert_equal '바뀐 text', utf8(page.raw_data) assert_equal 'ghi', page.version.message end @@ -90,7 +94,7 @@ context "Frontend Unicode support" do @wiki.update_page(@wiki.page('한글'), nil, nil, '다른 text', {}) @wiki = Gollum::Wiki.new(@path) page = @wiki.page('한글') - assert_equal '다른 text', page.raw_data.force_encoding('utf-8') + assert_equal '다른 text', utf8(page.raw_data) post '/edit/' + CGI.escape('한글'), :content => '바뀐 text', :format => 'markdown', :message => 'ghi' @@ -99,7 +103,7 @@ context "Frontend Unicode support" do @wiki = Gollum::Wiki.new(@path) page = @wiki.page('한글') - assert_equal '바뀐 text', page.raw_data.force_encoding('utf-8') + assert_equal '바뀐 text', utf8(page.raw_data) assert_equal 'ghi', page.version.message end