##// END OF EJS Templates
scm: mercurial: change "url" human attribute name from "Root directory" to "Path to repository"....
Toshi MARUYAMA -
r5408:82d5de4b4e3e
parent child
Show More
@@ -1,269 +1,271
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006 Jean-Philippe Lang
2 # Copyright (C) 2006 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 'iconv'
18 require 'iconv'
19 require 'redmine/codeset_util'
19 require 'redmine/codeset_util'
20
20
21 module RepositoriesHelper
21 module RepositoriesHelper
22 def format_revision(revision)
22 def format_revision(revision)
23 if revision.respond_to? :format_identifier
23 if revision.respond_to? :format_identifier
24 revision.format_identifier
24 revision.format_identifier
25 else
25 else
26 revision.to_s
26 revision.to_s
27 end
27 end
28 end
28 end
29
29
30 def truncate_at_line_break(text, length = 255)
30 def truncate_at_line_break(text, length = 255)
31 if text
31 if text
32 text.gsub(%r{^(.{#{length}}[^\n]*)\n.+$}m, '\\1...')
32 text.gsub(%r{^(.{#{length}}[^\n]*)\n.+$}m, '\\1...')
33 end
33 end
34 end
34 end
35
35
36 def render_properties(properties)
36 def render_properties(properties)
37 unless properties.nil? || properties.empty?
37 unless properties.nil? || properties.empty?
38 content = ''
38 content = ''
39 properties.keys.sort.each do |property|
39 properties.keys.sort.each do |property|
40 content << content_tag('li', "<b>#{h property}</b>: <span>#{h properties[property]}</span>")
40 content << content_tag('li', "<b>#{h property}</b>: <span>#{h properties[property]}</span>")
41 end
41 end
42 content_tag('ul', content, :class => 'properties')
42 content_tag('ul', content, :class => 'properties')
43 end
43 end
44 end
44 end
45
45
46 def render_changeset_changes
46 def render_changeset_changes
47 changes = @changeset.changes.find(:all, :limit => 1000, :order => 'path').collect do |change|
47 changes = @changeset.changes.find(:all, :limit => 1000, :order => 'path').collect do |change|
48 case change.action
48 case change.action
49 when 'A'
49 when 'A'
50 # Detects moved/copied files
50 # Detects moved/copied files
51 if !change.from_path.blank?
51 if !change.from_path.blank?
52 change.action =
52 change.action =
53 @changeset.changes.detect {|c| c.action == 'D' && c.path == change.from_path} ? 'R' : 'C'
53 @changeset.changes.detect {|c| c.action == 'D' && c.path == change.from_path} ? 'R' : 'C'
54 end
54 end
55 change
55 change
56 when 'D'
56 when 'D'
57 @changeset.changes.detect {|c| c.from_path == change.path} ? nil : change
57 @changeset.changes.detect {|c| c.from_path == change.path} ? nil : change
58 else
58 else
59 change
59 change
60 end
60 end
61 end.compact
61 end.compact
62
62
63 tree = { }
63 tree = { }
64 changes.each do |change|
64 changes.each do |change|
65 p = tree
65 p = tree
66 dirs = change.path.to_s.split('/').select {|d| !d.blank?}
66 dirs = change.path.to_s.split('/').select {|d| !d.blank?}
67 path = ''
67 path = ''
68 dirs.each do |dir|
68 dirs.each do |dir|
69 path += '/' + dir
69 path += '/' + dir
70 p[:s] ||= {}
70 p[:s] ||= {}
71 p = p[:s]
71 p = p[:s]
72 p[path] ||= {}
72 p[path] ||= {}
73 p = p[path]
73 p = p[path]
74 end
74 end
75 p[:c] = change
75 p[:c] = change
76 end
76 end
77 render_changes_tree(tree[:s])
77 render_changes_tree(tree[:s])
78 end
78 end
79
79
80 def render_changes_tree(tree)
80 def render_changes_tree(tree)
81 return '' if tree.nil?
81 return '' if tree.nil?
82 output = ''
82 output = ''
83 output << '<ul>'
83 output << '<ul>'
84 tree.keys.sort.each do |file|
84 tree.keys.sort.each do |file|
85 style = 'change'
85 style = 'change'
86 text = File.basename(h(file))
86 text = File.basename(h(file))
87 if s = tree[file][:s]
87 if s = tree[file][:s]
88 style << ' folder'
88 style << ' folder'
89 path_param = to_path_param(@repository.relative_path(file))
89 path_param = to_path_param(@repository.relative_path(file))
90 text = link_to(text, :controller => 'repositories',
90 text = link_to(text, :controller => 'repositories',
91 :action => 'show',
91 :action => 'show',
92 :id => @project,
92 :id => @project,
93 :path => path_param,
93 :path => path_param,
94 :rev => @changeset.identifier)
94 :rev => @changeset.identifier)
95 output << "<li class='#{style}'>#{text}</li>"
95 output << "<li class='#{style}'>#{text}</li>"
96 output << render_changes_tree(s)
96 output << render_changes_tree(s)
97 elsif c = tree[file][:c]
97 elsif c = tree[file][:c]
98 style << " change-#{c.action}"
98 style << " change-#{c.action}"
99 path_param = to_path_param(@repository.relative_path(c.path))
99 path_param = to_path_param(@repository.relative_path(c.path))
100 text = link_to(text, :controller => 'repositories',
100 text = link_to(text, :controller => 'repositories',
101 :action => 'entry',
101 :action => 'entry',
102 :id => @project,
102 :id => @project,
103 :path => path_param,
103 :path => path_param,
104 :rev => @changeset.identifier) unless c.action == 'D'
104 :rev => @changeset.identifier) unless c.action == 'D'
105 text << " - #{c.revision}" unless c.revision.blank?
105 text << " - #{c.revision}" unless c.revision.blank?
106 text << ' (' + link_to('diff', :controller => 'repositories',
106 text << ' (' + link_to('diff', :controller => 'repositories',
107 :action => 'diff',
107 :action => 'diff',
108 :id => @project,
108 :id => @project,
109 :path => path_param,
109 :path => path_param,
110 :rev => @changeset.identifier) + ') ' if c.action == 'M'
110 :rev => @changeset.identifier) + ') ' if c.action == 'M'
111 text << ' ' + content_tag('span', c.from_path, :class => 'copied-from') unless c.from_path.blank?
111 text << ' ' + content_tag('span', c.from_path, :class => 'copied-from') unless c.from_path.blank?
112 output << "<li class='#{style}'>#{text}</li>"
112 output << "<li class='#{style}'>#{text}</li>"
113 end
113 end
114 end
114 end
115 output << '</ul>'
115 output << '</ul>'
116 output
116 output
117 end
117 end
118
118
119 def to_utf8(str)
119 def to_utf8(str)
120 return str if str.nil?
120 return str if str.nil?
121 str = to_utf8_internal(str)
121 str = to_utf8_internal(str)
122 if str.respond_to?(:force_encoding)
122 if str.respond_to?(:force_encoding)
123 str.force_encoding('UTF-8')
123 str.force_encoding('UTF-8')
124 end
124 end
125 str
125 str
126 end
126 end
127
127
128 def to_utf8_internal(str)
128 def to_utf8_internal(str)
129 return str if str.nil?
129 return str if str.nil?
130 if str.respond_to?(:force_encoding)
130 if str.respond_to?(:force_encoding)
131 str.force_encoding('ASCII-8BIT')
131 str.force_encoding('ASCII-8BIT')
132 end
132 end
133 return str if str.empty?
133 return str if str.empty?
134 return str if /\A[\r\n\t\x20-\x7e]*\Z/n.match(str) # for us-ascii
134 return str if /\A[\r\n\t\x20-\x7e]*\Z/n.match(str) # for us-ascii
135 if str.respond_to?(:force_encoding)
135 if str.respond_to?(:force_encoding)
136 str.force_encoding('UTF-8')
136 str.force_encoding('UTF-8')
137 end
137 end
138 @encodings ||= Setting.repositories_encodings.split(',').collect(&:strip)
138 @encodings ||= Setting.repositories_encodings.split(',').collect(&:strip)
139 @encodings.each do |encoding|
139 @encodings.each do |encoding|
140 begin
140 begin
141 return Iconv.conv('UTF-8', encoding, str)
141 return Iconv.conv('UTF-8', encoding, str)
142 rescue Iconv::Failure
142 rescue Iconv::Failure
143 # do nothing here and try the next encoding
143 # do nothing here and try the next encoding
144 end
144 end
145 end
145 end
146 str = Redmine::CodesetUtil.replace_invalid_utf8(str)
146 str = Redmine::CodesetUtil.replace_invalid_utf8(str)
147 end
147 end
148 private :to_utf8_internal
148 private :to_utf8_internal
149
149
150 def repository_field_tags(form, repository)
150 def repository_field_tags(form, repository)
151 method = repository.class.name.demodulize.underscore + "_field_tags"
151 method = repository.class.name.demodulize.underscore + "_field_tags"
152 if repository.is_a?(Repository) &&
152 if repository.is_a?(Repository) &&
153 respond_to?(method) && method != 'repository_field_tags'
153 respond_to?(method) && method != 'repository_field_tags'
154 send(method, form, repository)
154 send(method, form, repository)
155 end
155 end
156 end
156 end
157
157
158 def scm_select_tag(repository)
158 def scm_select_tag(repository)
159 scm_options = [["--- #{l(:actionview_instancetag_blank_option)} ---", '']]
159 scm_options = [["--- #{l(:actionview_instancetag_blank_option)} ---", '']]
160 Redmine::Scm::Base.all.each do |scm|
160 Redmine::Scm::Base.all.each do |scm|
161 if Setting.enabled_scm.include?(scm) ||
161 if Setting.enabled_scm.include?(scm) ||
162 (repository && repository.class.name.demodulize == scm)
162 (repository && repository.class.name.demodulize == scm)
163 scm_options << ["Repository::#{scm}".constantize.scm_name, scm]
163 scm_options << ["Repository::#{scm}".constantize.scm_name, scm]
164 end
164 end
165 end
165 end
166 select_tag('repository_scm',
166 select_tag('repository_scm',
167 options_for_select(scm_options, repository.class.name.demodulize),
167 options_for_select(scm_options, repository.class.name.demodulize),
168 :disabled => (repository && !repository.new_record?),
168 :disabled => (repository && !repository.new_record?),
169 :onchange => remote_function(
169 :onchange => remote_function(
170 :url => {
170 :url => {
171 :controller => 'repositories',
171 :controller => 'repositories',
172 :action => 'edit',
172 :action => 'edit',
173 :id => @project
173 :id => @project
174 },
174 },
175 :method => :get,
175 :method => :get,
176 :with => "Form.serialize(this.form)")
176 :with => "Form.serialize(this.form)")
177 )
177 )
178 end
178 end
179
179
180 def with_leading_slash(path)
180 def with_leading_slash(path)
181 path.to_s.starts_with?('/') ? path : "/#{path}"
181 path.to_s.starts_with?('/') ? path : "/#{path}"
182 end
182 end
183
183
184 def without_leading_slash(path)
184 def without_leading_slash(path)
185 path.gsub(%r{^/+}, '')
185 path.gsub(%r{^/+}, '')
186 end
186 end
187
187
188 def subversion_field_tags(form, repository)
188 def subversion_field_tags(form, repository)
189 content_tag('p', form.text_field(:url, :size => 60, :required => true,
189 content_tag('p', form.text_field(:url, :size => 60, :required => true,
190 :disabled => (repository && !repository.root_url.blank?)) +
190 :disabled => (repository && !repository.root_url.blank?)) +
191 '<br />(file:///, http://, https://, svn://, svn+[tunnelscheme]://)') +
191 '<br />(file:///, http://, https://, svn://, svn+[tunnelscheme]://)') +
192 content_tag('p', form.text_field(:login, :size => 30)) +
192 content_tag('p', form.text_field(:login, :size => 30)) +
193 content_tag('p', form.password_field(
193 content_tag('p', form.password_field(
194 :password, :size => 30, :name => 'ignore',
194 :password, :size => 30, :name => 'ignore',
195 :value => ((repository.new_record? || repository.password.blank?) ? '' : ('x'*15)),
195 :value => ((repository.new_record? || repository.password.blank?) ? '' : ('x'*15)),
196 :onfocus => "this.value=''; this.name='repository[password]';",
196 :onfocus => "this.value=''; this.name='repository[password]';",
197 :onchange => "this.name='repository[password]';"))
197 :onchange => "this.name='repository[password]';"))
198 end
198 end
199
199
200 def darcs_field_tags(form, repository)
200 def darcs_field_tags(form, repository)
201 content_tag('p', form.text_field(:url, :label => 'Path to repository',
201 content_tag('p', form.text_field(:url, :label => 'Path to repository',
202 :size => 60, :required => true,
202 :size => 60, :required => true,
203 :disabled => (repository && !repository.new_record?))) +
203 :disabled => (repository && !repository.new_record?))) +
204 content_tag('p', form.select(
204 content_tag('p', form.select(
205 :log_encoding, [nil] + Setting::ENCODINGS,
205 :log_encoding, [nil] + Setting::ENCODINGS,
206 :label => l("field_commit_logs_encoding"), :required => true))
206 :label => l("field_commit_logs_encoding"), :required => true))
207 end
207 end
208
208
209 def mercurial_field_tags(form, repository)
209 def mercurial_field_tags(form, repository)
210 content_tag('p', form.text_field(:url, :label => 'Root directory',
210 content_tag('p', form.text_field(
211 :url, :label => 'Path to repository',
211 :size => 60, :required => true,
212 :size => 60, :required => true,
212 :disabled => (repository && !repository.root_url.blank?)) +
213 :disabled => (repository && !repository.root_url.blank?)
214 ) +
213 '<br />Local repository (e.g. /hgrepo, c:\hgrepo)' ) +
215 '<br />Local repository (e.g. /hgrepo, c:\hgrepo)' ) +
214 content_tag('p', form.select(
216 content_tag('p', form.select(
215 :path_encoding, [nil] + Setting::ENCODINGS,
217 :path_encoding, [nil] + Setting::ENCODINGS,
216 :label => l("field_scm_path_encoding")
218 :label => l("field_scm_path_encoding")
217 ) +
219 ) +
218 '<br />' + l("text_scm_path_encoding_note"))
220 '<br />' + l("text_scm_path_encoding_note"))
219 end
221 end
220
222
221 def git_field_tags(form, repository)
223 def git_field_tags(form, repository)
222 content_tag('p', form.text_field(:url, :label => 'Path to repository',
224 content_tag('p', form.text_field(:url, :label => 'Path to repository',
223 :size => 60, :required => true,
225 :size => 60, :required => true,
224 :disabled => (repository && !repository.root_url.blank?)) +
226 :disabled => (repository && !repository.root_url.blank?)) +
225 '<br />Bare and local repository (e.g. /gitrepo, c:\gitrepo)') +
227 '<br />Bare and local repository (e.g. /gitrepo, c:\gitrepo)') +
226 content_tag('p', form.select(
228 content_tag('p', form.select(
227 :path_encoding, [nil] + Setting::ENCODINGS,
229 :path_encoding, [nil] + Setting::ENCODINGS,
228 :label => l("field_scm_path_encoding")
230 :label => l("field_scm_path_encoding")
229 ) +
231 ) +
230 '<br />' + l("text_scm_path_encoding_note"))
232 '<br />' + l("text_scm_path_encoding_note"))
231 end
233 end
232
234
233 def cvs_field_tags(form, repository)
235 def cvs_field_tags(form, repository)
234 content_tag('p', form.text_field(:root_url,
236 content_tag('p', form.text_field(:root_url,
235 :label => 'CVSROOT', :size => 60, :required => true,
237 :label => 'CVSROOT', :size => 60, :required => true,
236 :disabled => !repository.new_record?)) +
238 :disabled => !repository.new_record?)) +
237 content_tag('p', form.text_field(:url, :label => 'Module',
239 content_tag('p', form.text_field(:url, :label => 'Module',
238 :size => 30, :required => true,
240 :size => 30, :required => true,
239 :disabled => !repository.new_record?)) +
241 :disabled => !repository.new_record?)) +
240 content_tag('p', form.select(
242 content_tag('p', form.select(
241 :log_encoding, [nil] + Setting::ENCODINGS,
243 :log_encoding, [nil] + Setting::ENCODINGS,
242 :label => l("field_commit_logs_encoding"), :required => true)) +
244 :label => l("field_commit_logs_encoding"), :required => true)) +
243 content_tag('p', form.select(
245 content_tag('p', form.select(
244 :path_encoding, [nil] + Setting::ENCODINGS,
246 :path_encoding, [nil] + Setting::ENCODINGS,
245 :label => l("field_scm_path_encoding")
247 :label => l("field_scm_path_encoding")
246 ) +
248 ) +
247 '<br />' + l("text_scm_path_encoding_note"))
249 '<br />' + l("text_scm_path_encoding_note"))
248 end
250 end
249
251
250 def bazaar_field_tags(form, repository)
252 def bazaar_field_tags(form, repository)
251 content_tag('p', form.text_field(:url, :label => 'Path to repository',
253 content_tag('p', form.text_field(:url, :label => 'Path to repository',
252 :size => 60, :required => true,
254 :size => 60, :required => true,
253 :disabled => (repository && !repository.new_record?))) +
255 :disabled => (repository && !repository.new_record?))) +
254 content_tag('p', form.select(
256 content_tag('p', form.select(
255 :log_encoding, [nil] + Setting::ENCODINGS,
257 :log_encoding, [nil] + Setting::ENCODINGS,
256 :label => l("field_commit_logs_encoding"), :required => true))
258 :label => l("field_commit_logs_encoding"), :required => true))
257 end
259 end
258
260
259 def filesystem_field_tags(form, repository)
261 def filesystem_field_tags(form, repository)
260 content_tag('p', form.text_field(:url, :label => 'Root directory',
262 content_tag('p', form.text_field(:url, :label => 'Root directory',
261 :size => 60, :required => true,
263 :size => 60, :required => true,
262 :disabled => (repository && !repository.root_url.blank?))) +
264 :disabled => (repository && !repository.root_url.blank?))) +
263 content_tag('p', form.select(
265 content_tag('p', form.select(
264 :path_encoding, [nil] + Setting::ENCODINGS,
266 :path_encoding, [nil] + Setting::ENCODINGS,
265 :label => l("field_scm_path_encoding")
267 :label => l("field_scm_path_encoding")
266 ) +
268 ) +
267 '<br />' + l("text_scm_path_encoding_note"))
269 '<br />' + l("text_scm_path_encoding_note"))
268 end
270 end
269 end
271 end
@@ -1,145 +1,145
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/mercurial_adapter'
18 require 'redmine/scm/adapters/mercurial_adapter'
19
19
20 class Repository::Mercurial < Repository
20 class Repository::Mercurial < Repository
21 # sort changesets by revision number
21 # sort changesets by revision number
22 has_many :changesets, :order => "#{Changeset.table_name}.id DESC", :foreign_key => 'repository_id'
22 has_many :changesets, :order => "#{Changeset.table_name}.id DESC", :foreign_key => 'repository_id'
23
23
24 attr_protected :root_url
24 attr_protected :root_url
25 validates_presence_of :url
25 validates_presence_of :url
26
26
27 FETCH_AT_ONCE = 100 # number of changesets to fetch at once
27 FETCH_AT_ONCE = 100 # number of changesets to fetch at once
28
28
29 ATTRIBUTE_KEY_NAMES = {
29 ATTRIBUTE_KEY_NAMES = {
30 "url" => "Root directory",
30 "url" => "Path to repository",
31 }
31 }
32 def self.human_attribute_name(attribute_key_name)
32 def self.human_attribute_name(attribute_key_name)
33 ATTRIBUTE_KEY_NAMES[attribute_key_name] || super
33 ATTRIBUTE_KEY_NAMES[attribute_key_name] || super
34 end
34 end
35
35
36 def self.scm_adapter_class
36 def self.scm_adapter_class
37 Redmine::Scm::Adapters::MercurialAdapter
37 Redmine::Scm::Adapters::MercurialAdapter
38 end
38 end
39
39
40 def self.scm_name
40 def self.scm_name
41 'Mercurial'
41 'Mercurial'
42 end
42 end
43
43
44 def supports_directory_revisions?
44 def supports_directory_revisions?
45 true
45 true
46 end
46 end
47
47
48 def repo_log_encoding
48 def repo_log_encoding
49 'UTF-8'
49 'UTF-8'
50 end
50 end
51
51
52 # Returns the readable identifier for the given mercurial changeset
52 # Returns the readable identifier for the given mercurial changeset
53 def self.format_changeset_identifier(changeset)
53 def self.format_changeset_identifier(changeset)
54 "#{changeset.revision}:#{changeset.scmid}"
54 "#{changeset.revision}:#{changeset.scmid}"
55 end
55 end
56
56
57 # Returns the identifier for the given Mercurial changeset
57 # Returns the identifier for the given Mercurial changeset
58 def self.changeset_identifier(changeset)
58 def self.changeset_identifier(changeset)
59 changeset.scmid
59 changeset.scmid
60 end
60 end
61
61
62 def diff_format_revisions(cs, cs_to, sep=':')
62 def diff_format_revisions(cs, cs_to, sep=':')
63 super(cs, cs_to, ' ')
63 super(cs, cs_to, ' ')
64 end
64 end
65
65
66 # Finds and returns a revision with a number or the beginning of a hash
66 # Finds and returns a revision with a number or the beginning of a hash
67 def find_changeset_by_name(name)
67 def find_changeset_by_name(name)
68 return nil if name.nil? || name.empty?
68 return nil if name.nil? || name.empty?
69 if /[^\d]/ =~ name or name.to_s.size > 8
69 if /[^\d]/ =~ name or name.to_s.size > 8
70 e = changesets.find(:first, :conditions => ['scmid = ?', name.to_s])
70 e = changesets.find(:first, :conditions => ['scmid = ?', name.to_s])
71 else
71 else
72 e = changesets.find(:first, :conditions => ['revision = ?', name.to_s])
72 e = changesets.find(:first, :conditions => ['revision = ?', name.to_s])
73 end
73 end
74 return e if e
74 return e if e
75 changesets.find(:first, :conditions => ['scmid LIKE ?', "#{name}%"]) # last ditch
75 changesets.find(:first, :conditions => ['scmid LIKE ?', "#{name}%"]) # last ditch
76 end
76 end
77
77
78 # Returns the latest changesets for +path+; sorted by revision number
78 # Returns the latest changesets for +path+; sorted by revision number
79 #
79 #
80 # Because :order => 'id DESC' is defined at 'has_many',
80 # Because :order => 'id DESC' is defined at 'has_many',
81 # there is no need to set 'order'.
81 # there is no need to set 'order'.
82 # But, MySQL test fails.
82 # But, MySQL test fails.
83 # Sqlite3 and PostgreSQL pass.
83 # Sqlite3 and PostgreSQL pass.
84 # Is this MySQL bug?
84 # Is this MySQL bug?
85 def latest_changesets(path, rev, limit=10)
85 def latest_changesets(path, rev, limit=10)
86 changesets.find(:all, :include => :user,
86 changesets.find(:all, :include => :user,
87 :conditions => latest_changesets_cond(path, rev, limit),
87 :conditions => latest_changesets_cond(path, rev, limit),
88 :limit => limit, :order => "#{Changeset.table_name}.id DESC")
88 :limit => limit, :order => "#{Changeset.table_name}.id DESC")
89 end
89 end
90
90
91 def latest_changesets_cond(path, rev, limit)
91 def latest_changesets_cond(path, rev, limit)
92 cond, args = [], []
92 cond, args = [], []
93 if scm.branchmap.member? rev
93 if scm.branchmap.member? rev
94 # Mercurial named branch is *stable* in each revision.
94 # Mercurial named branch is *stable* in each revision.
95 # So, named branch can be stored in database.
95 # So, named branch can be stored in database.
96 # Mercurial provides *bookmark* which is equivalent with git branch.
96 # Mercurial provides *bookmark* which is equivalent with git branch.
97 # But, bookmark is not implemented.
97 # But, bookmark is not implemented.
98 cond << "#{Changeset.table_name}.scmid IN (?)"
98 cond << "#{Changeset.table_name}.scmid IN (?)"
99 # Revisions in root directory and sub directory are not equal.
99 # Revisions in root directory and sub directory are not equal.
100 # So, in order to get correct limit, we need to get all revisions.
100 # So, in order to get correct limit, we need to get all revisions.
101 # But, it is very heavy.
101 # But, it is very heavy.
102 # Mercurial does not treat direcotry.
102 # Mercurial does not treat direcotry.
103 # So, "hg log DIR" is very heavy.
103 # So, "hg log DIR" is very heavy.
104 branch_limit = path.blank? ? limit : ( limit * 5 )
104 branch_limit = path.blank? ? limit : ( limit * 5 )
105 args << scm.nodes_in_branch(rev, :limit => branch_limit)
105 args << scm.nodes_in_branch(rev, :limit => branch_limit)
106 elsif last = rev ? find_changeset_by_name(scm.tagmap[rev] || rev) : nil
106 elsif last = rev ? find_changeset_by_name(scm.tagmap[rev] || rev) : nil
107 cond << "#{Changeset.table_name}.id <= ?"
107 cond << "#{Changeset.table_name}.id <= ?"
108 args << last.id
108 args << last.id
109 end
109 end
110
110
111 unless path.blank?
111 unless path.blank?
112 cond << "EXISTS (SELECT * FROM #{Change.table_name}
112 cond << "EXISTS (SELECT * FROM #{Change.table_name}
113 WHERE #{Change.table_name}.changeset_id = #{Changeset.table_name}.id
113 WHERE #{Change.table_name}.changeset_id = #{Changeset.table_name}.id
114 AND (#{Change.table_name}.path = ?
114 AND (#{Change.table_name}.path = ?
115 OR #{Change.table_name}.path LIKE ? ESCAPE ?))"
115 OR #{Change.table_name}.path LIKE ? ESCAPE ?))"
116 args << path.with_leading_slash
116 args << path.with_leading_slash
117 args << "#{path.with_leading_slash.gsub(/[%_\\]/) { |s| "\\#{s}" }}/%" << '\\'
117 args << "#{path.with_leading_slash.gsub(/[%_\\]/) { |s| "\\#{s}" }}/%" << '\\'
118 end
118 end
119
119
120 [cond.join(' AND '), *args] unless cond.empty?
120 [cond.join(' AND '), *args] unless cond.empty?
121 end
121 end
122 private :latest_changesets_cond
122 private :latest_changesets_cond
123
123
124 def fetch_changesets
124 def fetch_changesets
125 scm_rev = scm.info.lastrev.revision.to_i
125 scm_rev = scm.info.lastrev.revision.to_i
126 db_rev = latest_changeset ? latest_changeset.revision.to_i : -1
126 db_rev = latest_changeset ? latest_changeset.revision.to_i : -1
127 return unless db_rev < scm_rev # already up-to-date
127 return unless db_rev < scm_rev # already up-to-date
128
128
129 logger.debug "Fetching changesets for repository #{url}" if logger
129 logger.debug "Fetching changesets for repository #{url}" if logger
130 (db_rev + 1).step(scm_rev, FETCH_AT_ONCE) do |i|
130 (db_rev + 1).step(scm_rev, FETCH_AT_ONCE) do |i|
131 transaction do
131 transaction do
132 scm.each_revision('', i, [i + FETCH_AT_ONCE - 1, scm_rev].min) do |re|
132 scm.each_revision('', i, [i + FETCH_AT_ONCE - 1, scm_rev].min) do |re|
133 cs = Changeset.create(:repository => self,
133 cs = Changeset.create(:repository => self,
134 :revision => re.revision,
134 :revision => re.revision,
135 :scmid => re.scmid,
135 :scmid => re.scmid,
136 :committer => re.author,
136 :committer => re.author,
137 :committed_on => re.time,
137 :committed_on => re.time,
138 :comments => re.message)
138 :comments => re.message)
139 re.paths.each { |e| cs.create_change(e) }
139 re.paths.each { |e| cs.create_change(e) }
140 end
140 end
141 end
141 end
142 end
142 end
143 self
143 self
144 end
144 end
145 end
145 end
General Comments 0
You need to be logged in to leave comments. Login now