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 @ 1082:997f6d7738f7

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 1082:997f6d7738f7 chris
    if 'raw' == params[:format]
147 0:513646585e45 Chris
      # Force the download
148 441:cbce1fd3b1b7 Chris
      send_opt = { :filename => filename_for_content_disposition(@path.split('/').last) }
149
      send_type = Redmine::MimeType.of(@path)
150
      send_opt[:type] = send_type.to_s if send_type
151
      send_data @content, send_opt
152 0:513646585e45 Chris
    else
153 1082:997f6d7738f7 chris
      @display_raw = ((@content.size && @content.size > Setting.file_max_size_displayed.to_i.kilobyte) || !is_entry_text_data?(@content, @path))
154 0:513646585e45 Chris
      # Prevent empty lines when displaying a file with Windows style eol
155 441:cbce1fd3b1b7 Chris
      # TODO: UTF-16
156
      # Is this needs? AttachmentsController reads file simply.
157 0:513646585e45 Chris
      @content.gsub!("\r\n", "\n")
158 210:0579821a129a Chris
      @changeset = @repository.find_changeset_by_name(@rev)
159 441:cbce1fd3b1b7 Chris
    end
160 0:513646585e45 Chris
  end
161 210:0579821a129a Chris
162 441:cbce1fd3b1b7 Chris
  def is_entry_text_data?(ent, path)
163
    # UTF-16 contains "\x00".
164
    # It is very strict that file contains less than 30% of ascii symbols
165
    # in non Western Europe.
166
    return true if Redmine::MimeType.is_type?('text', path)
167
    # Ruby 1.8.6 has a bug of integer divisions.
168
    # http://apidock.com/ruby/v1_8_6_287/String/is_binary_data%3F
169
    return false if ent.is_binary_data?
170
    true
171
  end
172
  private :is_entry_text_data?
173
174 0:513646585e45 Chris
  def annotate
175
    @entry = @repository.entry(@path, @rev)
176
    (show_error_not_found; return) unless @entry
177 245:051f544170fe Chris
178 0:513646585e45 Chris
    @annotate = @repository.scm.annotate(@path, @rev)
179 909:cbb26bc654de Chris
    if @annotate.nil? || @annotate.empty?
180
      (render_error l(:error_scm_annotate); return)
181
    end
182
    ann_buf_size = 0
183
    @annotate.lines.each do |buf|
184
      ann_buf_size += buf.size
185
    end
186
    if ann_buf_size > Setting.file_max_size_displayed.to_i.kilobyte
187
      (render_error l(:error_scm_annotate_big_text_file); return)
188
    end
189 210:0579821a129a Chris
    @changeset = @repository.find_changeset_by_name(@rev)
190 0:513646585e45 Chris
  end
191 210:0579821a129a Chris
192 0:513646585e45 Chris
  def revision
193 128:07fa8a8b56a8 Chris
    raise ChangesetNotFound if @rev.blank?
194 0:513646585e45 Chris
    @changeset = @repository.find_changeset_by_name(@rev)
195
    raise ChangesetNotFound unless @changeset
196
197
    respond_to do |format|
198
      format.html
199
      format.js {render :layout => false}
200
    end
201
  rescue ChangesetNotFound
202
    show_error_not_found
203
  end
204 245:051f544170fe Chris
205 0:513646585e45 Chris
  def diff
206
    if params[:format] == 'diff'
207
      @diff = @repository.diff(@path, @rev, @rev_to)
208
      (show_error_not_found; return) unless @diff
209
      filename = "changeset_r#{@rev}"
210
      filename << "_r#{@rev_to}" if @rev_to
211
      send_data @diff.join, :filename => "#{filename}.diff",
212
                            :type => 'text/x-patch',
213
                            :disposition => 'attachment'
214
    else
215
      @diff_type = params[:type] || User.current.pref[:diff_type] || 'inline'
216
      @diff_type = 'inline' unless %w(inline sbs).include?(@diff_type)
217 441:cbce1fd3b1b7 Chris
218 0:513646585e45 Chris
      # Save diff type as user preference
