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