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