annotate .svn/pristine/5b/5bef2d38e6b8855b684e8739aef488dc430ba2cd.svn-base @ 1327:287f201c2802 redmine-2.2-integration

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