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