##// END OF EJS Templates
scm: mercurial: insert long id to DB unless existing id in DB is not short id (#14361)...
Toshi MARUYAMA -
r12502:b87a7b8984b3
parent child
Show More
@@ -1,204 +1,210
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2014 Jean-Philippe Lang
2 # Copyright (C) 2006-2014 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,
22 has_many :changesets,
23 :order => "#{Changeset.table_name}.id DESC",
23 :order => "#{Changeset.table_name}.id DESC",
24 :foreign_key => 'repository_id'
24 :foreign_key => 'repository_id'
25
25
26 attr_protected :root_url
26 attr_protected :root_url
27 validates_presence_of :url
27 validates_presence_of :url
28
28
29 # number of changesets to fetch at once
29 # number of changesets to fetch at once
30 FETCH_AT_ONCE = 100
30 FETCH_AT_ONCE = 100
31
31
32 def self.human_attribute_name(attribute_key_name, *args)
32 def self.human_attribute_name(attribute_key_name, *args)
33 attr_name = attribute_key_name.to_s
33 attr_name = attribute_key_name.to_s
34 if attr_name == "url"
34 if attr_name == "url"
35 attr_name = "path_to_repository"
35 attr_name = "path_to_repository"
36 end
36 end
37 super(attr_name, *args)
37 super(attr_name, *args)
38 end
38 end
39
39
40 def self.scm_adapter_class
40 def self.scm_adapter_class
41 Redmine::Scm::Adapters::MercurialAdapter
41 Redmine::Scm::Adapters::MercurialAdapter
42 end
42 end
43
43
44 def self.scm_name
44 def self.scm_name
45 'Mercurial'
45 'Mercurial'
46 end
46 end
47
47
48 def supports_directory_revisions?
48 def supports_directory_revisions?
49 true
49 true
50 end
50 end
51
51
52 def supports_revision_graph?
52 def supports_revision_graph?
53 true
53 true
54 end
54 end
55
55
56 def repo_log_encoding
56 def repo_log_encoding
57 'UTF-8'
57 'UTF-8'
58 end
58 end
59
59
60 # Returns the readable identifier for the given mercurial changeset
60 # Returns the readable identifier for the given mercurial changeset
61 def self.format_changeset_identifier(changeset)
61 def self.format_changeset_identifier(changeset)
62 "#{changeset.revision}:#{changeset.scmid[0, 12]}"
62 "#{changeset.revision}:#{changeset.scmid[0, 12]}"
63 end
63 end
64
64
65 # Returns the identifier for the given Mercurial changeset
65 # Returns the identifier for the given Mercurial changeset
66 def self.changeset_identifier(changeset)
66 def self.changeset_identifier(changeset)
67 changeset.scmid
67 changeset.scmid
68 end
68 end
69
69
70 def diff_format_revisions(cs, cs_to, sep=':')
70 def diff_format_revisions(cs, cs_to, sep=':')
71 super(cs, cs_to, ' ')
71 super(cs, cs_to, ' ')
72 end
72 end
73
73
74 def modify_entry_lastrev_identifier(entry)
74 def modify_entry_lastrev_identifier(entry)
75 if entry.lastrev && entry.lastrev.identifier
75 if entry.lastrev && entry.lastrev.identifier
76 entry.lastrev.identifier = scmid_for_inserting_db(entry.lastrev.identifier)
76 entry.lastrev.identifier = scmid_for_inserting_db(entry.lastrev.identifier)
77 end
77 end
78 end
78 end
79 private :modify_entry_lastrev_identifier
79 private :modify_entry_lastrev_identifier
80
80
81 def entry(path=nil, identifier=nil)
81 def entry(path=nil, identifier=nil)
82 entry = scm.entry(path, identifier)
82 entry = scm.entry(path, identifier)
83 return nil if entry.nil?
83 return nil if entry.nil?
84 modify_entry_lastrev_identifier(entry)
84 modify_entry_lastrev_identifier(entry)
85 entry
85 entry
86 end
86 end
87
87
88 def scm_entries(path=nil, identifier=nil)
88 def scm_entries(path=nil, identifier=nil)
89 entries = scm.entries(path, identifier)
89 entries = scm.entries(path, identifier)
90 return nil if entries.nil?
90 return nil if entries.nil?
91 entries.each {|entry| modify_entry_lastrev_identifier(entry)}
91 entries.each {|entry| modify_entry_lastrev_identifier(entry)}
92 entries
92 entries
93 end
93 end
94 protected :scm_entries
94 protected :scm_entries
95
95
96 # Finds and returns a revision with a number or the beginning of a hash
96 # Finds and returns a revision with a number or the beginning of a hash
97 def find_changeset_by_name(name)
97 def find_changeset_by_name(name)
98 return nil if name.blank?
98 return nil if name.blank?
99 s = name.to_s
99 s = name.to_s
100 if /[^\d]/ =~ s or s.size > 8
100 if /[^\d]/ =~ s or s.size > 8
101 cs = changesets.where(:scmid => s).first
101 cs = changesets.where(:scmid => s).first
102 else
102 else
103 cs = changesets.where(:revision => s).first
103 cs = changesets.where(:revision => s).first
104 end
104 end
105 return cs if cs
105 return cs if cs
106 changesets.where('scmid LIKE ?', "#{s}%").first
106 changesets.where('scmid LIKE ?', "#{s}%").first
107 end
107 end
108
108
109 # Returns the latest changesets for +path+; sorted by revision number
109 # Returns the latest changesets for +path+; sorted by revision number
110 #
110 #
111 # Because :order => 'id DESC' is defined at 'has_many',
111 # Because :order => 'id DESC' is defined at 'has_many',
112 # there is no need to set 'order'.
112 # there is no need to set 'order'.
113 # But, MySQL test fails.
113 # But, MySQL test fails.
114 # Sqlite3 and PostgreSQL pass.
114 # Sqlite3 and PostgreSQL pass.
115 # Is this MySQL bug?
115 # Is this MySQL bug?
116 def latest_changesets(path, rev, limit=10)
116 def latest_changesets(path, rev, limit=10)
117 changesets.
117 changesets.
118 includes(:user).
118 includes(:user).
119 where(latest_changesets_cond(path, rev, limit)).
119 where(latest_changesets_cond(path, rev, limit)).
120 limit(limit).
120 limit(limit).
121 order("#{Changeset.table_name}.id DESC").
121 order("#{Changeset.table_name}.id DESC").
122 all
122 all
123 end
123 end
124
124
125 def is_short_id_in_db?
126 return @is_short_id_in_db unless @is_short_id_in_db.nil?
127 cs = changesets.first
128 @is_short_id_in_db = (!cs.nil? && cs.scmid.length != 40)
129 end
130 private :is_short_id_in_db?
131
125 def scmid_for_inserting_db(scmid)
132 def scmid_for_inserting_db(scmid)
126 # TODO: switch short or long by existing value in DB
133 is_short_id_in_db? ? scmid[0, 12] : scmid
127 scmid[0, 12]
128 end
134 end
129
135
130 def nodes_in_branch(rev, branch_limit)
136 def nodes_in_branch(rev, branch_limit)
131 scm.nodes_in_branch(rev, :limit => branch_limit).collect do |b|
137 scm.nodes_in_branch(rev, :limit => branch_limit).collect do |b|
132 scmid_for_inserting_db(b)
138 scmid_for_inserting_db(b)
133 end
139 end
134 end
140 end
135
141
136 def tag_scmid(rev)
142 def tag_scmid(rev)
137 scmid = scm.tagmap[rev]
143 scmid = scm.tagmap[rev]
138 scmid.nil? ? nil : scmid_for_inserting_db(scmid)
144 scmid.nil? ? nil : scmid_for_inserting_db(scmid)
139 end
145 end
140
146
141 def latest_changesets_cond(path, rev, limit)
147 def latest_changesets_cond(path, rev, limit)
142 cond, args = [], []
148 cond, args = [], []
143 if scm.branchmap.member? rev
149 if scm.branchmap.member? rev
144 # Mercurial named branch is *stable* in each revision.
150 # Mercurial named branch is *stable* in each revision.
145 # So, named branch can be stored in database.
151 # So, named branch can be stored in database.
146 # Mercurial provides *bookmark* which is equivalent with git branch.
152 # Mercurial provides *bookmark* which is equivalent with git branch.
147 # But, bookmark is not implemented.
153 # But, bookmark is not implemented.
148 cond << "#{Changeset.table_name}.scmid IN (?)"
154 cond << "#{Changeset.table_name}.scmid IN (?)"
149 # Revisions in root directory and sub directory are not equal.
155 # Revisions in root directory and sub directory are not equal.
150 # So, in order to get correct limit, we need to get all revisions.
156 # So, in order to get correct limit, we need to get all revisions.
151 # But, it is very heavy.
157 # But, it is very heavy.
152 # Mercurial does not treat direcotry.
158 # Mercurial does not treat direcotry.
153 # So, "hg log DIR" is very heavy.
159 # So, "hg log DIR" is very heavy.
154 branch_limit = path.blank? ? limit : ( limit * 5 )
160 branch_limit = path.blank? ? limit : ( limit * 5 )
155 args << nodes_in_branch(rev, branch_limit)
161 args << nodes_in_branch(rev, branch_limit)
156 elsif last = rev ? find_changeset_by_name(tag_scmid(rev) || rev) : nil
162 elsif last = rev ? find_changeset_by_name(tag_scmid(rev) || rev) : nil
157 cond << "#{Changeset.table_name}.id <= ?"
163 cond << "#{Changeset.table_name}.id <= ?"
158 args << last.id
164 args << last.id
159 end
165 end
160 unless path.blank?
166 unless path.blank?
161 cond << "EXISTS (SELECT * FROM #{Change.table_name}
167 cond << "EXISTS (SELECT * FROM #{Change.table_name}
162 WHERE #{Change.table_name}.changeset_id = #{Changeset.table_name}.id
168 WHERE #{Change.table_name}.changeset_id = #{Changeset.table_name}.id
163 AND (#{Change.table_name}.path = ?
169 AND (#{Change.table_name}.path = ?
164 OR #{Change.table_name}.path LIKE ? ESCAPE ?))"
170 OR #{Change.table_name}.path LIKE ? ESCAPE ?))"
165 args << path.with_leading_slash
171 args << path.with_leading_slash
166 args << "#{path.with_leading_slash.gsub(%r{[%_\\]}) { |s| "\\#{s}" }}/%" << '\\'
172 args << "#{path.with_leading_slash.gsub(%r{[%_\\]}) { |s| "\\#{s}" }}/%" << '\\'
167 end
173 end
168 [cond.join(' AND '), *args] unless cond.empty?
174 [cond.join(' AND '), *args] unless cond.empty?
169 end
175 end
170 private :latest_changesets_cond
176 private :latest_changesets_cond
171
177
172 def fetch_changesets
178 def fetch_changesets
173 return if scm.info.nil?
179 return if scm.info.nil?
174 scm_rev = scm.info.lastrev.revision.to_i
180 scm_rev = scm.info.lastrev.revision.to_i
175 db_rev = latest_changeset ? latest_changeset.revision.to_i : -1
181 db_rev = latest_changeset ? latest_changeset.revision.to_i : -1
176 return unless db_rev < scm_rev # already up-to-date
182 return unless db_rev < scm_rev # already up-to-date
177
183
178 logger.debug "Fetching changesets for repository #{url}" if logger
184 logger.debug "Fetching changesets for repository #{url}" if logger
179 (db_rev + 1).step(scm_rev, FETCH_AT_ONCE) do |i|
185 (db_rev + 1).step(scm_rev, FETCH_AT_ONCE) do |i|
180 scm.each_revision('', i, [i + FETCH_AT_ONCE - 1, scm_rev].min) do |re|
186 scm.each_revision('', i, [i + FETCH_AT_ONCE - 1, scm_rev].min) do |re|
181 transaction do
187 transaction do
182 parents = (re.parents || []).collect do |rp|
188 parents = (re.parents || []).collect do |rp|
183 find_changeset_by_name(scmid_for_inserting_db(rp))
189 find_changeset_by_name(scmid_for_inserting_db(rp))
184 end.compact
190 end.compact
185 cs = Changeset.create(:repository => self,
191 cs = Changeset.create(:repository => self,
186 :revision => re.revision,
192 :revision => re.revision,
187 :scmid => scmid_for_inserting_db(re.scmid),
193 :scmid => scmid_for_inserting_db(re.scmid),
188 :committer => re.author,
194 :committer => re.author,
189 :committed_on => re.time,
195 :committed_on => re.time,
190 :comments => re.message,
196 :comments => re.message,
191 :parents => parents)
197 :parents => parents)
192 unless cs.new_record?
198 unless cs.new_record?
193 re.paths.each do |e|
199 re.paths.each do |e|
194 if from_revision = e[:from_revision]
200 if from_revision = e[:from_revision]
195 e[:from_revision] = scmid_for_inserting_db(from_revision)
201 e[:from_revision] = scmid_for_inserting_db(from_revision)
196 end
202 end
197 cs.create_change(e)
203 cs.create_change(e)
198 end
204 end
199 end
205 end
200 end
206 end
201 end
207 end
202 end
208 end
203 end
209 end
204 end
210 end
@@ -1,541 +1,653
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2014 Jean-Philippe Lang
2 # Copyright (C) 2006-2014 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 File.expand_path('../../test_helper', __FILE__)
18 require File.expand_path('../../test_helper', __FILE__)
19
19
20 class RepositoryMercurialTest < ActiveSupport::TestCase
20 class RepositoryMercurialTest < ActiveSupport::TestCase
21 fixtures :projects
21 fixtures :projects
22
22
23 include Redmine::I18n
23 include Redmine::I18n
24
24
25 REPOSITORY_PATH = Rails.root.join('tmp/test/mercurial_repository').to_s
25 REPOSITORY_PATH = Rails.root.join('tmp/test/mercurial_repository').to_s
26 NUM_REV = 32
26 NUM_REV = 32
27 CHAR_1_HEX = "\xc3\x9c"
27 CHAR_1_HEX = "\xc3\x9c"
28
28
29 def setup
29 def setup
30 @project = Project.find(3)
30 @project = Project.find(3)
31 @repository = Repository::Mercurial.create(
31 @repository = Repository::Mercurial.create(
32 :project => @project,
32 :project => @project,
33 :url => REPOSITORY_PATH,
33 :url => REPOSITORY_PATH,
34 :path_encoding => 'ISO-8859-1'
34 :path_encoding => 'ISO-8859-1'
35 )
35 )
36 assert @repository
36 assert @repository
37 @char_1 = CHAR_1_HEX.dup
37 @char_1 = CHAR_1_HEX.dup
38 @tag_char_1 = "tag-#{CHAR_1_HEX}-00"
38 @tag_char_1 = "tag-#{CHAR_1_HEX}-00"
39 @branch_char_0 = "branch-#{CHAR_1_HEX}-00"
39 @branch_char_0 = "branch-#{CHAR_1_HEX}-00"
40 @branch_char_1 = "branch-#{CHAR_1_HEX}-01"
40 @branch_char_1 = "branch-#{CHAR_1_HEX}-01"
41 if @char_1.respond_to?(:force_encoding)
41 if @char_1.respond_to?(:force_encoding)
42 @char_1.force_encoding('UTF-8')
42 @char_1.force_encoding('UTF-8')
43 @tag_char_1.force_encoding('UTF-8')
43 @tag_char_1.force_encoding('UTF-8')
44 @branch_char_0.force_encoding('UTF-8')
44 @branch_char_0.force_encoding('UTF-8')
45 @branch_char_1.force_encoding('UTF-8')
45 @branch_char_1.force_encoding('UTF-8')
46 end
46 end
47 end
47 end
48
48
49 def test_blank_path_to_repository_error_message
49 def test_blank_path_to_repository_error_message
50 set_language_if_valid 'en'
50 set_language_if_valid 'en'
51 repo = Repository::Mercurial.new(
51 repo = Repository::Mercurial.new(
52 :project => @project,
52 :project => @project,
53 :identifier => 'test'
53 :identifier => 'test'
54 )
54 )
55 assert !repo.save
55 assert !repo.save
56 assert_include "Path to repository can't be blank",
56 assert_include "Path to repository can't be blank",
57 repo.errors.full_messages
57 repo.errors.full_messages
58 end
58 end
59
59
60 def test_blank_path_to_repository_error_message_fr
60 def test_blank_path_to_repository_error_message_fr
61 set_language_if_valid 'fr'
61 set_language_if_valid 'fr'
62 str = "Chemin du d\xc3\xa9p\xc3\xb4t doit \xc3\xaatre renseign\xc3\xa9(e)"
62 str = "Chemin du d\xc3\xa9p\xc3\xb4t doit \xc3\xaatre renseign\xc3\xa9(e)"
63 str.force_encoding('UTF-8') if str.respond_to?(:force_encoding)
63 str.force_encoding('UTF-8') if str.respond_to?(:force_encoding)
64 repo = Repository::Mercurial.new(
64 repo = Repository::Mercurial.new(
65 :project => @project,
65 :project => @project,
66 :url => "",
66 :url => "",
67 :identifier => 'test',
67 :identifier => 'test',
68 :path_encoding => ''
68 :path_encoding => ''
69 )
69 )
70 assert !repo.save
70 assert !repo.save
71 assert_include str, repo.errors.full_messages
71 assert_include str, repo.errors.full_messages
72 end
72 end
73
73
74 if File.directory?(REPOSITORY_PATH)
74 if File.directory?(REPOSITORY_PATH)
75 def test_scm_available
75 def test_scm_available
76 klass = Repository::Mercurial
76 klass = Repository::Mercurial
77 assert_equal "Mercurial", klass.scm_name
77 assert_equal "Mercurial", klass.scm_name
78 assert klass.scm_adapter_class
78 assert klass.scm_adapter_class
79 assert_not_equal "", klass.scm_command
79 assert_not_equal "", klass.scm_command
80 assert_equal true, klass.scm_available
80 assert_equal true, klass.scm_available
81 end
81 end
82
82
83 def test_entries_on_tip
83 def test_entries_on_tip
84 entries = @repository.entries
84 entries = @repository.entries
85 assert_kind_of Redmine::Scm::Adapters::Entries, entries
85 assert_kind_of Redmine::Scm::Adapters::Entries, entries
86 end
86 end
87
87
88 def assert_entries(is_short_scmid=true)
88 def assert_entries(is_short_scmid=true)
89 hex = "9d5b5b00419901478496242e0768deba1ce8c51e"
89 hex = "9d5b5b00419901478496242e0768deba1ce8c51e"
90 scmid = scmid_for_assert(hex, is_short_scmid)
90 scmid = scmid_for_assert(hex, is_short_scmid)
91 [2, '400bb8672109', '400', 400].each do |r|
91 [2, '400bb8672109', '400', 400].each do |r|
92 entries1 = @repository.entries(nil, r)
92 entries1 = @repository.entries(nil, r)
93 assert entries1
93 assert entries1
94 assert_kind_of Redmine::Scm::Adapters::Entries, entries1
94 assert_kind_of Redmine::Scm::Adapters::Entries, entries1
95 assert_equal 3, entries1.size
95 assert_equal 3, entries1.size
96 readme = entries1[2]
96 readme = entries1[2]
97 assert_equal '1', readme.lastrev.revision
97 assert_equal '1', readme.lastrev.revision
98 assert_equal scmid, readme.lastrev.identifier
98 assert_equal scmid, readme.lastrev.identifier
99 assert_equal '1', readme.changeset.revision
99 assert_equal '1', readme.changeset.revision
100 assert_equal scmid, readme.changeset.scmid
100 assert_equal scmid, readme.changeset.scmid
101 end
101 end
102 end
102 end
103 private :assert_entries
103 private :assert_entries
104
104
105 def test_entries_short_id
105 def test_entries_short_id
106 assert_equal 0, @repository.changesets.count
106 assert_equal 0, @repository.changesets.count
107 create_rev0_short_id
108 assert_equal 1, @repository.changesets.count
107 @repository.fetch_changesets
109 @repository.fetch_changesets
108 @project.reload
110 @project.reload
109 assert_equal NUM_REV, @repository.changesets.count
111 assert_equal NUM_REV, @repository.changesets.count
110 assert_entries(true)
112 assert_entries(true)
111 end
113 end
112
114
115 def test_entries_long_id
116 assert_equal 0, @repository.changesets.count
117 @repository.fetch_changesets
118 @project.reload
119 assert_equal NUM_REV, @repository.changesets.count
120 assert_entries(false)
121 end
122
113 def test_entry_on_tip
123 def test_entry_on_tip
114 entry = @repository.entry
124 entry = @repository.entry
115 assert_kind_of Redmine::Scm::Adapters::Entry, entry
125 assert_kind_of Redmine::Scm::Adapters::Entry, entry
116 assert_equal "", entry.path
126 assert_equal "", entry.path
117 assert_equal 'dir', entry.kind
127 assert_equal 'dir', entry.kind
118 end
128 end
119
129
120 def assert_entry(is_short_scmid=true)
130 def assert_entry(is_short_scmid=true)
121 hex = "0885933ad4f68d77c2649cd11f8311276e7ef7ce"
131 hex = "0885933ad4f68d77c2649cd11f8311276e7ef7ce"
122 scmid = scmid_for_assert(hex, is_short_scmid)
132 scmid = scmid_for_assert(hex, is_short_scmid)
123 ["README", "/README"].each do |path|
133 ["README", "/README"].each do |path|
124 ["0", "0885933ad4f6", "0885933ad4f68d77c2649cd11f8311276e7ef7ce"].each do |rev|
134 ["0", "0885933ad4f6", "0885933ad4f68d77c2649cd11f8311276e7ef7ce"].each do |rev|
125 entry = @repository.entry(path, rev)
135 entry = @repository.entry(path, rev)
126 assert_kind_of Redmine::Scm::Adapters::Entry, entry
136 assert_kind_of Redmine::Scm::Adapters::Entry, entry
127 assert_equal "README", entry.path
137 assert_equal "README", entry.path
128 assert_equal "file", entry.kind
138 assert_equal "file", entry.kind
129 assert_equal '0', entry.lastrev.revision
139 assert_equal '0', entry.lastrev.revision
130 assert_equal scmid, entry.lastrev.identifier
140 assert_equal scmid, entry.lastrev.identifier
131 end
141 end
132 end
142 end
133 ["sources", "/sources", "/sources/"].each do |path|
143 ["sources", "/sources", "/sources/"].each do |path|
134 ["0", "0885933ad4f6", "0885933ad4f68d77c2649cd11f8311276e7ef7ce"].each do |rev|
144 ["0", "0885933ad4f6", "0885933ad4f68d77c2649cd11f8311276e7ef7ce"].each do |rev|
135 entry = @repository.entry(path, rev)
145 entry = @repository.entry(path, rev)
136 assert_kind_of Redmine::Scm::Adapters::Entry, entry
146 assert_kind_of Redmine::Scm::Adapters::Entry, entry
137 assert_equal "sources", entry.path
147 assert_equal "sources", entry.path
138 assert_equal "dir", entry.kind
148 assert_equal "dir", entry.kind
139 end
149 end
140 end
150 end
141 ["sources/watchers_controller.rb", "/sources/watchers_controller.rb"].each do |path|
151 ["sources/watchers_controller.rb", "/sources/watchers_controller.rb"].each do |path|
142 ["0", "0885933ad4f6", "0885933ad4f68d77c2649cd11f8311276e7ef7ce"].each do |rev|
152 ["0", "0885933ad4f6", "0885933ad4f68d77c2649cd11f8311276e7ef7ce"].each do |rev|
143 entry = @repository.entry(path, rev)
153 entry = @repository.entry(path, rev)
144 assert_kind_of Redmine::Scm::Adapters::Entry, entry
154 assert_kind_of Redmine::Scm::Adapters::Entry, entry
145 assert_equal "sources/watchers_controller.rb", entry.path
155 assert_equal "sources/watchers_controller.rb", entry.path
146 assert_equal "file", entry.kind
156 assert_equal "file", entry.kind
147 assert_equal '0', entry.lastrev.revision
157 assert_equal '0', entry.lastrev.revision
148 assert_equal scmid, entry.lastrev.identifier
158 assert_equal scmid, entry.lastrev.identifier
149 end
159 end
150 end
160 end
151 end
161 end
152 private :assert_entry
162 private :assert_entry
153
163
154 def test_entry_short_id
164 def test_entry_short_id
165 assert_equal 0, @repository.changesets.count
166 create_rev0_short_id
167 assert_equal 1, @repository.changesets.count
155 assert_entry(true)
168 assert_entry(true)
156 end
169 end
157
170
171 def test_entry_long_id
172 assert_entry(false)
173 end
174
158 def test_fetch_changesets_from_scratch
175 def test_fetch_changesets_from_scratch
159 assert_equal 0, @repository.changesets.count
176 assert_equal 0, @repository.changesets.count
160 @repository.fetch_changesets
177 @repository.fetch_changesets
161 @project.reload
178 @project.reload
162 assert_equal NUM_REV, @repository.changesets.count
179 assert_equal NUM_REV, @repository.changesets.count
163 assert_equal 46, @repository.filechanges.count
180 assert_equal 46, @repository.filechanges.count
164 rev0 = @repository.changesets.find_by_revision('0')
181 rev0 = @repository.changesets.find_by_revision('0')
165 assert_equal "Initial import.\nThe repository contains 3 files.",
182 assert_equal "Initial import.\nThe repository contains 3 files.",
166 rev0.comments
183 rev0.comments
167 assert_equal "0885933ad4f6", rev0.scmid
184 assert_equal "0885933ad4f68d77c2649cd11f8311276e7ef7ce", rev0.scmid
168 first_rev = @repository.changesets.first
185 first_rev = @repository.changesets.first
169 last_rev = @repository.changesets.last
186 last_rev = @repository.changesets.last
170 assert_equal "#{NUM_REV - 1}", first_rev.revision
187 assert_equal "#{NUM_REV - 1}", first_rev.revision
171 assert_equal "0", last_rev.revision
188 assert_equal "0", last_rev.revision
172 end
189 end
173
190
191 def test_fetch_changesets_keep_short_id
192 assert_equal 0, @repository.changesets.count
193 create_rev0_short_id
194 assert_equal 1, @repository.changesets.count
195 @repository.fetch_changesets
196 @project.reload
197 assert_equal NUM_REV, @repository.changesets.count
198 rev1 = @repository.changesets.find_by_revision('1')
199 assert_equal "9d5b5b004199", rev1.scmid
200 end
201
202 def test_fetch_changesets_keep_long_id
203 assert_equal 0, @repository.changesets.count
204 Changeset.create!(:repository => @repository,
205 :committed_on => Time.now,
206 :revision => '0',
207 :scmid => '0885933ad4f68d77c2649cd11f8311276e7ef7ce',
208 :comments => 'test')
209 assert_equal 1, @repository.changesets.count
210 @repository.fetch_changesets
211 @project.reload
212 assert_equal NUM_REV, @repository.changesets.count
213 rev1 = @repository.changesets.find_by_revision('1')
214 assert_equal "9d5b5b00419901478496242e0768deba1ce8c51e", rev1.scmid
215 end
216
174 def test_fetch_changesets_incremental
217 def test_fetch_changesets_incremental
175 assert_equal 0, @repository.changesets.count
218 assert_equal 0, @repository.changesets.count
176 @repository.fetch_changesets
219 @repository.fetch_changesets
177 @project.reload
220 @project.reload
178 assert_equal NUM_REV, @repository.changesets.count
221 assert_equal NUM_REV, @repository.changesets.count
179 # Remove changesets with revision > 2
222 # Remove changesets with revision > 2
180 @repository.changesets.each {|c| c.destroy if c.revision.to_i > 2}
223 @repository.changesets.each {|c| c.destroy if c.revision.to_i > 2}
181 @project.reload
224 @project.reload
182 @repository.reload
225 @repository.reload
183 assert_equal 3, @repository.changesets.count
226 assert_equal 3, @repository.changesets.count
184
227
185 @repository.fetch_changesets
228 @repository.fetch_changesets
186 @project.reload
229 @project.reload
187 assert_equal NUM_REV, @repository.changesets.count
230 assert_equal NUM_REV, @repository.changesets.count
188 end
231 end
189
232
190 def test_isodatesec
233 def test_isodatesec
191 # Template keyword 'isodatesec' supported in Mercurial 1.0 and higher
234 # Template keyword 'isodatesec' supported in Mercurial 1.0 and higher
192 if @repository.scm.class.client_version_above?([1, 0])
235 if @repository.scm.class.client_version_above?([1, 0])
193 assert_equal 0, @repository.changesets.count
236 assert_equal 0, @repository.changesets.count
194 @repository.fetch_changesets
237 @repository.fetch_changesets
195 @project.reload
238 @project.reload
196 assert_equal NUM_REV, @repository.changesets.count
239 assert_equal NUM_REV, @repository.changesets.count
197 rev0_committed_on = Time.gm(2007, 12, 14, 9, 22, 52)
240 rev0_committed_on = Time.gm(2007, 12, 14, 9, 22, 52)
198 assert_equal @repository.changesets.find_by_revision('0').committed_on, rev0_committed_on
241 assert_equal @repository.changesets.find_by_revision('0').committed_on, rev0_committed_on
199 end
242 end
200 end
243 end
201
244
202 def test_changeset_order_by_revision
245 def test_changeset_order_by_revision
203 assert_equal 0, @repository.changesets.count
246 assert_equal 0, @repository.changesets.count
204 @repository.fetch_changesets
247 @repository.fetch_changesets
205 @project.reload
248 @project.reload
206 assert_equal NUM_REV, @repository.changesets.count
249 assert_equal NUM_REV, @repository.changesets.count
207
250
208 c0 = @repository.latest_changeset
251 c0 = @repository.latest_changeset
209 c1 = @repository.changesets.find_by_revision('0')
252 c1 = @repository.changesets.find_by_revision('0')
210 # sorted by revision (id), not by date
253 # sorted by revision (id), not by date
211 assert c0.revision.to_i > c1.revision.to_i
254 assert c0.revision.to_i > c1.revision.to_i
212 assert c0.committed_on < c1.committed_on
255 assert c0.committed_on < c1.committed_on
213 end
256 end
214
257
215 def test_latest_changesets
258 def test_latest_changesets
216 assert_equal 0, @repository.changesets.count
259 assert_equal 0, @repository.changesets.count
217 @repository.fetch_changesets
260 @repository.fetch_changesets
218 @project.reload
261 @project.reload
219 assert_equal NUM_REV, @repository.changesets.count
262 assert_equal NUM_REV, @repository.changesets.count
220
263
221 # with_limit
264 # with_limit
222 changesets = @repository.latest_changesets('', nil, 2)
265 changesets = @repository.latest_changesets('', nil, 2)
223 assert_equal %w|31 30|, changesets.collect(&:revision)
266 assert_equal %w|31 30|, changesets.collect(&:revision)
224
267
225 # with_filepath
268 # with_filepath
226 changesets = @repository.latest_changesets(
269 changesets = @repository.latest_changesets(
227 '/sql_escape/percent%dir/percent%file1.txt', nil)
270 '/sql_escape/percent%dir/percent%file1.txt', nil)
228 assert_equal %w|30 11 10 9|, changesets.collect(&:revision)
271 assert_equal %w|30 11 10 9|, changesets.collect(&:revision)
229
272
230 changesets = @repository.latest_changesets(
273 changesets = @repository.latest_changesets(
231 '/sql_escape/underscore_dir/understrike_file.txt', nil)
274 '/sql_escape/underscore_dir/understrike_file.txt', nil)
232 assert_equal %w|30 12 9|, changesets.collect(&:revision)
275 assert_equal %w|30 12 9|, changesets.collect(&:revision)
233
276
234 changesets = @repository.latest_changesets('README', nil)
277 changesets = @repository.latest_changesets('README', nil)
235 assert_equal %w|31 30 28 17 8 6 1 0|, changesets.collect(&:revision)
278 assert_equal %w|31 30 28 17 8 6 1 0|, changesets.collect(&:revision)
236
279
237 changesets = @repository.latest_changesets('README','8')
280 changesets = @repository.latest_changesets('README','8')
238 assert_equal %w|8 6 1 0|, changesets.collect(&:revision)
281 assert_equal %w|8 6 1 0|, changesets.collect(&:revision)
239
282
240 changesets = @repository.latest_changesets('README','8', 2)
283 changesets = @repository.latest_changesets('README','8', 2)
241 assert_equal %w|8 6|, changesets.collect(&:revision)
284 assert_equal %w|8 6|, changesets.collect(&:revision)
242
285
243 # with_dirpath
286 # with_dirpath
244 changesets = @repository.latest_changesets('images', nil)
287 changesets = @repository.latest_changesets('images', nil)
245 assert_equal %w|1 0|, changesets.collect(&:revision)
288 assert_equal %w|1 0|, changesets.collect(&:revision)
246
289
247 path = 'sql_escape/percent%dir'
290 path = 'sql_escape/percent%dir'
248 changesets = @repository.latest_changesets(path, nil)
291 changesets = @repository.latest_changesets(path, nil)
249 assert_equal %w|30 13 11 10 9|, changesets.collect(&:revision)
292 assert_equal %w|30 13 11 10 9|, changesets.collect(&:revision)
250
293
251 changesets = @repository.latest_changesets(path, '11')
294 changesets = @repository.latest_changesets(path, '11')
252 assert_equal %w|11 10 9|, changesets.collect(&:revision)
295 assert_equal %w|11 10 9|, changesets.collect(&:revision)
253
296
254 changesets = @repository.latest_changesets(path, '11', 2)
297 changesets = @repository.latest_changesets(path, '11', 2)
255 assert_equal %w|11 10|, changesets.collect(&:revision)
298 assert_equal %w|11 10|, changesets.collect(&:revision)
256
299
257 path = 'sql_escape/underscore_dir'
300 path = 'sql_escape/underscore_dir'
258 changesets = @repository.latest_changesets(path, nil)
301 changesets = @repository.latest_changesets(path, nil)
259 assert_equal %w|30 13 12 9|, changesets.collect(&:revision)
302 assert_equal %w|30 13 12 9|, changesets.collect(&:revision)
260
303
261 changesets = @repository.latest_changesets(path, '12')
304 changesets = @repository.latest_changesets(path, '12')
262 assert_equal %w|12 9|, changesets.collect(&:revision)
305 assert_equal %w|12 9|, changesets.collect(&:revision)
263
306
264 changesets = @repository.latest_changesets(path, '12', 1)
307 changesets = @repository.latest_changesets(path, '12', 1)
265 assert_equal %w|12|, changesets.collect(&:revision)
308 assert_equal %w|12|, changesets.collect(&:revision)
266 end
309 end
267
310
268 def assert_latest_changesets_tag
311 def assert_latest_changesets_tag
269 changesets = @repository.latest_changesets('', 'tag_test.00')
312 changesets = @repository.latest_changesets('', 'tag_test.00')
270 assert_equal %w|5 4 3 2 1 0|, changesets.collect(&:revision)
313 assert_equal %w|5 4 3 2 1 0|, changesets.collect(&:revision)
271 end
314 end
272 private :assert_latest_changesets_tag
315 private :assert_latest_changesets_tag
273
316
274 def test_latest_changesets_tag
317 def test_latest_changesets_tag
275 assert_equal 0, @repository.changesets.count
318 assert_equal 0, @repository.changesets.count
276 @repository.fetch_changesets
319 @repository.fetch_changesets
277 @project.reload
320 @project.reload
278 assert_equal NUM_REV, @repository.changesets.count
321 assert_equal NUM_REV, @repository.changesets.count
279 assert_latest_changesets_tag
322 assert_latest_changesets_tag
280 end
323 end
281
324
325 def test_latest_changesets_tag_short_id
326 assert_equal 0, @repository.changesets.count
327 create_rev0_short_id
328 assert_equal 1, @repository.changesets.count
329 @repository.fetch_changesets
330 @project.reload
331 assert_equal NUM_REV, @repository.changesets.count
332 assert_latest_changesets_tag
333 end
334
282 def test_latest_changesets_tag_with_path
335 def test_latest_changesets_tag_with_path
283 assert_equal 0, @repository.changesets.count
336 assert_equal 0, @repository.changesets.count
284 @repository.fetch_changesets
337 @repository.fetch_changesets
285 @project.reload
338 @project.reload
286 assert_equal NUM_REV, @repository.changesets.count
339 assert_equal NUM_REV, @repository.changesets.count
287
340
288 changesets = @repository.latest_changesets('sources', 'tag_test.00')
341 changesets = @repository.latest_changesets('sources', 'tag_test.00')
289 assert_equal %w|4 3 2 1 0|, changesets.collect(&:revision)
342 assert_equal %w|4 3 2 1 0|, changesets.collect(&:revision)
290 end
343 end
291
344
292 def test_latest_changesets_tag_with_limit
345 def test_latest_changesets_tag_with_limit
293 assert_equal 0, @repository.changesets.count
346 assert_equal 0, @repository.changesets.count
294 @repository.fetch_changesets
347 @repository.fetch_changesets
295 @project.reload
348 @project.reload
296 assert_equal NUM_REV, @repository.changesets.count
349 assert_equal NUM_REV, @repository.changesets.count
297
350
298 changesets = @repository.latest_changesets('', 'tag_test.00', 2)
351 changesets = @repository.latest_changesets('', 'tag_test.00', 2)
299 assert_equal %w|5 4|, changesets.collect(&:revision)
352 assert_equal %w|5 4|, changesets.collect(&:revision)
300
353
301 changesets = @repository.latest_changesets('sources', 'tag_test.00', 2)
354 changesets = @repository.latest_changesets('sources', 'tag_test.00', 2)
302 assert_equal %w|4 3|, changesets.collect(&:revision)
355 assert_equal %w|4 3|, changesets.collect(&:revision)
303 end
356 end
304
357
305 def test_latest_changesets_branch
358 def test_latest_changesets_branch
306 assert_equal 0, @repository.changesets.count
359 assert_equal 0, @repository.changesets.count
307 @repository.fetch_changesets
360 @repository.fetch_changesets
308 @project.reload
361 @project.reload
309 assert_equal NUM_REV, @repository.changesets.count
362 assert_equal NUM_REV, @repository.changesets.count
310
363
311 if @repository.scm.class.client_version_above?([1, 6])
364 if @repository.scm.class.client_version_above?([1, 6])
312 changesets = @repository.latest_changesets('', @branch_char_1)
365 changesets = @repository.latest_changesets('', @branch_char_1)
313 assert_equal %w|27 26|, changesets.collect(&:revision)
366 assert_equal %w|27 26|, changesets.collect(&:revision)
314 end
367 end
315
368
316 changesets = @repository.latest_changesets("latin-1-dir/test-#{@char_1}-subdir", @branch_char_1)
369 changesets = @repository.latest_changesets("latin-1-dir/test-#{@char_1}-subdir", @branch_char_1)
317 assert_equal %w|27|, changesets.collect(&:revision)
370 assert_equal %w|27|, changesets.collect(&:revision)
318 end
371 end
319
372
320 def assert_latest_changesets_default_branch
373 def assert_latest_changesets_default_branch
321 changesets = @repository.latest_changesets('', 'default')
374 changesets = @repository.latest_changesets('', 'default')
322 assert_equal %w|31 28 24 6 4 3 2 1 0|, changesets.collect(&:revision)
375 assert_equal %w|31 28 24 6 4 3 2 1 0|, changesets.collect(&:revision)
323 end
376 end
324 private :assert_latest_changesets_default_branch
377 private :assert_latest_changesets_default_branch
325
378
326 def test_latest_changesets_default_branch
379 def test_latest_changesets_default_branch
327 assert_equal 0, @repository.changesets.count
380 assert_equal 0, @repository.changesets.count
328 @repository.fetch_changesets
381 @repository.fetch_changesets
329 @project.reload
382 @project.reload
330 assert_equal NUM_REV, @repository.changesets.count
383 assert_equal NUM_REV, @repository.changesets.count
331 assert_latest_changesets_default_branch
384 assert_latest_changesets_default_branch
332 end
385 end
333
386
387 def test_latest_changesets_default_branch_short_id
388 assert_equal 0, @repository.changesets.count
389 create_rev0_short_id
390 assert_equal 1, @repository.changesets.count
391 @repository.fetch_changesets
392 @project.reload
393 assert_equal NUM_REV, @repository.changesets.count
394 assert_latest_changesets_default_branch
395 end
396
334 def assert_copied_files(is_short_scmid=true)
397 def assert_copied_files(is_short_scmid=true)
335 cs1 = @repository.changesets.find_by_revision('13')
398 cs1 = @repository.changesets.find_by_revision('13')
336 assert_not_nil cs1
399 assert_not_nil cs1
337 c1 = cs1.filechanges.sort_by(&:path)
400 c1 = cs1.filechanges.sort_by(&:path)
338 assert_equal 2, c1.size
401 assert_equal 2, c1.size
339
402
340 hex1 = "3a330eb329586ea2adb3f83237c23310e744ebe9"
403 hex1 = "3a330eb329586ea2adb3f83237c23310e744ebe9"
341 scmid1 = scmid_for_assert(hex1, is_short_scmid)
404 scmid1 = scmid_for_assert(hex1, is_short_scmid)
342 assert_equal 'A', c1[0].action
405 assert_equal 'A', c1[0].action
343 assert_equal '/sql_escape/percent%dir/percentfile1.txt', c1[0].path
406 assert_equal '/sql_escape/percent%dir/percentfile1.txt', c1[0].path
344 assert_equal '/sql_escape/percent%dir/percent%file1.txt', c1[0].from_path
407 assert_equal '/sql_escape/percent%dir/percent%file1.txt', c1[0].from_path
345 assert_equal scmid1, c1[0].from_revision
408 assert_equal scmid1, c1[0].from_revision
346
409
347 assert_equal 'A', c1[1].action
410 assert_equal 'A', c1[1].action
348 assert_equal '/sql_escape/underscore_dir/understrike-file.txt', c1[1].path
411 assert_equal '/sql_escape/underscore_dir/understrike-file.txt', c1[1].path
349 assert_equal '/sql_escape/underscore_dir/understrike_file.txt', c1[1].from_path
412 assert_equal '/sql_escape/underscore_dir/understrike_file.txt', c1[1].from_path
350
413
351 cs2 = @repository.changesets.find_by_revision('15')
414 cs2 = @repository.changesets.find_by_revision('15')
352 c2 = cs2.filechanges
415 c2 = cs2.filechanges
353 assert_equal 1, c2.size
416 assert_equal 1, c2.size
354
417
355 hex2 = "933ca60293d78f7c7979dd123cc0c02431683575"
418 hex2 = "933ca60293d78f7c7979dd123cc0c02431683575"
356 scmid2 = scmid_for_assert(hex2, is_short_scmid)
419 scmid2 = scmid_for_assert(hex2, is_short_scmid)
357 assert_equal 'A', c2[0].action
420 assert_equal 'A', c2[0].action
358 assert_equal '/README (1)[2]&,%.-3_4', c2[0].path
421 assert_equal '/README (1)[2]&,%.-3_4', c2[0].path
359 assert_equal '/README', c2[0].from_path
422 assert_equal '/README', c2[0].from_path
360 assert_equal scmid2, c2[0].from_revision
423 assert_equal scmid2, c2[0].from_revision
361
424
362 cs3 = @repository.changesets.find_by_revision('19')
425 cs3 = @repository.changesets.find_by_revision('19')
363 c3 = cs3.filechanges
426 c3 = cs3.filechanges
364
427
365 hex3 = "5d9891a1b4258ea256552aa856e388f2da28256a"
428 hex3 = "5d9891a1b4258ea256552aa856e388f2da28256a"
366 scmid3 = scmid_for_assert(hex3, is_short_scmid)
429 scmid3 = scmid_for_assert(hex3, is_short_scmid)
367 assert_equal 1, c3.size
430 assert_equal 1, c3.size
368 assert_equal 'A', c3[0].action
431 assert_equal 'A', c3[0].action
369 assert_equal "/latin-1-dir/test-#{@char_1}-1.txt", c3[0].path
432 assert_equal "/latin-1-dir/test-#{@char_1}-1.txt", c3[0].path
370 assert_equal "/latin-1-dir/test-#{@char_1}.txt", c3[0].from_path
433 assert_equal "/latin-1-dir/test-#{@char_1}.txt", c3[0].from_path
371 assert_equal scmid3, c3[0].from_revision
434 assert_equal scmid3, c3[0].from_revision
372 end
435 end
373 private :assert_copied_files
436 private :assert_copied_files
374
437
375 def test_copied_files_short_id
438 def test_copied_files_short_id
376 assert_equal 0, @repository.changesets.count
439 assert_equal 0, @repository.changesets.count
440 create_rev0_short_id
441 assert_equal 1, @repository.changesets.count
377 @repository.fetch_changesets
442 @repository.fetch_changesets
378 @project.reload
443 @project.reload
379 assert_equal NUM_REV, @repository.changesets.count
444 assert_equal NUM_REV, @repository.changesets.count
380 assert_copied_files(true)
445 assert_copied_files(true)
381 end
446 end
382
447
448 def test_copied_files_long_id
449 assert_equal 0, @repository.changesets.count
450 @repository.fetch_changesets
451 @project.reload
452 assert_equal NUM_REV, @repository.changesets.count
453 assert_copied_files(false)
454 end
455
383 def test_find_changeset_by_name
456 def test_find_changeset_by_name
384 assert_equal 0, @repository.changesets.count
457 assert_equal 0, @repository.changesets.count
385 @repository.fetch_changesets
458 @repository.fetch_changesets
386 @project.reload
459 @project.reload
387 assert_equal NUM_REV, @repository.changesets.count
460 assert_equal NUM_REV, @repository.changesets.count
388 %w|2 400bb8672109 400|.each do |r|
461 %w|2 400bb8672109 400|.each do |r|
389 assert_equal '2', @repository.find_changeset_by_name(r).revision
462 assert_equal '2', @repository.find_changeset_by_name(r).revision
390 end
463 end
391 end
464 end
392
465
393 def test_find_changeset_by_invalid_name
466 def test_find_changeset_by_invalid_name
394 assert_equal 0, @repository.changesets.count
467 assert_equal 0, @repository.changesets.count
395 @repository.fetch_changesets
468 @repository.fetch_changesets
396 @project.reload
469 @project.reload
397 assert_equal NUM_REV, @repository.changesets.count
470 assert_equal NUM_REV, @repository.changesets.count
398 assert_nil @repository.find_changeset_by_name('100000')
471 assert_nil @repository.find_changeset_by_name('100000')
399 end
472 end
400
473
401 def test_identifier
474 def test_identifier
402 assert_equal 0, @repository.changesets.count
475 assert_equal 0, @repository.changesets.count
403 @repository.fetch_changesets
476 @repository.fetch_changesets
404 @project.reload
477 @project.reload
405 assert_equal NUM_REV, @repository.changesets.count
478 assert_equal NUM_REV, @repository.changesets.count
406 c = @repository.changesets.find_by_revision('2')
479 c = @repository.changesets.find_by_revision('2')
407 assert_equal c.scmid, c.identifier
480 assert_equal c.scmid, c.identifier
408 end
481 end
409
482
410 def test_format_identifier
483 def test_format_identifier
411 assert_equal 0, @repository.changesets.count
484 assert_equal 0, @repository.changesets.count
412 @repository.fetch_changesets
485 @repository.fetch_changesets
413 @project.reload
486 @project.reload
414 assert_equal NUM_REV, @repository.changesets.count
487 assert_equal NUM_REV, @repository.changesets.count
415 c = @repository.changesets.find_by_revision('2')
488 c = @repository.changesets.find_by_revision('2')
416 assert_equal '2:400bb8672109', c.format_identifier
489 assert_equal '2:400bb8672109', c.format_identifier
417 end
490 end
418
491
419 def test_format_identifier_long_id
492 def test_format_identifier_long_id
420 assert_equal 0, @repository.changesets.count
493 assert_equal 0, @repository.changesets.count
421 Changeset.create!(:repository => @repository,
494 Changeset.create!(:repository => @repository,
422 :committed_on => Time.now,
495 :committed_on => Time.now,
423 :revision => '0',
496 :revision => '0',
424 :scmid => '0885933ad4f68d77c2649cd11f8311276e7ef7ce',
497 :scmid => '0885933ad4f68d77c2649cd11f8311276e7ef7ce',
425 :comments => 'test')
498 :comments => 'test')
426 c = @repository.changesets.find_by_revision('0')
499 c = @repository.changesets.find_by_revision('0')
427 assert_equal '0:0885933ad4f6', c.format_identifier
500 assert_equal '0:0885933ad4f6', c.format_identifier
428 end
501 end
429
502
430 def test_find_changeset_by_empty_name
503 def test_find_changeset_by_empty_name
431 assert_equal 0, @repository.changesets.count
504 assert_equal 0, @repository.changesets.count
432 @repository.fetch_changesets
505 @repository.fetch_changesets
433 @project.reload
506 @project.reload
434 assert_equal NUM_REV, @repository.changesets.count
507 assert_equal NUM_REV, @repository.changesets.count
435 ['', ' ', nil].each do |r|
508 ['', ' ', nil].each do |r|
436 assert_nil @repository.find_changeset_by_name(r)
509 assert_nil @repository.find_changeset_by_name(r)
437 end
510 end
438 end
511 end
439
512
440 def assert_parents(is_short_scmid=true)
513 def assert_parents(is_short_scmid=true)
441 r1 = @repository.changesets.find_by_revision('0')
514 r1 = @repository.changesets.find_by_revision('0')
442 assert_equal [], r1.parents
515 assert_equal [], r1.parents
443 r2 = @repository.changesets.find_by_revision('1')
516 r2 = @repository.changesets.find_by_revision('1')
444 hex2 = "0885933ad4f68d77c2649cd11f8311276e7ef7ce"
517 hex2 = "0885933ad4f68d77c2649cd11f8311276e7ef7ce"
445 scmid2 = scmid_for_assert(hex2, is_short_scmid)
518 scmid2 = scmid_for_assert(hex2, is_short_scmid)
446 assert_equal 1, r2.parents.length
519 assert_equal 1, r2.parents.length
447 assert_equal scmid2, r2.parents[0].identifier
520 assert_equal scmid2, r2.parents[0].identifier
448 r3 = @repository.changesets.find_by_revision('30')
521 r3 = @repository.changesets.find_by_revision('30')
449 assert_equal 2, r3.parents.length
522 assert_equal 2, r3.parents.length
450 r4 = [r3.parents[0].identifier, r3.parents[1].identifier].sort
523 r4 = [r3.parents[0].identifier, r3.parents[1].identifier].sort
451 hex41 = "3a330eb329586ea2adb3f83237c23310e744ebe9"
524 hex41 = "3a330eb329586ea2adb3f83237c23310e744ebe9"
452 scmid41 = scmid_for_assert(hex41, is_short_scmid)
525 scmid41 = scmid_for_assert(hex41, is_short_scmid)
453 hex42 = "a94b0528f24fe05ebaef496ae0500bb050772e36"
526 hex42 = "a94b0528f24fe05ebaef496ae0500bb050772e36"
454 scmid42 = scmid_for_assert(hex42, is_short_scmid)
527 scmid42 = scmid_for_assert(hex42, is_short_scmid)
455 assert_equal scmid41, r4[0]
528 assert_equal scmid41, r4[0]
456 assert_equal scmid42, r4[1]
529 assert_equal scmid42, r4[1]
457 end
530 end
458 private :assert_parents
531 private :assert_parents
459
532
460 def test_parents_short_id
533 def test_parents_short_id
461 assert_equal 0, @repository.changesets.count
534 assert_equal 0, @repository.changesets.count
535 create_rev0_short_id
536 assert_equal 1, @repository.changesets.count
462 @repository.fetch_changesets
537 @repository.fetch_changesets
463 @project.reload
538 @project.reload
464 assert_equal NUM_REV, @repository.changesets.count
539 assert_equal NUM_REV, @repository.changesets.count
465 assert_parents(true)
540 assert_parents(true)
466 end
541 end
467
542
543 def test_parents_long_id
544 assert_equal 0, @repository.changesets.count
545 @repository.fetch_changesets
546 @project.reload
547 assert_equal NUM_REV, @repository.changesets.count
548 assert_parents(false)
549 end
550
468 def test_activities
551 def test_activities
469 c = Changeset.new(:repository => @repository,
552 c = Changeset.new(:repository => @repository,
470 :committed_on => Time.now,
553 :committed_on => Time.now,
471 :revision => '123',
554 :revision => '123',
472 :scmid => 'abc400bb8672',
555 :scmid => 'abc400bb8672',
473 :comments => 'test')
556 :comments => 'test')
474 assert c.event_title.include?('123:abc400bb8672:')
557 assert c.event_title.include?('123:abc400bb8672:')
475 assert_equal 'abc400bb8672', c.event_url[:rev]
558 assert_equal 'abc400bb8672', c.event_url[:rev]
476 end
559 end
477
560
478 def test_previous
561 def test_previous
479 assert_equal 0, @repository.changesets.count
562 assert_equal 0, @repository.changesets.count
480 @repository.fetch_changesets
563 @repository.fetch_changesets
481 @project.reload
564 @project.reload
482 assert_equal NUM_REV, @repository.changesets.count
565 assert_equal NUM_REV, @repository.changesets.count
483 %w|28 3ae45e2d177d 3ae45|.each do |r1|
566 %w|28 3ae45e2d177d 3ae45|.each do |r1|
484 changeset = @repository.find_changeset_by_name(r1)
567 changeset = @repository.find_changeset_by_name(r1)
485 %w|27 7bbf4c738e71 7bbf|.each do |r2|
568 %w|27 7bbf4c738e71 7bbf|.each do |r2|
486 assert_equal @repository.find_changeset_by_name(r2), changeset.previous
569 assert_equal @repository.find_changeset_by_name(r2), changeset.previous
487 end
570 end
488 end
571 end
489 end
572 end
490
573
491 def test_previous_nil
574 def test_previous_nil
492 assert_equal 0, @repository.changesets.count
575 assert_equal 0, @repository.changesets.count
493 @repository.fetch_changesets
576 @repository.fetch_changesets
494 @project.reload
577 @project.reload
495 assert_equal NUM_REV, @repository.changesets.count
578 assert_equal NUM_REV, @repository.changesets.count
496 %w|0 0885933ad4f6 0885|.each do |r1|
579 %w|0 0885933ad4f6 0885|.each do |r1|
497 changeset = @repository.find_changeset_by_name(r1)
580 changeset = @repository.find_changeset_by_name(r1)
498 assert_nil changeset.previous
581 assert_nil changeset.previous
499 end
582 end
500 end
583 end
501
584
502 def test_next
585 def test_next
503 assert_equal 0, @repository.changesets.count
586 assert_equal 0, @repository.changesets.count
504 @repository.fetch_changesets
587 @repository.fetch_changesets
505 @project.reload
588 @project.reload
506 assert_equal NUM_REV, @repository.changesets.count
589 assert_equal NUM_REV, @repository.changesets.count
507 %w|27 7bbf4c738e71 7bbf|.each do |r2|
590 %w|27 7bbf4c738e71 7bbf|.each do |r2|
508 changeset = @repository.find_changeset_by_name(r2)
591 changeset = @repository.find_changeset_by_name(r2)
509 %w|28 3ae45e2d177d 3ae45|.each do |r1|
592 %w|28 3ae45e2d177d 3ae45|.each do |r1|
510 assert_equal @repository.find_changeset_by_name(r1), changeset.next
593 assert_equal @repository.find_changeset_by_name(r1), changeset.next
511 end
594 end
512 end
595 end
513 end
596 end
514
597
515 def test_next_nil
598 def test_next_nil
516 assert_equal 0, @repository.changesets.count
599 assert_equal 0, @repository.changesets.count
517 @repository.fetch_changesets
600 @repository.fetch_changesets
518 @project.reload
601 @project.reload
519 assert_equal NUM_REV, @repository.changesets.count
602 assert_equal NUM_REV, @repository.changesets.count
520 %w|31 31eeee7395c8 31eee|.each do |r1|
603 %w|31 31eeee7395c8 31eee|.each do |r1|
521 changeset = @repository.find_changeset_by_name(r1)
604 changeset = @repository.find_changeset_by_name(r1)
522 assert_nil changeset.next
605 assert_nil changeset.next
523 end
606 end
524 end
607 end
525
608
609 def test_scmid_for_inserting_db_short_id
610 assert_equal 0, @repository.changesets.count
611 create_rev0_short_id
612 assert_equal 1, @repository.changesets.count
613 rev = "0123456789012345678901234567890123456789"
614 assert_equal 12, @repository.scmid_for_inserting_db(rev).length
615 end
616
617 def test_scmid_for_inserting_db_long_id
618 rev = "0123456789012345678901234567890123456789"
619 assert_equal 0, @repository.changesets.count
620 assert_equal 40, @repository.scmid_for_inserting_db(rev).length
621 Changeset.create!(:repository => @repository,
622 :committed_on => Time.now,
623 :revision => '0',
624 :scmid => rev,
625 :comments => 'test')
626 assert_equal 1, @repository.changesets.count
627 assert_equal 40, @repository.scmid_for_inserting_db(rev).length
628 end
629
526 def test_scmid_for_assert
630 def test_scmid_for_assert
527 rev = "0123456789012345678901234567890123456789"
631 rev = "0123456789012345678901234567890123456789"
528 assert_equal rev, scmid_for_assert(rev, false)
632 assert_equal rev, scmid_for_assert(rev, false)
529 assert_equal "012345678901", scmid_for_assert(rev, true)
633 assert_equal "012345678901", scmid_for_assert(rev, true)
530 end
634 end
531
635
532 private
636 private
533
637
534 def scmid_for_assert(hex, is_short=true)
638 def scmid_for_assert(hex, is_short=true)
535 is_short ? hex[0, 12] : hex
639 is_short ? hex[0, 12] : hex
536 end
640 end
641
642 def create_rev0_short_id
643 Changeset.create!(:repository => @repository,
644 :committed_on => Time.now,
645 :revision => '0',
646 :scmid => '0885933ad4f6',
647 :comments => 'test')
648 end
537 else
649 else
538 puts "Mercurial test repository NOT FOUND. Skipping unit tests !!!"
650 puts "Mercurial test repository NOT FOUND. Skipping unit tests !!!"
539 def test_fake; assert true end
651 def test_fake; assert true end
540 end
652 end
541 end
653 end
General Comments 0
You need to be logged in to leave comments. Login now