1
# Copyright (C) 2005 Canonical Ltd
1
# Copyright (C) 2005 by Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
11
# GNU General Public License for more details.
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
from bzrlib import bzrdir, repository
22
20
from bzrlib.branch import Branch
23
21
from bzrlib.bzrdir import BzrDir
24
22
from bzrlib.builtins import merge
25
23
import bzrlib.errors
26
from bzrlib.repofmt import knitrepo
27
24
from bzrlib.tests import TestCaseWithTransport
28
25
from bzrlib.tests.HTTPTestUtil import TestCaseWithWebserver
29
26
from bzrlib.tests.test_revision import make_branches
30
27
from bzrlib.trace import mutter
31
from bzrlib.upgrade import Convert
32
28
from bzrlib.workingtree import WorkingTree
95
91
# from its own revision history
96
92
br_a2.append_revision('a-b-c')
97
93
self.assertRaises(bzrlib.errors.InstallFailed, br_a3.fetch, br_a2)
99
# TODO: ADHB 20070116 Perhaps set_last_revision shouldn't accept
100
# revisions which are not present? In that case, this test
103
# RBC 20060403 the way to do this is to uncommit the revision from
104
# the repository after the commit
106
94
#TODO: test that fetch correctly does reweaving when needed. RBC 20051008
107
95
# Note that this means - updating the weave when ghosts are filled in to
108
96
# add the right parents.
115
103
br_a, br_b = make_branches(self)
116
104
fetch_steps(self, br_a, br_b, br_a)
118
def test_fetch_self(self):
119
wt = self.make_branch_and_tree('br')
120
self.assertEqual(wt.branch.fetch(wt.branch), (0, []))
122
def test_fetch_root_knit(self):
123
"""Ensure that knit2.fetch() updates the root knit
125
This tests the case where the root has a new revision, but there are no
126
corresponding filename, parent, contents or other changes.
128
knit1_format = bzrdir.BzrDirMetaFormat1()
129
knit1_format.repository_format = knitrepo.RepositoryFormatKnit1()
130
knit2_format = bzrdir.BzrDirMetaFormat1()
131
knit2_format.repository_format = knitrepo.RepositoryFormatKnit2()
132
# we start with a knit1 repository because that causes the
133
# root revision to change for each commit, even though the content,
134
# parent, name, and other attributes are unchanged.
135
tree = self.make_branch_and_tree('tree', knit1_format)
136
tree.set_root_id('tree-root')
137
tree.commit('rev1', rev_id='rev1')
138
tree.commit('rev2', rev_id='rev2')
140
# Now we convert it to a knit2 repository so that it has a root knit
141
Convert(tree.basedir, knit2_format)
142
tree = WorkingTree.open(tree.basedir)
143
branch = self.make_branch('branch', format=knit2_format)
144
branch.pull(tree.branch, stop_revision='rev1')
145
repo = branch.repository
146
root_knit = repo.weave_store.get_weave('tree-root',
147
repo.get_transaction())
148
# Make sure fetch retrieved only what we requested
149
self.assertTrue('rev1' in root_knit)
150
self.assertTrue('rev2' not in root_knit)
151
branch.pull(tree.branch)
152
root_knit = repo.weave_store.get_weave('tree-root',
153
repo.get_transaction())
154
# Make sure that the next revision in the root knit was retrieved,
155
# even though the text, name, parent_id, etc., were unchanged.
156
self.assertTrue('rev2' in root_knit)
159
107
class TestMergeFetch(TestCaseWithTransport):
238
186
br_rem_a = Branch.open(self.get_readonly_url('branch1'))
239
187
fetch_steps(self, br_rem_a, br_b, br_a)
241
def _count_log_matches(self, target, logs):
242
"""Count the number of times the target file pattern was fetched in an http log"""
243
get_succeeds_re = re.compile(
244
'.*"GET .*%s HTTP/1.1" 20[06] - "-" "bzr/%s' %
245
( target, bzrlib.__version__))
248
if get_succeeds_re.match(line):
252
189
def test_weaves_are_retrieved_once(self):
253
190
self.build_tree(("source/", "source/file", "target/"))
254
191
wt = self.make_branch_and_tree('source')
260
197
target = BzrDir.create_branch_and_repo("target/")
261
198
source = Branch.open(self.get_readonly_url("source/"))
262
199
self.assertEqual(target.fetch(source), (2, []))
200
log_pattern = '%%s HTTP/1.1" 200 - "-" "bzr/%s"' % bzrlib.__version__
263
201
# this is the path to the literal file. As format changes
264
202
# occur it needs to be updated. FIXME: ask the store for the
266
self.log("web server logs are:")
267
http_logs = self.get_readonly_server().logs
268
self.log('\n'.join(http_logs))
269
# unfortunately this log entry is branch format specific. We could
270
# factor out the 'what files does this format use' to a method on the
271
# repository, which would let us to this generically. RBC 20060419
272
self.assertEqual(1, self._count_log_matches('/ce/id.kndx', http_logs))
273
self.assertEqual(1, self._count_log_matches('/ce/id.knit', http_logs))
274
self.assertEqual(1, self._count_log_matches('inventory.kndx', http_logs))
204
weave_suffix = log_pattern % 'weaves/ce/id.weave'
206
len([log for log in self.get_readonly_server().logs if log.endswith(weave_suffix)]))
207
inventory_weave_suffix = log_pattern % 'inventory.weave'
209
len([log for log in self.get_readonly_server().logs if log.endswith(
210
inventory_weave_suffix)]))
275
211
# this r-h check test will prevent regressions, but it currently already
276
212
# passes, before the patch to cache-rh is applied :[
277
self.assertTrue(1 >= self._count_log_matches('revision-history',
279
self.assertTrue(1 >= self._count_log_matches('last-revision',
213
revision_history_suffix = log_pattern % 'revision-history'
215
len([log for log in self.get_readonly_server().logs if log.endswith(
216
revision_history_suffix)]))
281
217
# FIXME naughty poking in there.
282
218
self.get_readonly_server().logs = []
283
219
# check there is nothing more to fetch
284
220
source = Branch.open(self.get_readonly_url("source/"))
285
221
self.assertEqual(target.fetch(source), (0, []))
286
# should make just two requests
287
http_logs = self.get_readonly_server().logs
288
self.log("web server logs are:")
289
self.log('\n'.join(http_logs))
290
self.assertEqual(1, self._count_log_matches('branch-format', http_logs))
291
self.assertEqual(1, self._count_log_matches('branch/format', http_logs))
292
self.assertEqual(1, self._count_log_matches('repository/format', http_logs))
293
self.assertTrue(1 >= self._count_log_matches('revision-history',
295
self.assertTrue(1 >= self._count_log_matches('last-revision',
297
self.assertEqual(4, len(http_logs))
222
self.failUnless(self.get_readonly_server().logs[0].endswith(log_pattern % 'branch-format'))
223
self.failUnless(self.get_readonly_server().logs[1].endswith(log_pattern % 'revision-history'))
224
self.assertEqual(2, len(self.get_readonly_server().logs))