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