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 / .svn / text-base / repositories_controller.rb.svn-base @ 442:753f1380d6bc

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