diff app/controllers/repositories_controller.rb @ 441:cbce1fd3b1b7 redmine-1.2

Update to Redmine 1.2-stable branch (Redmine SVN rev 6000)
author Chris Cannam
date Mon, 06 Jun 2011 14:24:13 +0100
parents 051f544170fe
children 753f1380d6bc 0c939c159af4
line wrap: on
line diff
--- a/app/controllers/repositories_controller.rb	Thu Mar 03 11:42:28 2011 +0000
+++ b/app/controllers/repositories_controller.rb	Mon Jun 06 14:24:13 2011 +0100
@@ -1,16 +1,16 @@
 # Redmine - project management software
-# Copyright (C) 2006-2009  Jean-Philippe Lang
+# Copyright (C) 2006-2011  Jean-Philippe Lang
 #
 # This program is free software; you can redistribute it and/or
 # modify it under the terms of the GNU General Public License
 # as published by the Free Software Foundation; either version 2
 # of the License, or (at your option) any later version.
-# 
+#
 # This program is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 # GNU General Public License for more details.
-# 
+#
 # You should have received a copy of the GNU General Public License
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
@@ -26,33 +26,45 @@
   menu_item :repository
   menu_item :settings, :only => :edit
   default_search_scope :changesets
-  
+
   before_filter :find_repository, :except => :edit
   before_filter :find_project, :only => :edit
   before_filter :authorize
   accept_key_auth :revisions
-  
+
   rescue_from Redmine::Scm::Adapters::CommandFailed, :with => :show_error_command_failed
-  
+
   def edit
     @repository = @project.repository
-    if !@repository
+    if !@repository && !params[:repository_scm].blank?
       @repository = Repository.factory(params[:repository_scm])
       @repository.project = @project if @repository
     end
     if request.post? && @repository
-      @repository.attributes = params[:repository]
+      p1 = params[:repository]
+      p       = {}
+      p_extra = {}
+      p1.each do |k, v|
+        if k =~ /^extra_/
+          p_extra[k] = v
+        else
+          p[k] = v
+        end
+      end
+      @repository.attributes = p
+      @repository.merge_extra_info(p_extra)
       @repository.save
     end
     render(:update) do |page|
-      page.replace_html "tab-content-repository", :partial => 'projects/settings/repository'
+      page.replace_html "tab-content-repository",
+                        :partial => 'projects/settings/repository'
       if @repository && !@project.repository
-        @project.reload #needed to reload association
+        @project.reload # needed to reload association
         page.replace_html "main-menu", render_main_menu(@project)
       end
     end
   end
-  
+
   def committers
     @committers = @repository.committers
     @users = @project.users
@@ -70,13 +82,17 @@
 
   def destroy
     @repository.destroy
-    redirect_to :controller => 'projects', :action => 'settings', :id => @project, :tab => 'repository'
+    redirect_to :controller => 'projects',
+                :action     => 'settings',
+                :id         => @project,
+                :tab        => 'repository'
   end
 
   def show
     @repository.fetch_changesets if Setting.autofetch_changesets? && @path.empty?
 
     @entries = @repository.entries(@path, @rev)
+    @changeset = @repository.find_changeset_by_name(@rev)
     if request.xhr?
       @entries ? render(:partial => 'dir_list_content') : render(:nothing => true)
     else
@@ -122,17 +138,35 @@
 
     @content = @repository.cat(@path, @rev)
     (show_error_not_found; return) unless @content
-    if 'raw' == params[:format] || @content.is_binary_data? ||
-         (@entry.size && @entry.size > Setting.file_max_size_displayed.to_i.kilobyte)
+    if 'raw' == params[:format] ||
+         (@content.size && @content.size > Setting.file_max_size_displayed.to_i.kilobyte) ||
+         ! is_entry_text_data?(@content, @path)
       # Force the download
-      send_data @content, :filename => filename_for_content_disposition(@path.split('/').last)
+      send_opt = { :filename => filename_for_content_disposition(@path.split('/').last) }
+      send_type = Redmine::MimeType.of(@path)
+      send_opt[:type] = send_type.to_s if send_type
+      send_data @content, send_opt
     else
       # Prevent empty lines when displaying a file with Windows style eol
+      # TODO: UTF-16
+      # Is this needs? AttachmentsController reads file simply.
       @content.gsub!("\r\n", "\n")
       @changeset = @repository.find_changeset_by_name(@rev)
-   end
+    end
   end
 
+  def is_entry_text_data?(ent, path)
+    # UTF-16 contains "\x00".
+    # It is very strict that file contains less than 30% of ascii symbols
+    # in non Western Europe.
+    return true if Redmine::MimeType.is_type?('text', path)
+    # Ruby 1.8.6 has a bug of integer divisions.
+    # http://apidock.com/ruby/v1_8_6_287/String/is_binary_data%3F
+    return false if ent.is_binary_data?
+    true
+  end
+  private :is_entry_text_data?
+
   def annotate
     @entry = @repository.entry(@path, @rev)
     (show_error_not_found; return) unless @entry
