Commit d72f8db08a46cd03879035932ece2b4295bb3948

Authored by randx
2 parents d29ec9d7 11f90ae4

Merge branch 'fix_encoding' of https://github.com/brodock/gitlabhq into brodock-fix_encoding

Conflicts:
	lib/gitlab/encode.rb
app/controllers/refs_controller.rb
1 class RefsController < ApplicationController 1 class RefsController < ApplicationController
  2 + include Gitlabhq::Encode
2 before_filter :project 3 before_filter :project
3 4
4 # Authorize 5 # Authorize
@@ -43,23 +44,26 @@ class RefsController &lt; ApplicationController @@ -43,23 +44,26 @@ class RefsController &lt; ApplicationController
43 no_cache_headers 44 no_cache_headers
44 end 45 end
45 end 46 end
46 - rescue  
47 - return render_404  
48 end 47 end
49 48
50 def blob 49 def blob
51 if @tree.is_blob? 50 if @tree.is_blob?
  51 + if @tree.text?
  52 + encoding = detect_encoding(@tree.data)
  53 + mime_type = encoding ? "text/plain; charset=#{encoding}" : "text/plain"
  54 + else
  55 + mime_type = @tree.mime_type
  56 + end
  57 +
52 send_data( 58 send_data(
53 @tree.data, 59 @tree.data,
54 - :type => @tree.text? ? "text/plain" : @tree.mime_type, 60 + :type => mime_type,
55 :disposition => 'inline', 61 :disposition => 'inline',
56 :filename => @tree.name 62 :filename => @tree.name
57 ) 63 )
58 else 64 else
59 head(404) 65 head(404)
60 end 66 end
61 - rescue  
62 - return render_404  
63 end 67 end
64 68
65 def blame 69 def blame
app/views/commits/_commit.html.haml
@@ -8,7 +8,7 @@ @@ -8,7 +8,7 @@
8 %strong.cgray= commit.author_name 8 %strong.cgray= commit.author_name
9 &ndash; 9 &ndash;
10 = image_tag gravatar_icon(commit.author_email), :class => "avatar", :width => 16 10 = image_tag gravatar_icon(commit.author_email), :class => "avatar", :width => 16
11 - %span.row_title= truncate(commit.safe_message, :length => 50) rescue "--broken encoding" 11 + %span.row_title= truncate(commit.safe_message, :length => 50)
12 12
13 %span.right.cgray 13 %span.right.cgray
14 = time_ago_in_words(commit.committed_date) 14 = time_ago_in_words(commit.committed_date)
app/views/refs/_tree.html.haml
@@ -42,9 +42,9 @@ @@ -42,9 +42,9 @@
42 .readme 42 .readme
43 - if content.name =~ /\.(md|markdown)$/i 43 - if content.name =~ /\.(md|markdown)$/i
44 = preserve do 44 = preserve do
45 - = markdown(content.data.force_encoding('UTF-8')) 45 + = markdown(content.data.detect_encoding!)
46 - else 46 - else
47 - = simple_format(content.data.force_encoding('UTF-8')) 47 + = simple_format(content.data.detect_encoding!)
48 48
49 - if params[:path] 49 - if params[:path]
50 - history_path = tree_file_project_ref_path(@project, @ref, params[:path]) 50 - history_path = tree_file_project_ref_path(@project, @ref, params[:path])
app/views/refs/_tree_file.html.haml
@@ -13,7 +13,7 @@ @@ -13,7 +13,7 @@
13 #tree-readme-holder 13 #tree-readme-holder
14 .readme 14 .readme
15 = preserve do 15 = preserve do
16 - = markdown(file.data.force_encoding('UTF-8')) 16 + = markdown(file.data.detect_encoding!)
17 - else 17 - else
18 .view_file_content 18 .view_file_content
19 - unless file.empty? 19 - unless file.empty?
lib/gitlab/encode.rb
  1 +# Patch Strings to enable detect_encoding! on views
  2 +require 'charlock_holmes/string'
1 module Gitlab 3 module Gitlab
2 module Encode 4 module Encode
3 extend self 5 extend self
@@ -5,16 +7,26 @@ module Gitlab @@ -5,16 +7,26 @@ module Gitlab
5 def utf8 message 7 def utf8 message
6 return nil unless message 8 return nil unless message
7 9
8 - hash = CharlockHolmes::EncodingDetector.detect(message) rescue {}  
9 - if hash[:encoding]  
10 - CharlockHolmes::Converter.convert(message, hash[:encoding], 'UTF-8') 10 + detect = CharlockHolmes::EncodingDetector.detect(message) rescue {}
  11 +
  12 + # It's better to default to UTF-8 as sometimes it's wrongly detected as another charset
  13 + if detect[:encoding] && detect[:confidence] == 100
  14 + CharlockHolmes::Converter.convert(message, detect[:encoding], 'UTF-8')
11 else 15 else
12 message 16 message
13 end.force_encoding("utf-8") 17 end.force_encoding("utf-8")
  18 +
14 # Prevent app from crash cause of 19 # Prevent app from crash cause of
15 # encoding errors 20 # encoding errors
16 rescue 21 rescue
17 - "" 22 + "--broken encoding: #{encoding}"
  23 + end
  24 +
  25 + def detect_encoding message
  26 + return nil unless message
  27 +
  28 + hash = CharlockHolmes::EncodingDetector.detect(message) rescue {}
  29 + return hash[:encoding] ? hash[:encoding] : nil
18 end 30 end
19 end 31 end
20 end 32 end