1
# Copyright (C) 2004-2006 by 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
19
from bzrlib.builtins import merge
20
from bzrlib.bzrdir import BzrDir
21
from bzrlib.bundle.apply_bundle import install_bundle, merge_bundle
22
from bzrlib.bundle.bundle_data import BundleTree
23
from bzrlib.bundle.serializer import write_bundle, read_bundle
24
from bzrlib.diff import internal_diff
25
from bzrlib.errors import BzrError, TestamentMismatch, NotABundle
26
from bzrlib.merge import Merge3Merger
27
from bzrlib.osutils import has_symlinks, sha_file
28
from bzrlib.tests import (TestCaseInTempDir, TestCaseWithTransport,
29
TestCase, TestSkipped)
30
from bzrlib.transform import TreeTransform
31
from bzrlib.workingtree import WorkingTree
34
class MockTree(object):
36
from bzrlib.inventory import RootEntry, ROOT_ID
38
self.paths = {ROOT_ID: ""}
39
self.ids = {"": ROOT_ID}
41
self.root = RootEntry(ROOT_ID)
43
inventory = property(lambda x:x)
46
return self.paths.iterkeys()
48
def __getitem__(self, file_id):
49
if file_id == self.root.file_id:
52
return self.make_entry(file_id, self.paths[file_id])
54
def parent_id(self, file_id):
55
from os.path import dirname
56
parent_dir = dirname(self.paths[file_id])
59
return self.ids[parent_dir]
61
def iter_entries(self):
62
for path, file_id in self.ids.iteritems():
63
yield path, self[file_id]
65
def get_file_kind(self, file_id):
66
if file_id in self.contents:
72
def make_entry(self, file_id, path):
73
from os.path import basename
74
from bzrlib.inventory import (InventoryEntry, InventoryFile
75
, InventoryDirectory, InventoryLink)
77
kind = self.get_file_kind(file_id)
78
parent_id = self.parent_id(file_id)
79
text_sha_1, text_size = self.contents_stats(file_id)
80
if kind == 'directory':
81
ie = InventoryDirectory(file_id, name, parent_id)
83
ie = InventoryFile(file_id, name, parent_id)
84
elif kind == 'symlink':
85
ie = InventoryLink(file_id, name, parent_id)
87
raise BzrError('unknown kind %r' % kind)
88
ie.text_sha1 = text_sha_1
89
ie.text_size = text_size
92
def add_dir(self, file_id, path):
93
self.paths[file_id] = path
94
self.ids[path] = file_id
96
def add_file(self, file_id, path, contents):
97
self.add_dir(file_id, path)
98
self.contents[file_id] = contents
100
def path2id(self, path):
101
return self.ids.get(path)
103
def id2path(self, file_id):
104
return self.paths.get(file_id)
106
def has_id(self, file_id):
107
return self.id2path(file_id) is not None
109
def get_file(self, file_id):
111
result.write(self.contents[file_id])
115
def contents_stats(self, file_id):
116
if file_id not in self.contents:
118
text_sha1 = sha_file(self.get_file(file_id))
119
return text_sha1, len(self.contents[file_id])
122
class BTreeTester(TestCase):
123
"""A simple unittest tester for the BundleTree class."""
125
def make_tree_1(self):
127
mtree.add_dir("a", "grandparent")
128
mtree.add_dir("b", "grandparent/parent")
129
mtree.add_file("c", "grandparent/parent/file", "Hello\n")
130
mtree.add_dir("d", "grandparent/alt_parent")
131
return BundleTree(mtree, ''), mtree
133
def test_renames(self):
134
"""Ensure that file renames have the proper effect on children"""
135
btree = self.make_tree_1()[0]
136
self.assertEqual(btree.old_path("grandparent"), "grandparent")
137
self.assertEqual(btree.old_path("grandparent/parent"),
138
"grandparent/parent")
139
self.assertEqual(btree.old_path("grandparent/parent/file"),
140
"grandparent/parent/file")
142
self.assertEqual(btree.id2path("a"), "grandparent")
143
self.assertEqual(btree.id2path("b"), "grandparent/parent")
144
self.assertEqual(btree.id2path("c"), "grandparent/parent/file")
146
self.assertEqual(btree.path2id("grandparent"), "a")
147
self.assertEqual(btree.path2id("grandparent/parent"), "b")
148
self.assertEqual(btree.path2id("grandparent/parent/file"), "c")
150
assert btree.path2id("grandparent2") is None
151
assert btree.path2id("grandparent2/parent") is None
152
assert btree.path2id("grandparent2/parent/file") is None
154
btree.note_rename("grandparent", "grandparent2")
155
assert btree.old_path("grandparent") is None
156
assert btree.old_path("grandparent/parent") is None
157
assert btree.old_path("grandparent/parent/file") is None
159
self.assertEqual(btree.id2path("a"), "grandparent2")
160
self.assertEqual(btree.id2path("b"), "grandparent2/parent")
161
self.assertEqual(btree.id2path("c"), "grandparent2/parent/file")
163
self.assertEqual(btree.path2id("grandparent2"), "a")
164
self.assertEqual(btree.path2id("grandparent2/parent"), "b")
165
self.assertEqual(btree.path2id("grandparent2/parent/file"), "c")
167
assert btree.path2id("grandparent") is None
168
assert btree.path2id("grandparent/parent") is None
169
assert btree.path2id("grandparent/parent/file") is None
171
btree.note_rename("grandparent/parent", "grandparent2/parent2")
172
self.assertEqual(btree.id2path("a"), "grandparent2")
173
self.assertEqual(btree.id2path("b"), "grandparent2/parent2")
174
self.assertEqual(btree.id2path("c"), "grandparent2/parent2/file")
176
self.assertEqual(btree.path2id("grandparent2"), "a")
177
self.assertEqual(btree.path2id("grandparent2/parent2"), "b")
178
self.assertEqual(btree.path2id("grandparent2/parent2/file"), "c")
180
assert btree.path2id("grandparent2/parent") is None
181
assert btree.path2id("grandparent2/parent/file") is None
183
btree.note_rename("grandparent/parent/file",
184
"grandparent2/parent2/file2")
185
self.assertEqual(btree.id2path("a"), "grandparent2")
186
self.assertEqual(btree.id2path("b"), "grandparent2/parent2")
187
self.assertEqual(btree.id2path("c"), "grandparent2/parent2/file2")
189
self.assertEqual(btree.path2id("grandparent2"), "a")
190
self.assertEqual(btree.path2id("grandparent2/parent2"), "b")
191
self.assertEqual(btree.path2id("grandparent2/parent2/file2"), "c")
193
assert btree.path2id("grandparent2/parent2/file") is None
195
def test_moves(self):
196
"""Ensure that file moves have the proper effect on children"""
197
btree = self.make_tree_1()[0]
198
btree.note_rename("grandparent/parent/file",
199
"grandparent/alt_parent/file")
200
self.assertEqual(btree.id2path("c"), "grandparent/alt_parent/file")
201
self.assertEqual(btree.path2id("grandparent/alt_parent/file"), "c")
202
assert btree.path2id("grandparent/parent/file") is None
204
def unified_diff(self, old, new):
206
internal_diff("old", old, "new", new, out)
210
def make_tree_2(self):
211
btree = self.make_tree_1()[0]
212
btree.note_rename("grandparent/parent/file",
213
"grandparent/alt_parent/file")
214
assert btree.id2path("e") is None
215
assert btree.path2id("grandparent/parent/file") is None
216
btree.note_id("e", "grandparent/parent/file")
220
"""File/inventory adds"""
221
btree = self.make_tree_2()
222
add_patch = self.unified_diff([], ["Extra cheese\n"])
223
btree.note_patch("grandparent/parent/file", add_patch)
224
btree.note_id('f', 'grandparent/parent/symlink', kind='symlink')
225
btree.note_target('grandparent/parent/symlink', 'venus')
226
self.adds_test(btree)
228
def adds_test(self, btree):
229
self.assertEqual(btree.id2path("e"), "grandparent/parent/file")
230
self.assertEqual(btree.path2id("grandparent/parent/file"), "e")
231
self.assertEqual(btree.get_file("e").read(), "Extra cheese\n")
232
self.assertEqual(btree.get_symlink_target('f'), 'venus')
234
def test_adds2(self):
235
"""File/inventory adds, with patch-compatibile renames"""
236
btree = self.make_tree_2()
237
btree.contents_by_id = False
238
add_patch = self.unified_diff(["Hello\n"], ["Extra cheese\n"])
239
btree.note_patch("grandparent/parent/file", add_patch)
240
btree.note_id('f', 'grandparent/parent/symlink', kind='symlink')
241
btree.note_target('grandparent/parent/symlink', 'venus')
242
self.adds_test(btree)
244
def make_tree_3(self):
245
btree, mtree = self.make_tree_1()
246
mtree.add_file("e", "grandparent/parent/topping", "Anchovies\n")
247
btree.note_rename("grandparent/parent/file",
248
"grandparent/alt_parent/file")
249
btree.note_rename("grandparent/parent/topping",
250
"grandparent/alt_parent/stopping")
253
def get_file_test(self, btree):
254
self.assertEqual(btree.get_file("e").read(), "Lemon\n")
255
self.assertEqual(btree.get_file("c").read(), "Hello\n")
257
def test_get_file(self):
258
"""Get file contents"""
259
btree = self.make_tree_3()
260
mod_patch = self.unified_diff(["Anchovies\n"], ["Lemon\n"])
261
btree.note_patch("grandparent/alt_parent/stopping", mod_patch)
262
self.get_file_test(btree)
264
def test_get_file2(self):
265
"""Get file contents, with patch-compatibile renames"""
266
btree = self.make_tree_3()
267
btree.contents_by_id = False
268
mod_patch = self.unified_diff([], ["Lemon\n"])
269
btree.note_patch("grandparent/alt_parent/stopping", mod_patch)
270
mod_patch = self.unified_diff([], ["Hello\n"])
271
btree.note_patch("grandparent/alt_parent/file", mod_patch)
272
self.get_file_test(btree)
274
def test_delete(self):
276
btree = self.make_tree_1()[0]
277
self.assertEqual(btree.get_file("c").read(), "Hello\n")
278
btree.note_deletion("grandparent/parent/file")
279
assert btree.id2path("c") is None
280
assert btree.path2id("grandparent/parent/file") is None
282
def sorted_ids(self, tree):
287
def test_iteration(self):
288
"""Ensure that iteration through ids works properly"""
289
btree = self.make_tree_1()[0]
290
self.assertEqual(self.sorted_ids(btree), ['a', 'b', 'c', 'd'])
291
btree.note_deletion("grandparent/parent/file")
292
btree.note_id("e", "grandparent/alt_parent/fool", kind="directory")
293
btree.note_last_changed("grandparent/alt_parent/fool",
295
self.assertEqual(self.sorted_ids(btree), ['a', 'b', 'd', 'e'])
298
class BundleTester(TestCaseInTempDir):
300
def create_bundle_text(self, base_rev_id, rev_id):
301
bundle_txt = StringIO()
302
rev_ids = write_bundle(self.b1.repository, rev_id, base_rev_id,
305
self.assertEqual(bundle_txt.readline(),
306
'# Bazaar revision bundle v0.8\n')
307
self.assertEqual(bundle_txt.readline(), '#\n')
309
rev = self.b1.repository.get_revision(rev_id)
310
self.assertEqual(bundle_txt.readline().decode('utf-8'),
313
open(',,bundle', 'wb').write(bundle_txt.getvalue())
315
return bundle_txt, rev_ids
317
def get_valid_bundle(self, base_rev_id, rev_id, checkout_dir=None):
318
"""Create a bundle from base_rev_id -> rev_id in built-in branch.
319
Make sure that the text generated is valid, and that it
320
can be applied against the base, and generate the same information.
322
:return: The in-memory bundle
324
bundle_txt, rev_ids = self.create_bundle_text(base_rev_id, rev_id)
326
# This should also validate the generated bundle
327
bundle = read_bundle(bundle_txt)
328
repository = self.b1.repository
329
for bundle_rev in bundle.real_revisions:
330
# These really should have already been checked when we read the
331
# bundle, since it computes the sha1 hash for the revision, which
332
# only will match if everything is okay, but lets be explicit about
334
branch_rev = repository.get_revision(bundle_rev.revision_id)
335
for a in ('inventory_sha1', 'revision_id', 'parent_ids',
336
'timestamp', 'timezone', 'message', 'committer',
337
'parent_ids', 'properties'):
338
self.assertEqual(getattr(branch_rev, a),
339
getattr(bundle_rev, a))
340
self.assertEqual(len(branch_rev.parent_ids),
341
len(bundle_rev.parent_ids))
342
self.assertEqual(rev_ids,
343
[r.revision_id for r in bundle.real_revisions])
344
self.valid_apply_bundle(base_rev_id, bundle,
345
checkout_dir=checkout_dir)
349
def get_invalid_bundle(self, base_rev_id, rev_id):
350
"""Create a bundle from base_rev_id -> rev_id in built-in branch.
351
Munge the text so that it's invalid.
353
:return: The in-memory bundle
355
bundle_txt, rev_ids = self.create_bundle_text(base_rev_id, rev_id)
356
new_text = bundle_txt.getvalue().replace('executable:no',
358
bundle_txt = StringIO(new_text)
359
bundle = read_bundle(bundle_txt)
360
self.valid_apply_bundle(base_rev_id, bundle)
363
def test_non_bundle(self):
364
self.assertRaises(NotABundle, read_bundle, StringIO('#!/bin/sh\n'))
366
def get_checkout(self, rev_id, checkout_dir=None):
367
"""Get a new tree, with the specified revision in it.
369
from bzrlib.branch import Branch
372
if checkout_dir is None:
373
checkout_dir = tempfile.mkdtemp(prefix='test-branch-', dir='.')
376
if not os.path.exists(checkout_dir):
377
os.mkdir(checkout_dir)
378
tree = BzrDir.create_standalone_workingtree(checkout_dir)
380
ancestors = write_bundle(self.b1.repository, rev_id, None, s)
382
assert isinstance(s.getvalue(), str), (
383
"Bundle isn't a bytestring:\n %s..." % repr(s.getvalue())[:40])
384
install_bundle(tree.branch.repository, read_bundle(s))
385
for ancestor in ancestors:
386
old = self.b1.repository.revision_tree(ancestor)
387
new = tree.branch.repository.revision_tree(ancestor)
388
for inventory_id in old:
390
old_file = old.get_file(inventory_id)
395
self.assertEqual(old_file.read(),
396
new.get_file(inventory_id).read())
397
if rev_id is not None:
398
rh = self.b1.revision_history()
399
tree.branch.set_revision_history(rh[:rh.index(rev_id)+1])
403
def valid_apply_bundle(self, base_rev_id, info, checkout_dir=None):
404
"""Get the base revision, apply the changes, and make
405
sure everything matches the builtin branch.
407
to_tree = self.get_checkout(base_rev_id, checkout_dir=checkout_dir)
408
repository = to_tree.branch.repository
409
self.assertIs(repository.has_revision(base_rev_id), True)
410
for rev in info.real_revisions:
411
self.assert_(not repository.has_revision(rev.revision_id),
412
'Revision {%s} present before applying bundle'
414
merge_bundle(info, to_tree, True, Merge3Merger, False, False)
416
for rev in info.real_revisions:
417
self.assert_(repository.has_revision(rev.revision_id),
418
'Missing revision {%s} after applying bundle'
421
self.assert_(to_tree.branch.repository.has_revision(info.target))
422
# Do we also want to verify that all the texts have been added?
424
self.assert_(info.target in to_tree.pending_merges())
427
rev = info.real_revisions[-1]
428
base_tree = self.b1.repository.revision_tree(rev.revision_id)
429
to_tree = to_tree.branch.repository.revision_tree(rev.revision_id)
431
# TODO: make sure the target tree is identical to base tree
432
# we might also check the working tree.
434
base_files = list(base_tree.list_files())
435
to_files = list(to_tree.list_files())
436
self.assertEqual(len(base_files), len(to_files))
437
for base_file, to_file in zip(base_files, to_files):
438
self.assertEqual(base_file, to_file)
440
for path, status, kind, fileid, entry in base_files:
441
# Check that the meta information is the same
442
self.assertEqual(base_tree.get_file_size(fileid),
443
to_tree.get_file_size(fileid))
444
self.assertEqual(base_tree.get_file_sha1(fileid),
445
to_tree.get_file_sha1(fileid))
446
# Check that the contents are the same
447
# This is pretty expensive
448
# self.assertEqual(base_tree.get_file(fileid).read(),
449
# to_tree.get_file(fileid).read())
451
def test_bundle(self):
452
self.tree1 = BzrDir.create_standalone_workingtree('b1')
453
self.b1 = self.tree1.branch
455
open('b1/one', 'wb').write('one\n')
456
self.tree1.add('one')
457
self.tree1.commit('add one', rev_id='a@cset-0-1')
459
bundle = self.get_valid_bundle(None, 'a@cset-0-1')
460
# FIXME: The current write_bundle api no longer supports
461
# setting a custom summary message
462
# We should re-introduce the ability, and update
463
# the tests to make sure it works.
464
# bundle = self.get_valid_bundle(None, 'a@cset-0-1',
465
# message='With a specialized message')
467
# Make sure we can handle files with spaces, tabs, other
472
, 'b1/dir/filein subdir.c'
473
, 'b1/dir/WithCaps.txt'
474
, 'b1/dir/trailing space '
477
, 'b1/sub/sub/nonempty.txt'
478
# Tabs are not valid in filenames on windows
481
open('b1/sub/sub/emptyfile.txt', 'wb').close()
482
open('b1/dir/nolastnewline.txt', 'wb').write('bloop')
483
tt = TreeTransform(self.tree1)
484
tt.new_file('executable', tt.root, '#!/bin/sh\n', 'exe-1', True)
489
, 'dir/filein subdir.c'
491
, 'dir/trailing space '
492
, 'dir/nolastnewline.txt'
495
, 'sub/sub/nonempty.txt'
496
, 'sub/sub/emptyfile.txt'
498
self.tree1.commit('add whitespace', rev_id='a@cset-0-2')
500
bundle = self.get_valid_bundle('a@cset-0-1', 'a@cset-0-2')
502
# Check a rollup bundle
503
bundle = self.get_valid_bundle(None, 'a@cset-0-2')
507
['sub/sub/nonempty.txt'
508
, 'sub/sub/emptyfile.txt'
511
tt = TreeTransform(self.tree1)
512
trans_id = tt.trans_id_tree_file_id('exe-1')
513
tt.set_executability(False, trans_id)
515
self.tree1.commit('removed', rev_id='a@cset-0-3')
517
bundle = self.get_valid_bundle('a@cset-0-2', 'a@cset-0-3')
518
self.assertRaises(TestamentMismatch, self.get_invalid_bundle,
519
'a@cset-0-2', 'a@cset-0-3')
520
# Check a rollup bundle
521
bundle = self.get_valid_bundle(None, 'a@cset-0-3')
524
# Now move the directory
525
self.tree1.rename_one('dir', 'sub/dir')
526
self.tree1.commit('rename dir', rev_id='a@cset-0-4')
528
bundle = self.get_valid_bundle('a@cset-0-3', 'a@cset-0-4')
529
# Check a rollup bundle
530
bundle = self.get_valid_bundle(None, 'a@cset-0-4')
533
open('b1/sub/dir/WithCaps.txt', 'ab').write('\nAdding some text\n')
534
open('b1/sub/dir/trailing space ', 'ab').write('\nAdding some\nDOS format lines\n')
535
open('b1/sub/dir/nolastnewline.txt', 'ab').write('\n')
536
self.tree1.rename_one('sub/dir/trailing space ',
537
'sub/ start and end space ')
538
self.tree1.commit('Modified files', rev_id='a@cset-0-5')
539
bundle = self.get_valid_bundle('a@cset-0-4', 'a@cset-0-5')
541
# Handle international characters
543
f = open(u'b1/with Dod\xe9', 'wb')
544
except UnicodeEncodeError:
545
raise TestSkipped("Filesystem doesn't support unicode")
547
u'With international man of mystery\n'
548
u'William Dod\xe9\n').encode('utf-8'))
549
self.tree1.add([u'with Dod\xe9'])
550
# BUG: (sort of) You must set verbose=False, so that python doesn't try
551
# and print the name of William Dode as part of the commit
552
self.tree1.commit(u'i18n commit from William Dod\xe9',
553
rev_id='a@cset-0-6', committer=u'William Dod\xe9',
555
bundle = self.get_valid_bundle('a@cset-0-5', 'a@cset-0-6')
556
self.tree1.rename_one('sub/dir/WithCaps.txt', 'temp')
557
self.tree1.rename_one('with space.txt', 'WithCaps.txt')
558
self.tree1.rename_one('temp', 'with space.txt')
559
self.tree1.commit(u'swap filenames', rev_id='a@cset-0-7',
561
bundle = self.get_valid_bundle('a@cset-0-6', 'a@cset-0-7')
562
other = self.get_checkout('a@cset-0-6')
563
other.rename_one('sub/dir/nolastnewline.txt', 'sub/nolastnewline.txt')
564
other.commit('rename file', rev_id='a@cset-0-7b')
565
merge([other.basedir, -1], [None, None], this_dir=self.tree1.basedir)
566
self.tree1.commit(u'Merge', rev_id='a@cset-0-8',
568
bundle = self.get_valid_bundle('a@cset-0-7', 'a@cset-0-8')
570
def test_symlink_bundle(self):
571
if not has_symlinks():
572
raise TestSkipped("No symlink support")
573
self.tree1 = BzrDir.create_standalone_workingtree('b1')
574
self.b1 = self.tree1.branch
575
tt = TreeTransform(self.tree1)
576
tt.new_symlink('link', tt.root, 'bar/foo', 'link-1')
578
self.tree1.commit('add symlink', rev_id='l@cset-0-1')
579
self.get_valid_bundle(None, 'l@cset-0-1')
580
tt = TreeTransform(self.tree1)
581
trans_id = tt.trans_id_tree_file_id('link-1')
582
tt.adjust_path('link2', tt.root, trans_id)
583
tt.delete_contents(trans_id)
584
tt.create_symlink('mars', trans_id)
586
self.tree1.commit('rename and change symlink', rev_id='l@cset-0-2')
587
self.get_valid_bundle('l@cset-0-1', 'l@cset-0-2')
588
tt = TreeTransform(self.tree1)
589
trans_id = tt.trans_id_tree_file_id('link-1')
590
tt.delete_contents(trans_id)
591
tt.create_symlink('jupiter', trans_id)
593
self.tree1.commit('just change symlink target', rev_id='l@cset-0-3')
594
self.get_valid_bundle('l@cset-0-2', 'l@cset-0-3')
595
tt = TreeTransform(self.tree1)
596
trans_id = tt.trans_id_tree_file_id('link-1')
597
tt.delete_contents(trans_id)
599
self.tree1.commit('Delete symlink', rev_id='l@cset-0-4')
600
self.get_valid_bundle('l@cset-0-3', 'l@cset-0-4')
602
def test_binary_bundle(self):
603
self.tree1 = BzrDir.create_standalone_workingtree('b1')
604
self.b1 = self.tree1.branch
605
tt = TreeTransform(self.tree1)
606
tt.new_file('file', tt.root, '\x00\xff', 'binary-1')
607
tt.new_file('file2', tt.root, '\x00\xff', 'binary-2')
609
self.tree1.commit('add binary', rev_id='b@cset-0-1')
610
self.get_valid_bundle(None, 'b@cset-0-1')
611
tt = TreeTransform(self.tree1)
612
trans_id = tt.trans_id_tree_file_id('binary-1')
613
tt.delete_contents(trans_id)
615
self.tree1.commit('delete binary', rev_id='b@cset-0-2')
616
self.get_valid_bundle('b@cset-0-1', 'b@cset-0-2')
617
tt = TreeTransform(self.tree1)
618
trans_id = tt.trans_id_tree_file_id('binary-2')
619
tt.adjust_path('file3', tt.root, trans_id)
620
tt.delete_contents(trans_id)
621
tt.create_file('filecontents\x00', trans_id)
623
self.tree1.commit('rename and modify binary', rev_id='b@cset-0-3')
624
self.get_valid_bundle('b@cset-0-2', 'b@cset-0-3')
625
tt = TreeTransform(self.tree1)
626
trans_id = tt.trans_id_tree_file_id('binary-2')
627
tt.delete_contents(trans_id)
628
tt.create_file('\x00filecontents', trans_id)
630
self.tree1.commit('just modify binary', rev_id='b@cset-0-4')
631
self.get_valid_bundle('b@cset-0-3', 'b@cset-0-4')
633
def test_last_modified(self):
634
self.tree1 = BzrDir.create_standalone_workingtree('b1')
635
self.b1 = self.tree1.branch
636
tt = TreeTransform(self.tree1)
637
tt.new_file('file', tt.root, 'file', 'file')
639
self.tree1.commit('create file', rev_id='a@lmod-0-1')
641
tt = TreeTransform(self.tree1)
642
trans_id = tt.trans_id_tree_file_id('file')
643
tt.delete_contents(trans_id)
644
tt.create_file('file2', trans_id)
646
self.tree1.commit('modify text', rev_id='a@lmod-0-2a')
648
other = self.get_checkout('a@lmod-0-1')
649
tt = TreeTransform(other)
650
trans_id = tt.trans_id_tree_file_id('file')
651
tt.delete_contents(trans_id)
652
tt.create_file('file2', trans_id)
654
other.commit('modify text in another tree', rev_id='a@lmod-0-2b')
655
merge([other.basedir, -1], [None, None], this_dir=self.tree1.basedir)
656
self.tree1.commit(u'Merge', rev_id='a@lmod-0-3',
658
self.tree1.commit(u'Merge', rev_id='a@lmod-0-4')
659
bundle = self.get_valid_bundle('a@lmod-0-2a', 'a@lmod-0-4')
661
def test_hide_history(self):
662
self.tree1 = BzrDir.create_standalone_workingtree('b1')
663
self.b1 = self.tree1.branch
665
open('b1/one', 'wb').write('one\n')
666
self.tree1.add('one')
667
self.tree1.commit('add file', rev_id='a@cset-0-1')
668
open('b1/one', 'wb').write('two\n')
669
self.tree1.commit('modify', rev_id='a@cset-0-2')
670
open('b1/one', 'wb').write('three\n')
671
self.tree1.commit('modify', rev_id='a@cset-0-3')
672
bundle_file = StringIO()
673
rev_ids = write_bundle(self.tree1.branch.repository, 'a@cset-0-3',
674
'a@cset-0-1', bundle_file)
675
self.assertNotContainsRe(bundle_file.getvalue(), 'two')
676
self.assertContainsRe(bundle_file.getvalue(), 'one')
677
self.assertContainsRe(bundle_file.getvalue(), 'three')
680
class MungedBundleTester(TestCaseWithTransport):
682
def build_test_bundle(self):
683
wt = self.make_branch_and_tree('b1')
685
self.build_tree(['b1/one'])
687
wt.commit('add one', rev_id='a@cset-0-1')
688
self.build_tree(['b1/two'])
690
wt.commit('add two', rev_id='a@cset-0-2')
692
bundle_txt = StringIO()
693
rev_ids = write_bundle(wt.branch.repository, 'a@cset-0-2',
694
'a@cset-0-1', bundle_txt)
695
self.assertEqual(['a@cset-0-2'], rev_ids)
696
bundle_txt.seek(0, 0)
699
def check_valid(self, bundle):
700
"""Check that after whatever munging, the final object is valid."""
701
self.assertEqual(['a@cset-0-2'],
702
[r.revision_id for r in bundle.real_revisions])
704
def test_extra_whitespace(self):
705
bundle_txt = self.build_test_bundle()
707
# Seek to the end of the file
708
# Adding one extra newline used to give us
709
# TypeError: float() argument must be a string or a number
710
bundle_txt.seek(0, 2)
711
bundle_txt.write('\n')
714
bundle = read_bundle(bundle_txt)
715
self.check_valid(bundle)
717
def test_extra_whitespace_2(self):
718
bundle_txt = self.build_test_bundle()
720
# Seek to the end of the file
721
# Adding two extra newlines used to give us
722
# MalformedPatches: The first line of all patches should be ...
723
bundle_txt.seek(0, 2)
724
bundle_txt.write('\n\n')
727
bundle = read_bundle(bundle_txt)
728
self.check_valid(bundle)
730
def test_missing_trailing_whitespace(self):
731
bundle_txt = self.build_test_bundle()
733
# Remove a trailing newline, it shouldn't kill the parser
734
raw = bundle_txt.getvalue()
735
# The contents of the bundle don't have to be this, but this
736
# test is concerned with the exact case where the serializer
737
# creates a blank line at the end, and fails if that
739
self.assertEqual('\n\n', raw[-2:])
740
bundle_text = StringIO(raw[:-1])
742
bundle = read_bundle(bundle_txt)
743
self.check_valid(bundle)