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