From f2bfcf86999444335cc8789f72b837f41df039ec Mon Sep 17 00:00:00 2001 From: George Vinogradov Date: Sun, 5 Feb 2012 00:12:37 +0400 Subject: [PATCH 1/2] [issue #151] Probably fixed. --- app/controllers/git/blobs_controller.rb | 6 +++--- app/helpers/commit_helper.rb | 4 ++-- app/helpers/git_helper.rb | 11 +++++++++-- app/views/git/blobs/blame.html.haml | 10 ++++++---- app/views/git/blobs/show.html.haml | 2 +- app/views/git/commits/_commit_diff.html.haml | 6 +++--- app/views/git/commits/_commits.html.haml | 8 ++++---- app/views/git/repositories/show.html.haml | 6 +++--- config/initializers/core.rb | 1 + lib/core.rb | 3 +++ lib/ext/core/object.rb | 8 ++++++++ lib/ext/core/string.rb | 5 +++++ 12 files changed, 48 insertions(+), 22 deletions(-) create mode 100644 config/initializers/core.rb create mode 100644 lib/core.rb create mode 100644 lib/ext/core/object.rb create mode 100644 lib/ext/core/string.rb diff --git a/app/controllers/git/blobs_controller.rb b/app/controllers/git/blobs_controller.rb index 75663689e..31a20870b 100644 --- a/app/controllers/git/blobs_controller.rb +++ b/app/controllers/git/blobs_controller.rb @@ -5,7 +5,7 @@ class Git::BlobsController < Git::BaseController before_filter :find_tree def show - @blob = @tree / @path + @blob = @tree / @path.force_encoding(Encoding::ASCII_8BIT) if params[:raw] image_url = Rails.root.to_s + "/" + @path @@ -19,13 +19,13 @@ class Git::BlobsController < Git::BaseController end def blame - @blob = @tree / @path + @blob = @tree / @path.force_encoding(Encoding::ASCII_8BIT) @blame = Grit::Blob.blame(@git_repository.repo, @commit.try(:id), @path) end def raw - @blob = @tree / @path + @blob = @tree / @path.force_encoding(Encoding::ASCII_8BIT) headers["Content-Disposition"] = %[attachment;filename="#{@blob.name}"] render :text => @blob.data, :content_type => @blob.mime_type diff --git a/app/helpers/commit_helper.rb b/app/helpers/commit_helper.rb index 8093043b3..de2c92901 100644 --- a/app/helpers/commit_helper.rb +++ b/app/helpers/commit_helper.rb @@ -13,7 +13,7 @@ module CommitHelper end res << "" - res.join("\n").html_safe + res.join("\n").force_encoding(Encoding.default_internal || Encoding::UTF_8).html_safe end # def format_commit_message(message) @@ -34,7 +34,7 @@ module CommitHelper def short_commit_message(message) # Why 42? Because it is the Answer! - truncate(message, :length => 42, :omission => "...") + truncate(message, :length => 42, :omission => "...").force_encoding(Encoding.default_internal || Encoding::UTF_8) end end diff --git a/app/helpers/git_helper.rb b/app/helpers/git_helper.rb index 619376dd2..0cb377b23 100644 --- a/app/helpers/git_helper.rb +++ b/app/helpers/git_helper.rb @@ -26,7 +26,7 @@ module GitHelper res = "#{link_to @project.name, tree_path(@project)} /" end - res.html_safe + res.encode_to_default.html_safe end def render_line_numbers(n) @@ -38,7 +38,9 @@ module GitHelper def render_blob(blob) res = "" - blob.data.split("\n").collect{|line| "
#{line.present? ? h(line) : "
"}
"}.join + blob.data.force_encoding(Encoding.default_internal || Encoding::UTF_8).split("\n").collect do |line| + "
#{line.present? ? h(line) : "
"}
" + end.join end def choose_render_way(blob) @@ -46,4 +48,9 @@ module GitHelper return :text if blob.mime_type.match(/text|xml|json/) :binary end + + def force_encoding_to_site(string) + string.dup.force_encoding(Encoding.default_internal || Encoding::UTF_8) + end + end diff --git a/app/views/git/blobs/blame.html.haml b/app/views/git/blobs/blame.html.haml index c39582dd1..d6268b0f7 100644 --- a/app/views/git/blobs/blame.html.haml +++ b/app/views/git/blobs/blame.html.haml @@ -29,7 +29,9 @@ - @blame.each do |elem| %tr %td.message{ :rowspan => elem[1].length } - .commit #{link_to shortest_hash_id(elem[0].id), commit_path(@project, elem[0].id)} by #{elem[0].author} #{elem[0].author != elem[0].committer ? "(#{elem[0].committer})" : "" } + .commit + #{link_to shortest_hash_id(elem[0].id), commit_path(@project, elem[0].id)} by + #{elem[0].author.to_s.encode_to_default} #{elem[0].author != elem[0].committer ? "(#{elem[0].committer.to_s.encode_to_default})" : "" } .message %span.date= commit_date(elem[0].committed_date) %span.message= short_commit_message(elem[0].message) @@ -40,7 +42,7 @@ %td.code %pre - %div= elem[1].first + %div= elem[1].first.force_encoding(Encoding.default_internal || Encoding::UTF_8) - elem[1][1..-1].each do |line| %tr @@ -49,6 +51,6 @@ - index += 1 %td.code %pre - %div= line + %div= line.force_encoding(Encoding.default_internal || Encoding::UTF_8) -- content_for :sidebar, render(:partial => 'git/shared/sidebar') \ No newline at end of file +- content_for :sidebar, render(:partial => 'git/shared/sidebar') diff --git a/app/views/git/blobs/show.html.haml b/app/views/git/blobs/show.html.haml index 04b3a19b3..c58c41723 100644 --- a/app/views/git/blobs/show.html.haml +++ b/app/views/git/blobs/show.html.haml @@ -49,7 +49,7 @@ %td.blob :plain
-
#{ link_to @blob.basename, raw_path(@project, @treeish, @path) }
+
#{ link_to @blob.basename.force_encoding(Encoding.default_internal || Encoding::UTF_8), raw_path(@project, @treeish, @path) }

- content_for :sidebar, render(:partial => 'git/shared/sidebar') diff --git a/app/views/git/commits/_commit_diff.html.haml b/app/views/git/commits/_commit_diff.html.haml index 5d387b06a..9ac2922c4 100644 --- a/app/views/git/commits/_commit_diff.html.haml +++ b/app/views/git/commits/_commit_diff.html.haml @@ -2,13 +2,13 @@ .content .inner - %a{ :name => h(commit_diff.a_path) } + %a{ :name => h(commit_diff.a_path.force_encoding(Encoding.default_internal || Encoding::UTF_8)) } .blob_header - .size= h(commit_diff.a_path) + .size= h(commit_diff.a_path.force_encoding(Encoding.default_internal || Encoding::UTF_8)) - if commit_diff.b_path.present? .buttons - = link_to("view file @ #{short_hash_id(@commit.id)}", blob_commit_path(@project, @commit.id, commit_diff.b_path)) + = link_to("view file @ #{short_hash_id(@commit.id)}", blob_commit_path(@project, @commit.id, commit_diff.b_path.force_encoding(Encoding.default_internal || Encoding::UTF_8))) .clear .diff_data diff --git a/app/views/git/commits/_commits.html.haml b/app/views/git/commits/_commits.html.haml index b248234eb..9e4d9ae50 100644 --- a/app/views/git/commits/_commits.html.haml +++ b/app/views/git/commits/_commits.html.haml @@ -4,12 +4,12 @@ %table %tr %td.committers - .author #{commit.author}, #{commit_date(commit.authored_date)} + .author #{commit.author.to_s.encode_to_default}, #{commit_date(commit.authored_date)} - if commit.committer != commit.author .committer - (committed by: #{commit.committer}, #{commit_date(commit.committed_date)}) + (committed by: #{commit.committer.to_s.encode_to_default}, #{commit_date(commit.committed_date)}) %td.message - %p= link_to commit.message, commit_path(@project, commit.id) + %p= link_to commit.message.encode_to_default, commit_path(@project, commit.id) %td.trees .commit Commit: @@ -23,4 +23,4 @@ .parent Parent: %span{ :style => "float: right;"} - #{link_to short_hash_id(parent.id), tree_path(@project, :treeish => parent.id)} \ No newline at end of file + #{link_to short_hash_id(parent.id), tree_path(@project, :treeish => parent.id)} diff --git a/app/views/git/repositories/show.html.haml b/app/views/git/repositories/show.html.haml index a0e547531..41c205ebd 100644 --- a/app/views/git/repositories/show.html.haml +++ b/app/views/git/repositories/show.html.haml @@ -37,10 +37,10 @@ = image_tag("git/icons/folder_16.png") %td.tree_element - if entry.is_a?(Grit::Blob) - = link_to entry.name, blob_path(@project, @treeish, File.join([@path, entry.name].compact)) + = link_to entry.name.encode_to_default, blob_path(@project, @treeish, File.join([@path, entry.name.encode_to_default].compact)) - else - = link_to "#{entry.name}/", tree_path(@project, @treeish, File.join([@path, entry.name].compact)) + = link_to "#{entry.name.encode_to_default}/", tree_path(@project, @treeish, File.join([@path, entry.name.encode_to_default].compact)) %td==   %td.last==   -- content_for :sidebar, render(:partial => 'git/shared/sidebar') \ No newline at end of file +- content_for :sidebar, render(:partial => 'git/shared/sidebar') diff --git a/config/initializers/core.rb b/config/initializers/core.rb new file mode 100644 index 000000000..7683d8cb2 --- /dev/null +++ b/config/initializers/core.rb @@ -0,0 +1 @@ +require './lib/core' diff --git a/lib/core.rb b/lib/core.rb new file mode 100644 index 000000000..646cc111d --- /dev/null +++ b/lib/core.rb @@ -0,0 +1,3 @@ +Dir.glob(File.join('.', 'lib', 'ext', 'core', '*')) do |file| + require file +end diff --git a/lib/ext/core/object.rb b/lib/ext/core/object.rb new file mode 100644 index 000000000..84959c2dc --- /dev/null +++ b/lib/ext/core/object.rb @@ -0,0 +1,8 @@ +class Object + alias_method :base_to_s, :to_s + + def to_s + res = base_to_s.dup + res.force_encoding(Encoding.default_internal || Encoding::UTF_8) + end +end diff --git a/lib/ext/core/string.rb b/lib/ext/core/string.rb new file mode 100644 index 000000000..1810ec2f7 --- /dev/null +++ b/lib/ext/core/string.rb @@ -0,0 +1,5 @@ +class String + def encode_to_default + force_encoding(Encoding.default_internal || Encoding::UTF_8) + end +end From 70346f00150cc454a324b1dfe3c8039f7e137a13 Mon Sep 17 00:00:00 2001 From: Alexander Machehin Date: Mon, 6 Feb 2012 19:58:27 +0600 Subject: [PATCH 2/2] [refs #151] refactoring --- app/controllers/git/blobs_controller.rb | 13 ++++--------- app/helpers/commit_helper.rb | 4 ++-- app/helpers/git_helper.rb | 4 ++-- app/views/git/blobs/blame.html.haml | 4 ++-- app/views/git/blobs/show.html.haml | 2 +- app/views/git/commits/_commit_diff.html.haml | 6 +++--- config/initializers/core.rb | 2 +- lib/core.rb | 3 --- lib/ext/core/object.rb | 8 -------- 9 files changed, 15 insertions(+), 31 deletions(-) delete mode 100644 lib/core.rb delete mode 100644 lib/ext/core/object.rb diff --git a/app/controllers/git/blobs_controller.rb b/app/controllers/git/blobs_controller.rb index 31a20870b..0bfe6b4b4 100644 --- a/app/controllers/git/blobs_controller.rb +++ b/app/controllers/git/blobs_controller.rb @@ -1,12 +1,10 @@ # -*- encoding : utf-8 -*- class Git::BlobsController < Git::BaseController - before_filter :set_path - before_filter :set_commit_hash before_filter :find_tree + before_filter :set_path_blob + before_filter :set_commit_hash def show - @blob = @tree / @path.force_encoding(Encoding::ASCII_8BIT) - if params[:raw] image_url = Rails.root.to_s + "/" + @path @@ -19,21 +17,18 @@ class Git::BlobsController < Git::BaseController end def blame - @blob = @tree / @path.force_encoding(Encoding::ASCII_8BIT) - @blame = Grit::Blob.blame(@git_repository.repo, @commit.try(:id), @path) end def raw - @blob = @tree / @path.force_encoding(Encoding::ASCII_8BIT) - headers["Content-Disposition"] = %[attachment;filename="#{@blob.name}"] render :text => @blob.data, :content_type => @blob.mime_type end protected - def set_path + def set_path_blob @path = params[:path] + @blob = @tree / @path.encode_to_default end def set_commit_hash diff --git a/app/helpers/commit_helper.rb b/app/helpers/commit_helper.rb index de2c92901..180374610 100644 --- a/app/helpers/commit_helper.rb +++ b/app/helpers/commit_helper.rb @@ -13,7 +13,7 @@ module CommitHelper end res << "" - res.join("\n").force_encoding(Encoding.default_internal || Encoding::UTF_8).html_safe + res.join("\n").encode_to_default.html_safe end # def format_commit_message(message) @@ -34,7 +34,7 @@ module CommitHelper def short_commit_message(message) # Why 42? Because it is the Answer! - truncate(message, :length => 42, :omission => "...").force_encoding(Encoding.default_internal || Encoding::UTF_8) + truncate(message, :length => 42, :omission => "...").encode_to_default end end diff --git a/app/helpers/git_helper.rb b/app/helpers/git_helper.rb index 0cb377b23..f302b5aed 100644 --- a/app/helpers/git_helper.rb +++ b/app/helpers/git_helper.rb @@ -38,7 +38,7 @@ module GitHelper def render_blob(blob) res = "" - blob.data.force_encoding(Encoding.default_internal || Encoding::UTF_8).split("\n").collect do |line| + blob.data.encode_to_default.split("\n").collect do |line| "
#{line.present? ? h(line) : "
"}
" end.join end @@ -50,7 +50,7 @@ module GitHelper end def force_encoding_to_site(string) - string.dup.force_encoding(Encoding.default_internal || Encoding::UTF_8) + string.dup.encode_to_default end end diff --git a/app/views/git/blobs/blame.html.haml b/app/views/git/blobs/blame.html.haml index d6268b0f7..aaf184b15 100644 --- a/app/views/git/blobs/blame.html.haml +++ b/app/views/git/blobs/blame.html.haml @@ -42,7 +42,7 @@ %td.code %pre - %div= elem[1].first.force_encoding(Encoding.default_internal || Encoding::UTF_8) + %div= elem[1].first.encode_to_default - elem[1][1..-1].each do |line| %tr @@ -51,6 +51,6 @@ - index += 1 %td.code %pre - %div= line.force_encoding(Encoding.default_internal || Encoding::UTF_8) + %div= line.encode_to_default - content_for :sidebar, render(:partial => 'git/shared/sidebar') diff --git a/app/views/git/blobs/show.html.haml b/app/views/git/blobs/show.html.haml index c58c41723..43127ac7e 100644 --- a/app/views/git/blobs/show.html.haml +++ b/app/views/git/blobs/show.html.haml @@ -49,7 +49,7 @@ %td.blob :plain
-
#{ link_to @blob.basename.force_encoding(Encoding.default_internal || Encoding::UTF_8), raw_path(@project, @treeish, @path) }
+
#{ link_to @blob.basename.encode_to_default, raw_path(@project, @treeish, @path) }

- content_for :sidebar, render(:partial => 'git/shared/sidebar') diff --git a/app/views/git/commits/_commit_diff.html.haml b/app/views/git/commits/_commit_diff.html.haml index 9ac2922c4..05f68c621 100644 --- a/app/views/git/commits/_commit_diff.html.haml +++ b/app/views/git/commits/_commit_diff.html.haml @@ -2,13 +2,13 @@ .content .inner - %a{ :name => h(commit_diff.a_path.force_encoding(Encoding.default_internal || Encoding::UTF_8)) } + %a{ :name => h(commit_diff.a_path.encode_to_default) } .blob_header - .size= h(commit_diff.a_path.force_encoding(Encoding.default_internal || Encoding::UTF_8)) + .size= h(commit_diff.a_path.encode_to_default) - if commit_diff.b_path.present? .buttons - = link_to("view file @ #{short_hash_id(@commit.id)}", blob_commit_path(@project, @commit.id, commit_diff.b_path.force_encoding(Encoding.default_internal || Encoding::UTF_8))) + = link_to("view file @ #{short_hash_id(@commit.id)}", blob_commit_path(@project, @commit.id, commit_diff.b_path.encode_to_default)) .clear .diff_data diff --git a/config/initializers/core.rb b/config/initializers/core.rb index 7683d8cb2..fe7ee4f48 100644 --- a/config/initializers/core.rb +++ b/config/initializers/core.rb @@ -1 +1 @@ -require './lib/core' +require './lib/ext/core/string' diff --git a/lib/core.rb b/lib/core.rb deleted file mode 100644 index 646cc111d..000000000 --- a/lib/core.rb +++ /dev/null @@ -1,3 +0,0 @@ -Dir.glob(File.join('.', 'lib', 'ext', 'core', '*')) do |file| - require file -end diff --git a/lib/ext/core/object.rb b/lib/ext/core/object.rb deleted file mode 100644 index 84959c2dc..000000000 --- a/lib/ext/core/object.rb +++ /dev/null @@ -1,8 +0,0 @@ -class Object - alias_method :base_to_s, :to_s - - def to_s - res = base_to_s.dup - res.force_encoding(Encoding.default_internal || Encoding::UTF_8) - end -end