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