1
# Copyright (C) 2004, 2005, 2006, 2007 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
from cStringIO import StringIO
27
revision as _mod_revision,
30
from bzrlib.bzrdir import BzrDir
31
from bzrlib.bundle.apply_bundle import install_bundle, merge_bundle
32
from bzrlib.bundle.bundle_data import BundleTree
33
from bzrlib.bundle.serializer import write_bundle, read_bundle, v09, v4
34
from bzrlib.bundle.serializer.v08 import BundleSerializerV08
35
from bzrlib.bundle.serializer.v09 import BundleSerializerV09
36
from bzrlib.bundle.serializer.v4 import BundleSerializerV4
37
from bzrlib.branch import Branch
38
from bzrlib.diff import internal_diff
39
from bzrlib.errors import (BzrError, TestamentMismatch, NotABundle, BadBundle,
41
from bzrlib.merge import Merge3Merger
42
from bzrlib.repofmt import knitrepo
43
from bzrlib.osutils import sha_file
44
from bzrlib.tests import (
48
TestCaseWithTransport,
52
from bzrlib.transform import TreeTransform
55
class MockTree(object):
57
from bzrlib.inventory import InventoryDirectory, ROOT_ID
59
self.paths = {ROOT_ID: ""}
60
self.ids = {"": ROOT_ID}
62
self.root = InventoryDirectory(ROOT_ID, '', None)
64
inventory = property(lambda x:x)
67
return self.paths.iterkeys()
69
def __getitem__(self, file_id):
70
if file_id == self.root.file_id:
73
return self.make_entry(file_id, self.paths[file_id])
75
def parent_id(self, file_id):
76
parent_dir = os.path.dirname(self.paths[file_id])
79
return self.ids[parent_dir]
81
def iter_entries(self):
82
for path, file_id in self.ids.iteritems():
83
yield path, self[file_id]
85
def get_file_kind(self, file_id):
86
if file_id in self.contents:
92
def make_entry(self, file_id, path):
93
from bzrlib.inventory import (InventoryEntry, InventoryFile
94
, InventoryDirectory, InventoryLink)
95
name = os.path.basename(path)
96
kind = self.get_file_kind(file_id)
97
parent_id = self.parent_id(file_id)
98
text_sha_1, text_size = self.contents_stats(file_id)
99
if kind == 'directory':
100
ie = InventoryDirectory(file_id, name, parent_id)
102
ie = InventoryFile(file_id, name, parent_id)
103
elif kind == 'symlink':
104
ie = InventoryLink(file_id, name, parent_id)
106
raise BzrError('unknown kind %r' % kind)
107
ie.text_sha1 = text_sha_1
108
ie.text_size = text_size
111
def add_dir(self, file_id, path):
112
self.paths[file_id] = path
113
self.ids[path] = file_id
115
def add_file(self, file_id, path, contents):
116
self.add_dir(file_id, path)
117
self.contents[file_id] = contents
119
def path2id(self, path):
120
return self.ids.get(path)
122
def id2path(self, file_id):
123
return self.paths.get(file_id)
125
def has_id(self, file_id):
126
return self.id2path(file_id) is not None
128
def get_file(self, file_id):
130
result.write(self.contents[file_id])
134
def contents_stats(self, file_id):
135
if file_id not in self.contents:
137
text_sha1 = sha_file(self.get_file(file_id))
138
return text_sha1, len(self.contents[file_id])
141
class BTreeTester(TestCase):
142
"""A simple unittest tester for the BundleTree class."""
144
def make_tree_1(self):
146
mtree.add_dir("a", "grandparent")
147
mtree.add_dir("b", "grandparent/parent")
148
mtree.add_file("c", "grandparent/parent/file", "Hello\n")
149
mtree.add_dir("d", "grandparent/alt_parent")
150
return BundleTree(mtree, ''), mtree
152
def test_renames(self):
153
"""Ensure that file renames have the proper effect on children"""
154
btree = self.make_tree_1()[0]
155
self.assertEqual(btree.old_path("grandparent"), "grandparent")
156
self.assertEqual(btree.old_path("grandparent/parent"),
157
"grandparent/parent")
158
self.assertEqual(btree.old_path("grandparent/parent/file"),
159
"grandparent/parent/file")
161
self.assertEqual(btree.id2path("a"), "grandparent")
162
self.assertEqual(btree.id2path("b"), "grandparent/parent")
163
self.assertEqual(btree.id2path("c"), "grandparent/parent/file")
165
self.assertEqual(btree.path2id("grandparent"), "a")
166
self.assertEqual(btree.path2id("grandparent/parent"), "b")
167
self.assertEqual(btree.path2id("grandparent/parent/file"), "c")
169
assert btree.path2id("grandparent2") is None
170
assert btree.path2id("grandparent2/parent") is None
171
assert btree.path2id("grandparent2/parent/file") is None
173
btree.note_rename("grandparent", "grandparent2")
174
assert btree.old_path("grandparent") is None
175
assert btree.old_path("grandparent/parent") is None
176
assert btree.old_path("grandparent/parent/file") is None
178
self.assertEqual(btree.id2path("a"), "grandparent2")
179
self.assertEqual(btree.id2path("b"), "grandparent2/parent")
180
self.assertEqual(btree.id2path("c"), "grandparent2/parent/file")
182
self.assertEqual(btree.path2id("grandparent2"), "a")
183
self.assertEqual(btree.path2id("grandparent2/parent"), "b")
184
self.assertEqual(btree.path2id("grandparent2/parent/file"), "c")
186
assert btree.path2id("grandparent") is None
187
assert btree.path2id("grandparent/parent") is None
188
assert btree.path2id("grandparent/parent/file") is None
190
btree.note_rename("grandparent/parent", "grandparent2/parent2")
191
self.assertEqual(btree.id2path("a"), "grandparent2")
192
self.assertEqual(btree.id2path("b"), "grandparent2/parent2")
193
self.assertEqual(btree.id2path("c"), "grandparent2/parent2/file")
195
self.assertEqual(btree.path2id("grandparent2"), "a")
196
self.assertEqual(btree.path2id("grandparent2/parent2"), "b")
197
self.assertEqual(btree.path2id("grandparent2/parent2/file"), "c")
199
assert btree.path2id("grandparent2/parent") is None
200
assert btree.path2id("grandparent2/parent/file") is None
202
btree.note_rename("grandparent/parent/file",
203
"grandparent2/parent2/file2")
204
self.assertEqual(btree.id2path("a"), "grandparent2")
205
self.assertEqual(btree.id2path("b"), "grandparent2/parent2")
206
self.assertEqual(btree.id2path("c"), "grandparent2/parent2/file2")
208
self.assertEqual(btree.path2id("grandparent2"), "a")
209
self.assertEqual(btree.path2id("grandparent2/parent2"), "b")
210
self.assertEqual(btree.path2id("grandparent2/parent2/file2"), "c")
212
assert btree.path2id("grandparent2/parent2/file") is None
214
def test_moves(self):
215
"""Ensure that file moves have the proper effect on children"""
216
btree = self.make_tree_1()[0]
217
btree.note_rename("grandparent/parent/file",
218
"grandparent/alt_parent/file")
219
self.assertEqual(btree.id2path("c"), "grandparent/alt_parent/file")
220
self.assertEqual(btree.path2id("grandparent/alt_parent/file"), "c")
221
assert btree.path2id("grandparent/parent/file") is None
223
def unified_diff(self, old, new):
225
internal_diff("old", old, "new", new, out)
229
def make_tree_2(self):
230
btree = self.make_tree_1()[0]
231
btree.note_rename("grandparent/parent/file",
232
"grandparent/alt_parent/file")
233
assert btree.id2path("e") is None
234
assert btree.path2id("grandparent/parent/file") is None
235
btree.note_id("e", "grandparent/parent/file")
239
"""File/inventory adds"""
240
btree = self.make_tree_2()
241
add_patch = self.unified_diff([], ["Extra cheese\n"])
242
btree.note_patch("grandparent/parent/file", add_patch)
243
btree.note_id('f', 'grandparent/parent/symlink', kind='symlink')
244
btree.note_target('grandparent/parent/symlink', 'venus')
245
self.adds_test(btree)
247
def adds_test(self, btree):
248
self.assertEqual(btree.id2path("e"), "grandparent/parent/file")
249
self.assertEqual(btree.path2id("grandparent/parent/file"), "e")
250
self.assertEqual(btree.get_file("e").read(), "Extra cheese\n")
251
self.assertEqual(btree.get_symlink_target('f'), 'venus')
253
def test_adds2(self):
254
"""File/inventory adds, with patch-compatibile renames"""
255
btree = self.make_tree_2()
256
btree.contents_by_id = False
257
add_patch = self.unified_diff(["Hello\n"], ["Extra cheese\n"])
258
btree.note_patch("grandparent/parent/file", add_patch)
259
btree.note_id('f', 'grandparent/parent/symlink', kind='symlink')
260
btree.note_target('grandparent/parent/symlink', 'venus')
261
self.adds_test(btree)
263
def make_tree_3(self):
264
btree, mtree = self.make_tree_1()
265
mtree.add_file("e", "grandparent/parent/topping", "Anchovies\n")
266
btree.note_rename("grandparent/parent/file",
267
"grandparent/alt_parent/file")
268
btree.note_rename("grandparent/parent/topping",
269
"grandparent/alt_parent/stopping")
272
def get_file_test(self, btree):
273
self.assertEqual(btree.get_file("e").read(), "Lemon\n")
274
self.assertEqual(btree.get_file("c").read(), "Hello\n")
276
def test_get_file(self):
277
"""Get file contents"""
278
btree = self.make_tree_3()
279
mod_patch = self.unified_diff(["Anchovies\n"], ["Lemon\n"])
280
btree.note_patch("grandparent/alt_parent/stopping", mod_patch)
281
self.get_file_test(btree)
283
def test_get_file2(self):
284
"""Get file contents, with patch-compatibile renames"""
285
btree = self.make_tree_3()
286
btree.contents_by_id = False
287
mod_patch = self.unified_diff([], ["Lemon\n"])
288
btree.note_patch("grandparent/alt_parent/stopping", mod_patch)
289
mod_patch = self.unified_diff([], ["Hello\n"])
290
btree.note_patch("grandparent/alt_parent/file", mod_patch)
291
self.get_file_test(btree)
293
def test_delete(self):
295
btree = self.make_tree_1()[0]
296
self.assertEqual(btree.get_file("c").read(), "Hello\n")
297
btree.note_deletion("grandparent/parent/file")
298
assert btree.id2path("c") is None
299
assert btree.path2id("grandparent/parent/file") is None
301
def sorted_ids(self, tree):
306
def test_iteration(self):
307
"""Ensure that iteration through ids works properly"""
308
btree = self.make_tree_1()[0]
309
self.assertEqual(self.sorted_ids(btree),
310
[inventory.ROOT_ID, 'a', 'b', 'c', 'd'])
311
btree.note_deletion("grandparent/parent/file")
312
btree.note_id("e", "grandparent/alt_parent/fool", kind="directory")
313
btree.note_last_changed("grandparent/alt_parent/fool",
315
self.assertEqual(self.sorted_ids(btree),
316
[inventory.ROOT_ID, 'a', 'b', 'd', 'e'])
319
class BundleTester1(TestCaseWithTransport):
321
def test_mismatched_bundle(self):
322
format = bzrdir.BzrDirMetaFormat1()
323
format.repository_format = knitrepo.RepositoryFormatKnit3()
324
serializer = BundleSerializerV08('0.8')
325
b = self.make_branch('.', format=format)
326
self.assertRaises(errors.IncompatibleBundleFormat, serializer.write,
327
b.repository, [], {}, StringIO())
329
def test_matched_bundle(self):
330
"""Don't raise IncompatibleBundleFormat for knit2 and bundle0.9"""
331
format = bzrdir.BzrDirMetaFormat1()
332
format.repository_format = knitrepo.RepositoryFormatKnit3()
333
serializer = BundleSerializerV09('0.9')
334
b = self.make_branch('.', format=format)
335
serializer.write(b.repository, [], {}, StringIO())
337
def test_mismatched_model(self):
338
"""Try copying a bundle from knit2 to knit1"""
339
format = bzrdir.BzrDirMetaFormat1()
340
format.repository_format = knitrepo.RepositoryFormatKnit3()
341
source = self.make_branch_and_tree('source', format=format)
342
source.commit('one', rev_id='one-id')
343
source.commit('two', rev_id='two-id')
345
write_bundle(source.branch.repository, 'two-id', 'null:', text,
349
format = bzrdir.BzrDirMetaFormat1()
350
format.repository_format = knitrepo.RepositoryFormatKnit1()
351
target = self.make_branch('target', format=format)
352
self.assertRaises(errors.IncompatibleRevision, install_bundle,
353
target.repository, read_bundle(text))
356
class BundleTester(object):
358
def bzrdir_format(self):
359
format = bzrdir.BzrDirMetaFormat1()
360
format.repository_format = knitrepo.RepositoryFormatKnit1()
363
def make_branch_and_tree(self, path, format=None):
365
format = self.bzrdir_format()
366
return TestCaseWithTransport.make_branch_and_tree(self, path, format)
368
def make_branch(self, path, format=None):
370
format = self.bzrdir_format()
371
return TestCaseWithTransport.make_branch(self, path, format)
373
def create_bundle_text(self, base_rev_id, rev_id):
374
bundle_txt = StringIO()
375
rev_ids = write_bundle(self.b1.repository, rev_id, base_rev_id,
376
bundle_txt, format=self.format)
378
self.assertEqual(bundle_txt.readline(),
379
'# Bazaar revision bundle v%s\n' % self.format)
380
self.assertEqual(bundle_txt.readline(), '#\n')
382
rev = self.b1.repository.get_revision(rev_id)
383
self.assertEqual(bundle_txt.readline().decode('utf-8'),
386
return bundle_txt, rev_ids
388
def get_valid_bundle(self, base_rev_id, rev_id, checkout_dir=None):
389
"""Create a bundle from base_rev_id -> rev_id in built-in branch.
390
Make sure that the text generated is valid, and that it
391
can be applied against the base, and generate the same information.
393
:return: The in-memory bundle
395
bundle_txt, rev_ids = self.create_bundle_text(base_rev_id, rev_id)
397
# This should also validate the generated bundle
398
bundle = read_bundle(bundle_txt)
399
repository = self.b1.repository
400
for bundle_rev in bundle.real_revisions:
401
# These really should have already been checked when we read the
402
# bundle, since it computes the sha1 hash for the revision, which
403
# only will match if everything is okay, but lets be explicit about
405
branch_rev = repository.get_revision(bundle_rev.revision_id)
406
for a in ('inventory_sha1', 'revision_id', 'parent_ids',
407
'timestamp', 'timezone', 'message', 'committer',
408
'parent_ids', 'properties'):
409
self.assertEqual(getattr(branch_rev, a),
410
getattr(bundle_rev, a))
411
self.assertEqual(len(branch_rev.parent_ids),
412
len(bundle_rev.parent_ids))
413
self.assertEqual(rev_ids,
414
[r.revision_id for r in bundle.real_revisions])
415
self.valid_apply_bundle(base_rev_id, bundle,
416
checkout_dir=checkout_dir)
420
def get_invalid_bundle(self, base_rev_id, rev_id):
421
"""Create a bundle from base_rev_id -> rev_id in built-in branch.
422
Munge the text so that it's invalid.
424
:return: The in-memory bundle
426
bundle_txt, rev_ids = self.create_bundle_text(base_rev_id, rev_id)
427
new_text = bundle_txt.getvalue().replace('executable:no',
429
bundle_txt = StringIO(new_text)
430
bundle = read_bundle(bundle_txt)
431
self.valid_apply_bundle(base_rev_id, bundle)
434
def test_non_bundle(self):
435
self.assertRaises(NotABundle, read_bundle, StringIO('#!/bin/sh\n'))
437
def test_malformed(self):
438
self.assertRaises(BadBundle, read_bundle,
439
StringIO('# Bazaar revision bundle v'))
441
def test_crlf_bundle(self):
443
read_bundle(StringIO('# Bazaar revision bundle v0.8\r\n'))
445
# It is currently permitted for bundles with crlf line endings to
446
# make read_bundle raise a BadBundle, but this should be fixed.
447
# Anything else, especially NotABundle, is an error.
450
def get_checkout(self, rev_id, checkout_dir=None):
451
"""Get a new tree, with the specified revision in it.
454
if checkout_dir is None:
455
checkout_dir = tempfile.mkdtemp(prefix='test-branch-', dir='.')
457
if not os.path.exists(checkout_dir):
458
os.mkdir(checkout_dir)
459
tree = self.make_branch_and_tree(checkout_dir)
461
ancestors = write_bundle(self.b1.repository, rev_id, 'null:', s,
464
assert isinstance(s.getvalue(), str), (
465
"Bundle isn't a bytestring:\n %s..." % repr(s.getvalue())[:40])
466
install_bundle(tree.branch.repository, read_bundle(s))
467
for ancestor in ancestors:
468
old = self.b1.repository.revision_tree(ancestor)
469
new = tree.branch.repository.revision_tree(ancestor)
471
# Check that there aren't any inventory level changes
472
delta = new.changes_from(old)
473
self.assertFalse(delta.has_changed(),
474
'Revision %s not copied correctly.'
477
# Now check that the file contents are all correct
478
for inventory_id in old:
480
old_file = old.get_file(inventory_id)
485
self.assertEqual(old_file.read(),
486
new.get_file(inventory_id).read())
487
if not _mod_revision.is_null(rev_id):
488
rh = self.b1.revision_history()
489
tree.branch.set_revision_history(rh[:rh.index(rev_id)+1])
491
delta = tree.changes_from(self.b1.repository.revision_tree(rev_id))
492
self.assertFalse(delta.has_changed(),
493
'Working tree has modifications: %s' % delta)
496
def valid_apply_bundle(self, base_rev_id, info, checkout_dir=None):
497
"""Get the base revision, apply the changes, and make
498
sure everything matches the builtin branch.
500
to_tree = self.get_checkout(base_rev_id, checkout_dir=checkout_dir)
501
original_parents = to_tree.get_parent_ids()
502
repository = to_tree.branch.repository
503
original_parents = to_tree.get_parent_ids()
504
self.assertIs(repository.has_revision(base_rev_id), True)
505
for rev in info.real_revisions:
506
self.assert_(not repository.has_revision(rev.revision_id),
507
'Revision {%s} present before applying bundle'
509
merge_bundle(info, to_tree, True, Merge3Merger, False, False)
511
for rev in info.real_revisions:
512
self.assert_(repository.has_revision(rev.revision_id),
513
'Missing revision {%s} after applying bundle'
516
self.assert_(to_tree.branch.repository.has_revision(info.target))
517
# Do we also want to verify that all the texts have been added?
519
self.assertEqual(original_parents + [info.target],
520
to_tree.get_parent_ids())
522
rev = info.real_revisions[-1]
523
base_tree = self.b1.repository.revision_tree(rev.revision_id)
524
to_tree = to_tree.branch.repository.revision_tree(rev.revision_id)
526
# TODO: make sure the target tree is identical to base tree
527
# we might also check the working tree.
529
base_files = list(base_tree.list_files())
530
to_files = list(to_tree.list_files())
531
self.assertEqual(len(base_files), len(to_files))
532
for base_file, to_file in zip(base_files, to_files):
533
self.assertEqual(base_file, to_file)
535
for path, status, kind, fileid, entry in base_files:
536
# Check that the meta information is the same
537
self.assertEqual(base_tree.get_file_size(fileid),
538
to_tree.get_file_size(fileid))
539
self.assertEqual(base_tree.get_file_sha1(fileid),
540
to_tree.get_file_sha1(fileid))
541
# Check that the contents are the same
542
# This is pretty expensive
543
# self.assertEqual(base_tree.get_file(fileid).read(),
544
# to_tree.get_file(fileid).read())
546
def test_bundle(self):
547
self.tree1 = self.make_branch_and_tree('b1')
548
self.b1 = self.tree1.branch
550
open('b1/one', 'wb').write('one\n')
551
self.tree1.add('one')
552
self.tree1.commit('add one', rev_id='a@cset-0-1')
554
bundle = self.get_valid_bundle('null:', 'a@cset-0-1')
556
# Make sure we can handle files with spaces, tabs, other
561
, 'b1/dir/filein subdir.c'
562
, 'b1/dir/WithCaps.txt'
563
, 'b1/dir/ pre space'
566
, 'b1/sub/sub/nonempty.txt'
568
open('b1/sub/sub/emptyfile.txt', 'wb').close()
569
open('b1/dir/nolastnewline.txt', 'wb').write('bloop')
570
tt = TreeTransform(self.tree1)
571
tt.new_file('executable', tt.root, '#!/bin/sh\n', 'exe-1', True)
573
# have to fix length of file-id so that we can predictably rewrite
574
# a (length-prefixed) record containing it later.
575
self.tree1.add('with space.txt', 'withspace-id')
578
, 'dir/filein subdir.c'
581
, 'dir/nolastnewline.txt'
584
, 'sub/sub/nonempty.txt'
585
, 'sub/sub/emptyfile.txt'
587
self.tree1.commit('add whitespace', rev_id='a@cset-0-2')
589
bundle = self.get_valid_bundle('a@cset-0-1', 'a@cset-0-2')
591
# Check a rollup bundle
592
bundle = self.get_valid_bundle('null:', 'a@cset-0-2')
596
['sub/sub/nonempty.txt'
597
, 'sub/sub/emptyfile.txt'
600
tt = TreeTransform(self.tree1)
601
trans_id = tt.trans_id_tree_file_id('exe-1')
602
tt.set_executability(False, trans_id)
604
self.tree1.commit('removed', rev_id='a@cset-0-3')
606
bundle = self.get_valid_bundle('a@cset-0-2', 'a@cset-0-3')
607
self.assertRaises((TestamentMismatch,
608
errors.VersionedFileInvalidChecksum), self.get_invalid_bundle,
609
'a@cset-0-2', 'a@cset-0-3')
610
# Check a rollup bundle
611
bundle = self.get_valid_bundle('null:', 'a@cset-0-3')
613
# Now move the directory
614
self.tree1.rename_one('dir', 'sub/dir')
615
self.tree1.commit('rename dir', rev_id='a@cset-0-4')
617
bundle = self.get_valid_bundle('a@cset-0-3', 'a@cset-0-4')
618
# Check a rollup bundle
619
bundle = self.get_valid_bundle('null:', 'a@cset-0-4')
622
open('b1/sub/dir/WithCaps.txt', 'ab').write('\nAdding some text\n')
623
open('b1/sub/dir/ pre space', 'ab').write(
624
'\r\nAdding some\r\nDOS format lines\r\n')
625
open('b1/sub/dir/nolastnewline.txt', 'ab').write('\n')
626
self.tree1.rename_one('sub/dir/ pre space',
628
self.tree1.commit('Modified files', rev_id='a@cset-0-5')
629
bundle = self.get_valid_bundle('a@cset-0-4', 'a@cset-0-5')
631
self.tree1.rename_one('sub/dir/WithCaps.txt', 'temp')
632
self.tree1.rename_one('with space.txt', 'WithCaps.txt')
633
self.tree1.rename_one('temp', 'with space.txt')
634
self.tree1.commit(u'swap filenames', rev_id='a@cset-0-6',
636
bundle = self.get_valid_bundle('a@cset-0-5', 'a@cset-0-6')
637
other = self.get_checkout('a@cset-0-5')
638
tree1_inv = self.tree1.branch.repository.get_inventory_xml(
640
tree2_inv = other.branch.repository.get_inventory_xml('a@cset-0-5')
641
self.assertEqualDiff(tree1_inv, tree2_inv)
642
other.rename_one('sub/dir/nolastnewline.txt', 'sub/nolastnewline.txt')
643
other.commit('rename file', rev_id='a@cset-0-6b')
644
self.tree1.merge_from_branch(other.branch)
645
self.tree1.commit(u'Merge', rev_id='a@cset-0-7',
647
bundle = self.get_valid_bundle('a@cset-0-6', 'a@cset-0-7')
649
def test_symlink_bundle(self):
650
self.requireFeature(SymlinkFeature)
651
self.tree1 = self.make_branch_and_tree('b1')
652
self.b1 = self.tree1.branch
653
tt = TreeTransform(self.tree1)
654
tt.new_symlink('link', tt.root, 'bar/foo', 'link-1')
656
self.tree1.commit('add symlink', rev_id='l@cset-0-1')
657
self.get_valid_bundle('null:', 'l@cset-0-1')
658
tt = TreeTransform(self.tree1)
659
trans_id = tt.trans_id_tree_file_id('link-1')
660
tt.adjust_path('link2', tt.root, trans_id)
661
tt.delete_contents(trans_id)
662
tt.create_symlink('mars', trans_id)
664
self.tree1.commit('rename and change symlink', rev_id='l@cset-0-2')
665
self.get_valid_bundle('l@cset-0-1', 'l@cset-0-2')
666
tt = TreeTransform(self.tree1)
667
trans_id = tt.trans_id_tree_file_id('link-1')
668
tt.delete_contents(trans_id)
669
tt.create_symlink('jupiter', trans_id)
671
self.tree1.commit('just change symlink target', rev_id='l@cset-0-3')
672
self.get_valid_bundle('l@cset-0-2', 'l@cset-0-3')
673
tt = TreeTransform(self.tree1)
674
trans_id = tt.trans_id_tree_file_id('link-1')
675
tt.delete_contents(trans_id)
677
self.tree1.commit('Delete symlink', rev_id='l@cset-0-4')
678
self.get_valid_bundle('l@cset-0-3', 'l@cset-0-4')
680
def test_binary_bundle(self):
681
self.tree1 = self.make_branch_and_tree('b1')
682
self.b1 = self.tree1.branch
683
tt = TreeTransform(self.tree1)
686
tt.new_file('file', tt.root, '\x00\n\x00\r\x01\n\x02\r\xff', 'binary-1')
687
tt.new_file('file2', tt.root, '\x01\n\x02\r\x03\n\x04\r\xff',
690
self.tree1.commit('add binary', rev_id='b@cset-0-1')
691
self.get_valid_bundle('null:', 'b@cset-0-1')
694
tt = TreeTransform(self.tree1)
695
trans_id = tt.trans_id_tree_file_id('binary-1')
696
tt.delete_contents(trans_id)
698
self.tree1.commit('delete binary', rev_id='b@cset-0-2')
699
self.get_valid_bundle('b@cset-0-1', 'b@cset-0-2')
702
tt = TreeTransform(self.tree1)
703
trans_id = tt.trans_id_tree_file_id('binary-2')
704
tt.adjust_path('file3', tt.root, trans_id)
705
tt.delete_contents(trans_id)
706
tt.create_file('file\rcontents\x00\n\x00', trans_id)
708
self.tree1.commit('rename and modify binary', rev_id='b@cset-0-3')
709
self.get_valid_bundle('b@cset-0-2', 'b@cset-0-3')
712
tt = TreeTransform(self.tree1)
713
trans_id = tt.trans_id_tree_file_id('binary-2')
714
tt.delete_contents(trans_id)
715
tt.create_file('\x00file\rcontents', trans_id)
717
self.tree1.commit('just modify binary', rev_id='b@cset-0-4')
718
self.get_valid_bundle('b@cset-0-3', 'b@cset-0-4')
721
self.get_valid_bundle('null:', 'b@cset-0-4')
723
def test_last_modified(self):
724
self.tree1 = self.make_branch_and_tree('b1')
725
self.b1 = self.tree1.branch
726
tt = TreeTransform(self.tree1)
727
tt.new_file('file', tt.root, 'file', 'file')
729
self.tree1.commit('create file', rev_id='a@lmod-0-1')
731
tt = TreeTransform(self.tree1)
732
trans_id = tt.trans_id_tree_file_id('file')
733
tt.delete_contents(trans_id)
734
tt.create_file('file2', trans_id)
736
self.tree1.commit('modify text', rev_id='a@lmod-0-2a')
738
other = self.get_checkout('a@lmod-0-1')
739
tt = TreeTransform(other)
740
trans_id = tt.trans_id_tree_file_id('file')
741
tt.delete_contents(trans_id)
742
tt.create_file('file2', trans_id)
744
other.commit('modify text in another tree', rev_id='a@lmod-0-2b')
745
self.tree1.merge_from_branch(other.branch)
746
self.tree1.commit(u'Merge', rev_id='a@lmod-0-3',
748
self.tree1.commit(u'Merge', rev_id='a@lmod-0-4')
749
bundle = self.get_valid_bundle('a@lmod-0-2a', 'a@lmod-0-4')
751
def test_hide_history(self):
752
self.tree1 = self.make_branch_and_tree('b1')
753
self.b1 = self.tree1.branch
755
open('b1/one', 'wb').write('one\n')
756
self.tree1.add('one')
757
self.tree1.commit('add file', rev_id='a@cset-0-1')
758
open('b1/one', 'wb').write('two\n')
759
self.tree1.commit('modify', rev_id='a@cset-0-2')
760
open('b1/one', 'wb').write('three\n')
761
self.tree1.commit('modify', rev_id='a@cset-0-3')
762
bundle_file = StringIO()
763
rev_ids = write_bundle(self.tree1.branch.repository, 'a@cset-0-3',
764
'a@cset-0-1', bundle_file, format=self.format)
765
self.assertNotContainsRe(bundle_file.getvalue(), '\btwo\b')
766
self.assertContainsRe(self.get_raw(bundle_file), 'one')
767
self.assertContainsRe(self.get_raw(bundle_file), 'three')
769
def test_bundle_same_basis(self):
770
"""Ensure using the basis as the target doesn't cause an error"""
771
self.tree1 = self.make_branch_and_tree('b1')
772
self.tree1.commit('add file', rev_id='a@cset-0-1')
773
bundle_file = StringIO()
774
rev_ids = write_bundle(self.tree1.branch.repository, 'a@cset-0-1',
775
'a@cset-0-1', bundle_file)
778
def get_raw(bundle_file):
779
return bundle_file.getvalue()
781
def test_unicode_bundle(self):
782
# Handle international characters
785
f = open(u'b1/with Dod\xe9', 'wb')
786
except UnicodeEncodeError:
787
raise TestSkipped("Filesystem doesn't support unicode")
789
self.tree1 = self.make_branch_and_tree('b1')
790
self.b1 = self.tree1.branch
793
u'With international man of mystery\n'
794
u'William Dod\xe9\n').encode('utf-8'))
797
self.tree1.add([u'with Dod\xe9'], ['withdod-id'])
798
self.tree1.commit(u'i18n commit from William Dod\xe9',
799
rev_id='i18n-1', committer=u'William Dod\xe9')
801
if sys.platform == 'darwin':
802
from bzrlib.workingtree import WorkingTree3
803
if type(self.tree1) is WorkingTree3:
804
self.knownFailure("Bug #141438: fails for WorkingTree3 on OSX")
806
# On Mac the '\xe9' gets changed to 'e\u0301'
807
self.assertEqual([u'.bzr', u'with Dode\u0301'],
808
sorted(os.listdir(u'b1')))
809
delta = self.tree1.changes_from(self.tree1.basis_tree())
810
self.assertEqual([(u'with Dod\xe9', 'withdod-id', 'file')],
812
self.knownFailure("Mac OSX doesn't preserve unicode"
813
" combining characters.")
816
bundle = self.get_valid_bundle('null:', 'i18n-1')
819
f = open(u'b1/with Dod\xe9', 'wb')
820
f.write(u'Modified \xb5\n'.encode('utf8'))
822
self.tree1.commit(u'modified', rev_id='i18n-2')
824
bundle = self.get_valid_bundle('i18n-1', 'i18n-2')
827
self.tree1.rename_one(u'with Dod\xe9', u'B\xe5gfors')
828
self.tree1.commit(u'renamed, the new i18n man', rev_id='i18n-3',
829
committer=u'Erik B\xe5gfors')
831
bundle = self.get_valid_bundle('i18n-2', 'i18n-3')
834
self.tree1.remove([u'B\xe5gfors'])
835
self.tree1.commit(u'removed', rev_id='i18n-4')
837
bundle = self.get_valid_bundle('i18n-3', 'i18n-4')
840
bundle = self.get_valid_bundle('null:', 'i18n-4')
843
def test_whitespace_bundle(self):
844
if sys.platform in ('win32', 'cygwin'):
845
raise TestSkipped('Windows doesn\'t support filenames'
846
' with tabs or trailing spaces')
847
self.tree1 = self.make_branch_and_tree('b1')
848
self.b1 = self.tree1.branch
850
self.build_tree(['b1/trailing space '])
851
self.tree1.add(['trailing space '])
852
# TODO: jam 20060701 Check for handling files with '\t' characters
853
# once we actually support them
856
self.tree1.commit('funky whitespace', rev_id='white-1')
858
bundle = self.get_valid_bundle('null:', 'white-1')
861
open('b1/trailing space ', 'ab').write('add some text\n')
862
self.tree1.commit('add text', rev_id='white-2')
864
bundle = self.get_valid_bundle('white-1', 'white-2')
867
self.tree1.rename_one('trailing space ', ' start and end space ')
868
self.tree1.commit('rename', rev_id='white-3')
870
bundle = self.get_valid_bundle('white-2', 'white-3')
873
self.tree1.remove([' start and end space '])
874
self.tree1.commit('removed', rev_id='white-4')
876
bundle = self.get_valid_bundle('white-3', 'white-4')
878
# Now test a complet roll-up
879
bundle = self.get_valid_bundle('null:', 'white-4')
881
def test_alt_timezone_bundle(self):
882
self.tree1 = self.make_branch_and_memory_tree('b1')
883
self.b1 = self.tree1.branch
884
builder = treebuilder.TreeBuilder()
886
self.tree1.lock_write()
887
builder.start_tree(self.tree1)
888
builder.build(['newfile'])
889
builder.finish_tree()
891
# Asia/Colombo offset = 5 hours 30 minutes
892
self.tree1.commit('non-hour offset timezone', rev_id='tz-1',
893
timezone=19800, timestamp=1152544886.0)
895
bundle = self.get_valid_bundle('null:', 'tz-1')
897
rev = bundle.revisions[0]
898
self.assertEqual('Mon 2006-07-10 20:51:26.000000000 +0530', rev.date)
899
self.assertEqual(19800, rev.timezone)
900
self.assertEqual(1152544886.0, rev.timestamp)
903
def test_bundle_root_id(self):
904
self.tree1 = self.make_branch_and_tree('b1')
905
self.b1 = self.tree1.branch
906
self.tree1.commit('message', rev_id='revid1')
907
bundle = self.get_valid_bundle('null:', 'revid1')
908
tree = self.get_bundle_tree(bundle, 'revid1')
909
self.assertEqual('revid1', tree.inventory.root.revision)
911
def test_install_revisions(self):
912
self.tree1 = self.make_branch_and_tree('b1')
913
self.b1 = self.tree1.branch
914
self.tree1.commit('message', rev_id='rev2a')
915
bundle = self.get_valid_bundle('null:', 'rev2a')
916
branch2 = self.make_branch('b2')
917
self.assertFalse(branch2.repository.has_revision('rev2a'))
918
target_revision = bundle.install_revisions(branch2.repository)
919
self.assertTrue(branch2.repository.has_revision('rev2a'))
920
self.assertEqual('rev2a', target_revision)
922
def test_bundle_empty_property(self):
923
"""Test serializing revision properties with an empty value."""
924
tree = self.make_branch_and_memory_tree('tree')
926
self.addCleanup(tree.unlock)
927
tree.add([''], ['TREE_ROOT'])
928
tree.commit('One', revprops={'one':'two', 'empty':''}, rev_id='rev1')
929
self.b1 = tree.branch
930
bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
931
bundle = read_bundle(bundle_sio)
932
revision_info = bundle.revisions[0]
933
self.assertEqual('rev1', revision_info.revision_id)
934
rev = revision_info.as_revision()
935
self.assertEqual({'branch-nick':'tree', 'empty':'', 'one':'two'},
938
def test_bundle_sorted_properties(self):
939
"""For stability the writer should write properties in sorted order."""
940
tree = self.make_branch_and_memory_tree('tree')
942
self.addCleanup(tree.unlock)
944
tree.add([''], ['TREE_ROOT'])
945
tree.commit('One', rev_id='rev1',
946
revprops={'a':'4', 'b':'3', 'c':'2', 'd':'1'})
947
self.b1 = tree.branch
948
bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
949
bundle = read_bundle(bundle_sio)
950
revision_info = bundle.revisions[0]
951
self.assertEqual('rev1', revision_info.revision_id)
952
rev = revision_info.as_revision()
953
self.assertEqual({'branch-nick':'tree', 'a':'4', 'b':'3', 'c':'2',
954
'd':'1'}, rev.properties)
956
def test_bundle_unicode_properties(self):
957
"""We should be able to round trip a non-ascii property."""
958
tree = self.make_branch_and_memory_tree('tree')
960
self.addCleanup(tree.unlock)
962
tree.add([''], ['TREE_ROOT'])
963
# Revisions themselves do not require anything about revision property
964
# keys, other than that they are a basestring, and do not contain
966
# However, Testaments assert than they are str(), and thus should not
968
tree.commit('One', rev_id='rev1',
969
revprops={'omega':u'\u03a9', 'alpha':u'\u03b1'})
970
self.b1 = tree.branch
971
bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
972
bundle = read_bundle(bundle_sio)
973
revision_info = bundle.revisions[0]
974
self.assertEqual('rev1', revision_info.revision_id)
975
rev = revision_info.as_revision()
976
self.assertEqual({'branch-nick':'tree', 'omega':u'\u03a9',
977
'alpha':u'\u03b1'}, rev.properties)
979
def test_bundle_with_ghosts(self):
980
tree = self.make_branch_and_tree('tree')
981
self.b1 = tree.branch
982
self.build_tree_contents([('tree/file', 'content1')])
985
self.build_tree_contents([('tree/file', 'content2')])
986
tree.add_parent_tree_id('ghost')
987
tree.commit('rev2', rev_id='rev2')
988
bundle = self.get_valid_bundle('null:', 'rev2')
990
def make_simple_tree(self, format=None):
991
tree = self.make_branch_and_tree('b1', format=format)
992
self.b1 = tree.branch
993
self.build_tree(['b1/file'])
997
def test_across_serializers(self):
998
tree = self.make_simple_tree('knit')
999
tree.commit('hello', rev_id='rev1')
1000
tree.commit('hello', rev_id='rev2')
1001
bundle = read_bundle(self.create_bundle_text('null:', 'rev2')[0])
1002
repo = self.make_repository('repo', format='dirstate-with-subtree')
1003
bundle.install_revisions(repo)
1004
inv_text = repo.get_inventory_xml('rev2')
1005
self.assertNotContainsRe(inv_text, 'format="5"')
1006
self.assertContainsRe(inv_text, 'format="7"')
1008
def test_across_models(self):
1009
tree = self.make_simple_tree('knit')
1010
tree.commit('hello', rev_id='rev1')
1011
tree.commit('hello', rev_id='rev2')
1012
bundle = read_bundle(self.create_bundle_text('null:', 'rev2')[0])
1013
repo = self.make_repository('repo', format='dirstate-with-subtree')
1014
bundle.install_revisions(repo)
1015
inv = repo.get_inventory('rev2')
1016
self.assertEqual('rev2', inv.root.revision)
1017
root_vf = repo.weave_store.get_weave(inv.root.file_id,
1018
repo.get_transaction())
1019
self.assertEqual(root_vf.versions(), ['rev1', 'rev2'])
1021
def test_across_models_incompatible(self):
1022
tree = self.make_simple_tree('dirstate-with-subtree')
1023
tree.commit('hello', rev_id='rev1')
1024
tree.commit('hello', rev_id='rev2')
1026
bundle = read_bundle(self.create_bundle_text('null:', 'rev1')[0])
1027
except errors.IncompatibleBundleFormat:
1028
raise TestSkipped("Format 0.8 doesn't work with knit3")
1029
repo = self.make_repository('repo', format='knit')
1030
bundle.install_revisions(repo)
1032
bundle = read_bundle(self.create_bundle_text('null:', 'rev2')[0])
1033
self.assertRaises(errors.IncompatibleRevision,
1034
bundle.install_revisions, repo)
1036
def test_get_merge_request(self):
1037
tree = self.make_simple_tree()
1038
tree.commit('hello', rev_id='rev1')
1039
tree.commit('hello', rev_id='rev2')
1040
bundle = read_bundle(self.create_bundle_text('null:', 'rev1')[0])
1041
result = bundle.get_merge_request(tree.branch.repository)
1042
self.assertEqual((None, 'rev1', 'inapplicable'), result)
1044
def test_with_subtree(self):
1045
tree = self.make_branch_and_tree('tree',
1046
format='dirstate-with-subtree')
1047
self.b1 = tree.branch
1048
subtree = self.make_branch_and_tree('tree/subtree',
1049
format='dirstate-with-subtree')
1051
tree.commit('hello', rev_id='rev1')
1053
bundle = read_bundle(self.create_bundle_text('null:', 'rev1')[0])
1054
except errors.IncompatibleBundleFormat:
1055
raise TestSkipped("Format 0.8 doesn't work with knit3")
1056
if isinstance(bundle, v09.BundleInfo09):
1057
raise TestSkipped("Format 0.9 doesn't work with subtrees")
1058
repo = self.make_repository('repo', format='knit')
1059
self.assertRaises(errors.IncompatibleRevision,
1060
bundle.install_revisions, repo)
1061
repo2 = self.make_repository('repo2', format='dirstate-with-subtree')
1062
bundle.install_revisions(repo2)
1064
def test_revision_id_with_slash(self):
1065
self.tree1 = self.make_branch_and_tree('tree')
1066
self.b1 = self.tree1.branch
1068
self.tree1.commit('Revision/id/with/slashes', rev_id='rev/id')
1070
raise TestSkipped("Repository doesn't support revision ids with"
1072
bundle = self.get_valid_bundle('null:', 'rev/id')
1074
def test_skip_file(self):
1075
"""Make sure we don't accidentally write to the wrong versionedfile"""
1076
self.tree1 = self.make_branch_and_tree('tree')
1077
self.b1 = self.tree1.branch
1078
# rev1 is not present in bundle, done by fetch
1079
self.build_tree_contents([('tree/file2', 'contents1')])
1080
self.tree1.add('file2', 'file2-id')
1081
self.tree1.commit('rev1', rev_id='reva')
1082
self.build_tree_contents([('tree/file3', 'contents2')])
1083
# rev2 is present in bundle, and done by fetch
1084
# having file1 in the bunle causes file1's versionedfile to be opened.
1085
self.tree1.add('file3', 'file3-id')
1086
self.tree1.commit('rev2')
1087
# Updating file2 should not cause an attempt to add to file1's vf
1088
target = self.tree1.bzrdir.sprout('target').open_workingtree()
1089
self.build_tree_contents([('tree/file2', 'contents3')])
1090
self.tree1.commit('rev3', rev_id='rev3')
1091
bundle = self.get_valid_bundle('reva', 'rev3')
1092
if getattr(bundle, 'get_bundle_reader', None) is None:
1093
raise TestSkipped('Bundle format cannot provide reader')
1094
# be sure that file1 comes before file2
1095
for b, m, k, r, f in bundle.get_bundle_reader().iter_records():
1098
self.assertNotEqual(f, 'file2-id')
1099
bundle.install_revisions(target.branch.repository)
1102
class V08BundleTester(BundleTester, TestCaseWithTransport):
1106
def test_bundle_empty_property(self):
1107
"""Test serializing revision properties with an empty value."""
1108
tree = self.make_branch_and_memory_tree('tree')
1110
self.addCleanup(tree.unlock)
1111
tree.add([''], ['TREE_ROOT'])
1112
tree.commit('One', revprops={'one':'two', 'empty':''}, rev_id='rev1')
1113
self.b1 = tree.branch
1114
bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
1115
self.assertContainsRe(bundle_sio.getvalue(),
1117
'# branch-nick: tree\n'
1121
bundle = read_bundle(bundle_sio)
1122
revision_info = bundle.revisions[0]
1123
self.assertEqual('rev1', revision_info.revision_id)
1124
rev = revision_info.as_revision()
1125
self.assertEqual({'branch-nick':'tree', 'empty':'', 'one':'two'},
1128
def get_bundle_tree(self, bundle, revision_id):
1129
repository = self.make_repository('repo')
1130
return bundle.revision_tree(repository, 'revid1')
1132
def test_bundle_empty_property_alt(self):
1133
"""Test serializing revision properties with an empty value.
1135
Older readers had a bug when reading an empty property.
1136
They assumed that all keys ended in ': \n'. However they would write an
1137
empty value as ':\n'. This tests make sure that all newer bzr versions
1138
can handle th second form.
1140
tree = self.make_branch_and_memory_tree('tree')
1142
self.addCleanup(tree.unlock)
1143
tree.add([''], ['TREE_ROOT'])
1144
tree.commit('One', revprops={'one':'two', 'empty':''}, rev_id='rev1')
1145
self.b1 = tree.branch
1146
bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
1147
txt = bundle_sio.getvalue()
1148
loc = txt.find('# empty: ') + len('# empty:')
1149
# Create a new bundle, which strips the trailing space after empty
1150
bundle_sio = StringIO(txt[:loc] + txt[loc+1:])
1152
self.assertContainsRe(bundle_sio.getvalue(),
1154
'# branch-nick: tree\n'
1158
bundle = read_bundle(bundle_sio)
1159
revision_info = bundle.revisions[0]
1160
self.assertEqual('rev1', revision_info.revision_id)
1161
rev = revision_info.as_revision()
1162
self.assertEqual({'branch-nick':'tree', 'empty':'', 'one':'two'},
1165
def test_bundle_sorted_properties(self):
1166
"""For stability the writer should write properties in sorted order."""
1167
tree = self.make_branch_and_memory_tree('tree')
1169
self.addCleanup(tree.unlock)
1171
tree.add([''], ['TREE_ROOT'])
1172
tree.commit('One', rev_id='rev1',
1173
revprops={'a':'4', 'b':'3', 'c':'2', 'd':'1'})
1174
self.b1 = tree.branch
1175
bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
1176
self.assertContainsRe(bundle_sio.getvalue(),
1180
'# branch-nick: tree\n'
1184
bundle = read_bundle(bundle_sio)
1185
revision_info = bundle.revisions[0]
1186
self.assertEqual('rev1', revision_info.revision_id)
1187
rev = revision_info.as_revision()
1188
self.assertEqual({'branch-nick':'tree', 'a':'4', 'b':'3', 'c':'2',
1189
'd':'1'}, rev.properties)
1191
def test_bundle_unicode_properties(self):
1192
"""We should be able to round trip a non-ascii property."""
1193
tree = self.make_branch_and_memory_tree('tree')
1195
self.addCleanup(tree.unlock)
1197
tree.add([''], ['TREE_ROOT'])
1198
# Revisions themselves do not require anything about revision property
1199
# keys, other than that they are a basestring, and do not contain
1201
# However, Testaments assert than they are str(), and thus should not
1203
tree.commit('One', rev_id='rev1',
1204
revprops={'omega':u'\u03a9', 'alpha':u'\u03b1'})
1205
self.b1 = tree.branch
1206
bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
1207
self.assertContainsRe(bundle_sio.getvalue(),
1209
'# alpha: \xce\xb1\n'
1210
'# branch-nick: tree\n'
1211
'# omega: \xce\xa9\n'
1213
bundle = read_bundle(bundle_sio)
1214
revision_info = bundle.revisions[0]
1215
self.assertEqual('rev1', revision_info.revision_id)
1216
rev = revision_info.as_revision()
1217
self.assertEqual({'branch-nick':'tree', 'omega':u'\u03a9',
1218
'alpha':u'\u03b1'}, rev.properties)
1221
class V09BundleKnit2Tester(V08BundleTester):
1225
def bzrdir_format(self):
1226
format = bzrdir.BzrDirMetaFormat1()
1227
format.repository_format = knitrepo.RepositoryFormatKnit3()
1231
class V09BundleKnit1Tester(V08BundleTester):
1235
def bzrdir_format(self):
1236
format = bzrdir.BzrDirMetaFormat1()
1237
format.repository_format = knitrepo.RepositoryFormatKnit1()
1241
class V4BundleTester(BundleTester, TestCaseWithTransport):
1245
def get_valid_bundle(self, base_rev_id, rev_id, checkout_dir=None):
1246
"""Create a bundle from base_rev_id -> rev_id in built-in branch.
1247
Make sure that the text generated is valid, and that it
1248
can be applied against the base, and generate the same information.
1250
:return: The in-memory bundle
1252
bundle_txt, rev_ids = self.create_bundle_text(base_rev_id, rev_id)
1254
# This should also validate the generated bundle
1255
bundle = read_bundle(bundle_txt)
1256
repository = self.b1.repository
1257
for bundle_rev in bundle.real_revisions:
1258
# These really should have already been checked when we read the
1259
# bundle, since it computes the sha1 hash for the revision, which
1260
# only will match if everything is okay, but lets be explicit about
1262
branch_rev = repository.get_revision(bundle_rev.revision_id)
1263
for a in ('inventory_sha1', 'revision_id', 'parent_ids',
1264
'timestamp', 'timezone', 'message', 'committer',
1265
'parent_ids', 'properties'):
1266
self.assertEqual(getattr(branch_rev, a),
1267
getattr(bundle_rev, a))
1268
self.assertEqual(len(branch_rev.parent_ids),
1269
len(bundle_rev.parent_ids))
1270
self.assertEqual(set(rev_ids),
1271
set([r.revision_id for r in bundle.real_revisions]))
1272
self.valid_apply_bundle(base_rev_id, bundle,
1273
checkout_dir=checkout_dir)
1277
def get_invalid_bundle(self, base_rev_id, rev_id):
1278
"""Create a bundle from base_rev_id -> rev_id in built-in branch.
1279
Munge the text so that it's invalid.
1281
:return: The in-memory bundle
1283
from bzrlib.bundle import serializer
1284
bundle_txt, rev_ids = self.create_bundle_text(base_rev_id, rev_id)
1285
new_text = self.get_raw(StringIO(''.join(bundle_txt)))
1286
new_text = new_text.replace('<file file_id="exe-1"',
1287
'<file executable="y" file_id="exe-1"')
1288
new_text = new_text.replace('B222', 'B237')
1289
bundle_txt = StringIO()
1290
bundle_txt.write(serializer._get_bundle_header('4'))
1291
bundle_txt.write('\n')
1292
bundle_txt.write(new_text.encode('bz2'))
1294
bundle = read_bundle(bundle_txt)
1295
self.valid_apply_bundle(base_rev_id, bundle)
1298
def create_bundle_text(self, base_rev_id, rev_id):
1299
bundle_txt = StringIO()
1300
rev_ids = write_bundle(self.b1.repository, rev_id, base_rev_id,
1301
bundle_txt, format=self.format)
1303
self.assertEqual(bundle_txt.readline(),
1304
'# Bazaar revision bundle v%s\n' % self.format)
1305
self.assertEqual(bundle_txt.readline(), '#\n')
1306
rev = self.b1.repository.get_revision(rev_id)
1308
return bundle_txt, rev_ids
1310
def get_bundle_tree(self, bundle, revision_id):
1311
repository = self.make_repository('repo')
1312
bundle.install_revisions(repository)
1313
return repository.revision_tree(revision_id)
1315
def test_creation(self):
1316
tree = self.make_branch_and_tree('tree')
1317
self.build_tree_contents([('tree/file', 'contents1\nstatic\n')])
1318
tree.add('file', 'fileid-2')
1319
tree.commit('added file', rev_id='rev1')
1320
self.build_tree_contents([('tree/file', 'contents2\nstatic\n')])
1321
tree.commit('changed file', rev_id='rev2')
1323
serializer = BundleSerializerV4('1.0')
1324
serializer.write(tree.branch.repository, ['rev1', 'rev2'], {}, s)
1326
tree2 = self.make_branch_and_tree('target')
1327
target_repo = tree2.branch.repository
1328
install_bundle(target_repo, serializer.read(s))
1329
vf = target_repo.weave_store.get_weave('fileid-2',
1330
target_repo.get_transaction())
1331
self.assertEqual('contents1\nstatic\n', vf.get_text('rev1'))
1332
self.assertEqual('contents2\nstatic\n', vf.get_text('rev2'))
1333
rtree = target_repo.revision_tree('rev2')
1334
inventory_vf = target_repo.get_inventory_weave()
1335
self.assertEqual(['rev1'], inventory_vf.get_parents('rev2'))
1336
self.assertEqual('changed file',
1337
target_repo.get_revision('rev2').message)
1340
def get_raw(bundle_file):
1342
line = bundle_file.readline()
1343
line = bundle_file.readline()
1344
lines = bundle_file.readlines()
1345
return ''.join(lines).decode('bz2')
1347
def test_copy_signatures(self):
1348
tree_a = self.make_branch_and_tree('tree_a')
1350
import bzrlib.commit as commit
1351
oldstrategy = bzrlib.gpg.GPGStrategy
1352
branch = tree_a.branch
1353
repo_a = branch.repository
1354
tree_a.commit("base", allow_pointless=True, rev_id='A')
1355
self.failIf(branch.repository.has_signature_for_revision_id('A'))
1357
from bzrlib.testament import Testament
1358
# monkey patch gpg signing mechanism
1359
bzrlib.gpg.GPGStrategy = bzrlib.gpg.LoopbackGPGStrategy
1360
new_config = test_commit.MustSignConfig(branch)
1361
commit.Commit(config=new_config).commit(message="base",
1362
allow_pointless=True,
1364
working_tree=tree_a)
1366
return bzrlib.gpg.LoopbackGPGStrategy(None).sign(text)
1367
self.assertTrue(repo_a.has_signature_for_revision_id('B'))
1369
bzrlib.gpg.GPGStrategy = oldstrategy
1370
tree_b = self.make_branch_and_tree('tree_b')
1371
repo_b = tree_b.branch.repository
1373
serializer = BundleSerializerV4('4')
1374
serializer.write(tree_a.branch.repository, ['A', 'B'], {}, s)
1376
install_bundle(repo_b, serializer.read(s))
1377
self.assertTrue(repo_b.has_signature_for_revision_id('B'))
1378
self.assertEqual(repo_b.get_signature_text('B'),
1379
repo_a.get_signature_text('B'))
1381
# ensure repeat installs are harmless
1382
install_bundle(repo_b, serializer.read(s))
1385
class V4WeaveBundleTester(V4BundleTester):
1387
def bzrdir_format(self):
1391
class MungedBundleTester(object):
1393
def build_test_bundle(self):
1394
wt = self.make_branch_and_tree('b1')
1396
self.build_tree(['b1/one'])
1398
wt.commit('add one', rev_id='a@cset-0-1')
1399
self.build_tree(['b1/two'])
1401
wt.commit('add two', rev_id='a@cset-0-2',
1402
revprops={'branch-nick':'test'})
1404
bundle_txt = StringIO()
1405
rev_ids = write_bundle(wt.branch.repository, 'a@cset-0-2',
1406
'a@cset-0-1', bundle_txt, self.format)
1407
self.assertEqual(set(['a@cset-0-2']), set(rev_ids))
1408
bundle_txt.seek(0, 0)
1411
def check_valid(self, bundle):
1412
"""Check that after whatever munging, the final object is valid."""
1413
self.assertEqual(['a@cset-0-2'],
1414
[r.revision_id for r in bundle.real_revisions])
1416
def test_extra_whitespace(self):
1417
bundle_txt = self.build_test_bundle()
1419
# Seek to the end of the file
1420
# Adding one extra newline used to give us
1421
# TypeError: float() argument must be a string or a number
1422
bundle_txt.seek(0, 2)
1423
bundle_txt.write('\n')
1426
bundle = read_bundle(bundle_txt)
1427
self.check_valid(bundle)
1429
def test_extra_whitespace_2(self):
1430
bundle_txt = self.build_test_bundle()
1432
# Seek to the end of the file
1433
# Adding two extra newlines used to give us
1434
# MalformedPatches: The first line of all patches should be ...
1435
bundle_txt.seek(0, 2)
1436
bundle_txt.write('\n\n')
1439
bundle = read_bundle(bundle_txt)
1440
self.check_valid(bundle)
1443
class MungedBundleTesterV09(TestCaseWithTransport, MungedBundleTester):
1447
def test_missing_trailing_whitespace(self):
1448
bundle_txt = self.build_test_bundle()
1450
# Remove a trailing newline, it shouldn't kill the parser
1451
raw = bundle_txt.getvalue()
1452
# The contents of the bundle don't have to be this, but this
1453
# test is concerned with the exact case where the serializer
1454
# creates a blank line at the end, and fails if that
1456
self.assertEqual('\n\n', raw[-2:])
1457
bundle_txt = StringIO(raw[:-1])
1459
bundle = read_bundle(bundle_txt)
1460
self.check_valid(bundle)
1462
def test_opening_text(self):
1463
bundle_txt = self.build_test_bundle()
1465
bundle_txt = StringIO("Some random\nemail comments\n"
1466
+ bundle_txt.getvalue())
1468
bundle = read_bundle(bundle_txt)
1469
self.check_valid(bundle)
1471
def test_trailing_text(self):
1472
bundle_txt = self.build_test_bundle()
1474
bundle_txt = StringIO(bundle_txt.getvalue() +
1475
"Some trailing\nrandom\ntext\n")
1477
bundle = read_bundle(bundle_txt)
1478
self.check_valid(bundle)
1481
class MungedBundleTesterV4(TestCaseWithTransport, MungedBundleTester):
1486
class TestBundleWriterReader(TestCase):
1488
def test_roundtrip_record(self):
1489
fileobj = StringIO()
1490
writer = v4.BundleWriter(fileobj)
1492
writer.add_info_record(foo='bar')
1493
writer._add_record("Record body", {'parents': ['1', '3'],
1494
'storage_kind':'fulltext'}, 'file', 'revid', 'fileid')
1497
reader = v4.BundleReader(fileobj, stream_input=True)
1498
record_iter = reader.iter_records()
1499
record = record_iter.next()
1500
self.assertEqual((None, {'foo': 'bar', 'storage_kind': 'header'},
1501
'info', None, None), record)
1502
record = record_iter.next()
1503
self.assertEqual(("Record body", {'storage_kind': 'fulltext',
1504
'parents': ['1', '3']}, 'file', 'revid', 'fileid'),
1507
def test_roundtrip_record_memory_hungry(self):
1508
fileobj = StringIO()
1509
writer = v4.BundleWriter(fileobj)
1511
writer.add_info_record(foo='bar')
1512
writer._add_record("Record body", {'parents': ['1', '3'],
1513
'storage_kind':'fulltext'}, 'file', 'revid', 'fileid')
1516
reader = v4.BundleReader(fileobj, stream_input=False)
1517
record_iter = reader.iter_records()
1518
record = record_iter.next()
1519
self.assertEqual((None, {'foo': 'bar', 'storage_kind': 'header'},
1520
'info', None, None), record)
1521
record = record_iter.next()
1522
self.assertEqual(("Record body", {'storage_kind': 'fulltext',
1523
'parents': ['1', '3']}, 'file', 'revid', 'fileid'),
1526
def test_encode_name(self):
1527
self.assertEqual('revision/rev1',
1528
v4.BundleWriter.encode_name('revision', 'rev1'))
1529
self.assertEqual('file/rev//1/file-id-1',
1530
v4.BundleWriter.encode_name('file', 'rev/1', 'file-id-1'))
1531
self.assertEqual('info',
1532
v4.BundleWriter.encode_name('info', None, None))
1534
def test_decode_name(self):
1535
self.assertEqual(('revision', 'rev1', None),
1536
v4.BundleReader.decode_name('revision/rev1'))
1537
self.assertEqual(('file', 'rev/1', 'file-id-1'),
1538
v4.BundleReader.decode_name('file/rev//1/file-id-1'))
1539
self.assertEqual(('info', None, None),
1540
v4.BundleReader.decode_name('info'))
1542
def test_too_many_names(self):
1543
fileobj = StringIO()
1544
writer = v4.BundleWriter(fileobj)
1546
writer.add_info_record(foo='bar')
1547
writer._container.add_bytes_record('blah', ['two', 'names'])
1550
record_iter = v4.BundleReader(fileobj).iter_records()
1551
record = record_iter.next()
1552
self.assertEqual((None, {'foo': 'bar', 'storage_kind': 'header'},
1553
'info', None, None), record)
1554
self.assertRaises(BadBundle, record_iter.next)