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