@@ -167,14 +201,14 @@
     else
       @diff_type = params[:type] || User.current.pref[:diff_type] || 'inline'
       @diff_type = 'inline' unless %w(inline sbs).include?(@diff_type)
-      
+
       # Save diff type as user preference
       if User.current.logged? && @diff_type != User.current.pref[:diff_type]
         User.current.pref[:diff_type] = @diff_type
         User.current.preference.save
       end
-      
-      @cache_key = "repositories/diff/#{@repository.id}/" + Digest::MD5.hexdigest("#{@path}-#{@rev}-#{@rev_to}-#{@diff_type}")    
+      @cache_key = "repositories/diff/#{@repository.id}/" + 
+                      Digest::MD5.hexdigest("#{@path}-#{@rev}-#{@rev_to}-#{@diff_type}")
       unless read_fragment(@cache_key)
         @diff = @repository.diff(@path, @rev, @rev_to)
         show_error_not_found unless @diff
@@ -204,7 +238,7 @@
       render_404
     end
   end
-  
+
   private
 
   REV_PARAM_RE = %r{\A[a-f0-9]*\Z}i
@@ -217,7 +251,7 @@
     @path ||= ''
     @rev = params[:rev].blank? ? @repository.default_branch : params[:rev].strip
     @rev_to = params[:rev_to]
-    
+
     unless @rev.to_s.match(REV_PARAM_RE) && @rev_to.to_s.match(REV_PARAM_RE)
       if @repository.branches.blank?
         raise InvalidRevisionParam
@@ -232,27 +266,31 @@
   def show_error_not_found
     render_error :message => l(:error_scm_not_found), :status => 404
   end
-  
+
   # Handler for Redmine::Scm::Adapters::CommandFailed exception
   def show_error_command_failed(exception)
     render_error l(:error_scm_command_failed, exception.message)
   end
-  
+
   def graph_commits_per_month(repository)
     @date_to = Date.today
     @date_from = @date_to << 11
     @date_from = Date.civil(@date_from.year, @date_from.month, 1)
-    commits_by_day = repository.changesets.count(:all, :group => :commit_date, :conditions => ["commit_date BETWEEN ? AND ?", @date_from, @date_to])
+    commits_by_day = repository.changesets.count(
+                          :all, :group => :commit_date,
+                          :conditions => ["commit_date BETWEEN ? AND ?", @date_from, @date_to])
     commits_by_month = [0] * 12
     commits_by_day.each {|c| commits_by_month[c.first.to_date.months_ago] += c.last }
 
-    changes_by_day = repository.changes.count(:all, :group => :commit_date, :conditions => ["commit_date BETWEEN ? AND ?", @date_from, @date_to])
+    changes_by_day = repository.changes.count(
+                          :all, :group => :commit_date,
+                          :conditions => ["commit_date BETWEEN ? AND ?", @date_from, @date_to])
     changes_by_month = [0] * 12
     changes_by_day.each {|c| changes_by_month[c.first.to_date.months_ago] += c.last }
-   
+
     fields = []
     12.times {|m| fields << month_name(((Date.today.month - 1 - m) % 12) + 1)}
-  
+
     graph = SVG::Graph::Bar.new(
       :height => 300,
       :width => 800,
@@ -264,7 +302,7 @@
       :graph_title => l(:label_commits_per_month),
       :show_graph_title => true
     )
-    
+
     graph.add_data(
       :data => commits_by_month[0..11].reverse,
       :title => l(:label_revision_plural)
@@ -274,7 +312,7 @@
       :data => changes_by_month[0..11].reverse,
       :title => l(:label_change_plural)
     )
-    
+
     graph.burn
   end
 
@@ -284,18 +322,18 @@
 
     changes_by_author = repository.changes.count(:all, :group => :committer)
     h = changes_by_author.inject({}) {|o, i| o[i.first] = i.last; o}
-    
+
     fields = commits_by_author.collect {|r| r.first}
     commits_data = commits_by_author.collect {|r| r.last}
     changes_data = commits_by_author.collect {|r| h[r.first] || 0}
-    
+
     fields = fields + [""]*(10 - fields.length) if fields.length<10
     commits_data = commits_data + [0]*(10 - commits_data.length) if commits_data.length<10
     changes_data = changes_data + [0]*(10 - changes_data.length) if changes_data.length<10
-    
+
     # Remove email adress in usernames
     fields = fields.collect {|c| c.gsub(%r{<.+@.+>}, '') }
-    
+
     graph = SVG::Graph::BarHorizontal.new(
       :height => 400,
       :width => 800,
@@ -307,22 +345,18 @@
       :graph_title => l(:label_commits_per_author),
       :show_graph_title => true
     )
-    
     graph.add_data(
       :data => commits_data,
       :title => l(:label_revision_plural)
     )
-
     graph.add_data(
       :data => changes_data,
       :title => l(:label_change_plural)
     )
-       
     graph.burn
   end
+end
 
-end
-  
 class Date
   def months_ago(date = Date.today)
     (date.year - self.year)*12 + (date.month - self.month)