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