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