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