##// END OF EJS Templates
scm: mercurial: use 12 chars id for format_changeset_identifier (#14361)...
Toshi MARUYAMA -
r12474:5dcecec1bd8a
parent child
Show More
@@ -1,159 +1,159
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2014 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/mercurial_adapter'
19 19
20 20 class Repository::Mercurial < Repository
21 21 # sort changesets by revision number
22 22 has_many :changesets,
23 23 :order => "#{Changeset.table_name}.id DESC",
24 24 :foreign_key => 'repository_id'
25 25
26 26 attr_protected :root_url
27 27 validates_presence_of :url
28 28
29 29 # number of changesets to fetch at once
30 30 FETCH_AT_ONCE = 100
31 31
32 32 def self.human_attribute_name(attribute_key_name, *args)
33 33 attr_name = attribute_key_name.to_s
34 34 if attr_name == "url"
35 35 attr_name = "path_to_repository"
36 36 end
37 37 super(attr_name, *args)
38 38 end
39 39
40 40 def self.scm_adapter_class
41 41 Redmine::Scm::Adapters::MercurialAdapter
42 42 end
43 43
44 44 def self.scm_name
45 45 'Mercurial'
46 46 end
47 47
48 48 def supports_directory_revisions?
49 49 true
50 50 end
51 51
52 52 def supports_revision_graph?
53 53 true
54 54 end
55 55
56 56 def repo_log_encoding
57 57 'UTF-8'
58 58 end
59 59
60 60 # Returns the readable identifier for the given mercurial changeset
61 61 def self.format_changeset_identifier(changeset)
62 "#{changeset.revision}:#{changeset.scmid}"
62 "#{changeset.revision}:#{changeset.scmid[0, 12]}"
63 63 end
64 64
65 65 # Returns the identifier for the given Mercurial changeset
66 66 def self.changeset_identifier(changeset)
67 67 changeset.scmid
68 68 end
69 69
70 70 def diff_format_revisions(cs, cs_to, sep=':')
71 71 super(cs, cs_to, ' ')
72 72 end
73 73
74 74 # Finds and returns a revision with a number or the beginning of a hash
75 75 def find_changeset_by_name(name)
76 76 return nil if name.blank?
77 77 s = name.to_s
78 78 if /[^\d]/ =~ s or s.size > 8
79 79 cs = changesets.where(:scmid => s).first
80 80 else
81 81 cs = changesets.where(:revision => s).first
82 82 end
83 83 return cs if cs
84 84 changesets.where('scmid LIKE ?', "#{s}%").first
85 85 end
86 86
87 87 # Returns the latest changesets for +path+; sorted by revision number
88 88 #
89 89 # Because :order => 'id DESC' is defined at 'has_many',
90 90 # there is no need to set 'order'.
91 91 # But, MySQL test fails.
92 92 # Sqlite3 and PostgreSQL pass.
93 93 # Is this MySQL bug?
94 94 def latest_changesets(path, rev, limit=10)
95 95 changesets.
96 96 includes(:user).
97 97 where(latest_changesets_cond(path, rev, limit)).
98 98 limit(limit).
99 99 order("#{Changeset.table_name}.id DESC").
100 100 all
101 101 end
102 102
103 103 def latest_changesets_cond(path, rev, limit)
104 104 cond, args = [], []
105 105 if scm.branchmap.member? rev
106 106 # Mercurial named branch is *stable* in each revision.
107 107 # So, named branch can be stored in database.
108 108 # Mercurial provides *bookmark* which is equivalent with git branch.
109 109 # But, bookmark is not implemented.
110 110 cond << "#{Changeset.table_name}.scmid IN (?)"
111 111 # Revisions in root directory and sub directory are not equal.
112 112 # So, in order to get correct limit, we need to get all revisions.
113 113 # But, it is very heavy.
114 114 # Mercurial does not treat direcotry.
115 115 # So, "hg log DIR" is very heavy.
116 116 branch_limit = path.blank? ? limit : ( limit * 5 )
117 117 args << scm.nodes_in_branch(rev, :limit => branch_limit)
118 118 elsif last = rev ? find_changeset_by_name(scm.tagmap[rev] || rev) : nil
119 119 cond << "#{Changeset.table_name}.id <= ?"
120 120 args << last.id
121 121 end
122 122 unless path.blank?
123 123 cond << "EXISTS (SELECT * FROM #{Change.table_name}
124 124 WHERE #{Change.table_name}.changeset_id = #{Changeset.table_name}.id
125 125 AND (#{Change.table_name}.path = ?
126 126 OR #{Change.table_name}.path LIKE ? ESCAPE ?))"
127 127 args << path.with_leading_slash
128 128 args << "#{path.with_leading_slash.gsub(%r{[%_\\]}) { |s| "\\#{s}" }}/%" << '\\'
129 129 end
130 130 [cond.join(' AND '), *args] unless cond.empty?
131 131 end
132 132 private :latest_changesets_cond
133 133
134 134 def fetch_changesets
135 135 return if scm.info.nil?
136 136 scm_rev = scm.info.lastrev.revision.to_i
137 137 db_rev = latest_changeset ? latest_changeset.revision.to_i : -1
138 138 return unless db_rev < scm_rev # already up-to-date
139 139
140 140 logger.debug "Fetching changesets for repository #{url}" if logger
141 141 (db_rev + 1).step(scm_rev, FETCH_AT_ONCE) do |i|
142 142 scm.each_revision('', i, [i + FETCH_AT_ONCE - 1, scm_rev].min) do |re|
143 143 transaction do
144 144 parents = (re.parents || []).collect{|rp| find_changeset_by_name(rp)}.compact
145 145 cs = Changeset.create(:repository => self,
146 146 :revision => re.revision,
147 147 :scmid => re.scmid,
148 148 :committer => re.author,
149 149 :committed_on => re.time,
150 150 :comments => re.message,
151 151 :parents => parents)
152 152 unless cs.new_record?
153 153 re.paths.each { |e| cs.create_change(e) }
154 154 end
155 155 end
156 156 end
157 157 end
158 158 end
159 159 end
@@ -1,389 +1,400
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2014 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require File.expand_path('../../test_helper', __FILE__)
19 19
20 20 class RepositoryMercurialTest < ActiveSupport::TestCase
21 21 fixtures :projects
22 22
23 23 include Redmine::I18n
24 24
25 25 REPOSITORY_PATH = Rails.root.join('tmp/test/mercurial_repository').to_s
26 26 NUM_REV = 32
27 27 CHAR_1_HEX = "\xc3\x9c"
28 28
29 29 def setup
30 30 @project = Project.find(3)
31 31 @repository = Repository::Mercurial.create(
32 32 :project => @project,
33 33 :url => REPOSITORY_PATH,
34 34 :path_encoding => 'ISO-8859-1'
35 35 )
36 36 assert @repository
37 37 @char_1 = CHAR_1_HEX.dup
38 38 @tag_char_1 = "tag-#{CHAR_1_HEX}-00"
39 39 @branch_char_0 = "branch-#{CHAR_1_HEX}-00"
40 40 @branch_char_1 = "branch-#{CHAR_1_HEX}-01"
41 41 if @char_1.respond_to?(:force_encoding)
42 42 @char_1.force_encoding('UTF-8')
43 43 @tag_char_1.force_encoding('UTF-8')
44 44 @branch_char_0.force_encoding('UTF-8')
45 45 @branch_char_1.force_encoding('UTF-8')
46 46 end
47 47 end
48 48
49 49 def test_blank_path_to_repository_error_message
50 50 set_language_if_valid 'en'
51 51 repo = Repository::Mercurial.new(
52 52 :project => @project,
53 53 :identifier => 'test'
54 54 )
55 55 assert !repo.save
56 56 assert_include "Path to repository can't be blank",
57 57 repo.errors.full_messages
58 58 end
59 59
60 60 def test_blank_path_to_repository_error_message_fr
61 61 set_language_if_valid 'fr'
62 62 str = "Chemin du d\xc3\xa9p\xc3\xb4t doit \xc3\xaatre renseign\xc3\xa9(e)"
63 63 str.force_encoding('UTF-8') if str.respond_to?(:force_encoding)
64 64 repo = Repository::Mercurial.new(
65 65 :project => @project,
66 66 :url => "",
67 67 :identifier => 'test',
68 68 :path_encoding => ''
69 69 )
70 70 assert !repo.save
71 71 assert_include str, repo.errors.full_messages
72 72 end
73 73
74 74 if File.directory?(REPOSITORY_PATH)
75 75 def test_scm_available
76 76 klass = Repository::Mercurial
77 77 assert_equal "Mercurial", klass.scm_name
78 78 assert klass.scm_adapter_class
79 79 assert_not_equal "", klass.scm_command
80 80 assert_equal true, klass.scm_available
81 81 end
82 82
83 83 def test_entries
84 84 entries = @repository.entries
85 85 assert_kind_of Redmine::Scm::Adapters::Entries, entries
86 86 end
87 87
88 88 def test_fetch_changesets_from_scratch
89 89 assert_equal 0, @repository.changesets.count
90 90 @repository.fetch_changesets
91 91 @project.reload
92 92 assert_equal NUM_REV, @repository.changesets.count
93 93 assert_equal 46, @repository.filechanges.count
94 94 assert_equal "Initial import.\nThe repository contains 3 files.",
95 95 @repository.changesets.find_by_revision('0').comments
96 96 end
97 97
98 98 def test_fetch_changesets_incremental
99 99 assert_equal 0, @repository.changesets.count
100 100 @repository.fetch_changesets
101 101 @project.reload
102 102 assert_equal NUM_REV, @repository.changesets.count
103 103 # Remove changesets with revision > 2
104 104 @repository.changesets.each {|c| c.destroy if c.revision.to_i > 2}
105 105 @project.reload
106 106 @repository.reload
107 107 assert_equal 3, @repository.changesets.count
108 108
109 109 @repository.fetch_changesets
110 110 @project.reload
111 111 assert_equal NUM_REV, @repository.changesets.count
112 112 end
113 113
114 114 def test_isodatesec
115 115 # Template keyword 'isodatesec' supported in Mercurial 1.0 and higher
116 116 if @repository.scm.class.client_version_above?([1, 0])
117 117 assert_equal 0, @repository.changesets.count
118 118 @repository.fetch_changesets
119 119 @project.reload
120 120 assert_equal NUM_REV, @repository.changesets.count
121 121 rev0_committed_on = Time.gm(2007, 12, 14, 9, 22, 52)
122 122 assert_equal @repository.changesets.find_by_revision('0').committed_on, rev0_committed_on
123 123 end
124 124 end
125 125
126 126 def test_changeset_order_by_revision
127 127 assert_equal 0, @repository.changesets.count
128 128 @repository.fetch_changesets
129 129 @project.reload
130 130 assert_equal NUM_REV, @repository.changesets.count
131 131
132 132 c0 = @repository.latest_changeset
133 133 c1 = @repository.changesets.find_by_revision('0')
134 134 # sorted by revision (id), not by date
135 135 assert c0.revision.to_i > c1.revision.to_i
136 136 assert c0.committed_on < c1.committed_on
137 137 end
138 138
139 139 def test_latest_changesets
140 140 assert_equal 0, @repository.changesets.count
141 141 @repository.fetch_changesets
142 142 @project.reload
143 143 assert_equal NUM_REV, @repository.changesets.count
144 144
145 145 # with_limit
146 146 changesets = @repository.latest_changesets('', nil, 2)
147 147 assert_equal %w|31 30|, changesets.collect(&:revision)
148 148
149 149 # with_filepath
150 150 changesets = @repository.latest_changesets(
151 151 '/sql_escape/percent%dir/percent%file1.txt', nil)
152 152 assert_equal %w|30 11 10 9|, changesets.collect(&:revision)
153 153
154 154 changesets = @repository.latest_changesets(
155 155 '/sql_escape/underscore_dir/understrike_file.txt', nil)
156 156 assert_equal %w|30 12 9|, changesets.collect(&:revision)
157 157
158 158 changesets = @repository.latest_changesets('README', nil)
159 159 assert_equal %w|31 30 28 17 8 6 1 0|, changesets.collect(&:revision)
160 160
161 161 changesets = @repository.latest_changesets('README','8')
162 162 assert_equal %w|8 6 1 0|, changesets.collect(&:revision)
163 163
164 164 changesets = @repository.latest_changesets('README','8', 2)
165 165 assert_equal %w|8 6|, changesets.collect(&:revision)
166 166
167 167 # with_dirpath
168 168 changesets = @repository.latest_changesets('images', nil)
169 169 assert_equal %w|1 0|, changesets.collect(&:revision)
170 170
171 171 path = 'sql_escape/percent%dir'
172 172 changesets = @repository.latest_changesets(path, nil)
173 173 assert_equal %w|30 13 11 10 9|, changesets.collect(&:revision)
174 174
175 175 changesets = @repository.latest_changesets(path, '11')
176 176 assert_equal %w|11 10 9|, changesets.collect(&:revision)
177 177
178 178 changesets = @repository.latest_changesets(path, '11', 2)
179 179 assert_equal %w|11 10|, changesets.collect(&:revision)
180 180
181 181 path = 'sql_escape/underscore_dir'
182 182 changesets = @repository.latest_changesets(path, nil)
183 183 assert_equal %w|30 13 12 9|, changesets.collect(&:revision)
184 184
185 185 changesets = @repository.latest_changesets(path, '12')
186 186 assert_equal %w|12 9|, changesets.collect(&:revision)
187 187
188 188 changesets = @repository.latest_changesets(path, '12', 1)
189 189 assert_equal %w|12|, changesets.collect(&:revision)
190 190 end
191 191
192 192 def test_latest_changesets_tag
193 193 assert_equal 0, @repository.changesets.count
194 194 @repository.fetch_changesets
195 195 @project.reload
196 196 assert_equal NUM_REV, @repository.changesets.count
197 197
198 198 changesets = @repository.latest_changesets('', 'tag_test.00')
199 199 assert_equal %w|5 4 3 2 1 0|, changesets.collect(&:revision)
200 200
201 201 changesets = @repository.latest_changesets('', 'tag_test.00', 2)
202 202 assert_equal %w|5 4|, changesets.collect(&:revision)
203 203
204 204 changesets = @repository.latest_changesets('sources', 'tag_test.00')
205 205 assert_equal %w|4 3 2 1 0|, changesets.collect(&:revision)
206 206
207 207 changesets = @repository.latest_changesets('sources', 'tag_test.00', 2)
208 208 assert_equal %w|4 3|, changesets.collect(&:revision)
209 209 end
210 210
211 211 def test_latest_changesets_branch
212 212 assert_equal 0, @repository.changesets.count
213 213 @repository.fetch_changesets
214 214 @project.reload
215 215 assert_equal NUM_REV, @repository.changesets.count
216 216
217 217 if @repository.scm.class.client_version_above?([1, 6])
218 218 changesets = @repository.latest_changesets('', @branch_char_1)
219 219 assert_equal %w|27 26|, changesets.collect(&:revision)
220 220 end
221 221
222 222 changesets = @repository.latest_changesets("latin-1-dir/test-#{@char_1}-subdir", @branch_char_1)
223 223 assert_equal %w|27|, changesets.collect(&:revision)
224 224 end
225 225
226 226 def test_copied_files
227 227 assert_equal 0, @repository.changesets.count
228 228 @repository.fetch_changesets
229 229 @project.reload
230 230 assert_equal NUM_REV, @repository.changesets.count
231 231
232 232 cs1 = @repository.changesets.find_by_revision('13')
233 233 assert_not_nil cs1
234 234 c1 = cs1.filechanges.sort_by(&:path)
235 235 assert_equal 2, c1.size
236 236
237 237 assert_equal 'A', c1[0].action
238 238 assert_equal '/sql_escape/percent%dir/percentfile1.txt', c1[0].path
239 239 assert_equal '/sql_escape/percent%dir/percent%file1.txt', c1[0].from_path
240 240 assert_equal '3a330eb32958', c1[0].from_revision
241 241
242 242 assert_equal 'A', c1[1].action
243 243 assert_equal '/sql_escape/underscore_dir/understrike-file.txt', c1[1].path
244 244 assert_equal '/sql_escape/underscore_dir/understrike_file.txt', c1[1].from_path
245 245
246 246 cs2 = @repository.changesets.find_by_revision('15')
247 247 c2 = cs2.filechanges
248 248 assert_equal 1, c2.size
249 249
250 250 assert_equal 'A', c2[0].action
251 251 assert_equal '/README (1)[2]&,%.-3_4', c2[0].path
252 252 assert_equal '/README', c2[0].from_path
253 253 assert_equal '933ca60293d7', c2[0].from_revision
254 254
255 255 cs3 = @repository.changesets.find_by_revision('19')
256 256 c3 = cs3.filechanges
257 257 assert_equal 1, c3.size
258 258 assert_equal 'A', c3[0].action
259 259 assert_equal "/latin-1-dir/test-#{@char_1}-1.txt", c3[0].path
260 260 assert_equal "/latin-1-dir/test-#{@char_1}.txt", c3[0].from_path
261 261 assert_equal '5d9891a1b425', c3[0].from_revision
262 262 end
263 263
264 264 def test_find_changeset_by_name
265 265 assert_equal 0, @repository.changesets.count
266 266 @repository.fetch_changesets
267 267 @project.reload
268 268 assert_equal NUM_REV, @repository.changesets.count
269 269 %w|2 400bb8672109 400|.each do |r|
270 270 assert_equal '2', @repository.find_changeset_by_name(r).revision
271 271 end
272 272 end
273 273
274 274 def test_find_changeset_by_invalid_name
275 275 assert_equal 0, @repository.changesets.count
276 276 @repository.fetch_changesets
277 277 @project.reload
278 278 assert_equal NUM_REV, @repository.changesets.count
279 279 assert_nil @repository.find_changeset_by_name('100000')
280 280 end
281 281
282 282 def test_identifier
283 283 assert_equal 0, @repository.changesets.count
284 284 @repository.fetch_changesets
285 285 @project.reload
286 286 assert_equal NUM_REV, @repository.changesets.count
287 287 c = @repository.changesets.find_by_revision('2')
288 288 assert_equal c.scmid, c.identifier
289 289 end
290 290
291 291 def test_format_identifier
292 292 assert_equal 0, @repository.changesets.count
293 293 @repository.fetch_changesets
294 294 @project.reload
295 295 assert_equal NUM_REV, @repository.changesets.count
296 296 c = @repository.changesets.find_by_revision('2')
297 297 assert_equal '2:400bb8672109', c.format_identifier
298 298 end
299 299
300 def test_format_identifier_long_id
301 assert_equal 0, @repository.changesets.count
302 Changeset.create!(:repository => @repository,
303 :committed_on => Time.now,
304 :revision => '0',
305 :scmid => '0885933ad4f68d77c2649cd11f8311276e7ef7ce',
306 :comments => 'test')
307 c = @repository.changesets.find_by_revision('0')
308 assert_equal '0:0885933ad4f6', c.format_identifier
309 end
310
300 311 def test_find_changeset_by_empty_name
301 312 assert_equal 0, @repository.changesets.count
302 313 @repository.fetch_changesets
303 314 @project.reload
304 315 assert_equal NUM_REV, @repository.changesets.count
305 316 ['', ' ', nil].each do |r|
306 317 assert_nil @repository.find_changeset_by_name(r)
307 318 end
308 319 end
309 320
310 321 def test_parents
311 322 assert_equal 0, @repository.changesets.count
312 323 @repository.fetch_changesets
313 324 @project.reload
314 325 assert_equal NUM_REV, @repository.changesets.count
315 326 r1 = @repository.changesets.find_by_revision('0')
316 327 assert_equal [], r1.parents
317 328 r2 = @repository.changesets.find_by_revision('1')
318 329 assert_equal 1, r2.parents.length
319 330 assert_equal "0885933ad4f6",
320 331 r2.parents[0].identifier
321 332 r3 = @repository.changesets.find_by_revision('30')
322 333 assert_equal 2, r3.parents.length
323 334 r4 = [r3.parents[0].identifier, r3.parents[1].identifier].sort
324 335 assert_equal "3a330eb32958", r4[0]
325 336 assert_equal "a94b0528f24f", r4[1]
326 337 end
327 338
328 339 def test_activities
329 340 c = Changeset.new(:repository => @repository,
330 341 :committed_on => Time.now,
331 342 :revision => '123',
332 343 :scmid => 'abc400bb8672',
333 344 :comments => 'test')
334 345 assert c.event_title.include?('123:abc400bb8672:')
335 346 assert_equal 'abc400bb8672', c.event_url[:rev]
336 347 end
337 348
338 349 def test_previous
339 350 assert_equal 0, @repository.changesets.count
340 351 @repository.fetch_changesets
341 352 @project.reload
342 353 assert_equal NUM_REV, @repository.changesets.count
343 354 %w|28 3ae45e2d177d 3ae45|.each do |r1|
344 355 changeset = @repository.find_changeset_by_name(r1)
345 356 %w|27 7bbf4c738e71 7bbf|.each do |r2|
346 357 assert_equal @repository.find_changeset_by_name(r2), changeset.previous
347 358 end
348 359 end
349 360 end
350 361
351 362 def test_previous_nil
352 363 assert_equal 0, @repository.changesets.count
353 364 @repository.fetch_changesets
354 365 @project.reload
355 366 assert_equal NUM_REV, @repository.changesets.count
356 367 %w|0 0885933ad4f6 0885|.each do |r1|
357 368 changeset = @repository.find_changeset_by_name(r1)
358 369 assert_nil changeset.previous
359 370 end
360 371 end
361 372
362 373 def test_next
363 374 assert_equal 0, @repository.changesets.count
364 375 @repository.fetch_changesets
365 376 @project.reload
366 377 assert_equal NUM_REV, @repository.changesets.count
367 378 %w|27 7bbf4c738e71 7bbf|.each do |r2|
368 379 changeset = @repository.find_changeset_by_name(r2)
369 380 %w|28 3ae45e2d177d 3ae45|.each do |r1|
370 381 assert_equal @repository.find_changeset_by_name(r1), changeset.next
371 382 end
372 383 end
373 384 end
374 385
375 386 def test_next_nil
376 387 assert_equal 0, @repository.changesets.count
377 388 @repository.fetch_changesets
378 389 @project.reload
379 390 assert_equal NUM_REV, @repository.changesets.count
380 391 %w|31 31eeee7395c8 31eee|.each do |r1|
381 392 changeset = @repository.find_changeset_by_name(r1)
382 393 assert_nil changeset.next
383 394 end
384 395 end
385 396 else
386 397 puts "Mercurial test repository NOT FOUND. Skipping unit tests !!!"
387 398 def test_fake; assert true end
388 399 end
389 400 end
General Comments 0
You need to be logged in to leave comments. Login now