annotate app/controllers/repositories_controller.rb @ 1516:b450a9d58aed redmine-2.4

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