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