219
      if User.current.logged? && @diff_type != User.current.pref[:diff_type]
220
        User.current.pref[:diff_type] = @diff_type
221
        User.current.preference.save
222
      end
223 909:cbb26bc654de Chris
      @cache_key = "repositories/diff/#{@repository.id}/" +
224 507:0c939c159af4 Chris
                      Digest::MD5.hexdigest("#{@path}-#{@rev}-#{@rev_to}-#{@diff_type}-#{current_language}")
225 0:513646585e45 Chris
      unless read_fragment(@cache_key)
226
        @diff = @repository.diff(@path, @rev, @rev_to)
227
        show_error_not_found unless @diff
228
      end
229 119:8661b858af72 Chris
230
      @changeset = @repository.find_changeset_by_name(@rev)
231
      @changeset_to = @rev_to ? @repository.find_changeset_by_name(@rev_to) : nil
232
      @diff_format_revisions = @repository.diff_format_revisions(@changeset, @changeset_to)
233 0:513646585e45 Chris
    end
234
  end
235 119:8661b858af72 Chris
236 245:051f544170fe Chris
  def stats
237 0:513646585e45 Chris
  end
238 245:051f544170fe Chris
239 0:513646585e45 Chris
  def graph
240 245:051f544170fe Chris
    data = nil
241 0:513646585e45 Chris
    case params[:graph]
242
    when "commits_per_month"
243
      data = graph_commits_per_month(@repository)
244
    when "commits_per_author"
245
      data = graph_commits_per_author(@repository)
246
    end
247
    if data
248
      headers["Content-Type"] = "image/svg+xml"
249
      send_data(data, :type => "image/svg+xml", :disposition => "inline")
250
    else
251
      render_404
252
    end
253
  end
254 441:cbce1fd3b1b7 Chris
255 119:8661b858af72 Chris
  private
256
257
  REV_PARAM_RE = %r{\A[a-f0-9]*\Z}i
258
259 0:513646585e45 Chris
  def find_repository
260
    @project = Project.find(params[:id])
261
    @repository = @project.repository
262
    (render_404; return false) unless @repository
263
    @path = params[:path].join('/') unless params[:path].nil?
264
    @path ||= ''
265 909:cbb26bc654de Chris
    @rev = params[:rev].blank? ? @repository.default_branch : params[:rev].to_s.strip
266 0:513646585e45 Chris
    @rev_to = params[:rev_to]
267 441:cbce1fd3b1b7 Chris
268 245:051f544170fe Chris
    unless @rev.to_s.match(REV_PARAM_RE) && @rev_to.to_s.match(REV_PARAM_RE)
269 119:8661b858af72 Chris
      if @repository.branches.blank?
270
        raise InvalidRevisionParam
271
      end
272
    end
273 0:513646585e45 Chris
  rescue ActiveRecord::RecordNotFound
274
    render_404
275
  rescue InvalidRevisionParam
276
    show_error_not_found
277
  end
278
279
  def show_error_not_found
280 128:07fa8a8b56a8 Chris
    render_error :message => l(:error_scm_not_found), :status => 404
281 0:513646585e45 Chris
  end
282 441:cbce1fd3b1b7 Chris
283 0:513646585e45 Chris
  # Handler for Redmine::Scm::Adapters::CommandFailed exception
284
  def show_error_command_failed(exception)
285
    render_error l(:error_scm_command_failed, exception.message)
286
  end
287 441:cbce1fd3b1b7 Chris
288 0:513646585e45 Chris
  def graph_commits_per_month(repository)
289
    @date_to = Date.today
290
    @date_from = @date_to << 11
291
    @date_from = Date.civil(@date_from.year, @date_from.month, 1)
292 441:cbce1fd3b1b7 Chris
    commits_by_day = repository.changesets.count(
293
                          :all, :group => :commit_date,
294
                          :conditions => ["commit_date BETWEEN ? AND ?", @date_from, @date_to])
295 0:513646585e45 Chris
    commits_by_month = [0] * 12
296
    commits_by_day.each {|c| commits_by_month[c.first.to_date.months_ago] += c.last }
