@@ -1,324 +1,313 | |||
|
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 | 20 | require 'digest/sha1' |
|
21 | 21 | |
|
22 | 22 | class ChangesetNotFound < Exception; end |
|
23 | 23 | class InvalidRevisionParam < Exception; end |
|
24 | 24 | |
|
25 | 25 | class RepositoriesController < ApplicationController |
|
26 | 26 | layout 'base' |
|
27 | 27 | menu_item :repository |
|
28 | 28 | before_filter :find_repository, :except => :edit |
|
29 | 29 | before_filter :find_project, :only => :edit |
|
30 | 30 | before_filter :authorize |
|
31 | 31 | accept_key_auth :revisions |
|
32 | 32 | |
|
33 | rescue_from Redmine::Scm::Adapters::CommandFailed, :with => :show_error_command_failed | |
|
34 | ||
|
33 | 35 | def edit |
|
34 | 36 | @repository = @project.repository |
|
35 | 37 | if !@repository |
|
36 | 38 | @repository = Repository.factory(params[:repository_scm]) |
|
37 | 39 | @repository.project = @project if @repository |
|
38 | 40 | end |
|
39 | 41 | if request.post? && @repository |
|
40 | 42 | @repository.attributes = params[:repository] |
|
41 | 43 | @repository.save |
|
42 | 44 | end |
|
43 | 45 | render(:update) {|page| page.replace_html "tab-content-repository", :partial => 'projects/settings/repository'} |
|
44 | 46 | end |
|
45 | 47 | |
|
46 | 48 | def destroy |
|
47 | 49 | @repository.destroy |
|
48 | 50 | redirect_to :controller => 'projects', :action => 'settings', :id => @project, :tab => 'repository' |
|
49 | 51 | end |
|
50 | 52 | |
|
51 | 53 | def show |
|
52 | 54 | # check if new revisions have been committed in the repository |
|
53 | 55 | @repository.fetch_changesets if Setting.autofetch_changesets? |
|
54 | 56 | # root entries |
|
55 | 57 | @entries = @repository.entries('', @rev) |
|
56 | 58 | # latest changesets |
|
57 | 59 | @changesets = @repository.changesets.find(:all, :limit => 10, :order => "committed_on DESC") |
|
58 | 60 | show_error_not_found unless @entries || @changesets.any? |
|
59 | rescue Redmine::Scm::Adapters::CommandFailed => e | |
|
60 | show_error_command_failed(e.message) | |
|
61 | 61 | end |
|
62 | 62 | |
|
63 | 63 | def browse |
|
64 | 64 | @entries = @repository.entries(@path, @rev) |
|
65 | 65 | if request.xhr? |
|
66 | 66 | @entries ? render(:partial => 'dir_list_content') : render(:nothing => true) |
|
67 | 67 | else |
|
68 | 68 | show_error_not_found and return unless @entries |
|
69 | 69 | render :action => 'browse' |
|
70 | 70 | end |
|
71 | rescue Redmine::Scm::Adapters::CommandFailed => e | |
|
72 | show_error_command_failed(e.message) | |
|
73 | 71 | end |
|
74 | 72 | |
|
75 | 73 | def changes |
|
76 | 74 | @entry = @repository.entry(@path, @rev) |
|
77 | 75 | show_error_not_found and return unless @entry |
|
78 | 76 | @changesets = @repository.changesets_for_path(@path) |
|
79 | rescue Redmine::Scm::Adapters::CommandFailed => e | |
|
80 | show_error_command_failed(e.message) | |
|
81 | 77 | end |
|
82 | 78 | |
|
83 | 79 | def revisions |
|
84 | 80 | @changeset_count = @repository.changesets.count |
|
85 | 81 | @changeset_pages = Paginator.new self, @changeset_count, |
|
86 | 82 | per_page_option, |
|
87 | 83 | params['page'] |
|
88 | 84 | @changesets = @repository.changesets.find(:all, |
|
89 | 85 | :limit => @changeset_pages.items_per_page, |
|
90 | 86 | :offset => @changeset_pages.current.offset) |
|
91 | 87 | |
|
92 | 88 | respond_to do |format| |
|
93 | 89 | format.html { render :layout => false if request.xhr? } |
|
94 | 90 | format.atom { render_feed(@changesets, :title => "#{@project.name}: #{l(:label_revision_plural)}") } |
|
95 | 91 | end |
|
96 | 92 | end |
|
97 | 93 | |
|
98 | 94 | def entry |
|
99 | 95 | @entry = @repository.entry(@path, @rev) |
|
100 | 96 | show_error_not_found and return unless @entry |
|
101 | 97 | |
|
102 | 98 | # If the entry is a dir, show the browser |
|
103 | 99 | browse and return if @entry.is_dir? |
|
104 | 100 | |
|
105 | 101 | @content = @repository.cat(@path, @rev) |
|
106 | 102 | show_error_not_found and return unless @content |
|
107 | 103 | if 'raw' == params[:format] || @content.is_binary_data? |
|
108 | 104 | # Force the download if it's a binary file |
|
109 | 105 | send_data @content, :filename => @path.split('/').last |
|
110 | 106 | else |
|
111 | 107 | # Prevent empty lines when displaying a file with Windows style eol |
|
112 | 108 | @content.gsub!("\r\n", "\n") |
|
113 | 109 | end |
|
114 | rescue Redmine::Scm::Adapters::CommandFailed => e | |
|
115 | show_error_command_failed(e.message) | |
|
116 | 110 | end |
|
117 | 111 | |
|
118 | 112 | def annotate |
|
119 | 113 | @annotate = @repository.scm.annotate(@path, @rev) |
|
120 | 114 | render_error l(:error_scm_annotate) and return if @annotate.nil? || @annotate.empty? |
|
121 | rescue Redmine::Scm::Adapters::CommandFailed => e | |
|
122 | show_error_command_failed(e.message) | |
|
123 | 115 | end |
|
124 | 116 | |
|
125 | 117 | def revision |
|
126 | 118 | @changeset = @repository.changesets.find_by_revision(@rev) |
|
127 | 119 | raise ChangesetNotFound unless @changeset |
|
128 | 120 | @changes_count = @changeset.changes.size |
|
129 | 121 | @changes_pages = Paginator.new self, @changes_count, 150, params['page'] |
|
130 | 122 | @changes = @changeset.changes.find(:all, |
|
131 | 123 | :limit => @changes_pages.items_per_page, |
|
132 | 124 | :offset => @changes_pages.current.offset) |
|
133 | 125 | |
|
134 | 126 | respond_to do |format| |
|
135 | 127 | format.html |
|
136 | 128 | format.js {render :layout => false} |
|
137 | 129 | end |
|
138 | 130 | rescue ChangesetNotFound |
|
139 | 131 | show_error_not_found |
|
140 | rescue Redmine::Scm::Adapters::CommandFailed => e | |
|
141 | show_error_command_failed(e.message) | |
|
142 | 132 | end |
|
143 | 133 | |
|
144 | 134 | def diff |
|
145 | 135 | if params[:format] == 'diff' |
|
146 | 136 | @diff = @repository.diff(@path, @rev, @rev_to) |
|
147 | 137 | show_error_not_found and return unless @diff |
|
148 | 138 | filename = "changeset_r#{@rev}" |
|
149 | 139 | filename << "_r#{@rev_to}" if @rev_to |
|
150 | 140 | send_data @diff.join, :filename => "#{filename}.diff", |
|
151 | 141 | :type => 'text/x-patch', |
|
152 | 142 | :disposition => 'attachment' |
|
153 | 143 | else |
|
154 | 144 | @diff_type = params[:type] || User.current.pref[:diff_type] || 'inline' |
|
155 | 145 | @diff_type = 'inline' unless %w(inline sbs).include?(@diff_type) |
|
156 | 146 | |
|
157 | 147 | # Save diff type as user preference |
|
158 | 148 | if User.current.logged? && @diff_type != User.current.pref[:diff_type] |
|
159 | 149 | User.current.pref[:diff_type] = @diff_type |
|
160 | 150 | User.current.preference.save |
|
161 | 151 | end |
|
162 | 152 | |
|
163 | 153 | @cache_key = "repositories/diff/#{@repository.id}/" + Digest::MD5.hexdigest("#{@path}-#{@rev}-#{@rev_to}-#{@diff_type}") |
|
164 | 154 | unless read_fragment(@cache_key) |
|
165 | 155 | @diff = @repository.diff(@path, @rev, @rev_to) |
|
166 | 156 | show_error_not_found unless @diff |
|
167 | 157 | end |
|
168 | 158 | end |
|
169 | rescue Redmine::Scm::Adapters::CommandFailed => e | |
|
170 | show_error_command_failed(e.message) | |
|
171 | 159 | end |
|
172 | 160 | |
|
173 | 161 | def stats |
|
174 | 162 | end |
|
175 | 163 | |
|
176 | 164 | def graph |
|
177 | 165 | data = nil |
|
178 | 166 | case params[:graph] |
|
179 | 167 | when "commits_per_month" |
|
180 | 168 | data = graph_commits_per_month(@repository) |
|
181 | 169 | when "commits_per_author" |
|
182 | 170 | data = graph_commits_per_author(@repository) |
|
183 | 171 | end |
|
184 | 172 | if data |
|
185 | 173 | headers["Content-Type"] = "image/svg+xml" |
|
186 | 174 | send_data(data, :type => "image/svg+xml", :disposition => "inline") |
|
187 | 175 | else |
|
188 | 176 | render_404 |
|
189 | 177 | end |
|
190 | 178 | end |
|
191 | 179 | |
|
192 | 180 | private |
|
193 | 181 | def find_project |
|
194 | 182 | @project = Project.find(params[:id]) |
|
195 | 183 | rescue ActiveRecord::RecordNotFound |
|
196 | 184 | render_404 |
|
197 | 185 | end |
|
198 | 186 | |
|
199 | 187 | REV_PARAM_RE = %r{^[a-f0-9]*$} |
|
200 | 188 | |
|
201 | 189 | def find_repository |
|
202 | 190 | @project = Project.find(params[:id]) |
|
203 | 191 | @repository = @project.repository |
|
204 | 192 | render_404 and return false unless @repository |
|
205 | 193 | @path = params[:path].join('/') unless params[:path].nil? |
|
206 | 194 | @path ||= '' |
|
207 | 195 | @rev = params[:rev] |
|
208 | 196 | @rev_to = params[:rev_to] |
|
209 | 197 | raise InvalidRevisionParam unless @rev.to_s.match(REV_PARAM_RE) && @rev.to_s.match(REV_PARAM_RE) |
|
210 | 198 | rescue ActiveRecord::RecordNotFound |
|
211 | 199 | render_404 |
|
212 | 200 | rescue InvalidRevisionParam |
|
213 | 201 | show_error_not_found |
|
214 | 202 | end |
|
215 | 203 | |
|
216 | 204 | def show_error_not_found |
|
217 | 205 | render_error l(:error_scm_not_found) |
|
218 | 206 | end |
|
219 | 207 | |
|
220 | def show_error_command_failed(msg) | |
|
221 | render_error l(:error_scm_command_failed, msg) | |
|
208 | # Handler for Redmine::Scm::Adapters::CommandFailed exception | |
|
209 | def show_error_command_failed(exception) | |
|
210 | render_error l(:error_scm_command_failed, exception.message) | |
|
222 | 211 | end |
|
223 | 212 | |
|
224 | 213 | def graph_commits_per_month(repository) |
|
225 | 214 | @date_to = Date.today |
|
226 | 215 | @date_from = @date_to << 11 |
|
227 | 216 | @date_from = Date.civil(@date_from.year, @date_from.month, 1) |
|
228 | 217 | commits_by_day = repository.changesets.count(:all, :group => :commit_date, :conditions => ["commit_date BETWEEN ? AND ?", @date_from, @date_to]) |
|
229 | 218 | commits_by_month = [0] * 12 |
|
230 | 219 | commits_by_day.each {|c| commits_by_month[c.first.to_date.months_ago] += c.last } |
|
231 | 220 | |
|
232 | 221 | changes_by_day = repository.changes.count(:all, :group => :commit_date, :conditions => ["commit_date BETWEEN ? AND ?", @date_from, @date_to]) |
|
233 | 222 | changes_by_month = [0] * 12 |
|
234 | 223 | changes_by_day.each {|c| changes_by_month[c.first.to_date.months_ago] += c.last } |
|
235 | 224 | |
|
236 | 225 | fields = [] |
|
237 | 226 | month_names = l(:actionview_datehelper_select_month_names_abbr).split(',') |
|
238 | 227 | 12.times {|m| fields << month_names[((Date.today.month - 1 - m) % 12)]} |
|
239 | 228 | |
|
240 | 229 | graph = SVG::Graph::Bar.new( |
|
241 | 230 | :height => 300, |
|
242 | 231 | :width => 500, |
|
243 | 232 | :fields => fields.reverse, |
|
244 | 233 | :stack => :side, |
|
245 | 234 | :scale_integers => true, |
|
246 | 235 | :step_x_labels => 2, |
|
247 | 236 | :show_data_values => false, |
|
248 | 237 | :graph_title => l(:label_commits_per_month), |
|
249 | 238 | :show_graph_title => true |
|
250 | 239 | ) |
|
251 | 240 | |
|
252 | 241 | graph.add_data( |
|
253 | 242 | :data => commits_by_month[0..11].reverse, |
|
254 | 243 | :title => l(:label_revision_plural) |
|
255 | 244 | ) |
|
256 | 245 | |
|
257 | 246 | graph.add_data( |
|
258 | 247 | :data => changes_by_month[0..11].reverse, |
|
259 | 248 | :title => l(:label_change_plural) |
|
260 | 249 | ) |
|
261 | 250 | |
|
262 | 251 | graph.burn |
|
263 | 252 | end |
|
264 | 253 | |
|
265 | 254 | def graph_commits_per_author(repository) |
|
266 | 255 | commits_by_author = repository.changesets.count(:all, :group => :committer) |
|
267 | 256 | commits_by_author.sort! {|x, y| x.last <=> y.last} |
|
268 | 257 | |
|
269 | 258 | changes_by_author = repository.changes.count(:all, :group => :committer) |
|
270 | 259 | h = changes_by_author.inject({}) {|o, i| o[i.first] = i.last; o} |
|
271 | 260 | |
|
272 | 261 | fields = commits_by_author.collect {|r| r.first} |
|
273 | 262 | commits_data = commits_by_author.collect {|r| r.last} |
|
274 | 263 | changes_data = commits_by_author.collect {|r| h[r.first] || 0} |
|
275 | 264 | |
|
276 | 265 | fields = fields + [""]*(10 - fields.length) if fields.length<10 |
|
277 | 266 | commits_data = commits_data + [0]*(10 - commits_data.length) if commits_data.length<10 |
|
278 | 267 | changes_data = changes_data + [0]*(10 - changes_data.length) if changes_data.length<10 |
|
279 | 268 | |
|
280 | 269 | # Remove email adress in usernames |
|
281 | 270 | fields = fields.collect {|c| c.gsub(%r{<.+@.+>}, '') } |
|
282 | 271 | |
|
283 | 272 | graph = SVG::Graph::BarHorizontal.new( |
|
284 | 273 | :height => 300, |
|
285 | 274 | :width => 500, |
|
286 | 275 | :fields => fields, |
|
287 | 276 | :stack => :side, |
|
288 | 277 | :scale_integers => true, |
|
289 | 278 | :show_data_values => false, |
|
290 | 279 | :rotate_y_labels => false, |
|
291 | 280 | :graph_title => l(:label_commits_per_author), |
|
292 | 281 | :show_graph_title => true |
|
293 | 282 | ) |
|
294 | 283 | |
|
295 | 284 | graph.add_data( |
|
296 | 285 | :data => commits_data, |
|
297 | 286 | :title => l(:label_revision_plural) |
|
298 | 287 | ) |
|
299 | 288 | |
|
300 | 289 | graph.add_data( |
|
301 | 290 | :data => changes_data, |
|
302 | 291 | :title => l(:label_change_plural) |
|
303 | 292 | ) |
|
304 | 293 | |
|
305 | 294 | graph.burn |
|
306 | 295 | end |
|
307 | 296 | |
|
308 | 297 | end |
|
309 | 298 | |
|
310 | 299 | class Date |
|
311 | 300 | def months_ago(date = Date.today) |
|
312 | 301 | (date.year - self.year)*12 + (date.month - self.month) |
|
313 | 302 | end |
|
314 | 303 | |
|
315 | 304 | def weeks_ago(date = Date.today) |
|
316 | 305 | (date.year - self.year)*52 + (date.cweek - self.cweek) |
|
317 | 306 | end |
|
318 | 307 | end |
|
319 | 308 | |
|
320 | 309 | class String |
|
321 | 310 | def with_leading_slash |
|
322 | 311 | starts_with?('/') ? self : "/#{self}" |
|
323 | 312 | end |
|
324 | 313 | end |
General Comments 0
You need to be logged in to leave comments.
Login now