@@ -1,213 +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 | require 'digest/sha1' | |
21 |
|
21 | |||
22 | class RepositoriesController < ApplicationController |
|
22 | class RepositoriesController < ApplicationController | |
23 | layout 'base' |
|
23 | layout 'base' | |
24 | before_filter :find_project |
|
24 | before_filter :find_project | |
25 | before_filter :authorize, :except => [:stats, :graph] |
|
25 | before_filter :authorize, :except => [:stats, :graph] | |
26 | before_filter :check_project_privacy, :only => [:stats, :graph] |
|
26 | before_filter :check_project_privacy, :only => [:stats, :graph] | |
27 |
|
27 | |||
28 | def show |
|
28 | def show | |
29 | # get entries for the browse frame |
|
29 | # get entries for the browse frame | |
30 | @entries = @repository.scm.entries('') |
|
30 | @entries = @repository.scm.entries('') | |
31 | show_error and return unless @entries |
|
31 | show_error and return unless @entries | |
32 | # check if new revisions have been committed in the repository |
|
32 | # check if new revisions have been committed in the repository | |
33 | scm_latestrev = @entries.revisions.latest |
|
33 | scm_latestrev = @entries.revisions.latest | |
34 | 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)) | |
35 | @repository.fetch_changesets |
|
35 | @repository.fetch_changesets | |
36 | @repository.reload |
|
36 | @repository.reload | |
37 | end |
|
37 | end | |
38 | @changesets = @repository.changesets.find(:all, :limit => 5, :order => "committed_on DESC") |
|
38 | @changesets = @repository.changesets.find(:all, :limit => 5, :order => "committed_on DESC") | |
39 | end |
|
39 | end | |
40 |
|
40 | |||
41 | def browse |
|
41 | def browse | |
42 | @entries = @repository.scm.entries(@path, @rev) |
|
42 | @entries = @repository.scm.entries(@path, @rev) | |
43 | show_error and return unless @entries |
|
43 | show_error and return unless @entries | |
44 | end |
|
44 | end | |
45 |
|
45 | |||
46 | def revisions |
|
46 | def revisions | |
47 | unless @path == '' |
|
47 | unless @path == '' | |
48 | @entry = @repository.scm.entry(@path, @rev) |
|
48 | @entry = @repository.scm.entry(@path, @rev) | |
49 | show_error and return unless @entry |
|
49 | show_error and return unless @entry | |
50 | end |
|
50 | end | |
51 | @repository.changesets_with_path @path do |
|
51 | @repository.changesets_with_path @path do | |
52 | @changeset_count = @repository.changesets.count |
|
52 | @changeset_count = @repository.changesets.count | |
53 | @changeset_pages = Paginator.new self, @changeset_count, |
|
53 | @changeset_pages = Paginator.new self, @changeset_count, | |
54 | 25, |
|
54 | 25, | |
55 | params['page'] |
|
55 | params['page'] | |
56 | @changesets = @repository.changesets.find(:all, |
|
56 | @changesets = @repository.changesets.find(:all, | |
57 | :limit => @changeset_pages.items_per_page, |
|
57 | :limit => @changeset_pages.items_per_page, | |
58 | :offset => @changeset_pages.current.offset) |
|
58 | :offset => @changeset_pages.current.offset) | |
59 | end |
|
59 | end | |
60 | render :action => "revisions", :layout => false if request.xhr? |
|
60 | render :action => "revisions", :layout => false if request.xhr? | |
61 | end |
|
61 | end | |
62 |
|
62 | |||
63 | def entry |
|
63 | def entry | |
64 | @content = @repository.scm.cat(@path, @rev) |
|
64 | @content = @repository.scm.cat(@path, @rev) | |
65 | show_error and return unless @content |
|
65 | show_error and return unless @content | |
66 | if 'raw' == params[:format] |
|
66 | if 'raw' == params[:format] | |
67 | send_data @content, :filename => @path.split('/').last |
|
67 | send_data @content, :filename => @path.split('/').last | |
68 | end |
|
68 | end | |
69 | end |
|
69 | end | |
70 |
|
70 | |||
71 | def revision |
|
71 | def revision | |
72 | @changeset = @repository.changesets.find_by_revision(@rev) |
|
72 | @changeset = @repository.changesets.find_by_revision(@rev) | |
73 | show_error and return unless @changeset |
|
73 | show_error and return unless @changeset | |
74 | end |
|
74 | end | |
75 |
|
75 | |||
76 | def diff |
|
76 | def diff | |
77 | @rev_to = (params[:rev_to] && params[:rev_to].to_i > 0) ? params[:rev_to].to_i : (@rev - 1) |
|
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' |
|
78 | @diff_type = ('sbs' == params[:type]) ? 'sbs' : 'inline' | |
79 |
|
79 | |||
80 | @cache_key = "repositories/diff/#{@repository.id}/" + Digest::MD5.hexdigest("#{@path}-#{@rev}-#{@rev_to}-#{@diff_type}") |
|
80 | @cache_key = "repositories/diff/#{@repository.id}/" + Digest::MD5.hexdigest("#{@path}-#{@rev}-#{@rev_to}-#{@diff_type}") | |
81 | unless read_fragment(@cache_key) |
|
81 | unless read_fragment(@cache_key) | |
82 | @diff = @repository.scm.diff(@path, @rev, @rev_to, type) |
|
82 | @diff = @repository.scm.diff(@path, @rev, @rev_to, type) | |
83 | show_error and return unless @diff |
|
83 | show_error and return unless @diff | |
84 | end |
|
84 | end | |
85 | end |
|
85 | end | |
86 |
|
86 | |||
87 | def stats |
|
87 | def stats | |
88 | end |
|
88 | end | |
89 |
|
89 | |||
90 | def graph |
|
90 | def graph | |
91 | data = nil |
|
91 | data = nil | |
92 | case params[:graph] |
|
92 | case params[:graph] | |
93 | when "commits_per_month" |
|
93 | when "commits_per_month" | |
94 | data = graph_commits_per_month(@repository) |
|
94 | data = graph_commits_per_month(@repository) | |
95 | when "commits_per_author" |
|
95 | when "commits_per_author" | |
96 | data = graph_commits_per_author(@repository) |
|
96 | data = graph_commits_per_author(@repository) | |
97 | end |
|
97 | end | |
98 | if data |
|
98 | if data | |
99 | headers["Content-Type"] = "image/svg+xml" |
|
99 | headers["Content-Type"] = "image/svg+xml" | |
100 | send_data(data, :type => "image/svg+xml", :disposition => "inline") |
|
100 | send_data(data, :type => "image/svg+xml", :disposition => "inline") | |
101 | else |
|
101 | else | |
102 | render_404 |
|
102 | render_404 | |
103 | end |
|
103 | end | |
104 | end |
|
104 | end | |
105 |
|
105 | |||
106 | private |
|
106 | private | |
107 | def find_project |
|
107 | def find_project | |
108 | @project = Project.find(params[:id]) |
|
108 | @project = Project.find(params[:id]) | |
109 | @repository = @project.repository |
|
109 | @repository = @project.repository | |
110 | render_404 and return false unless @repository |
|
110 | render_404 and return false unless @repository | |
111 |
@path = params[:path].squeeze('/') |
|
111 | @path = params[:path].squeeze('/') if params[:path] | |
112 | @path ||= '' |
|
112 | @path ||= '' | |
113 | @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 | |
114 | rescue ActiveRecord::RecordNotFound |
|
114 | rescue ActiveRecord::RecordNotFound | |
115 | render_404 |
|
115 | render_404 | |
116 | end |
|
116 | end | |
117 |
|
117 | |||
118 | def show_error |
|
118 | def show_error | |
119 | flash.now[:notice] = l(:notice_scm_error) |
|
119 | flash.now[:notice] = l(:notice_scm_error) | |
120 | render :nothing => true, :layout => true |
|
120 | render :nothing => true, :layout => true | |
121 | end |
|
121 | end | |
122 |
|
122 | |||
123 | def graph_commits_per_month(repository) |
|
123 | def graph_commits_per_month(repository) | |
124 | @date_to = Date.today |
|
124 | @date_to = Date.today | |
125 | @date_from = @date_to << 12 |
|
125 | @date_from = @date_to << 12 | |
126 | 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]) | |
127 | commits_by_month = [0] * 12 |
|
127 | commits_by_month = [0] * 12 | |
128 | 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 } | |
129 |
|
129 | |||
130 | changes_by_day = repository.changes.count(:all, :group => :commit_date) |
|
130 | changes_by_day = repository.changes.count(:all, :group => :commit_date) | |
131 | changes_by_month = [0] * 12 |
|
131 | changes_by_month = [0] * 12 | |
132 | 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 } | |
133 |
|
133 | |||
134 | fields = [] |
|
134 | fields = [] | |
135 | month_names = l(:actionview_datehelper_select_month_names_abbr).split(',') |
|
135 | month_names = l(:actionview_datehelper_select_month_names_abbr).split(',') | |
136 | 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)]} | |
137 |
|
137 | |||
138 | graph = SVG::Graph::Bar.new( |
|
138 | graph = SVG::Graph::Bar.new( | |
139 | :height => 300, |
|
139 | :height => 300, | |
140 | :width => 500, |
|
140 | :width => 500, | |
141 | :fields => fields.reverse, |
|
141 | :fields => fields.reverse, | |
142 | :stack => :side, |
|
142 | :stack => :side, | |
143 | :scale_integers => true, |
|
143 | :scale_integers => true, | |
144 | :step_x_labels => 2, |
|
144 | :step_x_labels => 2, | |
145 | :show_data_values => false, |
|
145 | :show_data_values => false, | |
146 | :graph_title => l(:label_commits_per_month), |
|
146 | :graph_title => l(:label_commits_per_month), | |
147 | :show_graph_title => true |
|
147 | :show_graph_title => true | |
148 | ) |
|
148 | ) | |
149 |
|
149 | |||
150 | graph.add_data( |
|
150 | graph.add_data( | |
151 | :data => commits_by_month[0..11].reverse, |
|
151 | :data => commits_by_month[0..11].reverse, | |
152 | :title => l(:label_revision_plural) |
|
152 | :title => l(:label_revision_plural) | |
153 | ) |
|
153 | ) | |
154 |
|
154 | |||
155 | graph.add_data( |
|
155 | graph.add_data( | |
156 | :data => changes_by_month[0..11].reverse, |
|
156 | :data => changes_by_month[0..11].reverse, | |
157 | :title => l(:label_change_plural) |
|
157 | :title => l(:label_change_plural) | |
158 | ) |
|
158 | ) | |
159 |
|
159 | |||
160 | graph.burn |
|
160 | graph.burn | |
161 | end |
|
161 | end | |
162 |
|
162 | |||
163 | def graph_commits_per_author(repository) |
|
163 | def graph_commits_per_author(repository) | |
164 | commits_by_author = repository.changesets.count(:all, :group => :committer) |
|
164 | commits_by_author = repository.changesets.count(:all, :group => :committer) | |
165 | commits_by_author.sort! {|x, y| x.last <=> y.last} |
|
165 | commits_by_author.sort! {|x, y| x.last <=> y.last} | |
166 |
|
166 | |||
167 | changes_by_author = repository.changes.count(:all, :group => :committer) |
|
167 | changes_by_author = repository.changes.count(:all, :group => :committer) | |
168 | 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} | |
169 |
|
169 | |||
170 | fields = commits_by_author.collect {|r| r.first} |
|
170 | fields = commits_by_author.collect {|r| r.first} | |
171 | commits_data = commits_by_author.collect {|r| r.last} |
|
171 | commits_data = commits_by_author.collect {|r| r.last} | |
172 | changes_data = commits_by_author.collect {|r| h[r.first] || 0} |
|
172 | changes_data = commits_by_author.collect {|r| h[r.first] || 0} | |
173 |
|
173 | |||
174 | fields = fields + [""]*(10 - fields.length) if fields.length<10 |
|
174 | fields = fields + [""]*(10 - fields.length) if fields.length<10 | |
175 | 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 | |
176 | 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 | |
177 |
|
177 | |||
178 | graph = SVG::Graph::BarHorizontal.new( |
|
178 | graph = SVG::Graph::BarHorizontal.new( | |
179 | :height => 300, |
|
179 | :height => 300, | |
180 | :width => 500, |
|
180 | :width => 500, | |
181 | :fields => fields, |
|
181 | :fields => fields, | |
182 | :stack => :side, |
|
182 | :stack => :side, | |
183 | :scale_integers => true, |
|
183 | :scale_integers => true, | |
184 | :show_data_values => false, |
|
184 | :show_data_values => false, | |
185 | :rotate_y_labels => false, |
|
185 | :rotate_y_labels => false, | |
186 | :graph_title => l(:label_commits_per_author), |
|
186 | :graph_title => l(:label_commits_per_author), | |
187 | :show_graph_title => true |
|
187 | :show_graph_title => true | |
188 | ) |
|
188 | ) | |
189 |
|
189 | |||
190 | graph.add_data( |
|
190 | graph.add_data( | |
191 | :data => commits_data, |
|
191 | :data => commits_data, | |
192 | :title => l(:label_revision_plural) |
|
192 | :title => l(:label_revision_plural) | |
193 | ) |
|
193 | ) | |
194 |
|
194 | |||
195 | graph.add_data( |
|
195 | graph.add_data( | |
196 | :data => changes_data, |
|
196 | :data => changes_data, | |
197 | :title => l(:label_change_plural) |
|
197 | :title => l(:label_change_plural) | |
198 | ) |
|
198 | ) | |
199 |
|
199 | |||
200 | graph.burn |
|
200 | graph.burn | |
201 | end |
|
201 | end | |
202 |
|
202 | |||
203 | end |
|
203 | end | |
204 |
|
204 | |||
205 | class Date |
|
205 | class Date | |
206 | def months_ago(date = Date.today) |
|
206 | def months_ago(date = Date.today) | |
207 | (date.year - self.year)*12 + (date.month - self.month) |
|
207 | (date.year - self.year)*12 + (date.month - self.month) | |
208 | end |
|
208 | end | |
209 |
|
209 | |||
210 | def weeks_ago(date = Date.today) |
|
210 | def weeks_ago(date = Date.today) | |
211 | (date.year - self.year)*52 + (date.cweek - self.cweek) |
|
211 | (date.year - self.year)*52 + (date.cweek - self.cweek) | |
212 | end |
|
212 | end | |
213 | end |
|
213 | end |
General Comments 0
You need to be logged in to leave comments.
Login now