##// END OF EJS Templates
Added fragment caching for svn diffs....
Jean-Philippe Lang -
r496:0759212a4474
parent child
Show More
@@ -1,208 +1,213
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
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
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.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require 'SVG/Graph/Bar'
18 require 'SVG/Graph/Bar'
19 require 'SVG/Graph/BarHorizontal'
19 require 'SVG/Graph/BarHorizontal'
20 require 'digest/sha1'
20
21
21 class RepositoriesController < ApplicationController
22 class RepositoriesController < ApplicationController
22 layout 'base'
23 layout 'base'
23 before_filter :find_project
24 before_filter :find_project
24 before_filter :authorize, :except => [:stats, :graph]
25 before_filter :authorize, :except => [:stats, :graph]
25 before_filter :check_project_privacy, :only => [:stats, :graph]
26 before_filter :check_project_privacy, :only => [:stats, :graph]
26
27
27 def show
28 def show
28 # get entries for the browse frame
29 # get entries for the browse frame
29 @entries = @repository.scm.entries('')
30 @entries = @repository.scm.entries('')
30 show_error and return unless @entries
31 show_error and return unless @entries
31 # check if new revisions have been committed in the repository
32 # check if new revisions have been committed in the repository
32 scm_latestrev = @entries.revisions.latest
33 scm_latestrev = @entries.revisions.latest
33 if Setting.autofetch_changesets? && scm_latestrev && ((@repository.latest_changeset.nil?) || (@repository.latest_changeset.revision < scm_latestrev.identifier.to_i))
34 if Setting.autofetch_changesets? && scm_latestrev && ((@repository.latest_changeset.nil?) || (@repository.latest_changeset.revision < scm_latestrev.identifier.to_i))
34 @repository.fetch_changesets
35 @repository.fetch_changesets
35 @repository.reload
36 @repository.reload
36 end
37 end
37 @changesets = @repository.changesets.find(:all, :limit => 5, :order => "committed_on DESC")
38 @changesets = @repository.changesets.find(:all, :limit => 5, :order => "committed_on DESC")
38 end
39 end
39
40
40 def browse
41 def browse
41 @entries = @repository.scm.entries(@path, @rev)
42 @entries = @repository.scm.entries(@path, @rev)
42 show_error and return unless @entries
43 show_error and return unless @entries
43 end
44 end
44
45
45 def revisions
46 def revisions
46 unless @path == ''
47 unless @path == ''
47 @entry = @repository.scm.entry(@path, @rev)
48 @entry = @repository.scm.entry(@path, @rev)
48 show_error and return unless @entry
49 show_error and return unless @entry
49 end
50 end
50 @repository.changesets_with_path @path do
51 @repository.changesets_with_path @path do
51 @changeset_count = @repository.changesets.count
52 @changeset_count = @repository.changesets.count
52 @changeset_pages = Paginator.new self, @changeset_count,
53 @changeset_pages = Paginator.new self, @changeset_count,
53 25,
54 25,
54 params['page']
55 params['page']
55 @changesets = @repository.changesets.find(:all,
56 @changesets = @repository.changesets.find(:all,
56 :limit => @changeset_pages.items_per_page,
57 :limit => @changeset_pages.items_per_page,
57 :offset => @changeset_pages.current.offset)
58 :offset => @changeset_pages.current.offset)
58 end
59 end
59 render :action => "revisions", :layout => false if request.xhr?
60 render :action => "revisions", :layout => false if request.xhr?
60 end
61 end
61
62
62 def entry
63 def entry
63 if 'raw' == params[:format]
64 if 'raw' == params[:format]
64 content = @repository.scm.cat(@path, @rev)
65 content = @repository.scm.cat(@path, @rev)
65 show_error and return unless content
66 show_error and return unless content
66 send_data content, :filename => @path.split('/').last
67 send_data content, :filename => @path.split('/').last
67 end
68 end
68 end
69 end
69
70
70 def revision
71 def revision
71 @changeset = @repository.changesets.find_by_revision(@rev)
72 @changeset = @repository.changesets.find_by_revision(@rev)
72 show_error and return unless @changeset
73 show_error and return unless @changeset
73 end
74 end
74
75
75 def diff
76 def diff
76 @rev_to = params[:rev_to] || (@rev-1)
77 @rev_to = (params[:rev_to] && params[:rev_to].to_i > 0) ? params[:rev_to].to_i : (@rev - 1)
77 type = params[:type] || 'inline'
78 @diff_type = ('sbs' == params[:type]) ? 'sbs' : 'inline'
78 @diff = @repository.scm.diff(params[:path], @rev, @rev_to, type)
79
79 show_error and return unless @diff
80 @cache_key = "repositories/diff/#{@repository.id}/" + Digest::MD5.hexdigest("#{@path}-#{@rev}-#{@rev_to}-#{@diff_type}")
81 unless read_fragment(@cache_key)
82 @diff = @repository.scm.diff(@path, @rev, @rev_to, type)
83 show_error and return unless @diff
84 end
80 end
85 end
81
86
82 def stats
87 def stats
83 end
88 end
84
89
85 def graph
90 def graph
86 data = nil
91 data = nil
87 case params[:graph]
92 case params[:graph]
88 when "commits_per_month"
93 when "commits_per_month"
89 data = graph_commits_per_month(@repository)
94 data = graph_commits_per_month(@repository)
90 when "commits_per_author"
95 when "commits_per_author"
91 data = graph_commits_per_author(@repository)
96 data = graph_commits_per_author(@repository)
92 end
97 end
93 if data
98 if data
94 headers["Content-Type"] = "image/svg+xml"
99 headers["Content-Type"] = "image/svg+xml"
95 send_data(data, :type => "image/svg+xml", :disposition => "inline")
100 send_data(data, :type => "image/svg+xml", :disposition => "inline")
96 else
101 else
97 render_404
102 render_404
98 end
103 end
99 end
104 end
100
105
101 private
106 private
102 def find_project
107 def find_project
103 @project = Project.find(params[:id])
108 @project = Project.find(params[:id])
104 @repository = @project.repository
109 @repository = @project.repository
105 render_404 and return false unless @repository
110 render_404 and return false unless @repository
106 @path = params[:path].squeeze('/').gsub(/^\//, '') if params[:path]
111 @path = params[:path].squeeze('/').gsub(/^\//, '') if params[:path]
107 @path ||= ''
112 @path ||= ''
108 @rev = params[:rev].to_i if params[:rev] and params[:rev].to_i > 0
113 @rev = params[:rev].to_i if params[:rev] and params[:rev].to_i > 0
109 rescue ActiveRecord::RecordNotFound
114 rescue ActiveRecord::RecordNotFound
110 render_404
115 render_404
111 end
116 end
112
117
113 def show_error
118 def show_error
114 flash.now[:notice] = l(:notice_scm_error)
119 flash.now[:notice] = l(:notice_scm_error)
115 render :nothing => true, :layout => true
120 render :nothing => true, :layout => true
116 end
121 end
117
122
118 def graph_commits_per_month(repository)
123 def graph_commits_per_month(repository)
119 @date_to = Date.today
124 @date_to = Date.today
120 @date_from = @date_to << 12
125 @date_from = @date_to << 12
121 commits_by_day = repository.changesets.count(:all, :group => :commit_date, :conditions => ["commit_date BETWEEN ? AND ?", @date_from, @date_to])
126 commits_by_day = repository.changesets.count(:all, :group => :commit_date, :conditions => ["commit_date BETWEEN ? AND ?", @date_from, @date_to])
122 commits_by_month = [0] * 12
127 commits_by_month = [0] * 12
123 commits_by_day.each {|c| commits_by_month[c.first.to_date.months_ago] += c.last }
128 commits_by_day.each {|c| commits_by_month[c.first.to_date.months_ago] += c.last }
124
129
125 changes_by_day = repository.changes.count(:all, :group => :commit_date)
130 changes_by_day = repository.changes.count(:all, :group => :commit_date)
126 changes_by_month = [0] * 12
131 changes_by_month = [0] * 12
127 changes_by_day.each {|c| changes_by_month[c.first.to_date.months_ago] += c.last }
132 changes_by_day.each {|c| changes_by_month[c.first.to_date.months_ago] += c.last }
128
133
129 fields = []
134 fields = []
130 month_names = l(:actionview_datehelper_select_month_names_abbr).split(',')
135 month_names = l(:actionview_datehelper_select_month_names_abbr).split(',')
131 12.times {|m| fields << month_names[((Date.today.month - 1 - m) % 12)]}
136 12.times {|m| fields << month_names[((Date.today.month - 1 - m) % 12)]}
132
137
133 graph = SVG::Graph::Bar.new(
138 graph = SVG::Graph::Bar.new(
134 :height => 300,
139 :height => 300,
135 :width => 500,
140 :width => 500,
136 :fields => fields.reverse,
141 :fields => fields.reverse,
137 :stack => :side,
142 :stack => :side,
138 :scale_integers => true,
143 :scale_integers => true,
139 :step_x_labels => 2,
144 :step_x_labels => 2,
140 :show_data_values => false,
145 :show_data_values => false,
141 :graph_title => l(:label_commits_per_month),
146 :graph_title => l(:label_commits_per_month),
142 :show_graph_title => true
147 :show_graph_title => true
143 )
148 )
144
149
145 graph.add_data(
150 graph.add_data(
146 :data => commits_by_month[0..11].reverse,
151 :data => commits_by_month[0..11].reverse,
147 :title => l(:label_revision_plural)
152 :title => l(:label_revision_plural)
148 )
153 )
149
154
150 graph.add_data(
155 graph.add_data(
151 :data => changes_by_month[0..11].reverse,
156 :data => changes_by_month[0..11].reverse,
152 :title => l(:label_change_plural)
157 :title => l(:label_change_plural)
153 )
158 )
154
159
155 graph.burn
160 graph.burn
156 end
161 end
157
162
158 def graph_commits_per_author(repository)
163 def graph_commits_per_author(repository)
159 commits_by_author = repository.changesets.count(:all, :group => :committer)
164 commits_by_author = repository.changesets.count(:all, :group => :committer)
160 commits_by_author.sort! {|x, y| x.last <=> y.last}
165 commits_by_author.sort! {|x, y| x.last <=> y.last}
161
166
162 changes_by_author = repository.changes.count(:all, :group => :committer)
167 changes_by_author = repository.changes.count(:all, :group => :committer)
163 h = changes_by_author.inject({}) {|o, i| o[i.first] = i.last; o}
168 h = changes_by_author.inject({}) {|o, i| o[i.first] = i.last; o}
164
169
165 fields = commits_by_author.collect {|r| r.first}
170 fields = commits_by_author.collect {|r| r.first}
166 commits_data = commits_by_author.collect {|r| r.last}
171 commits_data = commits_by_author.collect {|r| r.last}
167 changes_data = commits_by_author.collect {|r| h[r.first] || 0}
172 changes_data = commits_by_author.collect {|r| h[r.first] || 0}
168
173
169 fields = fields + [""]*(10 - fields.length) if fields.length<10
174 fields = fields + [""]*(10 - fields.length) if fields.length<10
170 commits_data = commits_data + [0]*(10 - commits_data.length) if commits_data.length<10
175 commits_data = commits_data + [0]*(10 - commits_data.length) if commits_data.length<10
171 changes_data = changes_data + [0]*(10 - changes_data.length) if changes_data.length<10
176 changes_data = changes_data + [0]*(10 - changes_data.length) if changes_data.length<10
172
177
173 graph = SVG::Graph::BarHorizontal.new(
178 graph = SVG::Graph::BarHorizontal.new(
174 :height => 300,
179 :height => 300,
175 :width => 500,
180 :width => 500,
176 :fields => fields,
181 :fields => fields,
177 :stack => :side,
182 :stack => :side,
178 :scale_integers => true,
183 :scale_integers => true,
179 :show_data_values => false,
184 :show_data_values => false,
180 :rotate_y_labels => false,
185 :rotate_y_labels => false,
181 :graph_title => l(:label_commits_per_author),
186 :graph_title => l(:label_commits_per_author),
182 :show_graph_title => true
187 :show_graph_title => true
183 )
188 )
184
189
185 graph.add_data(
190 graph.add_data(
186 :data => commits_data,
191 :data => commits_data,
187 :title => l(:label_revision_plural)
192 :title => l(:label_revision_plural)
188 )
193 )
189
194
190 graph.add_data(
195 graph.add_data(
191 :data => changes_data,
196 :data => changes_data,
192 :title => l(:label_change_plural)
197 :title => l(:label_change_plural)
193 )
198 )
194
199
195 graph.burn
200 graph.burn
196 end
201 end
197
202
198 end
203 end
199
204
200 class Date
205 class Date
201 def months_ago(date = Date.today)
206 def months_ago(date = Date.today)
202 (date.year - self.year)*12 + (date.month - self.month)
207 (date.year - self.year)*12 + (date.month - self.month)
203 end
208 end
204
209
205 def weeks_ago(date = Date.today)
210 def weeks_ago(date = Date.today)
206 (date.year - self.year)*52 + (date.cweek - self.cweek)
211 (date.year - self.year)*52 + (date.cweek - self.cweek)
207 end
212 end
208 end
213 end
@@ -1,89 +1,91
1 <h2><%= l(:label_revision) %> <%= @rev %>: <%= @path.gsub(/^.*\//, '') %></h2>
1 <h2><%= l(:label_revision) %> <%= @rev %>: <%= @path.gsub(/^.*\//, '') %></h2>
2
2
3 <!-- Choose view type -->
3 <!-- Choose view type -->
4 <% form_tag({ :controller => 'repositories', :action => 'diff'}, :method => 'get') do %>
4 <% form_tag({ :controller => 'repositories', :action => 'diff'}, :method => 'get') do %>
5 <% params.each do |k, p| %>
5 <% params.each do |k, p| %>
6 <% if k != "type" %>
6 <% if k != "type" %>
7 <%= hidden_field_tag(k,p) %>
7 <%= hidden_field_tag(k,p) %>
8 <% end %>
8 <% end %>
9 <% end %>
9 <% end %>
10 <p><label><%= l(:label_view_diff) %></label>
10 <p><label><%= l(:label_view_diff) %></label>
11 <%= select_tag 'type', options_for_select([[l(:label_diff_inline), "inline"], [l(:label_diff_side_by_side), "sbs"]], params[:type]), :onchange => "if (this.value != '') {this.form.submit()}" %>
11 <%= select_tag 'type', options_for_select([[l(:label_diff_inline), "inline"], [l(:label_diff_side_by_side), "sbs"]], params[:type]), :onchange => "if (this.value != '') {this.form.submit()}" %></p>
12 <%= submit_tag l(:button_apply) %></p>
13 <% end %>
12 <% end %>
13
14 <% cache(@cache_key) do %>
14 <% @diff.each do |table_file| %>
15 <% @diff.each do |table_file| %>
15 <% if params[:type] == 'sbs' %>
16 <% if @diff_type == 'sbs' %>
16 <table class="list">
17 <table class="list">
17 <thead>
18 <thead>
18 <tr>
19 <tr>
19 <th colspan="4" class="list-filename">
20 <th colspan="4" class="list-filename">
20 <%= l(:label_attachment) %>: <%= table_file.file_name %>
21 <%= table_file.file_name %>
21 </th>
22 </th>
22 </tr>
23 </tr>
23 <tr>
24 <tr>
24 <th colspan="2"><%= l(:label_revision) %> <%= @rev %></th>
25 <th colspan="2">@<%= @rev %></th>
25 <th colspan="2"><%= l(:label_revision) %> <%= @rev_to %></th>
26 <th colspan="2">@<%= @rev_to %></th>
26 </tr>
27 </tr>
27 </thead>
28 </thead>
28 <tbody>
29 <tbody>
29 <% table_file.keys.sort.each do |key| %>
30 <% table_file.keys.sort.each do |key| %>
30 <tr>
31 <tr>
31 <th class="line-num">
32 <th class="line-num">
32 <%= table_file[key].nb_line_left %>
33 <%= table_file[key].nb_line_left %>
33 </th>
34 </th>
34 <td class="line-code <%= table_file[key].type_diff_left %>">
35 <td class="line-code <%= table_file[key].type_diff_left %>">
35 <%= table_file[key].line_left %>
36 <%= table_file[key].line_left %>
36 </td>
37 </td>
37 <th class="line-num">
38 <th class="line-num">
38 <%= table_file[key].nb_line_right %>
39 <%= table_file[key].nb_line_right %>
39 </th>
40 </th>
40 <td class="line-code <%= table_file[key].type_diff_right %>">
41 <td class="line-code <%= table_file[key].type_diff_right %>">
41 <%= table_file[key].line_right %>
42 <%= table_file[key].line_right %>
42 </td>
43 </td>
43 </tr>
44 </tr>
44 <% end %>
45 <% end %>
45 </tbody>
46 </tbody>
46 </table>
47 </table>
47
48
48 <% else %>
49 <% else %>
49 <table class="list">
50 <table class="list">
50 <thead>
51 <thead>
51 <tr>
52 <tr>
52 <th colspan="3" class="list-filename">
53 <th colspan="3" class="list-filename">
53 <%= l(:label_attachment) %>: <%= table_file.file_name %>
54 <%= table_file.file_name %>
54 </th>
55 </th>
55 </tr>
56 </tr>
56 <tr>
57 <tr>
57 <th>@<%= @rev %></th>
58 <th>@<%= @rev %></th>
58 <th>@<%= @rev_to %></th>
59 <th>@<%= @rev_to %></th>
59 <th></th>
60 <th></th>
60 </tr>
61 </tr>
61 </thead>
62 </thead>
62 <tbody>
63 <tbody>
63 <% table_file.keys.sort.each do |key, line| %>
64 <% table_file.keys.sort.each do |key, line| %>
64 <tr>
65 <tr>
65 <th class="line-num">
66 <th class="line-num">
66 <%= table_file[key].nb_line_left %>
67 <%= table_file[key].nb_line_left %>
67 </th>
68 </th>
68 <th class="line-num">
69 <th class="line-num">
69 <%= table_file[key].nb_line_right %>
70 <%= table_file[key].nb_line_right %>
70 </th>
71 </th>
71 <% if table_file[key].line_left.empty? %>
72 <% if table_file[key].line_left.empty? %>
72 <td class="line-code <%= table_file[key].type_diff_right %>">
73 <td class="line-code <%= table_file[key].type_diff_right %>">
73 <%= table_file[key].line_right %>
74 <%= table_file[key].line_right %>
74 </td>
75 </td>
75 <% else %>
76 <% else %>
76 <td class="line-code <%= table_file[key].type_diff_left %>">
77 <td class="line-code <%= table_file[key].type_diff_left %>">
77 <%= table_file[key].line_left %>
78 <%= table_file[key].line_left %>
78 </td>
79 </td>
79 <% end %>
80 <% end %>
80 </tr>
81 </tr>
81 <% end %>
82 <% end %>
82 </tbody>
83 </tbody>
83 </table>
84 </table>
84 <% end %>
85 <% end %>
85 <% end %>
86 <% end %>
87 <% end %>
86
88
87 <% content_for :header_tags do %>
89 <% content_for :header_tags do %>
88 <%= stylesheet_link_tag "scm" %>
90 <%= stylesheet_link_tag "scm" %>
89 <% end %>
91 <% end %>
General Comments 0
You need to be logged in to leave comments. Login now