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