annotate app/controllers/repositories_controller.rb @ 1298:4f746d8966dd redmine_2.3_integration

Merge from redmine-2.3 branch to create new branch redmine-2.3-integration
author Chris Cannam
date Fri, 14 Jun 2013 09:28:30 +0100
parents bb32da3bea34 622f24f53b42
children
rev   line source
Chris@0 1 # Redmine - project management software
Chris@1295 2 # Copyright (C) 2006-2013 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@1115 21 require 'redmine/scm/adapters/abstract_adapter'
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@1116 66 params[:repository_scm]='Mercurial'
Chris@1115 67 attrs = pickup_extra_info
Chris@1115 68 @repository.safe_attributes = attrs[:attrs]
Chris@1115 69 if attrs[:attrs_extra].keys.any?
Chris@1115 70 @repository.merge_extra_info(attrs[:attrs_extra])
Chris@0 71 end
Chris@1115 72 @repository.project = @project
Chris@1115 73 if request.put? && @repository.save
Chris@1115 74 redirect_to settings_project_path(@project, :tab => 'repositories')
Chris@1115 75 else
Chris@1115 76 render :action => 'edit'
Chris@0 77 end
Chris@1115 78 end
Chris@1115 79
Chris@1115 80 def pickup_extra_info
Chris@1115 81 p = {}
Chris@1115 82 p_extra = {}
Chris@1115 83 params[:repository].each do |k, v|
Chris@1115 84 if k =~ /^extra_/
Chris@1115 85 p_extra[k] = v
Chris@1115 86 else
Chris@1115 87 p[k] = v
Chris@0 88 end
Chris@0 89 end
Chris@1115 90 {:attrs => p, :attrs_extra => p_extra}
Chris@0 91 end
Chris@1115 92 private :pickup_extra_info
Chris@441 93
Chris@0 94 def committers
Chris@0 95 @committers = @repository.committers
Chris@0 96 @users = @project.users
Chris@0 97 additional_user_ids = @committers.collect(&:last).collect(&:to_i) - @users.collect(&:id)
Chris@0 98 @users += User.find_all_by_id(additional_user_ids) unless additional_user_ids.empty?
Chris@0 99 @users.compact!
Chris@0 100 @users.sort!
Chris@0 101 if request.post? && params[:committers].is_a?(Hash)
Chris@0 102 # Build a hash with repository usernames as keys and corresponding user ids as values
Chris@0 103 @repository.committer_ids = params[:committers].values.inject({}) {|h, c| h[c.first] = c.last; h}
Chris@0 104 flash[:notice] = l(:notice_successful_update)
Chris@1115 105 redirect_to settings_project_path(@project, :tab => 'repositories')
Chris@0 106 end
Chris@0 107 end
Chris@245 108
Chris@0 109 def destroy
Chris@1115 110 @repository.destroy if request.delete?
Chris@1115 111 redirect_to settings_project_path(@project, :tab => 'repositories')
Chris@0 112 end
Chris@245 113
Chris@245 114 def show
Chris@0 115 @repository.fetch_changesets if Setting.autofetch_changesets? && @path.empty?
Chris@0 116
Chris@0 117 @entries = @repository.entries(@path, @rev)
Chris@441 118 @changeset = @repository.find_changeset_by_name(@rev)
Chris@0 119 if request.xhr?
Chris@0 120 @entries ? render(:partial => 'dir_list_content') : render(:nothing => true)
Chris@0 121 else
Chris@0 122 (show_error_not_found; return) unless @entries
Chris@0 123 @changesets = @repository.latest_changesets(@path, @rev)
Chris@0 124 @properties = @repository.properties(@path, @rev)
Chris@1115 125 @repositories = @project.repositories
Chris@0 126 render :action => 'show'
Chris@0 127 end
Chris@0 128 end
Chris@0 129
Chris@0 130 alias_method :browse, :show
Chris@245 131
Chris@0 132 def changes
Chris@0 133 @entry = @repository.entry(@path, @rev)
Chris@0 134 (show_error_not_found; return) unless @entry
Chris@0 135 @changesets = @repository.latest_changesets(@path, @rev, Setting.repository_log_display_limit.to_i)
Chris@0 136 @properties = @repository.properties(@path, @rev)
Chris@210 137 @changeset = @repository.find_changeset_by_name(@rev)
Chris@0 138 end
Chris@245 139
Chris@0 140 def revisions
Chris@0 141 @changeset_count = @repository.changesets.count
Chris@1295 142 @changeset_pages = Paginator.new @changeset_count,
Chris@245 143 per_page_option,
Chris@245 144 params['page']
Chris@1295 145 @changesets = @repository.changesets.
Chris@1295 146 limit(@changeset_pages.per_page).
Chris@1295 147 offset(@changeset_pages.offset).
Chris@1295 148 includes(:user, :repository, :parents).
Chris@1295 149 all
Chris@0 150
Chris@0 151 respond_to do |format|
Chris@0 152 format.html { render :layout => false if request.xhr? }
Chris@0 153 format.atom { render_feed(@changesets, :title => "#{@project.name}: #{l(:label_revision_plural)}") }
Chris@0 154 end
Chris@0 155 end
Chris@245 156
Chris@1115 157 def raw
Chris@1115 158 entry_and_raw(true)
Chris@1115 159 end
Chris@1115 160
Chris@0 161 def entry
Chris@1115 162 entry_and_raw(false)
Chris@1115 163 end
Chris@1115 164
Chris@1115 165 def entry_and_raw(is_raw)
Chris@0 166 @entry = @repository.entry(@path, @rev)
Chris@0 167 (show_error_not_found; return) unless @entry
Chris@0 168
Chris@0 169 # If the entry is a dir, show the browser
Chris@0 170 (show; return) if @entry.is_dir?
Chris@0 171
Chris@0 172 @content = @repository.cat(@path, @rev)
Chris@0 173 (show_error_not_found; return) unless @content
Chris@1116 174 if is_raw
Chris@0 175 # Force the download
Chris@441 176 send_opt = { :filename => filename_for_content_disposition(@path.split('/').last) }
Chris@441 177 send_type = Redmine::MimeType.of(@path)
Chris@441 178 send_opt[:type] = send_type.to_s if send_type
Chris@1115 179 send_opt[:disposition] = (Redmine::MimeType.is_type?('image', @path) && !is_raw ? 'inline' : 'attachment')
Chris@441 180 send_data @content, send_opt
Chris@0 181 else
chris@1082 182 @display_raw = ((@content.size && @content.size > Setting.file_max_size_displayed.to_i.kilobyte) || !is_entry_text_data?(@content, @path))
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@1115 232 @issue = @changeset.find_referenced_issue_by_id(params[:issue_id])
Chris@1115 233 if @issue && (!@issue.visible? || @changeset.issues.include?(@issue))
Chris@1115 234 @issue = nil
Chris@1115 235 end
Chris@1115 236
Chris@1115 237 if @issue
Chris@1115 238 @changeset.issues << @issue
Chris@1115 239 end
Chris@1115 240 end
Chris@1115 241
Chris@1115 242 # Removes a related issue from a changeset
Chris@1115 243 # DELETE /projects/:project_id/repository/(:repository_id/)revisions/:rev/issues/:issue_id
Chris@1115 244 def remove_related_issue
Chris@1115 245 @issue = Issue.visible.find_by_id(params[:issue_id])
Chris@1115 246 if @issue
Chris@1115 247 @changeset.issues.delete(@issue)
Chris@1115 248 end
Chris@0 249 end
Chris@245 250
Chris@0 251 def diff
Chris@0 252 if params[:format] == 'diff'
Chris@0 253 @diff = @repository.diff(@path, @rev, @rev_to)
Chris@0 254 (show_error_not_found; return) unless @diff
Chris@0 255 filename = "changeset_r#{@rev}"
Chris@0 256 filename << "_r#{@rev_to}" if @rev_to
Chris@0 257 send_data @diff.join, :filename => "#{filename}.diff",
Chris@0 258 :type => 'text/x-patch',
Chris@0 259 :disposition => 'attachment'
Chris@0 260 else
Chris@0 261 @diff_type = params[:type] || User.current.pref[:diff_type] || 'inline'
Chris@0 262 @diff_type = 'inline' unless %w(inline sbs).include?(@diff_type)
Chris@441 263
Chris@0 264 # Save diff type as user preference
Chris@0 265 if User.current.logged? && @diff_type != User.current.pref[:diff_type]
Chris@0 266 User.current.pref[:diff_type] = @diff_type
Chris@0 267 User.current.preference.save
Chris@0 268 end
Chris@909 269 @cache_key = "repositories/diff/#{@repository.id}/" +
Chris@507 270 Digest::MD5.hexdigest("#{@path}-#{@rev}-#{@rev_to}-#{@diff_type}-#{current_language}")
Chris@0 271 unless read_fragment(@cache_key)
Chris@0 272 @diff = @repository.diff(@path, @rev, @rev_to)
Chris@0 273 show_error_not_found unless @diff
Chris@0 274 end
Chris@119 275
Chris@119 276 @changeset = @repository.find_changeset_by_name(@rev)
Chris@119 277 @changeset_to = @rev_to ? @repository.find_changeset_by_name(@rev_to) : nil
Chris@119 278 @diff_format_revisions = @repository.diff_format_revisions(@changeset, @changeset_to)
Chris@0 279 end
Chris@0 280 end
Chris@119 281
Chris@245 282 def stats
Chris@0 283 end
Chris@245 284
Chris@0 285 def graph
Chris@245 286 data = nil
Chris@0 287 case params[:graph]
Chris@0 288 when "commits_per_month"
Chris@0 289 data = graph_commits_per_month(@repository)
Chris@0 290 when "commits_per_author"
Chris@0 291 data = graph_commits_per_author(@repository)
Chris@0 292 end
Chris@0 293 if data
Chris@0 294 headers["Content-Type"] = "image/svg+xml"
Chris@0 295 send_data(data, :type => "image/svg+xml", :disposition => "inline")
Chris@0 296 else
Chris@0 297 render_404
Chris@0 298 end
Chris@0 299 end
Chris@441 300
Chris@119 301 private
Chris@119 302
Chris@1115 303 def find_repository
Chris@1115 304 @repository = Repository.find(params[:id])
Chris@1115 305 @project = @repository.project
Chris@1115 306 rescue ActiveRecord::RecordNotFound
Chris@1115 307 render_404
Chris@1115 308 end
Chris@1115 309
Chris@119 310 REV_PARAM_RE = %r{\A[a-f0-9]*\Z}i
Chris@119 311
Chris@1115 312 def find_project_repository
Chris@0 313 @project = Project.find(params[:id])
Chris@1115 314 if params[:repository_id].present?
Chris@1115 315 @repository = @project.repositories.find_by_identifier_param(params[:repository_id])
Chris@1115 316 else
Chris@1115 317 @repository = @project.repository
Chris@1115 318 end
Chris@0 319 (render_404; return false) unless @repository
Chris@1115 320 @path = params[:path].is_a?(Array) ? params[:path].join('/') : params[:path].to_s
Chris@909 321 @rev = params[:rev].blank? ? @repository.default_branch : params[:rev].to_s.strip
Chris@0 322 @rev_to = params[:rev_to]
Chris@441 323
Chris@245 324 unless @rev.to_s.match(REV_PARAM_RE) && @rev_to.to_s.match(REV_PARAM_RE)
Chris@119 325 if @repository.branches.blank?
Chris@119 326 raise InvalidRevisionParam
Chris@119 327 end
Chris@119 328 end
Chris@0 329 rescue ActiveRecord::RecordNotFound
Chris@0 330 render_404
Chris@0 331 rescue InvalidRevisionParam
Chris@0 332 show_error_not_found
Chris@0 333 end
Chris@0 334
Chris@1115 335 def find_changeset
Chris@1115 336 if @rev.present?
Chris@1115 337 @changeset = @repository.find_changeset_by_name(@rev)
Chris@1115 338 end
Chris@1115 339 show_error_not_found unless @changeset
Chris@1115 340 end
Chris@1115 341
Chris@0 342 def show_error_not_found
Chris@128 343 render_error :message => l(:error_scm_not_found), :status => 404
Chris@0 344 end
Chris@441 345
Chris@0 346 # Handler for Redmine::Scm::Adapters::CommandFailed exception
Chris@0 347 def show_error_command_failed(exception)
Chris@0 348 render_error l(:error_scm_command_failed, exception.message)
Chris@0 349 end
Chris@441 350
Chris@0 351 def graph_commits_per_month(repository)
Chris@0 352 @date_to = Date.today
Chris@0 353 @date_from = @date_to << 11
Chris@0 354 @date_from = Date.civil(@date_from.year, @date_from.month, 1)
Chris@1115 355 commits_by_day = Changeset.count(
Chris@441 356 :all, :group => :commit_date,
Chris@1115 357 :conditions => ["repository_id = ? AND commit_date BETWEEN ? AND ?", repository.id, @date_from, @date_to])
Chris@0 358 commits_by_month = [0] * 12
Chris@1115 359 commits_by_day.each {|c| commits_by_month[(@date_to.month - c.first.to_date.month) % 12] += c.last }
Chris@0 360
Chris@1115 361 changes_by_day = Change.count(
Chris@1115 362 :all, :group => :commit_date, :include => :changeset,
Chris@1115 363 :conditions => ["#{Changeset.table_name}.repository_id = ? AND #{Changeset.table_name}.commit_date BETWEEN ? AND ?", repository.id, @date_from, @date_to])
Chris@0 364 changes_by_month = [0] * 12
Chris@1115 365 changes_by_day.each {|c| changes_by_month[(@date_to.month - c.first.to_date.month) % 12] += c.last }
Chris@441 366
Chris@0 367 fields = []
Chris@0 368 12.times {|m| fields << month_name(((Date.today.month - 1 - m) % 12) + 1)}
Chris@441 369
Chris@0 370 graph = SVG::Graph::Bar.new(
Chris@0 371 :height => 300,
Chris@0 372 :width => 800,
Chris@0 373 :fields => fields.reverse,
Chris@0 374 :stack => :side,
Chris@0 375 :scale_integers => true,
Chris@0 376 :step_x_labels => 2,
Chris@0 377 :show_data_values => false,
Chris@0 378 :graph_title => l(:label_commits_per_month),
Chris@0 379 :show_graph_title => true
Chris@0 380 )
Chris@441 381
Chris@0 382 graph.add_data(
Chris@0 383 :data => commits_by_month[0..11].reverse,
Chris@0 384 :title => l(:label_revision_plural)
Chris@0 385 )
Chris@0 386
Chris@0 387 graph.add_data(
Chris@0 388 :data => changes_by_month[0..11].reverse,
Chris@0 389 :title => l(:label_change_plural)
Chris@0 390 )
Chris@441 391
Chris@0 392 graph.burn
Chris@0 393 end
Chris@0 394
Chris@0 395 def graph_commits_per_author(repository)
Chris@1115 396 commits_by_author = Changeset.count(:all, :group => :committer, :conditions => ["repository_id = ?", repository.id])
Chris@0 397 commits_by_author.to_a.sort! {|x, y| x.last <=> y.last}
Chris@0 398
Chris@1115 399 changes_by_author = Change.count(:all, :group => :committer, :include => :changeset, :conditions => ["#{Changeset.table_name}.repository_id = ?", repository.id])
Chris@0 400 h = changes_by_author.inject({}) {|o, i| o[i.first] = i.last; o}
Chris@441 401
Chris@0 402 fields = commits_by_author.collect {|r| r.first}
Chris@0 403 commits_data = commits_by_author.collect {|r| r.last}
Chris@0 404 changes_data = commits_by_author.collect {|r| h[r.first] || 0}
Chris@441 405
Chris@0 406 fields = fields + [""]*(10 - fields.length) if fields.length<10
Chris@0 407 commits_data = commits_data + [0]*(10 - commits_data.length) if commits_data.length<10
Chris@0 408 changes_data = changes_data + [0]*(10 - changes_data.length) if changes_data.length<10
Chris@441 409
Chris@0 410 # Remove email adress in usernames
Chris@0 411 fields = fields.collect {|c| c.gsub(%r{<.+@.+>}, '') }
Chris@441 412
Chris@0 413 graph = SVG::Graph::BarHorizontal.new(
Chris@0 414 :height => 400,
Chris@0 415 :width => 800,
Chris@0 416 :fields => fields,
Chris@0 417 :stack => :side,
Chris@0 418 :scale_integers => true,
Chris@0 419 :show_data_values => false,
Chris@0 420 :rotate_y_labels => false,
Chris@0 421 :graph_title => l(:label_commits_per_author),
Chris@0 422 :show_graph_title => true
Chris@0 423 )
Chris@0 424 graph.add_data(
Chris@0 425 :data => commits_data,
Chris@0 426 :title => l(:label_revision_plural)
Chris@0 427 )
Chris@0 428 graph.add_data(
Chris@0 429 :data => changes_data,
Chris@0 430 :title => l(:label_change_plural)
Chris@0 431 )
Chris@0 432 graph.burn
Chris@0 433 end
Chris@441 434 end