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
29
from bzrlib.builtins import _merge_helper
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 has_symlinks, sha_file
44
from bzrlib.tests import (TestCaseInTempDir, TestCaseWithTransport,
45
TestCase, TestSkipped, test_commit)
46
from bzrlib.transform import TreeTransform
47
from bzrlib.workingtree import WorkingTree
50
class MockTree(object):
52
from bzrlib.inventory import InventoryDirectory, ROOT_ID
54
self.paths = {ROOT_ID: ""}
55
self.ids = {"": ROOT_ID}
57
self.root = InventoryDirectory(ROOT_ID, '', None)
59
inventory = property(lambda x:x)
62
return self.paths.iterkeys()
64
def __getitem__(self, file_id):
65
if file_id == self.root.file_id:
68
return self.make_entry(file_id, self.paths[file_id])
70
def parent_id(self, file_id):
71
parent_dir = os.path.dirname(self.paths[file_id])
74
return self.ids[parent_dir]
76
def iter_entries(self):
77
for path, file_id in self.ids.iteritems():
78
yield path, self[file_id]
80
def get_file_kind(self, file_id):
81
if file_id in self.contents:
87
def make_entry(self, file_id, path):
88
from bzrlib.inventory import (InventoryEntry, InventoryFile
89
, InventoryDirectory, InventoryLink)
90
name = os.path.basename(path)
91
kind = self.get_file_kind(file_id)
92
parent_id = self.parent_id(file_id)
93
text_sha_1, text_size = self.contents_stats(file_id)
94
if kind == 'directory':
95
ie = InventoryDirectory(file_id, name, parent_id)
97
ie = InventoryFile(file_id, name, parent_id)
98
elif kind == 'symlink':
99
ie = InventoryLink(file_id, name, parent_id)
101
raise BzrError('unknown kind %r' % kind)
102
ie.text_sha1 = text_sha_1
103
ie.text_size = text_size
106
def add_dir(self, file_id, path):
107
self.paths[file_id] = path
108
self.ids[path] = file_id
110
def add_file(self, file_id, path, contents):
111
self.add_dir(file_id, path)
112
self.contents[file_id] = contents
114
def path2id(self, path):
115
return self.ids.get(path)
117
def id2path(self, file_id):
118
return self.paths.get(file_id)
120
def has_id(self, file_id):
121
return self.id2path(file_id) is not None
123
def get_file(self, file_id):
125
result.write(self.contents[file_id])
129
def contents_stats(self, file_id):
130
if file_id not in self.contents:
132
text_sha1 = sha_file(self.get_file(file_id))
133
return text_sha1, len(self.contents[file_id])
136
class BTreeTester(TestCase):
137
"""A simple unittest tester for the BundleTree class."""
139
def make_tree_1(self):
141
mtree.add_dir("a", "grandparent")
142
mtree.add_dir("b", "grandparent/parent")
143
mtree.add_file("c", "grandparent/parent/file", "Hello\n")
144
mtree.add_dir("d", "grandparent/alt_parent")
145
return BundleTree(mtree, ''), mtree
147
def test_renames(self):
148
"""Ensure that file renames have the proper effect on children"""
149
btree = self.make_tree_1()[0]
150
self.assertEqual(btree.old_path("grandparent"), "grandparent")
151
self.assertEqual(btree.old_path("grandparent/parent"),
152
"grandparent/parent")
153
self.assertEqual(btree.old_path("grandparent/parent/file"),
154
"grandparent/parent/file")
156
self.assertEqual(btree.id2path("a"), "grandparent")
157
self.assertEqual(btree.id2path("b"), "grandparent/parent")
158
self.assertEqual(btree.id2path("c"), "grandparent/parent/file")
160
self.assertEqual(btree.path2id("grandparent"), "a")
161
self.assertEqual(btree.path2id("grandparent/parent"), "b")
162
self.assertEqual(btree.path2id("grandparent/parent/file"), "c")
164
assert btree.path2id("grandparent2") is None
165
assert btree.path2id("grandparent2/parent") is None
166
assert btree.path2id("grandparent2/parent/file") is None
168
btree.note_rename("grandparent", "grandparent2")
169
assert btree.old_path("grandparent") is None
170
assert btree.old_path("grandparent/parent") is None
171
assert btree.old_path("grandparent/parent/file") is None
173
self.assertEqual(btree.id2path("a"), "grandparent2")
174
self.assertEqual(btree.id2path("b"), "grandparent2/parent")
175
self.assertEqual(btree.id2path("c"), "grandparent2/parent/file")
177
self.assertEqual(btree.path2id("grandparent2"), "a")
178
self.assertEqual(btree.path2id("grandparent2/parent"), "b")
179
self.assertEqual(btree.path2id("grandparent2/parent/file"), "c")
181
assert btree.path2id("grandparent") is None
182
assert btree.path2id("grandparent/parent") is None
183
assert btree.path2id("grandparent/parent/file") is None
185
btree.note_rename("grandparent/parent", "grandparent2/parent2")
186
self.assertEqual(btree.id2path("a"), "grandparent2")
187
self.assertEqual(btree.id2path("b"), "grandparent2/parent2")
188
self.assertEqual(btree.id2path("c"), "grandparent2/parent2/file")
190
self.assertEqual(btree.path2id("grandparent2"), "a")
191
self.assertEqual(btree.path2id("grandparent2/parent2"), "b")
192
self.assertEqual(btree.path2id("grandparent2/parent2/file"), "c")
194
assert btree.path2id("grandparent2/parent") is None
195
assert btree.path2id("grandparent2/parent/file") is None
197
btree.note_rename("grandparent/parent/file",
198
"grandparent2/parent2/file2")
199
self.assertEqual(btree.id2path("a"), "grandparent2")
200
self.assertEqual(btree.id2path("b"), "grandparent2/parent2")
201
self.assertEqual(btree.id2path("c"), "grandparent2/parent2/file2")
203
self.assertEqual(btree.path2id("grandparent2"), "a")
204
self.assertEqual(btree.path2id("grandparent2/parent2"), "b")
205
self.assertEqual(btree.path2id("grandparent2/parent2/file2"), "c")
207
assert btree.path2id("grandparent2/parent2/file") is None
209
def test_moves(self):
210
"""Ensure that file moves have the proper effect on children"""
211
btree = self.make_tree_1()[0]
212
btree.note_rename("grandparent/parent/file",
213
"grandparent/alt_parent/file")
214
self.assertEqual(btree.id2path("c"), "grandparent/alt_parent/file")
215
self.assertEqual(btree.path2id("grandparent/alt_parent/file"), "c")
216
assert btree.path2id("grandparent/parent/file") is None
218
def unified_diff(self, old, new):
220
internal_diff("old", old, "new", new, out)
224
def make_tree_2(self):
225
btree = self.make_tree_1()[0]
226
btree.note_rename("grandparent/parent/file",
227
"grandparent/alt_parent/file")
228
assert btree.id2path("e") is None
229
assert btree.path2id("grandparent/parent/file") is None
230
btree.note_id("e", "grandparent/parent/file")
234
"""File/inventory adds"""
235
btree = self.make_tree_2()
236
add_patch = self.unified_diff([], ["Extra cheese\n"])
237
btree.note_patch("grandparent/parent/file", add_patch)
238
btree.note_id('f', 'grandparent/parent/symlink', kind='symlink')
239
btree.note_target('grandparent/parent/symlink', 'venus')
240
self.adds_test(btree)
242
def adds_test(self, btree):
243
self.assertEqual(btree.id2path("e"), "grandparent/parent/file")
244
self.assertEqual(btree.path2id("grandparent/parent/file"), "e")
245
self.assertEqual(btree.get_file("e").read(), "Extra cheese\n")
246
self.assertEqual(btree.get_symlink_target('f'), 'venus')
248
def test_adds2(self):
249
"""File/inventory adds, with patch-compatibile renames"""
250
btree = self.make_tree_2()
251
btree.contents_by_id = False
252
add_patch = self.unified_diff(["Hello\n"], ["Extra cheese\n"])
253
btree.note_patch("grandparent/parent/file", add_patch)
254
btree.note_id('f', 'grandparent/parent/symlink', kind='symlink')
255
btree.note_target('grandparent/parent/symlink', 'venus')
256
self.adds_test(btree)
258
def make_tree_3(self):
259
btree, mtree = self.make_tree_1()
260
mtree.add_file("e", "grandparent/parent/topping", "Anchovies\n")
261
btree.note_rename("grandparent/parent/file",
262
"grandparent/alt_parent/file")
263
btree.note_rename("grandparent/parent/topping",
264
"grandparent/alt_parent/stopping")
267
def get_file_test(self, btree):
268
self.assertEqual(btree.get_file("e").read(), "Lemon\n")
269
self.assertEqual(btree.get_file("c").read(), "Hello\n")
271
def test_get_file(self):
272
"""Get file contents"""
273
btree = self.make_tree_3()
274
mod_patch = self.unified_diff(["Anchovies\n"], ["Lemon\n"])
275
btree.note_patch("grandparent/alt_parent/stopping", mod_patch)
276
self.get_file_test(btree)
278
def test_get_file2(self):
279
"""Get file contents, with patch-compatibile renames"""
280
btree = self.make_tree_3()
281
btree.contents_by_id = False
282
mod_patch = self.unified_diff([], ["Lemon\n"])
283
btree.note_patch("grandparent/alt_parent/stopping", mod_patch)
284
mod_patch = self.unified_diff([], ["Hello\n"])
285
btree.note_patch("grandparent/alt_parent/file", mod_patch)
286
self.get_file_test(btree)
288
def test_delete(self):
290
btree = self.make_tree_1()[0]
291
self.assertEqual(btree.get_file("c").read(), "Hello\n")
292
btree.note_deletion("grandparent/parent/file")
293
assert btree.id2path("c") is None
294
assert btree.path2id("grandparent/parent/file") is None
296
def sorted_ids(self, tree):
301
def test_iteration(self):
302
"""Ensure that iteration through ids works properly"""
303
btree = self.make_tree_1()[0]
304
self.assertEqual(self.sorted_ids(btree),
305
[inventory.ROOT_ID, 'a', 'b', 'c', 'd'])
306
btree.note_deletion("grandparent/parent/file")
307
btree.note_id("e", "grandparent/alt_parent/fool", kind="directory")
308
btree.note_last_changed("grandparent/alt_parent/fool",
310
self.assertEqual(self.sorted_ids(btree),
311
[inventory.ROOT_ID, 'a', 'b', 'd', 'e'])
314
class BundleTester1(TestCaseWithTransport):
316
def test_mismatched_bundle(self):
317
format = bzrdir.BzrDirMetaFormat1()
318
format.repository_format = knitrepo.RepositoryFormatKnit3()
319
serializer = BundleSerializerV08('0.8')
320
b = self.make_branch('.', format=format)
321
self.assertRaises(errors.IncompatibleBundleFormat, serializer.write,
322
b.repository, [], {}, StringIO())
324
def test_matched_bundle(self):
325
"""Don't raise IncompatibleBundleFormat for knit2 and bundle0.9"""
326
format = bzrdir.BzrDirMetaFormat1()
327
format.repository_format = knitrepo.RepositoryFormatKnit3()
328
serializer = BundleSerializerV09('0.9')
329
b = self.make_branch('.', format=format)
330
serializer.write(b.repository, [], {}, StringIO())
332
def test_mismatched_model(self):
333
"""Try copying a bundle from knit2 to knit1"""
334
format = bzrdir.BzrDirMetaFormat1()
335
format.repository_format = knitrepo.RepositoryFormatKnit3()
336
source = self.make_branch_and_tree('source', format=format)
337
source.commit('one', rev_id='one-id')
338
source.commit('two', rev_id='two-id')
340
write_bundle(source.branch.repository, 'two-id', None, text,
344
format = bzrdir.BzrDirMetaFormat1()
345
format.repository_format = knitrepo.RepositoryFormatKnit1()
346
target = self.make_branch('target', format=format)
347
self.assertRaises(errors.IncompatibleRevision, install_bundle,
348
target.repository, read_bundle(text))
351
class BundleTester(object):
353
def bzrdir_format(self):
354
format = bzrdir.BzrDirMetaFormat1()
355
format.repository_format = knitrepo.RepositoryFormatKnit1()
358
def make_branch_and_tree(self, path, format=None):
360
format = self.bzrdir_format()
361
return TestCaseWithTransport.make_branch_and_tree(self, path, format)
363
def make_branch(self, path, format=None):
365
format = self.bzrdir_format()
366
return TestCaseWithTransport.make_branch(self, path, format)
368
def create_bundle_text(self, base_rev_id, rev_id):
369
bundle_txt = StringIO()
370
rev_ids = write_bundle(self.b1.repository, rev_id, base_rev_id,
371
bundle_txt, format=self.format)
373
self.assertEqual(bundle_txt.readline(),
374
'# Bazaar revision bundle v%s\n' % self.format)
375
self.assertEqual(bundle_txt.readline(), '#\n')
377
rev = self.b1.repository.get_revision(rev_id)
378
self.assertEqual(bundle_txt.readline().decode('utf-8'),
381
return bundle_txt, rev_ids
383
def get_valid_bundle(self, base_rev_id, rev_id, checkout_dir=None):
384
"""Create a bundle from base_rev_id -> rev_id in built-in branch.
385
Make sure that the text generated is valid, and that it
386
can be applied against the base, and generate the same information.
388
:return: The in-memory bundle
390
bundle_txt, rev_ids = self.create_bundle_text(base_rev_id, rev_id)
392
# This should also validate the generated bundle
393
bundle = read_bundle(bundle_txt)
394
repository = self.b1.repository
395
for bundle_rev in bundle.real_revisions:
396
# These really should have already been checked when we read the
397
# bundle, since it computes the sha1 hash for the revision, which
398
# only will match if everything is okay, but lets be explicit about
400
branch_rev = repository.get_revision(bundle_rev.revision_id)
401
for a in ('inventory_sha1', 'revision_id', 'parent_ids',
402
'timestamp', 'timezone', 'message', 'committer',
403
'parent_ids', 'properties'):
404
self.assertEqual(getattr(branch_rev, a),
405
getattr(bundle_rev, a))
406
self.assertEqual(len(branch_rev.parent_ids),
407
len(bundle_rev.parent_ids))
408
self.assertEqual(rev_ids,
409
[r.revision_id for r in bundle.real_revisions])
410
self.valid_apply_bundle(base_rev_id, bundle,
411
checkout_dir=checkout_dir)
415
def get_invalid_bundle(self, base_rev_id, rev_id):
416
"""Create a bundle from base_rev_id -> rev_id in built-in branch.
417
Munge the text so that it's invalid.
419
:return: The in-memory bundle
421
bundle_txt, rev_ids = self.create_bundle_text(base_rev_id, rev_id)
422
new_text = bundle_txt.getvalue().replace('executable:no',
424
bundle_txt = StringIO(new_text)
425
bundle = read_bundle(bundle_txt)
426
self.valid_apply_bundle(base_rev_id, bundle)
429
def test_non_bundle(self):
430
self.assertRaises(NotABundle, read_bundle, StringIO('#!/bin/sh\n'))
432
def test_malformed(self):
433
self.assertRaises(BadBundle, read_bundle,
434
StringIO('# Bazaar revision bundle v'))
436
def test_crlf_bundle(self):
438
read_bundle(StringIO('# Bazaar revision bundle v0.8\r\n'))
440
# It is currently permitted for bundles with crlf line endings to
441
# make read_bundle raise a BadBundle, but this should be fixed.
442
# Anything else, especially NotABundle, is an error.
445
def get_checkout(self, rev_id, checkout_dir=None):
446
"""Get a new tree, with the specified revision in it.
449
if checkout_dir is None:
450
checkout_dir = tempfile.mkdtemp(prefix='test-branch-', dir='.')
452
if not os.path.exists(checkout_dir):
453
os.mkdir(checkout_dir)
454
tree = self.make_branch_and_tree(checkout_dir)
456
ancestors = write_bundle(self.b1.repository, rev_id, None, s,
459
assert isinstance(s.getvalue(), str), (
460
"Bundle isn't a bytestring:\n %s..." % repr(s.getvalue())[:40])
461
install_bundle(tree.branch.repository, read_bundle(s))
462
for ancestor in ancestors:
463
old = self.b1.repository.revision_tree(ancestor)
464
new = tree.branch.repository.revision_tree(ancestor)
466
# Check that there aren't any inventory level changes
467
delta = new.changes_from(old)
468
self.assertFalse(delta.has_changed(),
469
'Revision %s not copied correctly.'
472
# Now check that the file contents are all correct
473
for inventory_id in old:
475
old_file = old.get_file(inventory_id)
480
self.assertEqual(old_file.read(),
481
new.get_file(inventory_id).read())
482
if rev_id is not None:
483
rh = self.b1.revision_history()
484
tree.branch.set_revision_history(rh[:rh.index(rev_id)+1])
486
delta = tree.changes_from(self.b1.repository.revision_tree(rev_id))
487
self.assertFalse(delta.has_changed(),
488
'Working tree has modifications: %s' % delta)
491
def valid_apply_bundle(self, base_rev_id, info, checkout_dir=None):
492
"""Get the base revision, apply the changes, and make
493
sure everything matches the builtin branch.
495
to_tree = self.get_checkout(base_rev_id, checkout_dir=checkout_dir)
496
original_parents = to_tree.get_parent_ids()
497
repository = to_tree.branch.repository
498
original_parents = to_tree.get_parent_ids()
499
self.assertIs(repository.has_revision(base_rev_id), True)
500
for rev in info.real_revisions:
501
self.assert_(not repository.has_revision(rev.revision_id),
502
'Revision {%s} present before applying bundle'
504
merge_bundle(info, to_tree, True, Merge3Merger, False, False)
506
for rev in info.real_revisions:
507
self.assert_(repository.has_revision(rev.revision_id),
508
'Missing revision {%s} after applying bundle'
511
self.assert_(to_tree.branch.repository.has_revision(info.target))
512
# Do we also want to verify that all the texts have been added?
514
self.assertEqual(original_parents + [info.target],
515
to_tree.get_parent_ids())
517
rev = info.real_revisions[-1]
518
base_tree = self.b1.repository.revision_tree(rev.revision_id)
519
to_tree = to_tree.branch.repository.revision_tree(rev.revision_id)
521
# TODO: make sure the target tree is identical to base tree
522
# we might also check the working tree.
524
base_files = list(base_tree.list_files())
525
to_files = list(to_tree.list_files())
526
self.assertEqual(len(base_files), len(to_files))
527
for base_file, to_file in zip(base_files, to_files):
528
self.assertEqual(base_file, to_file)
530
for path, status, kind, fileid, entry in base_files:
531
# Check that the meta information is the same
532
self.assertEqual(base_tree.get_file_size(fileid),
533
to_tree.get_file_size(fileid))
534
self.assertEqual(base_tree.get_file_sha1(fileid),
535
to_tree.get_file_sha1(fileid))
536
# Check that the contents are the same
537
# This is pretty expensive
538
# self.assertEqual(base_tree.get_file(fileid).read(),
539
# to_tree.get_file(fileid).read())
541
def test_bundle(self):
542
self.tree1 = self.make_branch_and_tree('b1')
543
self.b1 = self.tree1.branch
545
open('b1/one', 'wb').write('one\n')
546
self.tree1.add('one')
547
self.tree1.commit('add one', rev_id='a@cset-0-1')
549
bundle = self.get_valid_bundle(None, 'a@cset-0-1')
551
# Make sure we can handle files with spaces, tabs, other
556
, 'b1/dir/filein subdir.c'
557
, 'b1/dir/WithCaps.txt'
558
, 'b1/dir/ pre space'
561
, 'b1/sub/sub/nonempty.txt'
563
open('b1/sub/sub/emptyfile.txt', 'wb').close()
564
open('b1/dir/nolastnewline.txt', 'wb').write('bloop')
565
tt = TreeTransform(self.tree1)
566
tt.new_file('executable', tt.root, '#!/bin/sh\n', 'exe-1', True)
568
# have to fix length of file-id so that we can predictably rewrite
569
# a (length-prefixed) record containing it later.
570
self.tree1.add('with space.txt', 'withspace-id')
573
, 'dir/filein subdir.c'
576
, 'dir/nolastnewline.txt'
579
, 'sub/sub/nonempty.txt'
580
, 'sub/sub/emptyfile.txt'
582
self.tree1.commit('add whitespace', rev_id='a@cset-0-2')
584
bundle = self.get_valid_bundle('a@cset-0-1', 'a@cset-0-2')
586
# Check a rollup bundle
587
bundle = self.get_valid_bundle(None, 'a@cset-0-2')
591
['sub/sub/nonempty.txt'
592
, 'sub/sub/emptyfile.txt'
595
tt = TreeTransform(self.tree1)
596
trans_id = tt.trans_id_tree_file_id('exe-1')
597
tt.set_executability(False, trans_id)
599
self.tree1.commit('removed', rev_id='a@cset-0-3')
601
bundle = self.get_valid_bundle('a@cset-0-2', 'a@cset-0-3')
602
self.assertRaises((TestamentMismatch,
603
errors.VersionedFileInvalidChecksum), self.get_invalid_bundle,
604
'a@cset-0-2', 'a@cset-0-3')
605
# Check a rollup bundle
606
bundle = self.get_valid_bundle(None, 'a@cset-0-3')
608
# Now move the directory
609
self.tree1.rename_one('dir', 'sub/dir')
610
self.tree1.commit('rename dir', rev_id='a@cset-0-4')
612
bundle = self.get_valid_bundle('a@cset-0-3', 'a@cset-0-4')
613
# Check a rollup bundle
614
bundle = self.get_valid_bundle(None, 'a@cset-0-4')
617
open('b1/sub/dir/WithCaps.txt', 'ab').write('\nAdding some text\n')
618
open('b1/sub/dir/ pre space', 'ab').write(
619
'\r\nAdding some\r\nDOS format lines\r\n')
620
open('b1/sub/dir/nolastnewline.txt', 'ab').write('\n')
621
self.tree1.rename_one('sub/dir/ pre space',
623
self.tree1.commit('Modified files', rev_id='a@cset-0-5')
624
bundle = self.get_valid_bundle('a@cset-0-4', 'a@cset-0-5')
626
self.tree1.rename_one('sub/dir/WithCaps.txt', 'temp')
627
self.tree1.rename_one('with space.txt', 'WithCaps.txt')
628
self.tree1.rename_one('temp', 'with space.txt')
629
self.tree1.commit(u'swap filenames', rev_id='a@cset-0-6',
631
bundle = self.get_valid_bundle('a@cset-0-5', 'a@cset-0-6')
632
other = self.get_checkout('a@cset-0-5')
633
tree1_inv = self.tree1.branch.repository.get_inventory_xml(
635
tree2_inv = other.branch.repository.get_inventory_xml('a@cset-0-5')
636
self.assertEqualDiff(tree1_inv, tree2_inv)
637
other.rename_one('sub/dir/nolastnewline.txt', 'sub/nolastnewline.txt')
638
other.commit('rename file', rev_id='a@cset-0-6b')
639
_merge_helper([other.basedir, -1], [None, None],
640
this_dir=self.tree1.basedir)
641
self.tree1.commit(u'Merge', rev_id='a@cset-0-7',
643
bundle = self.get_valid_bundle('a@cset-0-6', 'a@cset-0-7')
645
def test_symlink_bundle(self):
646
if not has_symlinks():
647
raise TestSkipped("No symlink support")
648
self.tree1 = self.make_branch_and_tree('b1')
649
self.b1 = self.tree1.branch
650
tt = TreeTransform(self.tree1)
651
tt.new_symlink('link', tt.root, 'bar/foo', 'link-1')
653
self.tree1.commit('add symlink', rev_id='l@cset-0-1')
654
self.get_valid_bundle(None, 'l@cset-0-1')
655
tt = TreeTransform(self.tree1)
656
trans_id = tt.trans_id_tree_file_id('link-1')
657
tt.adjust_path('link2', tt.root, trans_id)
658
tt.delete_contents(trans_id)
659
tt.create_symlink('mars', trans_id)
661
self.tree1.commit('rename and change symlink', rev_id='l@cset-0-2')
662
self.get_valid_bundle('l@cset-0-1', 'l@cset-0-2')
663
tt = TreeTransform(self.tree1)
664
trans_id = tt.trans_id_tree_file_id('link-1')
665
tt.delete_contents(trans_id)
666
tt.create_symlink('jupiter', trans_id)
668
self.tree1.commit('just change symlink target', rev_id='l@cset-0-3')
669
self.get_valid_bundle('l@cset-0-2', 'l@cset-0-3')
670
tt = TreeTransform(self.tree1)
671
trans_id = tt.trans_id_tree_file_id('link-1')
672
tt.delete_contents(trans_id)
674
self.tree1.commit('Delete symlink', rev_id='l@cset-0-4')
675
self.get_valid_bundle('l@cset-0-3', 'l@cset-0-4')
677
def test_binary_bundle(self):
678
self.tree1 = self.make_branch_and_tree('b1')
679
self.b1 = self.tree1.branch
680
tt = TreeTransform(self.tree1)
683
tt.new_file('file', tt.root, '\x00\n\x00\r\x01\n\x02\r\xff', 'binary-1')
684
tt.new_file('file2', tt.root, '\x01\n\x02\r\x03\n\x04\r\xff',
687
self.tree1.commit('add binary', rev_id='b@cset-0-1')
688
self.get_valid_bundle(None, 'b@cset-0-1')
691
tt = TreeTransform(self.tree1)
692
trans_id = tt.trans_id_tree_file_id('binary-1')
693
tt.delete_contents(trans_id)
695
self.tree1.commit('delete binary', rev_id='b@cset-0-2')
696
self.get_valid_bundle('b@cset-0-1', 'b@cset-0-2')
699
tt = TreeTransform(self.tree1)
700
trans_id = tt.trans_id_tree_file_id('binary-2')
701
tt.adjust_path('file3', tt.root, trans_id)
702
tt.delete_contents(trans_id)
703
tt.create_file('file\rcontents\x00\n\x00', trans_id)
705
self.tree1.commit('rename and modify binary', rev_id='b@cset-0-3')
706
self.get_valid_bundle('b@cset-0-2', 'b@cset-0-3')
709
tt = TreeTransform(self.tree1)
710
trans_id = tt.trans_id_tree_file_id('binary-2')
711
tt.delete_contents(trans_id)
712
tt.create_file('\x00file\rcontents', trans_id)
714
self.tree1.commit('just modify binary', rev_id='b@cset-0-4')
715
self.get_valid_bundle('b@cset-0-3', 'b@cset-0-4')
718
self.get_valid_bundle(None, 'b@cset-0-4')
720
def test_last_modified(self):
721
self.tree1 = self.make_branch_and_tree('b1')
722
self.b1 = self.tree1.branch
723
tt = TreeTransform(self.tree1)
724
tt.new_file('file', tt.root, 'file', 'file')
726
self.tree1.commit('create file', rev_id='a@lmod-0-1')
728
tt = TreeTransform(self.tree1)
729
trans_id = tt.trans_id_tree_file_id('file')
730
tt.delete_contents(trans_id)
731
tt.create_file('file2', trans_id)
733
self.tree1.commit('modify text', rev_id='a@lmod-0-2a')
735
other = self.get_checkout('a@lmod-0-1')
736
tt = TreeTransform(other)
737
trans_id = tt.trans_id_tree_file_id('file')
738
tt.delete_contents(trans_id)
739
tt.create_file('file2', trans_id)
741
other.commit('modify text in another tree', rev_id='a@lmod-0-2b')
742
_merge_helper([other.basedir, -1], [None, None],
743
this_dir=self.tree1.basedir)
744
self.tree1.commit(u'Merge', rev_id='a@lmod-0-3',
746
self.tree1.commit(u'Merge', rev_id='a@lmod-0-4')
747
bundle = self.get_valid_bundle('a@lmod-0-2a', 'a@lmod-0-4')
749
def test_hide_history(self):
750
self.tree1 = self.make_branch_and_tree('b1')
751
self.b1 = self.tree1.branch
753
open('b1/one', 'wb').write('one\n')
754
self.tree1.add('one')
755
self.tree1.commit('add file', rev_id='a@cset-0-1')
756
open('b1/one', 'wb').write('two\n')
757
self.tree1.commit('modify', rev_id='a@cset-0-2')
758
open('b1/one', 'wb').write('three\n')
759
self.tree1.commit('modify', rev_id='a@cset-0-3')
760
bundle_file = StringIO()
761
rev_ids = write_bundle(self.tree1.branch.repository, 'a@cset-0-3',
762
'a@cset-0-1', bundle_file, format=self.format)
763
self.assertNotContainsRe(bundle_file.getvalue(), '\btwo\b')
764
self.assertContainsRe(self.get_raw(bundle_file), 'one')
765
self.assertContainsRe(self.get_raw(bundle_file), 'three')
767
def test_bundle_same_basis(self):
768
"""Ensure using the basis as the target doesn't cause an error"""
769
self.tree1 = self.make_branch_and_tree('b1')
770
self.tree1.commit('add file', rev_id='a@cset-0-1')
771
bundle_file = StringIO()
772
rev_ids = write_bundle(self.tree1.branch.repository, 'a@cset-0-1',
773
'a@cset-0-1', bundle_file)
776
def get_raw(bundle_file):
777
return bundle_file.getvalue()
779
def test_unicode_bundle(self):
780
# Handle international characters
783
f = open(u'b1/with Dod\xe9', 'wb')
784
except UnicodeEncodeError:
785
raise TestSkipped("Filesystem doesn't support unicode")
787
self.tree1 = self.make_branch_and_tree('b1')
788
self.b1 = self.tree1.branch
791
u'With international man of mystery\n'
792
u'William Dod\xe9\n').encode('utf-8'))
795
self.tree1.add([u'with Dod\xe9'], ['withdod-id'])
796
self.tree1.commit(u'i18n commit from William Dod\xe9',
797
rev_id='i18n-1', committer=u'William Dod\xe9')
799
if sys.platform == 'darwin':
800
# On Mac the '\xe9' gets changed to 'e\u0301'
801
self.assertEqual([u'.bzr', u'with Dode\u0301'],
802
sorted(os.listdir(u'b1')))
803
delta = self.tree1.changes_from(self.tree1.basis_tree())
804
self.assertEqual([(u'with Dod\xe9', 'withdod-id', 'file')],
806
self.knownFailure("Mac OSX doesn't preserve unicode"
807
" combining characters.")
810
bundle = self.get_valid_bundle(None, 'i18n-1')
813
f = open(u'b1/with Dod\xe9', 'wb')
814
f.write(u'Modified \xb5\n'.encode('utf8'))
816
self.tree1.commit(u'modified', rev_id='i18n-2')
818
bundle = self.get_valid_bundle('i18n-1', 'i18n-2')
821
self.tree1.rename_one(u'with Dod\xe9', u'B\xe5gfors')
822
self.tree1.commit(u'renamed, the new i18n man', rev_id='i18n-3',
823
committer=u'Erik B\xe5gfors')
825
bundle = self.get_valid_bundle('i18n-2', 'i18n-3')
828
self.tree1.remove([u'B\xe5gfors'])
829
self.tree1.commit(u'removed', rev_id='i18n-4')
831
bundle = self.get_valid_bundle('i18n-3', 'i18n-4')
834
bundle = self.get_valid_bundle(None, 'i18n-4')
837
def test_whitespace_bundle(self):
838
if sys.platform in ('win32', 'cygwin'):
839
raise TestSkipped('Windows doesn\'t support filenames'
840
' with tabs or trailing spaces')
841
self.tree1 = self.make_branch_and_tree('b1')
842
self.b1 = self.tree1.branch
844
self.build_tree(['b1/trailing space '])
845
self.tree1.add(['trailing space '])
846
# TODO: jam 20060701 Check for handling files with '\t' characters
847
# once we actually support them
850
self.tree1.commit('funky whitespace', rev_id='white-1')
852
bundle = self.get_valid_bundle(None, 'white-1')
855
open('b1/trailing space ', 'ab').write('add some text\n')
856
self.tree1.commit('add text', rev_id='white-2')
858
bundle = self.get_valid_bundle('white-1', 'white-2')
861
self.tree1.rename_one('trailing space ', ' start and end space ')
862
self.tree1.commit('rename', rev_id='white-3')
864
bundle = self.get_valid_bundle('white-2', 'white-3')
867
self.tree1.remove([' start and end space '])
868
self.tree1.commit('removed', rev_id='white-4')
870
bundle = self.get_valid_bundle('white-3', 'white-4')
872
# Now test a complet roll-up
873
bundle = self.get_valid_bundle(None, 'white-4')
875
def test_alt_timezone_bundle(self):
876
self.tree1 = self.make_branch_and_memory_tree('b1')
877
self.b1 = self.tree1.branch
878
builder = treebuilder.TreeBuilder()
880
self.tree1.lock_write()
881
builder.start_tree(self.tree1)
882
builder.build(['newfile'])
883
builder.finish_tree()
885
# Asia/Colombo offset = 5 hours 30 minutes
886
self.tree1.commit('non-hour offset timezone', rev_id='tz-1',
887
timezone=19800, timestamp=1152544886.0)
889
bundle = self.get_valid_bundle(None, 'tz-1')
891
rev = bundle.revisions[0]
892
self.assertEqual('Mon 2006-07-10 20:51:26.000000000 +0530', rev.date)
893
self.assertEqual(19800, rev.timezone)
894
self.assertEqual(1152544886.0, rev.timestamp)
897
def test_bundle_root_id(self):
898
self.tree1 = self.make_branch_and_tree('b1')
899
self.b1 = self.tree1.branch
900
self.tree1.commit('message', rev_id='revid1')
901
bundle = self.get_valid_bundle(None, 'revid1')
902
tree = self.get_bundle_tree(bundle, 'revid1')
903
self.assertEqual('revid1', tree.inventory.root.revision)
905
def test_install_revisions(self):
906
self.tree1 = self.make_branch_and_tree('b1')
907
self.b1 = self.tree1.branch
908
self.tree1.commit('message', rev_id='rev2a')
909
bundle = self.get_valid_bundle(None, 'rev2a')
910
branch2 = self.make_branch('b2')
911
self.assertFalse(branch2.repository.has_revision('rev2a'))
912
target_revision = bundle.install_revisions(branch2.repository)
913
self.assertTrue(branch2.repository.has_revision('rev2a'))
914
self.assertEqual('rev2a', target_revision)
916
def test_bundle_empty_property(self):
917
"""Test serializing revision properties with an empty value."""
918
tree = self.make_branch_and_memory_tree('tree')
920
self.addCleanup(tree.unlock)
921
tree.add([''], ['TREE_ROOT'])
922
tree.commit('One', revprops={'one':'two', 'empty':''}, rev_id='rev1')
923
self.b1 = tree.branch
924
bundle_sio, revision_ids = self.create_bundle_text(None, 'rev1')
925
bundle = read_bundle(bundle_sio)
926
revision_info = bundle.revisions[0]
927
self.assertEqual('rev1', revision_info.revision_id)
928
rev = revision_info.as_revision()
929
self.assertEqual({'branch-nick':'tree', 'empty':'', 'one':'two'},
932
def test_bundle_sorted_properties(self):
933
"""For stability the writer should write properties in sorted order."""
934
tree = self.make_branch_and_memory_tree('tree')
936
self.addCleanup(tree.unlock)
938
tree.add([''], ['TREE_ROOT'])
939
tree.commit('One', rev_id='rev1',
940
revprops={'a':'4', 'b':'3', 'c':'2', 'd':'1'})
941
self.b1 = tree.branch
942
bundle_sio, revision_ids = self.create_bundle_text(None, 'rev1')
943
bundle = read_bundle(bundle_sio)
944
revision_info = bundle.revisions[0]
945
self.assertEqual('rev1', revision_info.revision_id)
946
rev = revision_info.as_revision()
947
self.assertEqual({'branch-nick':'tree', 'a':'4', 'b':'3', 'c':'2',
948
'd':'1'}, rev.properties)
950
def test_bundle_unicode_properties(self):
951
"""We should be able to round trip a non-ascii property."""
952
tree = self.make_branch_and_memory_tree('tree')
954
self.addCleanup(tree.unlock)
956
tree.add([''], ['TREE_ROOT'])
957
# Revisions themselves do not require anything about revision property
958
# keys, other than that they are a basestring, and do not contain
960
# However, Testaments assert than they are str(), and thus should not
962
tree.commit('One', rev_id='rev1',
963
revprops={'omega':u'\u03a9', 'alpha':u'\u03b1'})
964
self.b1 = tree.branch
965
bundle_sio, revision_ids = self.create_bundle_text(None, 'rev1')
966
bundle = read_bundle(bundle_sio)
967
revision_info = bundle.revisions[0]
968
self.assertEqual('rev1', revision_info.revision_id)
969
rev = revision_info.as_revision()
970
self.assertEqual({'branch-nick':'tree', 'omega':u'\u03a9',
971
'alpha':u'\u03b1'}, rev.properties)
973
def test_bundle_with_ghosts(self):
974
tree = self.make_branch_and_tree('tree')
975
self.b1 = tree.branch
976
self.build_tree_contents([('tree/file', 'content1')])
979
self.build_tree_contents([('tree/file', 'content2')])
980
tree.add_parent_tree_id('ghost')
981
tree.commit('rev2', rev_id='rev2')
982
bundle = self.get_valid_bundle(None, 'rev2')
984
def make_simple_tree(self, format=None):
985
tree = self.make_branch_and_tree('b1', format=format)
986
self.b1 = tree.branch
987
self.build_tree(['b1/file'])
991
def test_across_serializers(self):
992
tree = self.make_simple_tree('knit')
993
tree.commit('hello', rev_id='rev1')
994
tree.commit('hello', rev_id='rev2')
995
bundle = read_bundle(self.create_bundle_text(None, 'rev2')[0])
996
repo = self.make_repository('repo', format='dirstate-with-subtree')
997
bundle.install_revisions(repo)
998
inv_text = repo.get_inventory_xml('rev2')
999
self.assertNotContainsRe(inv_text, 'format="5"')
1000
self.assertContainsRe(inv_text, 'format="7"')
1002
def test_across_models(self):
1003
tree = self.make_simple_tree('knit')
1004
tree.commit('hello', rev_id='rev1')
1005
tree.commit('hello', rev_id='rev2')
1006
bundle = read_bundle(self.create_bundle_text(None, 'rev2')[0])
1007
repo = self.make_repository('repo', format='dirstate-with-subtree')
1008
bundle.install_revisions(repo)
1009
inv = repo.get_inventory('rev2')
1010
self.assertEqual('rev2', inv.root.revision)
1011
root_vf = repo.weave_store.get_weave(inv.root.file_id,
1012
repo.get_transaction())
1013
self.assertEqual(root_vf.versions(), ['rev1', 'rev2'])
1015
def test_across_models_incompatible(self):
1016
tree = self.make_simple_tree('dirstate-with-subtree')
1017
tree.commit('hello', rev_id='rev1')
1018
tree.commit('hello', rev_id='rev2')
1020
bundle = read_bundle(self.create_bundle_text(None, 'rev1')[0])
1021
except errors.IncompatibleBundleFormat:
1022
raise TestSkipped("Format 0.8 doesn't work with knit3")
1023
repo = self.make_repository('repo', format='knit')
1024
bundle.install_revisions(repo)
1026
bundle = read_bundle(self.create_bundle_text(None, 'rev2')[0])
1027
self.assertRaises(errors.IncompatibleRevision,
1028
bundle.install_revisions, repo)
1030
def test_get_merge_request(self):
1031
tree = self.make_simple_tree()
1032
tree.commit('hello', rev_id='rev1')
1033
tree.commit('hello', rev_id='rev2')
1034
bundle = read_bundle(self.create_bundle_text(None, 'rev1')[0])
1035
result = bundle.get_merge_request(tree.branch.repository)
1036
self.assertEqual((None, 'rev1', 'inapplicable'), result)
1038
def test_with_subtree(self):
1039
tree = self.make_branch_and_tree('tree',
1040
format='dirstate-with-subtree')
1041
self.b1 = tree.branch
1042
subtree = self.make_branch_and_tree('tree/subtree',
1043
format='dirstate-with-subtree')
1045
tree.commit('hello', rev_id='rev1')
1047
bundle = read_bundle(self.create_bundle_text(None, 'rev1')[0])
1048
except errors.IncompatibleBundleFormat:
1049
raise TestSkipped("Format 0.8 doesn't work with knit3")
1050
if isinstance(bundle, v09.BundleInfo09):
1051
raise TestSkipped("Format 0.9 doesn't work with subtrees")
1052
repo = self.make_repository('repo', format='knit')
1053
self.assertRaises(errors.IncompatibleRevision,
1054
bundle.install_revisions, repo)
1055
repo2 = self.make_repository('repo2', format='dirstate-with-subtree')
1056
bundle.install_revisions(repo2)
1058
def test_revision_id_with_slash(self):
1059
self.tree1 = self.make_branch_and_tree('tree')
1060
self.b1 = self.tree1.branch
1062
self.tree1.commit('Revision/id/with/slashes', rev_id='rev/id')
1064
raise TestSkipped("Repository doesn't support revision ids with"
1066
bundle = self.get_valid_bundle(None, 'rev/id')
1069
class V08BundleTester(BundleTester, TestCaseWithTransport):
1073
def test_bundle_empty_property(self):
1074
"""Test serializing revision properties with an empty value."""
1075
tree = self.make_branch_and_memory_tree('tree')
1077
self.addCleanup(tree.unlock)
1078
tree.add([''], ['TREE_ROOT'])
1079
tree.commit('One', revprops={'one':'two', 'empty':''}, rev_id='rev1')
1080
self.b1 = tree.branch
1081
bundle_sio, revision_ids = self.create_bundle_text(None, 'rev1')
1082
self.assertContainsRe(bundle_sio.getvalue(),
1084
'# branch-nick: tree\n'
1088
bundle = read_bundle(bundle_sio)
1089
revision_info = bundle.revisions[0]
1090
self.assertEqual('rev1', revision_info.revision_id)
1091
rev = revision_info.as_revision()
1092
self.assertEqual({'branch-nick':'tree', 'empty':'', 'one':'two'},
1095
def get_bundle_tree(self, bundle, revision_id):
1096
repository = self.make_repository('repo')
1097
return bundle.revision_tree(repository, 'revid1')
1099
def test_bundle_empty_property_alt(self):
1100
"""Test serializing revision properties with an empty value.
1102
Older readers had a bug when reading an empty property.
1103
They assumed that all keys ended in ': \n'. However they would write an
1104
empty value as ':\n'. This tests make sure that all newer bzr versions
1105
can handle th second form.
1107
tree = self.make_branch_and_memory_tree('tree')
1109
self.addCleanup(tree.unlock)
1110
tree.add([''], ['TREE_ROOT'])
1111
tree.commit('One', revprops={'one':'two', 'empty':''}, rev_id='rev1')
1112
self.b1 = tree.branch
1113
bundle_sio, revision_ids = self.create_bundle_text(None, 'rev1')
1114
txt = bundle_sio.getvalue()
1115
loc = txt.find('# empty: ') + len('# empty:')
1116
# Create a new bundle, which strips the trailing space after empty
1117
bundle_sio = StringIO(txt[:loc] + txt[loc+1:])
1119
self.assertContainsRe(bundle_sio.getvalue(),
1121
'# branch-nick: tree\n'
1125
bundle = read_bundle(bundle_sio)
1126
revision_info = bundle.revisions[0]
1127
self.assertEqual('rev1', revision_info.revision_id)
1128
rev = revision_info.as_revision()
1129
self.assertEqual({'branch-nick':'tree', 'empty':'', 'one':'two'},
1132
def test_bundle_sorted_properties(self):
1133
"""For stability the writer should write properties in sorted order."""
1134
tree = self.make_branch_and_memory_tree('tree')
1136
self.addCleanup(tree.unlock)
1138
tree.add([''], ['TREE_ROOT'])
1139
tree.commit('One', rev_id='rev1',
1140
revprops={'a':'4', 'b':'3', 'c':'2', 'd':'1'})
1141
self.b1 = tree.branch
1142
bundle_sio, revision_ids = self.create_bundle_text(None, 'rev1')
1143
self.assertContainsRe(bundle_sio.getvalue(),
1147
'# branch-nick: tree\n'
1151
bundle = read_bundle(bundle_sio)
1152
revision_info = bundle.revisions[0]
1153
self.assertEqual('rev1', revision_info.revision_id)
1154
rev = revision_info.as_revision()
1155
self.assertEqual({'branch-nick':'tree', 'a':'4', 'b':'3', 'c':'2',
1156
'd':'1'}, rev.properties)
1158
def test_bundle_unicode_properties(self):
1159
"""We should be able to round trip a non-ascii property."""
1160
tree = self.make_branch_and_memory_tree('tree')
1162
self.addCleanup(tree.unlock)
1164
tree.add([''], ['TREE_ROOT'])
1165
# Revisions themselves do not require anything about revision property
1166
# keys, other than that they are a basestring, and do not contain
1168
# However, Testaments assert than they are str(), and thus should not
1170
tree.commit('One', rev_id='rev1',
1171
revprops={'omega':u'\u03a9', 'alpha':u'\u03b1'})
1172
self.b1 = tree.branch
1173
bundle_sio, revision_ids = self.create_bundle_text(None, 'rev1')
1174
self.assertContainsRe(bundle_sio.getvalue(),
1176
'# alpha: \xce\xb1\n'
1177
'# branch-nick: tree\n'
1178
'# omega: \xce\xa9\n'
1180
bundle = read_bundle(bundle_sio)
1181
revision_info = bundle.revisions[0]
1182
self.assertEqual('rev1', revision_info.revision_id)
1183
rev = revision_info.as_revision()
1184
self.assertEqual({'branch-nick':'tree', 'omega':u'\u03a9',
1185
'alpha':u'\u03b1'}, rev.properties)
1188
class V09BundleKnit2Tester(V08BundleTester):
1192
def bzrdir_format(self):
1193
format = bzrdir.BzrDirMetaFormat1()
1194
format.repository_format = knitrepo.RepositoryFormatKnit3()
1198
class V09BundleKnit1Tester(V08BundleTester):
1202
def bzrdir_format(self):
1203
format = bzrdir.BzrDirMetaFormat1()
1204
format.repository_format = knitrepo.RepositoryFormatKnit1()
1208
class V4BundleTester(BundleTester, TestCaseWithTransport):
1212
def get_valid_bundle(self, base_rev_id, rev_id, checkout_dir=None):
1213
"""Create a bundle from base_rev_id -> rev_id in built-in branch.
1214
Make sure that the text generated is valid, and that it
1215
can be applied against the base, and generate the same information.
1217
:return: The in-memory bundle
1219
bundle_txt, rev_ids = self.create_bundle_text(base_rev_id, rev_id)
1221
# This should also validate the generated bundle
1222
bundle = read_bundle(bundle_txt)
1223
repository = self.b1.repository
1224
for bundle_rev in bundle.real_revisions:
1225
# These really should have already been checked when we read the
1226
# bundle, since it computes the sha1 hash for the revision, which
1227
# only will match if everything is okay, but lets be explicit about
1229
branch_rev = repository.get_revision(bundle_rev.revision_id)
1230
for a in ('inventory_sha1', 'revision_id', 'parent_ids',
1231
'timestamp', 'timezone', 'message', 'committer',
1232
'parent_ids', 'properties'):
1233
self.assertEqual(getattr(branch_rev, a),
1234
getattr(bundle_rev, a))
1235
self.assertEqual(len(branch_rev.parent_ids),
1236
len(bundle_rev.parent_ids))
1237
self.assertEqual(set(rev_ids),
1238
set([r.revision_id for r in bundle.real_revisions]))
1239
self.valid_apply_bundle(base_rev_id, bundle,
1240
checkout_dir=checkout_dir)
1244
def get_invalid_bundle(self, base_rev_id, rev_id):
1245
"""Create a bundle from base_rev_id -> rev_id in built-in branch.
1246
Munge the text so that it's invalid.
1248
:return: The in-memory bundle
1250
from bzrlib.bundle import serializer
1251
bundle_txt, rev_ids = self.create_bundle_text(base_rev_id, rev_id)
1252
new_text = self.get_raw(StringIO(''.join(bundle_txt)))
1253
new_text = new_text.replace('<file file_id="exe-1"',
1254
'<file executable="y" file_id="exe-1"')
1255
new_text = new_text.replace('B372', 'B387')
1256
bundle_txt = StringIO()
1257
bundle_txt.write(serializer._get_bundle_header('4alpha'))
1258
bundle_txt.write('\n')
1259
bundle_txt.write(new_text.encode('bz2'))
1261
bundle = read_bundle(bundle_txt)
1262
self.valid_apply_bundle(base_rev_id, bundle)
1265
def create_bundle_text(self, base_rev_id, rev_id):
1266
bundle_txt = StringIO()
1267
rev_ids = write_bundle(self.b1.repository, rev_id, base_rev_id,
1268
bundle_txt, format=self.format)
1270
self.assertEqual(bundle_txt.readline(),
1271
'# Bazaar revision bundle v%s\n' % self.format)
1272
self.assertEqual(bundle_txt.readline(), '#\n')
1273
rev = self.b1.repository.get_revision(rev_id)
1275
return bundle_txt, rev_ids
1277
def get_bundle_tree(self, bundle, revision_id):
1278
repository = self.make_repository('repo')
1279
bundle.install_revisions(repository)
1280
return repository.revision_tree(revision_id)
1282
def test_creation(self):
1283
tree = self.make_branch_and_tree('tree')
1284
self.build_tree_contents([('tree/file', 'contents1\nstatic\n')])
1285
tree.add('file', 'fileid-2')
1286
tree.commit('added file', rev_id='rev1')
1287
self.build_tree_contents([('tree/file', 'contents2\nstatic\n')])
1288
tree.commit('changed file', rev_id='rev2')
1290
serializer = BundleSerializerV4('1.0')
1291
serializer.write(tree.branch.repository, ['rev1', 'rev2'], {}, s)
1293
tree2 = self.make_branch_and_tree('target')
1294
target_repo = tree2.branch.repository
1295
install_bundle(target_repo, serializer.read(s))
1296
vf = target_repo.weave_store.get_weave('fileid-2',
1297
target_repo.get_transaction())
1298
self.assertEqual('contents1\nstatic\n', vf.get_text('rev1'))
1299
self.assertEqual('contents2\nstatic\n', vf.get_text('rev2'))
1300
rtree = target_repo.revision_tree('rev2')
1301
inventory_vf = target_repo.get_inventory_weave()
1302
self.assertEqual(['rev1'], inventory_vf.get_parents('rev2'))
1303
self.assertEqual('changed file',
1304
target_repo.get_revision('rev2').message)
1307
def get_raw(bundle_file):
1309
line = bundle_file.readline()
1310
line = bundle_file.readline()
1311
lines = bundle_file.readlines()
1312
return ''.join(lines).decode('bz2')
1314
def test_copy_signatures(self):
1315
tree_a = self.make_branch_and_tree('tree_a')
1317
import bzrlib.commit as commit
1318
oldstrategy = bzrlib.gpg.GPGStrategy
1319
branch = tree_a.branch
1320
repo_a = branch.repository
1321
tree_a.commit("base", allow_pointless=True, rev_id='A')
1322
self.failIf(branch.repository.has_signature_for_revision_id('A'))
1324
from bzrlib.testament import Testament
1325
# monkey patch gpg signing mechanism
1326
bzrlib.gpg.GPGStrategy = bzrlib.gpg.LoopbackGPGStrategy
1327
new_config = test_commit.MustSignConfig(branch)
1328
commit.Commit(config=new_config).commit(message="base",
1329
allow_pointless=True,
1331
working_tree=tree_a)
1333
return bzrlib.gpg.LoopbackGPGStrategy(None).sign(text)
1334
self.assertTrue(repo_a.has_signature_for_revision_id('B'))
1336
bzrlib.gpg.GPGStrategy = oldstrategy
1337
tree_b = self.make_branch_and_tree('tree_b')
1338
repo_b = tree_b.branch.repository
1340
serializer = BundleSerializerV4('4alpha')
1341
serializer.write(tree_a.branch.repository, ['A', 'B'], {}, s)
1343
install_bundle(repo_b, serializer.read(s))
1344
self.assertTrue(repo_b.has_signature_for_revision_id('B'))
1345
self.assertEqual(repo_b.get_signature_text('B'),
1346
repo_a.get_signature_text('B'))
1348
# ensure repeat installs are harmless
1349
install_bundle(repo_b, serializer.read(s))
1352
class V4WeaveBundleTester(V4BundleTester):
1354
def bzrdir_format(self):
1358
class MungedBundleTester(object):
1360
def build_test_bundle(self):
1361
wt = self.make_branch_and_tree('b1')
1363
self.build_tree(['b1/one'])
1365
wt.commit('add one', rev_id='a@cset-0-1')
1366
self.build_tree(['b1/two'])
1368
wt.commit('add two', rev_id='a@cset-0-2',
1369
revprops={'branch-nick':'test'})
1371
bundle_txt = StringIO()
1372
rev_ids = write_bundle(wt.branch.repository, 'a@cset-0-2',
1373
'a@cset-0-1', bundle_txt, self.format)
1374
self.assertEqual(set(['a@cset-0-2']), set(rev_ids))
1375
bundle_txt.seek(0, 0)
1378
def check_valid(self, bundle):
1379
"""Check that after whatever munging, the final object is valid."""
1380
self.assertEqual(['a@cset-0-2'],
1381
[r.revision_id for r in bundle.real_revisions])
1383
def test_extra_whitespace(self):
1384
bundle_txt = self.build_test_bundle()
1386
# Seek to the end of the file
1387
# Adding one extra newline used to give us
1388
# TypeError: float() argument must be a string or a number
1389
bundle_txt.seek(0, 2)
1390
bundle_txt.write('\n')
1393
bundle = read_bundle(bundle_txt)
1394
self.check_valid(bundle)
1396
def test_extra_whitespace_2(self):
1397
bundle_txt = self.build_test_bundle()
1399
# Seek to the end of the file
1400
# Adding two extra newlines used to give us
1401
# MalformedPatches: The first line of all patches should be ...
1402
bundle_txt.seek(0, 2)
1403
bundle_txt.write('\n\n')
1406
bundle = read_bundle(bundle_txt)
1407
self.check_valid(bundle)
1410
class MungedBundleTesterV09(TestCaseWithTransport, MungedBundleTester):
1414
def test_missing_trailing_whitespace(self):
1415
bundle_txt = self.build_test_bundle()
1417
# Remove a trailing newline, it shouldn't kill the parser
1418
raw = bundle_txt.getvalue()
1419
# The contents of the bundle don't have to be this, but this
1420
# test is concerned with the exact case where the serializer
1421
# creates a blank line at the end, and fails if that
1423
self.assertEqual('\n\n', raw[-2:])
1424
bundle_txt = StringIO(raw[:-1])
1426
bundle = read_bundle(bundle_txt)
1427
self.check_valid(bundle)
1429
def test_opening_text(self):
1430
bundle_txt = self.build_test_bundle()
1432
bundle_txt = StringIO("Some random\nemail comments\n"
1433
+ bundle_txt.getvalue())
1435
bundle = read_bundle(bundle_txt)
1436
self.check_valid(bundle)
1438
def test_trailing_text(self):
1439
bundle_txt = self.build_test_bundle()
1441
bundle_txt = StringIO(bundle_txt.getvalue() +
1442
"Some trailing\nrandom\ntext\n")
1444
bundle = read_bundle(bundle_txt)
1445
self.check_valid(bundle)
1448
class MungedBundleTesterV4(TestCaseWithTransport, MungedBundleTester):
1453
class TestBundleWriterReader(TestCase):
1455
def test_roundtrip_record(self):
1456
fileobj = StringIO()
1457
writer = v4.BundleWriter(fileobj)
1459
writer.add_info_record(foo='bar')
1460
writer._add_record("Record body", {'parents': ['1', '3'],
1461
'storage_kind':'fulltext'}, 'file', 'revid', 'fileid')
1464
record_iter = v4.BundleReader(fileobj).iter_records()
1465
record = record_iter.next()
1466
self.assertEqual((None, {'foo': 'bar', 'storage_kind': 'header'},
1467
'info', None, None), record)
1468
record = record_iter.next()
1469
self.assertEqual(("Record body", {'storage_kind': 'fulltext',
1470
'parents': ['1', '3']}, 'file', 'revid', 'fileid'),
1473
def test_encode_name(self):
1474
self.assertEqual('revision/rev1',
1475
v4.BundleWriter.encode_name('revision', 'rev1'))
1476
self.assertEqual('file/rev//1/file-id-1',
1477
v4.BundleWriter.encode_name('file', 'rev/1', 'file-id-1'))
1478
self.assertEqual('info',
1479
v4.BundleWriter.encode_name('info', None, None))
1481
def test_decode_name(self):
1482
self.assertEqual(('revision', 'rev1', None),
1483
v4.BundleReader.decode_name('revision/rev1'))
1484
self.assertEqual(('file', 'rev/1', 'file-id-1'),
1485
v4.BundleReader.decode_name('file/rev//1/file-id-1'))
1486
self.assertEqual(('info', None, None),
1487
v4.BundleReader.decode_name('info'))