##// END OF EJS Templates
Fixed: empty lines when displaying repository files with Windows style eol....
Jean-Philippe Lang -
r980:38b185f1dc4d
parent child
Show More
@@ -1,277 +1,280
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 require 'digest/sha1'
21 21
22 22 class ChangesetNotFound < Exception
23 23 end
24 24
25 25 class RepositoriesController < ApplicationController
26 26 layout 'base'
27 27 before_filter :find_repository, :except => :edit
28 28 before_filter :find_project, :only => :edit
29 29 before_filter :authorize
30 30 accept_key_auth :revisions
31 31
32 32 def edit
33 33 @repository = @project.repository
34 34 if !@repository
35 35 @repository = Repository.factory(params[:repository_scm])
36 36 @repository.project = @project
37 37 end
38 38 if request.post?
39 39 @repository.attributes = params[:repository]
40 40 @repository.save
41 41 end
42 42 render(:update) {|page| page.replace_html "tab-content-repository", :partial => 'projects/settings/repository'}
43 43 end
44 44
45 45 def destroy
46 46 @repository.destroy
47 47 redirect_to :controller => 'projects', :action => 'settings', :id => @project, :tab => 'repository'
48 48 end
49 49
50 50 def show
51 51 # check if new revisions have been committed in the repository
52 52 @repository.fetch_changesets if Setting.autofetch_changesets?
53 53 # get entries for the browse frame
54 54 @entries = @repository.entries('')
55 55 # latest changesets
56 56 @changesets = @repository.changesets.find(:all, :limit => 10, :order => "committed_on DESC")
57 57 show_error and return unless @entries || @changesets.any?
58 58 end
59 59
60 60 def browse
61 61 @entries = @repository.entries(@path, @rev)
62 62 if request.xhr?
63 63 @entries ? render(:partial => 'dir_list_content') : render(:nothing => true)
64 64 else
65 65 show_error unless @entries
66 66 end
67 67 end
68 68
69 69 def changes
70 70 @entry = @repository.scm.entry(@path, @rev)
71 71 show_error and return unless @entry
72 72 @changesets = @repository.changesets_for_path(@path)
73 73 end
74 74
75 75 def revisions
76 76 @changeset_count = @repository.changesets.count
77 77 @changeset_pages = Paginator.new self, @changeset_count,
78 78 25,
79 79 params['page']
80 80 @changesets = @repository.changesets.find(:all,
81 81 :limit => @changeset_pages.items_per_page,
82 82 :offset => @changeset_pages.current.offset)
83 83
84 84 respond_to do |format|
85 85 format.html { render :layout => false if request.xhr? }
86 86 format.atom { render_feed(@changesets, :title => "#{@project.name}: #{l(:label_revision_plural)}") }
87 87 end
88 88 end
89 89
90 90 def entry
91 91 @content = @repository.scm.cat(@path, @rev)
92 92 show_error and return unless @content
93 93 if 'raw' == params[:format]
94 94 send_data @content, :filename => @path.split('/').last
95 else
96 # Prevent empty lines when displaying a file with Windows style eol
97 @content.gsub!("\r\n", "\n")
95 98 end
96 99 end
97 100
98 101 def annotate
99 102 @annotate = @repository.scm.annotate(@path, @rev)
100 103 show_error and return if @annotate.nil? || @annotate.empty?
101 104 end
102 105
103 106 def revision
104 107 @changeset = @repository.changesets.find_by_revision(@rev)
105 108 raise ChangesetNotFound unless @changeset
106 109 @changes_count = @changeset.changes.size
107 110 @changes_pages = Paginator.new self, @changes_count, 150, params['page']
108 111 @changes = @changeset.changes.find(:all,
109 112 :limit => @changes_pages.items_per_page,
110 113 :offset => @changes_pages.current.offset)
111 114
112 115 respond_to do |format|
113 116 format.html
114 117 format.js {render :layout => false}
115 118 end
116 119 rescue ChangesetNotFound
117 120 show_error
118 121 end
119 122
120 123 def diff
121 124 @rev_to = params[:rev_to] ? params[:rev_to].to_i : (@rev - 1)
122 125 @diff_type = params[:type] || User.current.pref[:diff_type] || 'inline'
123 126 @diff_type = 'inline' unless %w(inline sbs).include?(@diff_type)
124 127
125 128 # Save diff type as user preference
126 129 if User.current.logged? && @diff_type != User.current.pref[:diff_type]
127 130 User.current.pref[:diff_type] = @diff_type
128 131 User.current.preference.save
129 132 end
130 133
131 134 @cache_key = "repositories/diff/#{@repository.id}/" + Digest::MD5.hexdigest("#{@path}-#{@rev}-#{@rev_to}-#{@diff_type}")
132 135 unless read_fragment(@cache_key)
133 136 @diff = @repository.diff(@path, @rev, @rev_to, @diff_type)
134 137 show_error and return unless @diff
135 138 end
136 139 end
137 140
138 141 def stats
139 142 end
140 143
141 144 def graph
142 145 data = nil
143 146 case params[:graph]
144 147 when "commits_per_month"
145 148 data = graph_commits_per_month(@repository)
146 149 when "commits_per_author"
147 150 data = graph_commits_per_author(@repository)
148 151 end
149 152 if data
150 153 headers["Content-Type"] = "image/svg+xml"
151 154 send_data(data, :type => "image/svg+xml", :disposition => "inline")
152 155 else
153 156 render_404
154 157 end
155 158 end
156 159
157 160 private
158 161 def find_project
159 162 @project = Project.find(params[:id])
160 163 rescue ActiveRecord::RecordNotFound
161 164 render_404
162 165 end
163 166
164 167 def find_repository
165 168 @project = Project.find(params[:id])
166 169 @repository = @project.repository
167 170 render_404 and return false unless @repository
168 171 @path = params[:path].join('/') unless params[:path].nil?
169 172 @path ||= ''
170 173 @rev = params[:rev].to_i if params[:rev]
171 174 rescue ActiveRecord::RecordNotFound
172 175 render_404
173 176 end
174 177
175 178 def show_error
176 179 flash.now[:error] = l(:notice_scm_error)
177 180 render :nothing => true, :layout => true
178 181 end
179 182
180 183 def graph_commits_per_month(repository)
181 184 @date_to = Date.today
182 185 @date_from = @date_to << 11
183 186 @date_from = Date.civil(@date_from.year, @date_from.month, 1)
184 187 commits_by_day = repository.changesets.count(:all, :group => :commit_date, :conditions => ["commit_date BETWEEN ? AND ?", @date_from, @date_to])
185 188 commits_by_month = [0] * 12
186 189 commits_by_day.each {|c| commits_by_month[c.first.to_date.months_ago] += c.last }
187 190
188 191 changes_by_day = repository.changes.count(:all, :group => :commit_date, :conditions => ["commit_date BETWEEN ? AND ?", @date_from, @date_to])
189 192 changes_by_month = [0] * 12
190 193 changes_by_day.each {|c| changes_by_month[c.first.to_date.months_ago] += c.last }
191 194
192 195 fields = []
193 196 month_names = l(:actionview_datehelper_select_month_names_abbr).split(',')
194 197 12.times {|m| fields << month_names[((Date.today.month - 1 - m) % 12)]}
195 198
196 199 graph = SVG::Graph::Bar.new(
197 200 :height => 300,
198 201 :width => 500,
199 202 :fields => fields.reverse,
200 203 :stack => :side,
201 204 :scale_integers => true,
202 205 :step_x_labels => 2,
203 206 :show_data_values => false,
204 207 :graph_title => l(:label_commits_per_month),
205 208 :show_graph_title => true
206 209 )
207 210
208 211 graph.add_data(
209 212 :data => commits_by_month[0..11].reverse,
210 213 :title => l(:label_revision_plural)
211 214 )
212 215
213 216 graph.add_data(
214 217 :data => changes_by_month[0..11].reverse,
215 218 :title => l(:label_change_plural)
216 219 )
217 220
218 221 graph.burn
219 222 end
220 223
221 224 def graph_commits_per_author(repository)
222 225 commits_by_author = repository.changesets.count(:all, :group => :committer)
223 226 commits_by_author.sort! {|x, y| x.last <=> y.last}
224 227
225 228 changes_by_author = repository.changes.count(:all, :group => :committer)
226 229 h = changes_by_author.inject({}) {|o, i| o[i.first] = i.last; o}
227 230
228 231 fields = commits_by_author.collect {|r| r.first}
229 232 commits_data = commits_by_author.collect {|r| r.last}
230 233 changes_data = commits_by_author.collect {|r| h[r.first] || 0}
231 234
232 235 fields = fields + [""]*(10 - fields.length) if fields.length<10
233 236 commits_data = commits_data + [0]*(10 - commits_data.length) if commits_data.length<10
234 237 changes_data = changes_data + [0]*(10 - changes_data.length) if changes_data.length<10
235 238
236 239 graph = SVG::Graph::BarHorizontal.new(
237 240 :height => 300,
238 241 :width => 500,
239 242 :fields => fields,
240 243 :stack => :side,
241 244 :scale_integers => true,
242 245 :show_data_values => false,
243 246 :rotate_y_labels => false,
244 247 :graph_title => l(:label_commits_per_author),
245 248 :show_graph_title => true
246 249 )
247 250
248 251 graph.add_data(
249 252 :data => commits_data,
250 253 :title => l(:label_revision_plural)
251 254 )
252 255
253 256 graph.add_data(
254 257 :data => changes_data,
255 258 :title => l(:label_change_plural)
256 259 )
257 260
258 261 graph.burn
259 262 end
260 263
261 264 end
262 265
263 266 class Date
264 267 def months_ago(date = Date.today)
265 268 (date.year - self.year)*12 + (date.month - self.month)
266 269 end
267 270
268 271 def weeks_ago(date = Date.today)
269 272 (date.year - self.year)*52 + (date.cweek - self.cweek)
270 273 end
271 274 end
272 275
273 276 class String
274 277 def with_leading_slash
275 278 starts_with?('/') ? self : "/#{self}"
276 279 end
277 280 end
General Comments 0
You need to be logged in to leave comments. Login now