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')
134
141
# root revision to change for each commit, even though the content,
135
142
# parent, name, and other attributes are unchanged.
136
143
tree = self.make_branch_and_tree('tree', knit1_format)
137
tree.set_root_id('tree-root')
138
tree.commit('rev1', rev_id='rev1')
139
tree.commit('rev2', rev_id='rev2')
144
tree.set_root_id(b'tree-root')
145
tree.commit('rev1', rev_id=b'rev1')
146
tree.commit('rev2', rev_id=b'rev2')
141
148
# Now we convert it to a knit2 repository so that it has a root knit
142
149
Convert(tree.basedir, knit2_format)
181
188
"""Merge brings across history from unrelated source"""
182
189
wt1 = self.make_branch_and_tree('br1')
184
wt1.commit(message='rev 1-1', rev_id='1-1')
185
wt1.commit(message='rev 1-2', rev_id='1-2')
191
wt1.commit(message='rev 1-1', rev_id=b'1-1')
192
wt1.commit(message='rev 1-2', rev_id=b'1-2')
186
193
wt2 = self.make_branch_and_tree('br2')
188
wt2.commit(message='rev 2-1', rev_id='2-1')
195
wt2.commit(message='rev 2-1', rev_id=b'2-1')
189
196
wt2.merge_from_branch(br1, from_revision='null:')
190
197
self._check_revs_present(br2)
193
200
"""Merge brings across history from source"""
194
201
wt1 = self.make_branch_and_tree('br1')
196
wt1.commit(message='rev 1-1', rev_id='1-1')
197
dir_2 = br1.bzrdir.sprout('br2')
203
wt1.commit(message='rev 1-1', rev_id=b'1-1')
204
dir_2 = br1.controldir.sprout('br2')
198
205
br2 = dir_2.open_branch()
199
wt1.commit(message='rev 1-2', rev_id='1-2')
206
wt1.commit(message='rev 1-2', rev_id=b'1-2')
200
207
wt2 = dir_2.open_workingtree()
201
wt2.commit(message='rev 2-1', rev_id='2-1')
208
wt2.commit(message='rev 2-1', rev_id=b'2-1')
202
209
wt2.merge_from_branch(br1)
203
210
self._check_revs_present(br2)
216
223
super(TestMergeFileHistory, self).setUp()
217
224
wt1 = self.make_branch_and_tree('br1')
219
self.build_tree_contents([('br1/file', 'original contents\n')])
220
wt1.add('file', 'this-file-id')
221
wt1.commit(message='rev 1-1', rev_id='1-1')
222
dir_2 = br1.bzrdir.sprout('br2')
226
self.build_tree_contents([('br1/file', b'original contents\n')])
227
wt1.add('file', b'this-file-id')
228
wt1.commit(message='rev 1-1', rev_id=b'1-1')
229
dir_2 = br1.controldir.sprout('br2')
223
230
br2 = dir_2.open_branch()
224
231
wt2 = dir_2.open_workingtree()
225
self.build_tree_contents([('br1/file', 'original from 1\n')])
226
wt1.commit(message='rev 1-2', rev_id='1-2')
227
self.build_tree_contents([('br1/file', 'agreement\n')])
228
wt1.commit(message='rev 1-3', rev_id='1-3')
229
self.build_tree_contents([('br2/file', 'contents in 2\n')])
230
wt2.commit(message='rev 2-1', rev_id='2-1')
231
self.build_tree_contents([('br2/file', 'agreement\n')])
232
wt2.commit(message='rev 2-2', rev_id='2-2')
232
self.build_tree_contents([('br1/file', b'original from 1\n')])
233
wt1.commit(message='rev 1-2', rev_id=b'1-2')
234
self.build_tree_contents([('br1/file', b'agreement\n')])
235
wt1.commit(message='rev 1-3', rev_id=b'1-3')
236
self.build_tree_contents([('br2/file', b'contents in 2\n')])
237
wt2.commit(message='rev 2-1', rev_id=b'2-1')
238
self.build_tree_contents([('br2/file', b'agreement\n')])
239
wt2.commit(message='rev 2-2', rev_id=b'2-2')
234
241
def test_merge_fetches_file_history(self):
235
242
"""Merge brings across file histories"""
268
275
tree = self.make_branch_and_tree('source', format='dirstate')
269
276
target = self.make_repository('target', format='pack-0.92')
270
277
self.build_tree(['source/file'])
271
tree.set_root_id('root-id')
272
tree.add('file', 'file-id')
273
tree.commit('one', rev_id='rev-one')
278
tree.set_root_id(b'root-id')
279
tree.add('file', b'file-id')
280
tree.commit('one', rev_id=b'rev-one')
274
281
source = tree.branch.repository
275
282
source.texts = versionedfile.RecordingVersionedFilesDecorator(
308
315
tree = self.make_branch_and_tree('source', format='dirstate')
309
316
target = self.make_repository('target', format='pack-0.92')
310
317
self.build_tree(['source/file'])
311
tree.set_root_id('root-id')
312
tree.add('file', 'file-id')
313
tree.commit('one', rev_id='rev-one')
318
tree.set_root_id(b'root-id')
319
tree.add('file', b'file-id')
320
tree.commit('one', rev_id=b'rev-one')
314
321
source = tree.branch.repository
315
322
source.texts = versionedfile.RecordingVersionedFilesDecorator(
353
360
tree = self.make_branch_and_tree('source', format='dirstate')
354
361
target = self.make_repository('target', format='pack-0.92')
355
362
self.build_tree(['source/file'])
356
tree.set_root_id('root-id')
357
tree.add('file', 'file-id')
358
tree.commit('one', rev_id='rev-one')
363
tree.set_root_id(b'root-id')
364
tree.add('file', b'file-id')
365
tree.commit('one', rev_id=b'rev-one')
359
366
# Hack the KVF for revisions so that it "accidentally" allows a delta
360
367
tree.branch.repository.revisions._max_delta_chain = 200
361
tree.commit('two', rev_id='rev-two')
368
tree.commit('two', rev_id=b'rev-two')
362
369
source = tree.branch.repository
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')))
403
builder.build_snapshot('A', None, to_add)
404
builder.build_snapshot('B', ['A'], [])
405
builder.build_snapshot('C', ['A'], [])
406
builder.build_snapshot('D', ['C'], [])
407
builder.build_snapshot('E', ['D'], [])
408
builder.build_snapshot('F', ['E', 'B'], [])
410
builder.build_snapshot(None, to_add, revision_id='A')
411
builder.build_snapshot(['A'], [], revision_id='B')
412
builder.build_snapshot(['A'], [], revision_id='C')
413
builder.build_snapshot(['C'], [], revision_id='D')
414
builder.build_snapshot(['D'], [], revision_id='E')
415
builder.build_snapshot(['E', 'B'], [], revision_id='F')
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
481
488
def test_fetch_ghosts(self):
482
489
self.make_tree_and_repo()
483
self.tree.commit('first commit', rev_id='left-parent')
490
self.tree.commit('first commit', rev_id=b'left-parent')
484
491
self.tree.add_parent_tree_id('ghost-parent')
485
fork = self.tree.bzrdir.sprout('fork', 'null:').open_workingtree()
486
fork.commit('not a ghost', rev_id='not-ghost-parent')
492
fork = self.tree.controldir.sprout('fork', 'null:').open_workingtree()
493
fork.commit('not a ghost', rev_id=b'not-ghost-parent')
487
494
self.tree.branch.repository.fetch(fork.branch.repository,
488
495
'not-ghost-parent')
489
496
self.tree.add_parent_tree_id('not-ghost-parent')
490
self.tree.commit('second commit', rev_id='second-id')
497
self.tree.commit('second commit', rev_id=b'second-id')
491
498
self.repo.fetch(self.tree.branch.repository, 'second-id')
492
499
root_id = self.tree.get_root_id()
493
500
self.assertEqual(
497
504
def make_two_commits(self, change_root, fetch_twice):
498
505
self.make_tree_and_repo()
499
self.tree.commit('first commit', rev_id='first-id')
506
self.tree.commit('first commit', rev_id=b'first-id')
501
self.tree.set_root_id('unique-id')
502
self.tree.commit('second commit', rev_id='second-id')
508
self.tree.set_root_id(b'unique-id')
509
self.tree.commit('second commit', rev_id=b'second-id')
504
511
self.repo.fetch(self.tree.branch.repository, 'first-id')
505
512
self.repo.fetch(self.tree.branch.repository, 'second-id')