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 1 class RefsController < ApplicationController
  2 + include Gitlabhq::Encode
2 3 before_filter :project
3 4  
4 5 # Authorize
... ... @@ -43,23 +44,26 @@ class RefsController &lt; ApplicationController
43 44 no_cache_headers
44 45 end
45 46 end
46   - rescue
47   - return render_404
48 47 end
49 48  
50 49 def blob
51 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 58 send_data(
53 59 @tree.data,
54   - :type => @tree.text? ? "text/plain" : @tree.mime_type,
  60 + :type => mime_type,
55 61 :disposition => 'inline',
56 62 :filename => @tree.name
57 63 )
58 64 else
59 65 head(404)
60 66 end
61   - rescue
62   - return render_404
63 67 end
64 68  
65 69 def blame
... ...
app/views/commits/_commit.html.haml
... ... @@ -8,7 +8,7 @@
8 8 %strong.cgray= commit.author_name
9 9 &ndash;
10 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 13 %span.right.cgray
14 14 = time_ago_in_words(commit.committed_date)
... ...
app/views/refs/_tree.html.haml
... ... @@ -42,9 +42,9 @@
42 42 .readme
43 43 - if content.name =~ /\.(md|markdown)$/i
44 44 = preserve do
45   - = markdown(content.data.force_encoding('UTF-8'))
  45 + = markdown(content.data.detect_encoding!)
46 46 - else
47   - = simple_format(content.data.force_encoding('UTF-8'))
  47 + = simple_format(content.data.detect_encoding!)
48 48  
49 49 - if params[:path]
50 50 - history_path = tree_file_project_ref_path(@project, @ref, params[:path])
... ...
app/views/refs/_tree_file.html.haml
... ... @@ -13,7 +13,7 @@
13 13 #tree-readme-holder
14 14 .readme
15 15 = preserve do
16   - = markdown(file.data.force_encoding('UTF-8'))
  16 + = markdown(file.data.detect_encoding!)
17 17 - else
18 18 .view_file_content
19 19 - unless file.empty?
... ...
lib/gitlab/encode.rb
  1 +# Patch Strings to enable detect_encoding! on views
  2 +require 'charlock_holmes/string'
1 3 module Gitlab
2 4 module Encode
3 5 extend self
... ... @@ -5,16 +7,26 @@ module Gitlab
5 7 def utf8 message
6 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 15 else
12 16 message
13 17 end.force_encoding("utf-8")
  18 +
14 19 # Prevent app from crash cause of
15 20 # encoding errors
16 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 30 end
19 31 end
20 32 end
... ...