annotate .svn/pristine/40/406f7b24c26c10e7c0b7a7dfc884f38ff3b10cd2.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

Merge from branch "live"
author Chris Cannam
date Tue, 09 Sep 2014 09:34:53 +0100
parents cbb26bc654de
children
rev   line source
Chris@909 1 # Redmine - project management software
Chris@909 2 # Copyright (C) 2006-2011 Jean-Philippe Lang
Chris@909 3 #
Chris@909 4 # This program is free software; you can redistribute it and/or
Chris@909 5 # modify it under the terms of the GNU General Public License
Chris@909 6 # as published by the Free Software Foundation; either version 2
Chris@909 7 # of the License, or (at your option) any later version.
Chris@909 8 #
Chris@909 9 # This program is distributed in the hope that it will be useful,
Chris@909 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@909 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@909 12 # GNU General Public License for more details.
Chris@909 13 #
Chris@909 14 # You should have received a copy of the GNU General Public License
Chris@909 15 # along with this program; if not, write to the Free Software
Chris@909 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@909 17
Chris@909 18 require 'SVG/Graph/Bar'
Chris@909 19 require 'SVG/Graph/BarHorizontal'
Chris@909 20 require 'digest/sha1'
Chris@909 21
Chris@909 22 class ChangesetNotFound < Exception; end
Chris@909 23 class InvalidRevisionParam < Exception; end
Chris@909 24
Chris@909 25 class RepositoriesController < ApplicationController
Chris@909 26 menu_item :repository
Chris@909 27 menu_item :settings, :only => :edit
Chris@909 28 default_search_scope :changesets
Chris@909 29
Chris@909 30 before_filter :find_repository, :except => :edit
Chris@909 31 before_filter :find_project, :only => :edit
Chris@909 32 before_filter :authorize
Chris@909 33 accept_rss_auth :revisions
Chris@909 34
Chris@909 35 rescue_from Redmine::Scm::Adapters::CommandFailed, :with => :show_error_command_failed
Chris@909 36
Chris@909 37 def edit
Chris@909 38 @repository = @project.repository
Chris@909 39 if !@repository && !params[:repository_scm].blank?
Chris@909 40 @repository = Repository.factory(params[:repository_scm])
Chris@909 41 @repository.project = @project if @repository
Chris@909 42 end
Chris@909 43 if request.post? && @repository
Chris@909 44 p1 = params[:repository]
Chris@909 45 p = {}
Chris@909 46 p_extra = {}
Chris@909 47 p1.each do |k, v|
Chris@909 48 if k =~ /^extra_/
Chris@909 49 p_extra[k] = v
Chris@909 50 else
Chris@909 51 p[k] = v
Chris@909 52 end
Chris@909 53 end
Chris@909 54 @repository.attributes = p
Chris@909 55 @repository.merge_extra_info(p_extra)
Chris@909 56 @repository.save
Chris@909 57 end
Chris@909 58 render(:update) do |page|
Chris@909 59 page.replace_html "tab-content-repository",
Chris@909 60 :partial => 'projects/settings/repository'
Chris@909 61 if @repository && !@project.repository
Chris@909 62 @project.reload # needed to reload association
Chris@909 63 page.replace_html "main-menu", render_main_menu(@project)
Chris@909 64 end
Chris@909 65 end
Chris@909 66 end
Chris@909 67
Chris@909 68 def committers
Chris@909 69 @committers = @repository.committers
Chris@909 70 @users = @project.users
Chris@909 71 additional_user_ids = @committers.collect(&:last).collect(&:to_i) - @users.collect(&:id)
Chris@909 72 @users += User.find_all_by_id(additional_user_ids) unless additional_user_ids.empty?
Chris@909 73 @users.compact!
Chris@909 74 @users.sort!
Chris@909 75 if request.post? && params[:committers].is_a?(Hash)
Chris@909 76 # Build a hash with repository usernames as keys and corresponding user ids as values
Chris@909 77 @repository.committer_ids = params[:committers].values.inject({}) {|h, c| h[c.first] = c.last; h}
Chris@909 78 flash[:notice] = l(:notice_successful_update)
Chris@909 79 redirect_to :action => 'committers', :id => @project
Chris@909 80 end
Chris@909 81 end
Chris@909 82
Chris@909 83 def destroy
Chris@909 84 @repository.destroy
Chris@909 85 redirect_to :controller => 'projects',
Chris@909 86 :action => 'settings',
Chris@909 87 :id => @project,
Chris@909 88 :tab => 'repository'
Chris@909 89 end
Chris@909 90
Chris@909 91 def show
Chris@909 92 @repository.fetch_changesets if Setting.autofetch_changesets? && @path.empty?
Chris@909 93
Chris@909 94 @entries = @repository.entries(@path, @rev)
Chris@909 95 @changeset = @repository.find_changeset_by_name(@rev)
Chris@909 96 if request.xhr?
Chris@909 97 @entries ? render(:partial => 'dir_list_content') : render(:nothing => true)
Chris@909 98 else
Chris@909 99 (show_error_not_found; return) unless @entries
Chris@909 100 @changesets = @repository.latest_changesets(@path, @rev)
Chris@909 101 @properties = @repository.properties(@path, @rev)
Chris@909 102 render :action => 'show'
Chris@909 103 end
Chris@909 104 end
Chris@909 105
Chris@909 106 alias_method :browse, :show
Chris@909 107
Chris@909 108 def changes
Chris@909 109 @entry = @repository.entry(@path, @rev)
Chris@909 110 (show_error_not_found; return) unless @entry
Chris@909 111 @changesets = @repository.latest_changesets(@path, @rev, Setting.repository_log_display_limit.to_i)
Chris@909 112 @properties = @repository.properties(@path, @rev)
Chris@909 113 @changeset = @repository.find_changeset_by_name(@rev)
Chris@909 114 end
Chris@909 115
Chris@909 116 def revisions
Chris@909 117 @changeset_count = @repository.changesets.count
Chris@909 118 @changeset_pages = Paginator.new self, @changeset_count,
Chris@909 119 per_page_option,
Chris@909 120 params['page']
Chris@909 121 @changesets = @repository.changesets.find(:all,
Chris@909 122 :limit => @changeset_pages.items_per_page,
Chris@909 123 :offset => @changeset_pages.current.offset,
Chris@909 124 :include => [:user, :repository, :parents])
Chris@909 125
Chris@909 126 respond_to do |format|
Chris@909 127 format.html { render :layout => false if request.xhr? }
Chris@909 128 format.atom { render_feed(@changesets, :title => "#{@project.name}: #{l(:label_revision_plural)}") }
Chris@909 129 end
Chris@909 130 end
Chris@909 131
Chris@909 132 def entry
Chris@909 133 @entry = @repository.entry(@path, @rev)
Chris@909 134 (show_error_not_found; return) unless @entry
Chris@909 135
Chris@909 136 # If the entry is a dir, show the browser
Chris@909 137 (show; return) if @entry.is_dir?
Chris@909 138
Chris@909 139 @content = @repository.cat(@path, @rev)
Chris@909 140 (show_error_not_found; return) unless @content
Chris@909 141 if 'raw' == params[:format] ||
Chris@909 142 (@content.size && @content.size > Setting.file_max_size_displayed.to_i.kilobyte) ||
Chris@909 143 ! is_entry_text_data?(@content, @path)
Chris@909 144 # Force the download
Chris@909 145 send_opt = { :filename => filename_for_content_disposition(@path.split('/').last) }
Chris@909 146 send_type = Redmine::MimeType.of(@path)
Chris@909 147 send_opt[:type] = send_type.to_s if send_type
Chris@909 148 send_data @content, send_opt
Chris@909 149 else
Chris@909 150 # Prevent empty lines when displaying a file with Windows style eol
Chris@909 151 # TODO: UTF-16
Chris@909 152 # Is this needs? AttachmentsController reads file simply.
Chris@909 153 @content.gsub!("\r\n", "\n")
Chris@909 154 @changeset = @repository.find_changeset_by_name(@rev)
Chris@909 155 end
Chris@909 156 end
Chris@909 157
Chris@909 158 def is_entry_text_data?(ent, path)
Chris@909 159 # UTF-16 contains "\x00".
Chris@909 160 # It is very strict that file contains less than 30% of ascii symbols
Chris@909 161 # in non Western Europe.
Chris@909 162 return true if Redmine::MimeType.is_type?('text', path)
Chris@909 163 # Ruby 1.8.6 has a bug of integer divisions.
Chris@909 164 # http://apidock.com/ruby/v1_8_6_287/String/is_binary_data%3F
Chris@909 165 return false if ent.is_binary_data?
Chris@909 166 true
Chris@909 167 end
Chris@909 168 private :is_entry_text_data?
Chris@909 169
Chris@909 170 def annotate
Chris@909 171 @entry = @repository.entry(@path, @rev)
Chris@909 172 (show_error_not_found; return) unless @entry
Chris@909 173
Chris@909 174 @annotate = @repository.scm.annotate(@path, @rev)
Chris@909 175 if @annotate.nil? || @annotate.empty?
Chris@909 176 (render_error l(:error_scm_annotate); return)
Chris@909 177 end
Chris@909 178 ann_buf_size = 0
Chris@909 179 @annotate.lines.each do |buf|
Chris@909 180 ann_buf_size += buf.size
Chris@909 181 end
Chris@909 182 if ann_buf_size > Setting.file_max_size_displayed.to_i.kilobyte
Chris@909 183 (render_error l(:error_scm_annotate_big_text_file); return)
Chris@909 184 end
Chris@909 185 @changeset = @repository.find_changeset_by_name(@rev)
Chris@909 186 end
Chris@909 187
Chris@909 188 def revision
Chris@909 189 raise ChangesetNotFound if @rev.blank?
Chris@909 190 @changeset = @repository.find_changeset_by_name(@rev)
Chris@909 191 raise ChangesetNotFound unless @changeset
Chris@909 192
Chris@909 193 respond_to do |format|
Chris@909 194 format.html
Chris@909 195 format.js {render :layout => false}
Chris@909 196 end
Chris@909 197 rescue ChangesetNotFound
Chris@909 198 show_error_not_found
Chris@909 199 end
Chris@909 200
Chris@909 201 def diff
Chris@909 202 if params[:format] == 'diff'
Chris@909 203 @diff = @repository.diff(@path, @rev, @rev_to)
Chris@909 204 (show_error_not_found; return) unless @diff
Chris@909 205 filename = "changeset_r#{@rev}"
Chris@909 206 filename << "_r#{@rev_to}" if @rev_to
Chris@909 207 send_data @diff.join, :filename => "#{filename}.diff",
Chris@909 208 :type => 'text/x-patch',
Chris@909 209 :disposition => 'attachment'
Chris@909 210 else
Chris@909 211 @diff_type = params[:type] || User.current.pref[:diff_type] || 'inline'
Chris@909 212 @diff_type = 'inline' unless %w(inline sbs).include?(@diff_type)
Chris@909 213
Chris@909 214 # Save diff type as user preference
Chris@909 215 if User.current.logged? && @diff_type != User.current.pref[:diff_type]
Chris@909 216 User.current.pref[:diff_type] = @diff_type
Chris@909 217 User.current.preference.save
Chris@909 218 end
Chris@909 219 @cache_key = "repositories/diff/#{@repository.id}/" +
Chris@909 220 Digest::MD5.hexdigest("#{@path}-#{@rev}-#{@rev_to}-#{@diff_type}-#{current_language}")
Chris@909 221 unless read_fragment(@cache_key)
Chris@909 222 @diff = @repository.diff(@path, @rev, @rev_to)
Chris@909 223 show_error_not_found unless @diff
Chris@909 224 end
Chris@909 225
Chris@909 226 @changeset = @repository.find_changeset_by_name(@rev)
Chris@909 227 @changeset_to = @rev_to ? @repository.find_changeset_by_name(@rev_to) : nil
Chris@909 228 @diff_format_revisions = @repository.diff_format_revisions(@changeset, @changeset_to)
Chris@909 229 end
Chris@909 230 end
Chris@909 231
Chris@909 232 def stats
Chris@909 233 end
Chris@909 234
Chris@909 235 def graph
Chris@909 236 data = nil
Chris@909 237 case params[:graph]
Chris@909 238 when "commits_per_month"
Chris@909 239 data = graph_commits_per_month(@repository)
Chris@909 240 when "commits_per_author"
Chris@909 241 data = graph_commits_per_author(@repository)
Chris@909 242 end
Chris@909 243 if data
Chris@909 244 headers["Content-Type"] = "image/svg+xml"
Chris@909 245 send_data(data, :type => "image/svg+xml", :disposition => "inline")
Chris@909 246 else
Chris@909 247 render_404
Chris@909 248 end
Chris@909 249 end
Chris@909 250
Chris@909 251 private
Chris@909 252
Chris@909 253 REV_PARAM_RE = %r{\A[a-f0-9]*\Z}i
Chris@909 254
Chris@909 255 def find_repository
Chris@909 256 @project = Project.find(params[:id])
Chris@909 257 @repository = @project.repository
Chris@909 258 (render_404; return false) unless @repository
Chris@909 259 @path = params[:path].join('/') unless params[:path].nil?
Chris@909 260 @path ||= ''
Chris@909 261 @rev = params[:rev].blank? ? @repository.default_branch : params[:rev].to_s.strip
Chris@909 262 @rev_to = params[:rev_to]
Chris@909 263
Chris@909 264 unless @rev.to_s.match(REV_PARAM_RE) && @rev_to.to_s.match(REV_PARAM_RE)
Chris@909 265 if @repository.branches.blank?
Chris@909 266 raise InvalidRevisionParam
Chris@909 267 end
Chris@909 268 end
Chris@909 269 rescue ActiveRecord::RecordNotFound
Chris@909 270 render_404
Chris@909 271 rescue InvalidRevisionParam
Chris@909 272 show_error_not_found
Chris@909 273 end
Chris@909 274
Chris@909 275 def show_error_not_found
Chris@909 276 render_error :message => l(:error_scm_not_found), :status => 404
Chris@909 277 end
Chris@909 278
Chris@909 279 # Handler for Redmine::Scm::Adapters::CommandFailed exception
Chris@909 280 def show_error_command_failed(exception)
Chris@909 281 render_error l(:error_scm_command_failed, exception.message)
Chris@909 282 end
Chris@909 283
Chris@909 284 def graph_commits_per_month(repository)
Chris@909 285 @date_to = Date.today
Chris@909 286 @date_from = @date_to << 11
Chris@909 287 @date_from = Date.civil(@date_from.year, @date_from.month, 1)
Chris@909 288 commits_by_day = repository.changesets.count(
Chris@909 289 :all, :group => :commit_date,
Chris@909 290 :conditions => ["commit_date BETWEEN ? AND ?", @date_from, @date_to])
Chris@909 291 commits_by_month = [0] * 12
Chris@909 292 commits_by_day.each {|c| commits_by_month[c.first.to_date.months_ago] += c.last }
Chris@909 293
Chris@909 294 changes_by_day = repository.changes.count(
Chris@909 295 :all, :group => :commit_date,
Chris@909 296 :conditions => ["commit_date BETWEEN ? AND ?", @date_from, @date_to])
Chris@909 297 changes_by_month = [0] * 12
Chris@909 298 changes_by_day.each {|c| changes_by_month[c.first.to_date.months_ago] += c.last }
Chris@909 299
Chris@909 300 fields = []
Chris@909 301 12.times {|m| fields << month_name(((Date.today.month - 1 - m) % 12) + 1)}
Chris@909 302
Chris@909 303 graph = SVG::Graph::Bar.new(
Chris@909 304 :height => 300,
Chris@909 305 :width => 800,
Chris@909 306 :fields => fields.reverse,
Chris@909 307 :stack => :side,
Chris@909 308 :scale_integers => true,
Chris@909 309 :step_x_labels => 2,
Chris@909 310 :show_data_values => false,
Chris@909 311 :graph_title => l(:label_commits_per_month),
Chris@909 312 :show_graph_title => true
Chris@909 313 )
Chris@909 314
Chris@909 315 graph.add_data(
Chris@909 316 :data => commits_by_month[0..11].reverse,
Chris@909 317 :title => l(:label_revision_plural)
Chris@909 318 )
Chris@909 319
Chris@909 320 graph.add_data(
Chris@909 321 :data => changes_by_month[0..11].reverse,
Chris@909 322 :title => l(:label_change_plural)
Chris@909 323 )
Chris@909 324
Chris@909 325 graph.burn
Chris@909 326 end
Chris@909 327
Chris@909 328 def graph_commits_per_author(repository)
Chris@909 329 commits_by_author = repository.changesets.count(:all, :group => :committer)
Chris@909 330 commits_by_author.to_a.sort! {|x, y| x.last <=> y.last}
Chris@909 331
Chris@909 332 changes_by_author = repository.changes.count(:all, :group => :committer)
Chris@909 333 h = changes_by_author.inject({}) {|o, i| o[i.first] = i.last; o}
Chris@909 334
Chris@909 335 fields = commits_by_author.collect {|r| r.first}
Chris@909 336 commits_data = commits_by_author.collect {|r| r.last}
Chris@909 337 changes_data = commits_by_author.collect {|r| h[r.first] || 0}
Chris@909 338
Chris@909 339 fields = fields + [""]*(10 - fields.length) if fields.length<10
Chris@909 340 commits_data = commits_data + [0]*(10 - commits_data.length) if commits_data.length<10
Chris@909 341 changes_data = changes_data + [0]*(10 - changes_data.length) if changes_data.length<10
Chris@909 342
Chris@909 343 # Remove email adress in usernames
Chris@909 344 fields = fields.collect {|c| c.gsub(%r{<.+@.+>}, '') }
Chris@909 345
Chris@909 346 graph = SVG::Graph::BarHorizontal.new(
Chris@909 347 :height => 400,
Chris@909 348 :width => 800,
Chris@909 349 :fields => fields,
Chris@909 350 :stack => :side,
Chris@909 351 :scale_integers => true,
Chris@909 352 :show_data_values => false,
Chris@909 353 :rotate_y_labels => false,
Chris@909 354 :graph_title => l(:label_commits_per_author),
Chris@909 355 :show_graph_title => true
Chris@909 356 )
Chris@909 357 graph.add_data(
Chris@909 358 :data => commits_data,
Chris@909 359 :title => l(:label_revision_plural)
Chris@909 360 )
Chris@909 361 graph.add_data(
Chris@909 362 :data => changes_data,
Chris@909 363 :title => l(:label_change_plural)
Chris@909 364 )
Chris@909 365 graph.burn
Chris@909 366 end
Chris@909 367 end
Chris@909 368
Chris@909 369 class Date
Chris@909 370 def months_ago(date = Date.today)
Chris@909 371 (date.year - self.year)*12 + (date.month - self.month)
Chris@909 372 end
Chris@909 373
Chris@909 374 def weeks_ago(date = Date.today)
Chris@909 375 (date.year - self.year)*52 + (date.cweek - self.cweek)
Chris@909 376 end
Chris@909 377 end
Chris@909 378
Chris@909 379 class String
Chris@909 380 def with_leading_slash
Chris@909 381 starts_with?('/') ? self : "/#{self}"
Chris@909 382 end
Chris@909 383 end