annotate .svn/pristine/e4/e4ae930b47db6b43c63ad3f883857c9099aec958.svn-base @ 1524:82fac3dcf466 redmine-2.5-integration

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