##// END OF EJS Templates
Verify rev and rev_to params format in RepositoriesController. And turn revision arguments into integers in SubversionAdapter....
Jean-Philippe Lang -
r1309:14a2b7e9b576
parent child
Show More
@@ -1,299 +1,304
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 class ChangesetNotFound < Exception
23 end
22 class ChangesetNotFound < Exception; end
23 class InvalidRevisionParam < Exception; 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 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 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 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 99 show_error_not_found and return unless @content
100 100 if 'raw' == params[:format] || @content.is_binary_data?
101 101 # Force the download if it's a binary file
102 102 send_data @content, :filename => @path.split('/').last
103 103 else
104 104 # Prevent empty lines when displaying a file with Windows style eol
105 105 @content.gsub!("\r\n", "\n")
106 106 end
107 107 rescue Redmine::Scm::Adapters::CommandFailed => e
108 108 show_error_command_failed(e.message)
109 109 end
110 110
111 111 def annotate
112 112 @annotate = @repository.scm.annotate(@path, @rev)
113 113 render_error l(:error_scm_annotate) and return if @annotate.nil? || @annotate.empty?
114 114 rescue Redmine::Scm::Adapters::CommandFailed => e
115 115 show_error_command_failed(e.message)
116 116 end
117 117
118 118 def revision
119 119 @changeset = @repository.changesets.find_by_revision(@rev)
120 120 raise ChangesetNotFound unless @changeset
121 121 @changes_count = @changeset.changes.size
122 122 @changes_pages = Paginator.new self, @changes_count, 150, params['page']
123 123 @changes = @changeset.changes.find(:all,
124 124 :limit => @changes_pages.items_per_page,
125 125 :offset => @changes_pages.current.offset)
126 126
127 127 respond_to do |format|
128 128 format.html
129 129 format.js {render :layout => false}
130 130 end
131 131 rescue ChangesetNotFound
132 132 show_error_not_found
133 133 rescue Redmine::Scm::Adapters::CommandFailed => e
134 134 show_error_command_failed(e.message)
135 135 end
136 136
137 137 def diff
138 @rev_to = params[:rev_to]
139 138 @diff_type = params[:type] || User.current.pref[:diff_type] || 'inline'
140 139 @diff_type = 'inline' unless %w(inline sbs).include?(@diff_type)
141 140
142 141 # Save diff type as user preference
143 142 if User.current.logged? && @diff_type != User.current.pref[:diff_type]
144 143 User.current.pref[:diff_type] = @diff_type
145 144 User.current.preference.save
146 145 end
147 146
148 147 @cache_key = "repositories/diff/#{@repository.id}/" + Digest::MD5.hexdigest("#{@path}-#{@rev}-#{@rev_to}-#{@diff_type}")
149 148 unless read_fragment(@cache_key)
150 149 @diff = @repository.diff(@path, @rev, @rev_to, @diff_type)
151 150 show_error_not_found unless @diff
152 151 end
153 152 rescue Redmine::Scm::Adapters::CommandFailed => e
154 153 show_error_command_failed(e.message)
155 154 end
156 155
157 156 def stats
158 157 end
159 158
160 159 def graph
161 160 data = nil
162 161 case params[:graph]
163 162 when "commits_per_month"
164 163 data = graph_commits_per_month(@repository)
165 164 when "commits_per_author"
166 165 data = graph_commits_per_author(@repository)
167 166 end
168 167 if data
169 168 headers["Content-Type"] = "image/svg+xml"
170 169 send_data(data, :type => "image/svg+xml", :disposition => "inline")
171 170 else
172 171 render_404
173 172 end
174 173 end
175 174
176 175 private
177 176 def find_project
178 177 @project = Project.find(params[:id])
179 178 rescue ActiveRecord::RecordNotFound
180 179 render_404
181 180 end
182 181
182 REV_PARAM_RE = %r{^[a-f0-9]*$}
183
183 184 def find_repository
184 185 @project = Project.find(params[:id])
185 186 @repository = @project.repository
186 187 render_404 and return false unless @repository
187 188 @path = params[:path].join('/') unless params[:path].nil?
188 189 @path ||= ''
189 190 @rev = params[:rev]
191 @rev_to = params[:rev_to]
192 raise InvalidRevisionParam unless @rev.to_s.match(REV_PARAM_RE) && @rev.to_s.match(REV_PARAM_RE)
190 193 rescue ActiveRecord::RecordNotFound
191 194 render_404
195 rescue InvalidRevisionParam
196 show_error_not_found
192 197 end
193 198
194 199 def show_error_not_found
195 200 render_error l(:error_scm_not_found)
196 201 end
197 202
198 203 def show_error_command_failed(msg)
199 204 render_error l(:error_scm_command_failed, msg)
200 205 end
201 206
202 207 def graph_commits_per_month(repository)
203 208 @date_to = Date.today
204 209 @date_from = @date_to << 11
205 210 @date_from = Date.civil(@date_from.year, @date_from.month, 1)
206 211 commits_by_day = repository.changesets.count(:all, :group => :commit_date, :conditions => ["commit_date BETWEEN ? AND ?", @date_from, @date_to])
207 212 commits_by_month = [0] * 12
208 213 commits_by_day.each {|c| commits_by_month[c.first.to_date.months_ago] += c.last }
209 214
210 215 changes_by_day = repository.changes.count(:all, :group => :commit_date, :conditions => ["commit_date BETWEEN ? AND ?", @date_from, @date_to])
211 216 changes_by_month = [0] * 12
212 217 changes_by_day.each {|c| changes_by_month[c.first.to_date.months_ago] += c.last }
213 218
214 219 fields = []
215 220 month_names = l(:actionview_datehelper_select_month_names_abbr).split(',')
216 221 12.times {|m| fields << month_names[((Date.today.month - 1 - m) % 12)]}
217 222
218 223 graph = SVG::Graph::Bar.new(
219 224 :height => 300,
220 225 :width => 500,
221 226 :fields => fields.reverse,
222 227 :stack => :side,
223 228 :scale_integers => true,
224 229 :step_x_labels => 2,
225 230 :show_data_values => false,
226 231 :graph_title => l(:label_commits_per_month),
227 232 :show_graph_title => true
228 233 )
229 234
230 235 graph.add_data(
231 236 :data => commits_by_month[0..11].reverse,
232 237 :title => l(:label_revision_plural)
233 238 )
234 239
235 240 graph.add_data(
236 241 :data => changes_by_month[0..11].reverse,
237 242 :title => l(:label_change_plural)
238 243 )
239 244
240 245 graph.burn
241 246 end
242 247
243 248 def graph_commits_per_author(repository)
244 249 commits_by_author = repository.changesets.count(:all, :group => :committer)
245 250 commits_by_author.sort! {|x, y| x.last <=> y.last}
246 251
247 252 changes_by_author = repository.changes.count(:all, :group => :committer)
248 253 h = changes_by_author.inject({}) {|o, i| o[i.first] = i.last; o}
249 254
250 255 fields = commits_by_author.collect {|r| r.first}
251 256 commits_data = commits_by_author.collect {|r| r.last}
252 257 changes_data = commits_by_author.collect {|r| h[r.first] || 0}
253 258
254 259 fields = fields + [""]*(10 - fields.length) if fields.length<10
255 260 commits_data = commits_data + [0]*(10 - commits_data.length) if commits_data.length<10
256 261 changes_data = changes_data + [0]*(10 - changes_data.length) if changes_data.length<10
257 262
258 263 graph = SVG::Graph::BarHorizontal.new(
259 264 :height => 300,
260 265 :width => 500,
261 266 :fields => fields,
262 267 :stack => :side,
263 268 :scale_integers => true,
264 269 :show_data_values => false,
265 270 :rotate_y_labels => false,
266 271 :graph_title => l(:label_commits_per_author),
267 272 :show_graph_title => true
268 273 )
269 274
270 275 graph.add_data(
271 276 :data => commits_data,
272 277 :title => l(:label_revision_plural)
273 278 )
274 279
275 280 graph.add_data(
276 281 :data => changes_data,
277 282 :title => l(:label_change_plural)
278 283 )
279 284
280 285 graph.burn
281 286 end
282 287
283 288 end
284 289
285 290 class Date
286 291 def months_ago(date = Date.today)
287 292 (date.year - self.year)*12 + (date.month - self.month)
288 293 end
289 294
290 295 def weeks_ago(date = Date.today)
291 296 (date.year - self.year)*52 + (date.cweek - self.cweek)
292 297 end
293 298 end
294 299
295 300 class String
296 301 def with_leading_slash
297 302 starts_with?('/') ? self : "/#{self}"
298 303 end
299 304 end
@@ -1,193 +1,191
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 'redmine/scm/adapters/abstract_adapter'
19 19 require 'rexml/document'
20 20
21 21 module Redmine
22 22 module Scm
23 23 module Adapters
24 24 class SubversionAdapter < AbstractAdapter
25 25
26 26 # SVN executable name
27 27 SVN_BIN = "svn"
28 28
29 29 # Get info about the svn repository
30 30 def info
31 31 cmd = "#{SVN_BIN} info --xml #{target('')}"
32 32 cmd << credentials_string
33 33 info = nil
34 34 shellout(cmd) do |io|
35 35 begin
36 36 doc = REXML::Document.new(io)
37 37 #root_url = doc.elements["info/entry/repository/root"].text
38 38 info = Info.new({:root_url => doc.elements["info/entry/repository/root"].text,
39 39 :lastrev => Revision.new({
40 40 :identifier => doc.elements["info/entry/commit"].attributes['revision'],
41 41 :time => Time.parse(doc.elements["info/entry/commit/date"].text).localtime,
42 42 :author => (doc.elements["info/entry/commit/author"] ? doc.elements["info/entry/commit/author"].text : "")
43 43 })
44 44 })
45 45 rescue
46 46 end
47 47 end
48 48 return nil if $? && $?.exitstatus != 0
49 49 info
50 50 rescue CommandFailed
51 51 return nil
52 52 end
53 53
54 54 # Returns the entry identified by path and revision identifier
55 55 # or nil if entry doesn't exist in the repository
56 56 def entry(path=nil, identifier=nil)
57 57 e = entries(path, identifier)
58 58 e ? e.first : nil
59 59 end
60 60
61 61 # Returns an Entries collection
62 62 # or nil if the given path doesn't exist in the repository
63 63 def entries(path=nil, identifier=nil)
64 64 path ||= ''
65 identifier = 'HEAD' unless identifier and identifier > 0
65 identifier = (identifier and identifier.to_i > 0) ? identifier.to_i : "HEAD"
66 66 entries = Entries.new
67 67 cmd = "#{SVN_BIN} list --xml #{target(path)}@#{identifier}"
68 68 cmd << credentials_string
69 69 shellout(cmd) do |io|
70 70 output = io.read
71 71 begin
72 72 doc = REXML::Document.new(output)
73 73 doc.elements.each("lists/list/entry") do |entry|
74 74 entries << Entry.new({:name => entry.elements['name'].text,
75 75 :path => ((path.empty? ? "" : "#{path}/") + entry.elements['name'].text),
76 76 :kind => entry.attributes['kind'],
77 77 :size => (entry.elements['size'] and entry.elements['size'].text).to_i,
78 78 :lastrev => Revision.new({
79 79 :identifier => entry.elements['commit'].attributes['revision'],
80 80 :time => Time.parse(entry.elements['commit'].elements['date'].text).localtime,
81 81 :author => (entry.elements['commit'].elements['author'] ? entry.elements['commit'].elements['author'].text : "")
82 82 })
83 83 })
84 84 end
85 85 rescue Exception => e
86 86 logger.error("Error parsing svn output: #{e.message}")
87 87 logger.error("Output was:\n #{output}")
88 88 end
89 89 end
90 90 return nil if $? && $?.exitstatus != 0
91 91 logger.debug("Found #{entries.size} entries in the repository for #{target(path)}") if logger && logger.debug?
92 92 entries.sort_by_name
93 93 end
94 94
95 95 def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={})
96 96 path ||= ''
97 identifier_from = 'HEAD' unless identifier_from and identifier_from.to_i > 0
98 identifier_to = 1 unless identifier_to and identifier_to.to_i > 0
97 identifier_from = (identifier_from and identifier_from.to_i > 0) ? identifier_from.to_i : "HEAD"
98 identifier_to = (identifier_to and identifier_to.to_i > 0) ? identifier_to.to_i : 1
99 99 revisions = Revisions.new
100 100 cmd = "#{SVN_BIN} log --xml -r #{identifier_from}:#{identifier_to}"
101 101 cmd << credentials_string
102 102 cmd << " --verbose " if options[:with_paths]
103 103 cmd << ' ' + target(path)
104 104 shellout(cmd) do |io|
105 105 begin
106 106 doc = REXML::Document.new(io)
107 107 doc.elements.each("log/logentry") do |logentry|
108 108 paths = []
109 109 logentry.elements.each("paths/path") do |path|
110 110 paths << {:action => path.attributes['action'],
111 111 :path => path.text,
112 112 :from_path => path.attributes['copyfrom-path'],
113 113 :from_revision => path.attributes['copyfrom-rev']
114 114 }
115 115 end
116 116 paths.sort! { |x,y| x[:path] <=> y[:path] }
117 117
118 118 revisions << Revision.new({:identifier => logentry.attributes['revision'],
119 119 :author => (logentry.elements['author'] ? logentry.elements['author'].text : ""),
120 120 :time => Time.parse(logentry.elements['date'].text).localtime,
121 121 :message => logentry.elements['msg'].text,
122 122 :paths => paths
123 123 })
124 124 end
125 125 rescue
126 126 end
127 127 end
128 128 return nil if $? && $?.exitstatus != 0
129 129 revisions
130 130 end
131 131
132 132 def diff(path, identifier_from, identifier_to=nil, type="inline")
133 133 path ||= ''
134 if identifier_to and identifier_to.to_i > 0
135 identifier_to = identifier_to.to_i
136 else
137 identifier_to = identifier_from.to_i - 1
138 end
134 identifier_from = (identifier_from and identifier_from.to_i > 0) ? identifier_from.to_i : ''
135 identifier_to = (identifier_to and identifier_to.to_i > 0) ? identifier_to.to_i : (identifier_from.to_i - 1)
136
139 137 cmd = "#{SVN_BIN} diff -r "
140 138 cmd << "#{identifier_to}:"
141 139 cmd << "#{identifier_from}"
142 140 cmd << " #{target(path)}@#{identifier_from}"
143 141 cmd << credentials_string
144 142 diff = []
145 143 shellout(cmd) do |io|
146 144 io.each_line do |line|
147 145 diff << line
148 146 end
149 147 end
150 148 return nil if $? && $?.exitstatus != 0
151 149 DiffTableList.new diff, type
152 150 end
153 151
154 152 def cat(path, identifier=nil)
155 153 identifier = (identifier and identifier.to_i > 0) ? identifier.to_i : "HEAD"
156 154 cmd = "#{SVN_BIN} cat #{target(path)}@#{identifier}"
157 155 cmd << credentials_string
158 156 cat = nil
159 157 shellout(cmd) do |io|
160 158 io.binmode
161 159 cat = io.read
162 160 end
163 161 return nil if $? && $?.exitstatus != 0
164 162 cat
165 163 end
166 164
167 165 def annotate(path, identifier=nil)
168 166 identifier = (identifier and identifier.to_i > 0) ? identifier.to_i : "HEAD"
169 167 cmd = "#{SVN_BIN} blame #{target(path)}@#{identifier}"
170 168 cmd << credentials_string
171 169 blame = Annotate.new
172 170 shellout(cmd) do |io|
173 171 io.each_line do |line|
174 172 next unless line =~ %r{^\s*(\d+)\s*(\S+)\s(.*)$}
175 173 blame.add_line($3.rstrip, Revision.new(:identifier => $1.to_i, :author => $2.strip))
176 174 end
177 175 end
178 176 return nil if $? && $?.exitstatus != 0
179 177 blame
180 178 end
181 179
182 180 private
183 181
184 182 def credentials_string
185 183 str = ''
186 184 str << " --username #{shell_quote(@login)}" unless @login.blank?
187 185 str << " --password #{shell_quote(@password)}" unless @login.blank? || @password.blank?
188 186 str
189 187 end
190 188 end
191 189 end
192 190 end
193 191 end
General Comments 0
You need to be logged in to leave comments. Login now