297
298 441:cbce1fd3b1b7 Chris
    changes_by_day = repository.changes.count(
299
                          :all, :group => :commit_date,
300
                          :conditions => ["commit_date BETWEEN ? AND ?", @date_from, @date_to])
301 0:513646585e45 Chris
    changes_by_month = [0] * 12
302
    changes_by_day.each {|c| changes_by_month[c.first.to_date.months_ago] += c.last }
303 441:cbce1fd3b1b7 Chris
304 0:513646585e45 Chris
    fields = []
305
    12.times {|m| fields << month_name(((Date.today.month - 1 - m) % 12) + 1)}
306 441:cbce1fd3b1b7 Chris
307 0:513646585e45 Chris
    graph = SVG::Graph::Bar.new(
308
      :height => 300,
309
      :width => 800,
310
      :fields => fields.reverse,
311
      :stack => :side,
312
      :scale_integers => true,
313
      :step_x_labels => 2,
314
      :show_data_values => false,
315
      :graph_title => l(:label_commits_per_month),
316
      :show_graph_title => true
317
    )
318 441:cbce1fd3b1b7 Chris
319 0:513646585e45 Chris
    graph.add_data(
320
      :data => commits_by_month[0..11].reverse,
321
      :title => l(:label_revision_plural)
322
    )
323
324
    graph.add_data(
325
      :data => changes_by_month[0..11].reverse,
326
      :title => l(:label_change_plural)
327
    )
328 441:cbce1fd3b1b7 Chris
329 0:513646585e45 Chris
    graph.burn
330
  end
331
332
  def graph_commits_per_author(repository)
333
    commits_by_author = repository.changesets.count(:all, :group => :committer)
334
    commits_by_author.to_a.sort! {|x, y| x.last <=> y.last}
335
336
    changes_by_author = repository.changes.count(:all, :group => :committer)
337
    h = changes_by_author.inject({}) {|o, i| o[i.first] = i.last; o}
338 441:cbce1fd3b1b7 Chris
339 0:513646585e45 Chris
    fields = commits_by_author.collect {|r| r.first}
340
    commits_data = commits_by_author.collect {|r| r.last}
341
    changes_data = commits_by_author.collect {|r| h[r.first] || 0}
342 441:cbce1fd3b1b7 Chris
343 0:513646585e45 Chris
    fields = fields + [""]*(10 - fields.length) if fields.length<10
344
    commits_data = commits_data + [0]*(10 - commits_data.length) if commits_data.length<10
345
    changes_data = changes_data + [0]*(10 - changes_data.length) if changes_data.length<10
346 441:cbce1fd3b1b7 Chris
347 0:513646585e45 Chris
    # Remove email adress in usernames
348
    fields = fields.collect {|c| c.gsub(%r{<.+@.+>}, '') }
349 441:cbce1fd3b1b7 Chris
350 0:513646585e45 Chris
    graph = SVG::Graph::BarHorizontal.new(
351
      :height => 400,
352
      :width => 800,
353
      :fields => fields,
354
      :stack => :side,
355
      :scale_integers => true,
356
      :show_data_values => false,
357
      :rotate_y_labels => false,
358
      :graph_title => l(:label_commits_per_author),
359
      :show_graph_title => true
360
    )
361
    graph.add_data(
362
      :data => commits_data,
363
      :title => l(:label_revision_plural)
364
    )
365
    graph.add_data(
366
      :data => changes_data,
367
      :title => l(:label_change_plural)
368
    )
369
    graph.burn
370
  end
371 441:cbce1fd3b1b7 Chris
end
372 0:513646585e45 Chris
373
class Date
374
  def months_ago(date = Date.today)
375
    (date.year - self.year)*12 + (date.month - self.month)
376
  end
377
378
  def weeks_ago(date = Date.today)
379
    (date.year - self.year)*52 + (date.cweek - self.cweek)
380
  end
381
end
382
383
class String
384
  def with_leading_slash
385
    starts_with?('/') ? self : "/#{self}"
386
  end
387
end