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