To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / app / controllers / repositories_controller.rb @ 1298:4f746d8966dd

History | View | Annotate | Download (14.5 KB)

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