annotate .svn/pristine/df/df4288bbed742ae45b6043b135789eb3f2d620b1.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-2013 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 @changeset_count,
Chris@1295 142 per_page_option,
Chris@1295 143 params['page']
Chris@1295 144 @changesets = @repository.changesets.
Chris@1295 145 limit(@changeset_pages.per_page).
Chris@1295 146 offset(@changeset_pages.offset).
Chris@1295 147 includes(:user, :repository, :parents).
Chris@1295 148 all
Chris@1295 149
Chris@1295 150 respond_to do |format|
Chris@1295 151 format.html { render :layout => false if request.xhr? }
Chris@1295 152 format.atom { render_feed(@changesets, :title => "#{@project.name}: #{l(:label_revision_plural)}") }
Chris@1295 153 end
Chris@1295 154 end
Chris@1295 155
Chris@1295 156 def raw
Chris@1295 157 entry_and_raw(true)
Chris@1295 158 end
Chris@1295 159
Chris@1295 160 def entry
Chris@1295 161 entry_and_raw(false)
Chris@1295 162 end
Chris@1295 163
Chris@1295 164 def entry_and_raw(is_raw)
Chris@1295 165 @entry = @repository.entry(@path, @rev)
Chris@1295 166 (show_error_not_found; return) unless @entry
Chris@1295 167
Chris@1295 168 # If the entry is a dir, show the browser
Chris@1295 169 (show; return) if @entry.is_dir?
Chris@1295 170
Chris@1295 171 @content = @repository.cat(@path, @rev)
Chris@1295 172 (show_error_not_found; return) unless @content
Chris@1295 173 if is_raw ||
Chris@1295 174 (@content.size && @content.size > Setting.file_max_size_displayed.to_i.kilobyte) ||
Chris@1295 175 ! is_entry_text_data?(@content, @path)
Chris@1295 176 # Force the download
Chris@1295 177 send_opt = { :filename => filename_for_content_disposition(@path.split('/').last) }
Chris@1295 178 send_type = Redmine::MimeType.of(@path)
Chris@1295 179 send_opt[:type] = send_type.to_s if send_type
Chris@1295 180 send_opt[:disposition] = (Redmine::MimeType.is_type?('image', @path) && !is_raw ? 'inline' : 'attachment')
Chris@1295 181 send_data @content, send_opt
Chris@1295 182 else
Chris@1295 183 # Prevent empty lines when displaying a file with Windows style eol
Chris@1295 184 # TODO: UTF-16
Chris@1295 185 # Is this needs? AttachmentsController reads file simply.
Chris@1295 186 @content.gsub!("\r\n", "\n")
Chris@1295 187 @changeset = @repository.find_changeset_by_name(@rev)
Chris@1295 188 end
Chris@1295 189 end
Chris@1295 190 private :entry_and_raw
Chris@1295 191
Chris@1295 192 def is_entry_text_data?(ent, path)
Chris@1295 193 # UTF-16 contains "\x00".
Chris@1295 194 # It is very strict that file contains less than 30% of ascii symbols
Chris@1295 195 # in non Western Europe.
Chris@1295 196 return true if Redmine::MimeType.is_type?('text', path)
Chris@1295 197 # Ruby 1.8.6 has a bug of integer divisions.
Chris@1295 198 # http://apidock.com/ruby/v1_8_6_287/String/is_binary_data%3F
Chris@1295 199 return false if ent.is_binary_data?
Chris@1295 200 true
Chris@1295 201 end
Chris@1295 202 private :is_entry_text_data?
Chris@1295 203
Chris@1295 204 def annotate
Chris@1295 205 @entry = @repository.entry(@path, @rev)
Chris@1295 206 (show_error_not_found; return) unless @entry
Chris@1295 207
Chris@1295 208 @annotate = @repository.scm.annotate(@path, @rev)
Chris@1295 209 if @annotate.nil? || @annotate.empty?
Chris@1295 210 (render_error l(:error_scm_annotate); return)
Chris@1295 211 end
Chris@1295 212 ann_buf_size = 0
Chris@1295 213 @annotate.lines.each do |buf|
Chris@1295 214 ann_buf_size += buf.size
Chris@1295 215 end
Chris@1295 216 if ann_buf_size > Setting.file_max_size_displayed.to_i.kilobyte
Chris@1295 217 (render_error l(:error_scm_annotate_big_text_file); return)
Chris@1295 218 end
Chris@1295 219 @changeset = @repository.find_changeset_by_name(@rev)
Chris@1295 220 end
Chris@1295 221
Chris@1295 222 def revision
Chris@1295 223 respond_to do |format|
Chris@1295 224 format.html
Chris@1295 225 format.js {render :layout => false}
Chris@1295 226 end
Chris@1295 227 end
Chris@1295 228
Chris@1295 229 # Adds a related issue to a changeset
Chris@1295 230 # POST /projects/:project_id/repository/(:repository_id/)revisions/:rev/issues
Chris@1295 231 def add_related_issue
Chris@1295 232 @issue = @changeset.find_referenced_issue_by_id(params[:issue_id])
Chris@1295 233 if @issue && (!@issue.visible? || @changeset.issues.include?(@issue))
Chris@1295 234 @issue = nil
Chris@1295 235 end
Chris@1295 236
Chris@1295 237 if @issue
Chris@1295 238 @changeset.issues << @issue
Chris@1295 239 end
Chris@1295 240 end
Chris@1295 241
Chris@1295 242 # Removes a related issue from a changeset
Chris@1295 243 # DELETE /projects/:project_id/repository/(:repository_id/)revisions/:rev/issues/:issue_id
Chris@1295 244 def remove_related_issue
Chris@1295 245 @issue = Issue.visible.find_by_id(params[:issue_id])
Chris@1295 246 if @issue
Chris@1295 247 @changeset.issues.delete(@issue)
Chris@1295 248 end
Chris@1295 249 end
Chris@1295 250
Chris@1295 251 def diff
Chris@1295 252 if params[:format] == 'diff'
Chris@1295 253 @diff = @repository.diff(@path, @rev, @rev_to)
Chris@1295 254 (show_error_not_found; return) unless @diff
Chris@1295 255 filename = "changeset_r#{@rev}"
Chris@1295 256 filename << "_r#{@rev_to}" if @rev_to
Chris@1295 257 send_data @diff.join, :filename => "#{filename}.diff",
Chris@1295 258 :type => 'text/x-patch',
Chris@1295 259 :disposition => 'attachment'
Chris@1295 260 else
Chris@1295 261 @diff_type = params[:type] || User.current.pref[:diff_type] || 'inline'
Chris@1295 262 @diff_type = 'inline' unless %w(inline sbs).include?(@diff_type)
Chris@1295 263
Chris@1295 264 # Save diff type as user preference
Chris@1295 265 if User.current.logged? && @diff_type != User.current.pref[:diff_type]
Chris@1295 266 User.current.pref[:diff_type] = @diff_type
Chris@1295 267 User.current.preference.save
Chris@1295 268 end
Chris@1295 269 @cache_key = "repositories/diff/#{@repository.id}/" +
Chris@1295 270 Digest::MD5.hexdigest("#{@path}-#{@rev}-#{@rev_to}-#{@diff_type}-#{current_language}")
Chris@1295 271 unless read_fragment(@cache_key)
Chris@1295 272 @diff = @repository.diff(@path, @rev, @rev_to)
Chris@1295 273 show_error_not_found unless @diff
Chris@1295 274 end
Chris@1295 275
Chris@1295 276 @changeset = @repository.find_changeset_by_name(@rev)
Chris@1295 277 @changeset_to = @rev_to ? @repository.find_changeset_by_name(@rev_to) : nil
Chris@1295 278 @diff_format_revisions = @repository.diff_format_revisions(@changeset, @changeset_to)
Chris@1295 279 end
Chris@1295 280 end
Chris@1295 281
Chris@1295 282 def stats
Chris@1295 283 end
Chris@1295 284
Chris@1295 285 def graph
Chris@1295 286 data = nil
Chris@1295 287 case params[:graph]
Chris@1295 288 when "commits_per_month"
Chris@1295 289 data = graph_commits_per_month(@repository)
Chris@1295 290 when "commits_per_author"
Chris@1295 291 data = graph_commits_per_author(@repository)
Chris@1295 292 end
Chris@1295 293 if data
Chris@1295 294 headers["Content-Type"] = "image/svg+xml"
Chris@1295 295 send_data(data, :type => "image/svg+xml", :disposition => "inline")
Chris@1295 296 else
Chris@1295 297 render_404
Chris@1295 298 end
Chris@1295 299 end
Chris@1295 300
Chris@1295 301 private
Chris@1295 302
Chris@1295 303 def find_repository
Chris@1295 304 @repository = Repository.find(params[:id])
Chris@1295 305 @project = @repository.project
Chris@1295 306 rescue ActiveRecord::RecordNotFound
Chris@1295 307 render_404
Chris@1295 308 end
Chris@1295 309
Chris@1295 310 REV_PARAM_RE = %r{\A[a-f0-9]*\Z}i
Chris@1295 311
Chris@1295 312 def find_project_repository
Chris@1295 313 @project = Project.find(params[:id])
Chris@1295 314 if params[:repository_id].present?
Chris@1295 315 @repository = @project.repositories.find_by_identifier_param(params[:repository_id])
Chris@1295 316 else
Chris@1295 317 @repository = @project.repository
Chris@1295 318 end
Chris@1295 319 (render_404; return false) unless @repository
Chris@1295 320 @path = params[:path].is_a?(Array) ? params[:path].join('/') : params[:path].to_s
Chris@1295 321 @rev = params[:rev].blank? ? @repository.default_branch : params[:rev].to_s.strip
Chris@1295 322 @rev_to = params[:rev_to]
Chris@1295 323
Chris@1295 324 unless @rev.to_s.match(REV_PARAM_RE) && @rev_to.to_s.match(REV_PARAM_RE)
Chris@1295 325 if @repository.branches.blank?
Chris@1295 326 raise InvalidRevisionParam
Chris@1295 327 end
Chris@1295 328 end
Chris@1295 329 rescue ActiveRecord::RecordNotFound
Chris@1295 330 render_404
Chris@1295 331 rescue InvalidRevisionParam
Chris@1295 332 show_error_not_found
Chris@1295 333 end
Chris@1295 334
Chris@1295 335 def find_changeset
Chris@1295 336 if @rev.present?
Chris@1295 337 @changeset = @repository.find_changeset_by_name(@rev)
Chris@1295 338 end
Chris@1295 339 show_error_not_found unless @changeset
Chris@1295 340 end
Chris@1295 341
Chris@1295 342 def show_error_not_found
Chris@1295 343 render_error :message => l(:error_scm_not_found), :status => 404
Chris@1295 344 end
Chris@1295 345
Chris@1295 346 # Handler for Redmine::Scm::Adapters::CommandFailed exception
Chris@1295 347 def show_error_command_failed(exception)
Chris@1295 348 render_error l(:error_scm_command_failed, exception.message)
Chris@1295 349 end
Chris@1295 350
Chris@1295 351 def graph_commits_per_month(repository)
Chris@1295 352 @date_to = Date.today
Chris@1295 353 @date_from = @date_to << 11
Chris@1295 354 @date_from = Date.civil(@date_from.year, @date_from.month, 1)
Chris@1295 355 commits_by_day = Changeset.count(
Chris@1295 356 :all, :group => :commit_date,
Chris@1295 357 :conditions => ["repository_id = ? AND commit_date BETWEEN ? AND ?", repository.id, @date_from, @date_to])
Chris@1295 358 commits_by_month = [0] * 12
Chris@1295 359 commits_by_day.each {|c| commits_by_month[(@date_to.month - c.first.to_date.month) % 12] += c.last }
Chris@1295 360
Chris@1295 361 changes_by_day = Change.count(
Chris@1295 362 :all, :group => :commit_date, :include => :changeset,
Chris@1295 363 :conditions => ["#{Changeset.table_name}.repository_id = ? AND #{Changeset.table_name}.commit_date BETWEEN ? AND ?", repository.id, @date_from, @date_to])
Chris@1295 364 changes_by_month = [0] * 12
Chris@1295 365 changes_by_day.each {|c| changes_by_month[(@date_to.month - c.first.to_date.month) % 12] += c.last }
Chris@1295 366
Chris@1295 367 fields = []
Chris@1295 368 12.times {|m| fields << month_name(((Date.today.month - 1 - m) % 12) + 1)}
Chris@1295 369
Chris@1295 370 graph = SVG::Graph::Bar.new(
Chris@1295 371 :height => 300,
Chris@1295 372 :width => 800,
Chris@1295 373 :fields => fields.reverse,
Chris@1295 374 :stack => :side,
Chris@1295 375 :scale_integers => true,
Chris@1295 376 :step_x_labels => 2,
Chris@1295 377 :show_data_values => false,
Chris@1295 378 :graph_title => l(:label_commits_per_month),
Chris@1295 379 :show_graph_title => true
Chris@1295 380 )
Chris@1295 381
Chris@1295 382 graph.add_data(
Chris@1295 383 :data => commits_by_month[0..11].reverse,
Chris@1295 384 :title => l(:label_revision_plural)
Chris@1295 385 )
Chris@1295 386
Chris@1295 387 graph.add_data(
Chris@1295 388 :data => changes_by_month[0..11].reverse,
Chris@1295 389 :title => l(:label_change_plural)
Chris@1295 390 )
Chris@1295 391
Chris@1295 392 graph.burn
Chris@1295 393 end
Chris@1295 394
Chris@1295 395 def graph_commits_per_author(repository)
Chris@1295 396 commits_by_author = Changeset.count(:all, :group => :committer, :conditions => ["repository_id = ?", repository.id])
Chris@1295 397 commits_by_author.to_a.sort! {|x, y| x.last <=> y.last}
Chris@1295 398
Chris@1295 399 changes_by_author = Change.count(:all, :group => :committer, :include => :changeset, :conditions => ["#{Changeset.table_name}.repository_id = ?", repository.id])
Chris@1295 400 h = changes_by_author.inject({}) {|o, i| o[i.first] = i.last; o}
Chris@1295 401
Chris@1295 402 fields = commits_by_author.collect {|r| r.first}
Chris@1295 403 commits_data = commits_by_author.collect {|r| r.last}
Chris@1295 404 changes_data = commits_by_author.collect {|r| h[r.first] || 0}
Chris@1295 405
Chris@1295 406 fields = fields + [""]*(10 - fields.length) if fields.length<10
Chris@1295 407 commits_data = commits_data + [0]*(10 - commits_data.length) if commits_data.length<10
Chris@1295 408 changes_data = changes_data + [0]*(10 - changes_data.length) if changes_data.length<10
Chris@1295 409
Chris@1295 410 # Remove email adress in usernames
Chris@1295 411 fields = fields.collect {|c| c.gsub(%r{<.+@.+>}, '') }
Chris@1295 412
Chris@1295 413 graph = SVG::Graph::BarHorizontal.new(
Chris@1295 414 :height => 400,
Chris@1295 415 :width => 800,
Chris@1295 416 :fields => fields,
Chris@1295 417 :stack => :side,
Chris@1295 418 :scale_integers => true,
Chris@1295 419 :show_data_values => false,
Chris@1295 420 :rotate_y_labels => false,
Chris@1295 421 :graph_title => l(:label_commits_per_author),
Chris@1295 422 :show_graph_title => true
Chris@1295 423 )
Chris@1295 424 graph.add_data(
Chris@1295 425 :data => commits_data,
Chris@1295 426 :title => l(:label_revision_plural)
Chris@1295 427 )
Chris@1295 428 graph.add_data(
Chris@1295 429 :data => changes_data,
Chris@1295 430 :title => l(:label_change_plural)
Chris@1295 431 )
Chris@1295 432 graph.burn
Chris@1295 433 end
Chris@1295 434 end