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 @ 1304:6137548ba453

History | View | Annotate | Download (12.7 KB)

1
# Redmine - project management software
2
# Copyright (C) 2006-2011  Jean-Philippe Lang
3
#
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
#
9
# 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
#
14
# 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

    
30
  before_filter :find_repository, :except => :edit
31
  before_filter :find_project, :only => :edit
32
  before_filter :authorize
33
  accept_rss_auth :revisions
34

    
35
  rescue_from Redmine::Scm::Adapters::CommandFailed, :with => :show_error_command_failed
36

    
37
  def edit
38
    @repository = @project.repository
39

    
40
    if !@repository
41

    
42
      params[:repository_scm]='Mercurial'
43

    
44
      @repository = Repository.factory(params[:repository_scm])
45
      @repository.project = @project if @repository
46
    end
47
    if request.post? && @repository
48
      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
      @repository.save
61
    end
62

    
63
    render(:update) do |page|
64
      page.replace_html "tab-content-repository",
65
                        :partial => 'projects/settings/repository'
66
      if @repository && !@project.repository
67
        @project.reload # needed to reload association
68
        page.replace_html "main-menu", render_main_menu(@project)
69
      end
70
    end
71
  end
72

    
73
  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

    
88
  def destroy
89
    @repository.destroy
90
    redirect_to :controller => 'projects',
91
                :action     => 'settings',
92
                :id         => @project,
93
                :tab        => 'repository'
94
  end
95

    
96
  def show
97
    @repository.fetch_changesets if Setting.autofetch_changesets? && @path.empty?
98

    
99
    @entries = @repository.entries(@path, @rev)
100
    @changeset = @repository.find_changeset_by_name(@rev)
101
    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

    
113
  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
    @changeset = @repository.find_changeset_by_name(@rev)
119
  end
120

    
121
  def revisions
122
    @changeset_count = @repository.changesets.count
123
    @changeset_pages = Paginator.new self, @changeset_count,
124
                                     per_page_option,
125
                                     params['page']
126
    @changesets = @repository.changesets.find(:all,
127
                       :limit  =>  @changeset_pages.items_per_page,
128
                       :offset =>  @changeset_pages.current.offset,
129
                       :include => [:user, :repository, :parents])
130

    
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

    
137
  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
    if 'raw' == params[:format]
147
      # Force the download
148
      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
    else
153
      @display_raw = ((@content.size && @content.size > Setting.file_max_size_displayed.to_i.kilobyte) || !is_entry_text_data?(@content, @path))
154
      # Prevent empty lines when displaying a file with Windows style eol
155
      # TODO: UTF-16
156
      # Is this needs? AttachmentsController reads file simply.
157
      @content.gsub!("\r\n", "\n")
158
      @changeset = @repository.find_changeset_by_name(@rev)
159
    end
160
  end
161

    
162
  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
  def annotate
175
    @entry = @repository.entry(@path, @rev)
176
    (show_error_not_found; return) unless @entry
177

    
178
    @annotate = @repository.scm.annotate(@path, @rev)
179
    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
    @changeset = @repository.find_changeset_by_name(@rev)
190
  end
191

    
192
  def revision
193
    raise ChangesetNotFound if @rev.blank?
194
    @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

    
205
  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

    
218
      # 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
      @cache_key = "repositories/diff/#{@repository.id}/" +
224
                      Digest::MD5.hexdigest("#{@path}-#{@rev}-#{@rev_to}-#{@diff_type}-#{current_language}")
225
      unless read_fragment(@cache_key)
226
        @diff = @repository.diff(@path, @rev, @rev_to)
227
        show_error_not_found unless @diff
228
      end
229

    
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
    end
234
  end
235

    
236
  def stats
237
  end
238

    
239
  def graph
240
    data = nil
241
    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

    
255
  private
256

    
257
  REV_PARAM_RE = %r{\A[a-f0-9]*\Z}i
258

    
259
  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
    @rev = params[:rev].blank? ? @repository.default_branch : params[:rev].to_s.strip
266
    @rev_to = params[:rev_to]
267

    
268
    unless @rev.to_s.match(REV_PARAM_RE) && @rev_to.to_s.match(REV_PARAM_RE)
269
      if @repository.branches.blank?
270
        raise InvalidRevisionParam
271
      end
272
    end
273
  rescue ActiveRecord::RecordNotFound
274
    render_404
275
  rescue InvalidRevisionParam
276
    show_error_not_found
277
  end
278

    
279
  def show_error_not_found
280
    render_error :message => l(:error_scm_not_found), :status => 404
281
  end
282

    
283
  # 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

    
288
  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
    commits_by_day = repository.changesets.count(
293
                          :all, :group => :commit_date,
294
                          :conditions => ["commit_date BETWEEN ? AND ?", @date_from, @date_to])
295
    commits_by_month = [0] * 12
296
    commits_by_day.each {|c| commits_by_month[c.first.to_date.months_ago] += c.last }
297

    
298
    changes_by_day = repository.changes.count(
299
                          :all, :group => :commit_date,
300
                          :conditions => ["commit_date BETWEEN ? AND ?", @date_from, @date_to])
301
    changes_by_month = [0] * 12
302
    changes_by_day.each {|c| changes_by_month[c.first.to_date.months_ago] += c.last }
303

    
304
    fields = []
305
    12.times {|m| fields << month_name(((Date.today.month - 1 - m) % 12) + 1)}
306

    
307
    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

    
319
    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

    
329
    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

    
339
    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

    
343
    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

    
347
    # Remove email adress in usernames
348
    fields = fields.collect {|c| c.gsub(%r{<.+@.+>}, '') }
349

    
350
    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
end
372

    
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