1
# Copyright (C) 2005, 2007, 2010 Canonical Ltd
1
# Copyright (C) 2005-2011, 2016 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
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
revision as _mod_revision,
30
from bzrlib.branch import Branch
31
from bzrlib.bzrdir import BzrDir
32
from bzrlib.repofmt import knitrepo
33
from bzrlib.tests import TestCaseWithTransport
34
from bzrlib.tests.test_revision import make_branches
35
from bzrlib.trace import mutter
36
from bzrlib.upgrade import Convert
37
from bzrlib.workingtree import WorkingTree
26
from ..branch import Branch
27
from ..bzr import knitrepo
28
from . import TestCaseWithTransport
29
from .test_revision import make_branches
30
from ..upgrade import Convert
31
from ..workingtree import WorkingTree
39
33
# These tests are a bit old; please instead add new tests into
40
34
# per_interrepository/ so they'll run on all relevant
44
38
def has_revision(branch, revision_id):
45
39
return branch.repository.has_revision(revision_id)
42
def revision_history(branch):
45
graph = branch.repository.get_graph()
46
history = list(graph.iter_lefthand_ancestry(branch.last_revision(),
47
[_mod_revision.NULL_REVISION]))
47
54
def fetch_steps(self, br_a, br_b, writable_a):
48
55
"""A foreign test method for testing fetch locally and remotely."""
50
57
# TODO RBC 20060201 make this a repository test.
51
58
repo_b = br_b.repository
52
self.assertFalse(repo_b.has_revision(br_a.revision_history()[3]))
53
self.assertTrue(repo_b.has_revision(br_a.revision_history()[2]))
54
self.assertEquals(len(br_b.revision_history()), 7)
55
br_b.fetch(br_a, br_a.revision_history()[2])
59
self.assertFalse(repo_b.has_revision(revision_history(br_a)[3]))
60
self.assertTrue(repo_b.has_revision(revision_history(br_a)[2]))
61
self.assertEqual(len(revision_history(br_b)), 7)
62
br_b.fetch(br_a, revision_history(br_a)[2])
56
63
# branch.fetch is not supposed to alter the revision history
57
self.assertEquals(len(br_b.revision_history()), 7)
58
self.assertFalse(repo_b.has_revision(br_a.revision_history()[3]))
64
self.assertEqual(len(revision_history(br_b)), 7)
65
self.assertFalse(repo_b.has_revision(revision_history(br_a)[3]))
60
67
# fetching the next revision up in sample data copies one revision
61
br_b.fetch(br_a, br_a.revision_history()[3])
62
self.assertTrue(repo_b.has_revision(br_a.revision_history()[3]))
63
self.assertFalse(has_revision(br_a, br_b.revision_history()[6]))
64
self.assertTrue(br_a.repository.has_revision(br_b.revision_history()[5]))
68
br_b.fetch(br_a, revision_history(br_a)[3])
69
self.assertTrue(repo_b.has_revision(revision_history(br_a)[3]))
70
self.assertFalse(has_revision(br_a, revision_history(br_b)[6]))
71
self.assertTrue(br_a.repository.has_revision(revision_history(br_b)[5]))
66
73
# When a non-branch ancestor is missing, it should be unlisted...
67
74
# as its not reference from the inventory weave.
71
78
writable_a.fetch(br_b)
72
self.assertTrue(has_revision(br_a, br_b.revision_history()[3]))
73
self.assertTrue(has_revision(br_a, br_b.revision_history()[4]))
79
self.assertTrue(has_revision(br_a, revision_history(br_b)[3]))
80
self.assertTrue(has_revision(br_a, revision_history(br_b)[4]))
75
82
br_b2 = self.make_branch('br_b2')
77
self.assertTrue(has_revision(br_b2, br_b.revision_history()[4]))
78
self.assertTrue(has_revision(br_b2, br_a.revision_history()[2]))
79
self.assertFalse(has_revision(br_b2, br_a.revision_history()[3]))
84
self.assertTrue(has_revision(br_b2, revision_history(br_b)[4]))
85
self.assertTrue(has_revision(br_b2, revision_history(br_a)[2]))
86
self.assertFalse(has_revision(br_b2, revision_history(br_a)[3]))
81
88
br_a2 = self.make_branch('br_a2')
83
self.assertTrue(has_revision(br_a2, br_b.revision_history()[4]))
84
self.assertTrue(has_revision(br_a2, br_a.revision_history()[3]))
85
self.assertTrue(has_revision(br_a2, br_a.revision_history()[2]))
90
self.assertTrue(has_revision(br_a2, revision_history(br_b)[4]))
91
self.assertTrue(has_revision(br_a2, revision_history(br_a)[3]))
92
self.assertTrue(has_revision(br_a2, revision_history(br_a)[2]))
87
94
br_a3 = self.make_branch('br_a3')
88
95
# pulling a branch with no revisions grabs nothing, regardless of
91
98
for revno in range(4):
93
br_a3.repository.has_revision(br_a.revision_history()[revno]))
94
br_a3.fetch(br_a2, br_a.revision_history()[2])
100
br_a3.repository.has_revision(revision_history(br_a)[revno]))
101
br_a3.fetch(br_a2, revision_history(br_a)[2])
95
102
# pull the 3 revisions introduced by a@u-0-3
96
br_a3.fetch(br_a2, br_a.revision_history()[3])
103
br_a3.fetch(br_a2, revision_history(br_a)[3])
97
104
# NoSuchRevision should be raised if the branch is missing the revision
98
105
# that was requested.
99
106
self.assertRaises(errors.NoSuchRevision, br_a3.fetch, br_a2, 'pizza')
194
201
wt1 = self.make_branch_and_tree('br1')
196
203
wt1.commit(message='rev 1-1', rev_id='1-1')
197
dir_2 = br1.bzrdir.sprout('br2')
204
dir_2 = br1.controldir.sprout('br2')
198
205
br2 = dir_2.open_branch()
199
206
wt1.commit(message='rev 1-2', rev_id='1-2')
200
207
wt2 = dir_2.open_workingtree()
219
226
self.build_tree_contents([('br1/file', 'original contents\n')])
220
227
wt1.add('file', 'this-file-id')
221
228
wt1.commit(message='rev 1-1', rev_id='1-1')
222
dir_2 = br1.bzrdir.sprout('br2')
229
dir_2 = br1.controldir.sprout('br2')
223
230
br2 = dir_2.open_branch()
224
231
wt2 = dir_2.open_workingtree()
225
232
self.build_tree_contents([('br1/file', 'original from 1\n')])
363
370
# Ensure that we stored a delta
364
371
source.lock_read()
365
372
self.addCleanup(source.unlock)
366
record = source.revisions.get_record_stream([('rev-two',)],
367
'unordered', False).next()
373
record = next(source.revisions.get_record_stream([('rev-two',)],
368
375
self.assertEqual('knit-delta-gz', record.storage_kind)
369
376
target.fetch(tree.branch.repository, revision_id='rev-two')
370
377
# The record should get expanded back to a fulltext
371
378
target.lock_read()
372
379
self.addCleanup(target.unlock)
373
record = target.revisions.get_record_stream([('rev-two',)],
374
'unordered', False).next()
380
record = next(target.revisions.get_record_stream([('rev-two',)],
375
382
self.assertEqual('knit-ft-gz', record.storage_kind)
377
384
def test_fetch_with_fallback_and_merge(self):
396
403
# well and the deltas get bigger.
398
405
('add', ('', 'TREE_ROOT', 'directory', None))]
400
407
fname = 'file%03d' % (i,)
401
408
fileid = '%s-%s' % (fname, osutils.rand_chars(64))
402
409
to_add.append(('add', (fname, fileid, 'file', 'content\n')))
408
415
builder.build_snapshot('F', ['E', 'B'], [])
409
416
builder.finish_series()
410
417
source_branch = builder.get_branch()
411
source_branch.bzrdir.sprout('base', revision_id='B')
418
source_branch.controldir.sprout('base', revision_id='B')
412
419
target_branch = self.make_branch('target', format='1.6')
413
420
target_branch.set_stacked_on_url('../base')
414
421
source = source_branch.repository
482
489
self.make_tree_and_repo()
483
490
self.tree.commit('first commit', rev_id='left-parent')
484
491
self.tree.add_parent_tree_id('ghost-parent')
485
fork = self.tree.bzrdir.sprout('fork', 'null:').open_workingtree()
492
fork = self.tree.controldir.sprout('fork', 'null:').open_workingtree()
486
493
fork.commit('not a ghost', rev_id='not-ghost-parent')
487
494
self.tree.branch.repository.fetch(fork.branch.repository,
488
495
'not-ghost-parent')