1
# Copyright (C) 2005 by Canonical Ltd
1
# Copyright (C) 2005 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
20
26
from bzrlib.branch import Branch
21
27
from bzrlib.bzrdir import BzrDir
22
28
from bzrlib.builtins import merge
23
29
import bzrlib.errors
30
from bzrlib.repofmt import knitrepo
24
31
from bzrlib.tests import TestCaseWithTransport
25
32
from bzrlib.tests.HTTPTestUtil import TestCaseWithWebserver
26
33
from bzrlib.tests.test_revision import make_branches
27
34
from bzrlib.trace import mutter
35
from bzrlib.upgrade import Convert
28
36
from bzrlib.workingtree import WorkingTree
92
100
br_a2.append_revision('a-b-c')
93
101
self.assertRaises(bzrlib.errors.InstallFailed, br_a3.fetch, br_a2)
95
# TODO: jam 20051218 Branch should no longer allow append_revision for revisions
96
# which don't exist. So this test needs to be rewritten
97
# RBC 20060403 the way to do this is to uncommit the revision from the
98
# repository after the commit
103
# TODO: ADHB 20070116 Perhaps set_last_revision shouldn't accept
104
# revisions which are not present? In that case, this test
107
# RBC 20060403 the way to do this is to uncommit the revision from
108
# the repository after the commit
100
110
#TODO: test that fetch correctly does reweaving when needed. RBC 20051008
101
111
# Note that this means - updating the weave when ghosts are filled in to
113
123
wt = self.make_branch_and_tree('br')
114
124
self.assertEqual(wt.branch.fetch(wt.branch), (0, []))
126
def test_fetch_root_knit(self):
127
"""Ensure that knit2.fetch() updates the root knit
129
This tests the case where the root has a new revision, but there are no
130
corresponding filename, parent, contents or other changes.
132
knit1_format = bzrdir.BzrDirMetaFormat1()
133
knit1_format.repository_format = knitrepo.RepositoryFormatKnit1()
134
knit2_format = bzrdir.BzrDirMetaFormat1()
135
knit2_format.repository_format = knitrepo.RepositoryFormatKnit3()
136
# we start with a knit1 repository because that causes the
137
# root revision to change for each commit, even though the content,
138
# parent, name, and other attributes are unchanged.
139
tree = self.make_branch_and_tree('tree', knit1_format)
140
tree.set_root_id('tree-root')
141
tree.commit('rev1', rev_id='rev1')
142
tree.commit('rev2', rev_id='rev2')
144
# Now we convert it to a knit2 repository so that it has a root knit
145
Convert(tree.basedir, knit2_format)
146
tree = WorkingTree.open(tree.basedir)
147
branch = self.make_branch('branch', format=knit2_format)
148
branch.pull(tree.branch, stop_revision='rev1')
149
repo = branch.repository
150
root_knit = repo.weave_store.get_weave('tree-root',
151
repo.get_transaction())
152
# Make sure fetch retrieved only what we requested
153
self.assertTrue('rev1' in root_knit)
154
self.assertTrue('rev2' not in root_knit)
155
branch.pull(tree.branch)
156
root_knit = repo.weave_store.get_weave('tree-root',
157
repo.get_transaction())
158
# Make sure that the next revision in the root knit was retrieved,
159
# even though the text, name, parent_id, etc., were unchanged.
160
self.assertTrue('rev2' in root_knit)
162
def test_fetch_incompatible(self):
163
knit_tree = self.make_branch_and_tree('knit', format='knit')
164
knit3_tree = self.make_branch_and_tree('knit3',
165
format='dirstate-with-subtree')
166
knit3_tree.commit('blah')
167
self.assertRaises(errors.IncompatibleRepositories,
168
knit_tree.branch.fetch, knit3_tree.branch)
117
171
class TestMergeFetch(TestCaseWithTransport):
199
253
def _count_log_matches(self, target, logs):
200
254
"""Count the number of times the target file pattern was fetched in an http log"""
201
log_pattern = '%s HTTP/1.1" 200 - "-" "bzr/%s' % \
202
(target, bzrlib.__version__)
255
get_succeeds_re = re.compile(
256
'.*"GET .*%s HTTP/1.1" 20[06] - "-" "bzr/%s' %
257
( target, bzrlib.__version__))
204
259
for line in logs:
205
# TODO: perhaps use a regexp instead so we can match more
207
if line.find(log_pattern) > -1:
260
if get_succeeds_re.match(line):
219
272
target = BzrDir.create_branch_and_repo("target/")
220
273
source = Branch.open(self.get_readonly_url("source/"))
221
274
self.assertEqual(target.fetch(source), (2, []))
222
log_pattern = '%%s HTTP/1.1" 200 - "-" "bzr/%s' % bzrlib.__version__
223
275
# this is the path to the literal file. As format changes
224
276
# occur it needs to be updated. FIXME: ask the store for the
226
278
self.log("web server logs are:")
227
279
http_logs = self.get_readonly_server().logs
228
280
self.log('\n'.join(http_logs))
229
self.assertEqual(1, self._count_log_matches('weaves/ce/id.weave', http_logs))
230
self.assertEqual(1, self._count_log_matches('inventory.weave', http_logs))
281
# unfortunately this log entry is branch format specific. We could
282
# factor out the 'what files does this format use' to a method on the
283
# repository, which would let us to this generically. RBC 20060419
284
self.assertEqual(1, self._count_log_matches('/ce/id.kndx', http_logs))
285
self.assertEqual(1, self._count_log_matches('/ce/id.knit', http_logs))
286
self.assertEqual(1, self._count_log_matches('inventory.kndx', http_logs))
231
287
# this r-h check test will prevent regressions, but it currently already
232
288
# passes, before the patch to cache-rh is applied :[
233
self.assertEqual(1, self._count_log_matches('revision-history', http_logs))
289
self.assertTrue(1 >= self._count_log_matches('revision-history',
291
self.assertTrue(1 >= self._count_log_matches('last-revision',
234
293
# FIXME naughty poking in there.
235
294
self.get_readonly_server().logs = []
236
295
# check there is nothing more to fetch
240
299
http_logs = self.get_readonly_server().logs
241
300
self.log("web server logs are:")
242
301
self.log('\n'.join(http_logs))
243
self.assertEqual(1, self._count_log_matches('branch-format', http_logs[0:1]))
244
self.assertEqual(1, self._count_log_matches('revision-history', http_logs[1:2]))
245
self.assertEqual(2, len(http_logs))
302
self.assertEqual(1, self._count_log_matches('branch-format', http_logs))
303
self.assertEqual(1, self._count_log_matches('branch/format', http_logs))
304
self.assertEqual(1, self._count_log_matches('repository/format', http_logs))
305
self.assertTrue(1 >= self._count_log_matches('revision-history',
307
self.assertTrue(1 >= self._count_log_matches('last-revision',
309
self.assertEqual(4, len(http_logs))