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