83
91
return "%s|%s|%s" % foreign_revid
86
class DummyForeignVcsBranch(branch.BzrBranch6,foreign.ForeignBranch):
94
class DummyForeignVcsBranch(bzrbranch.BzrBranch6,foreign.ForeignBranch):
87
95
"""A Dummy VCS Branch."""
89
def __init__(self, _format, _control_files, a_bzrdir, *args, **kwargs):
98
def user_transport(self):
99
return self.controldir.user_transport
101
def __init__(self, _format, _control_files, a_controldir, *args, **kwargs):
90
102
self._format = _format
91
self._base = a_bzrdir.transport.base
103
self._base = a_controldir.transport.base
92
104
self._ignore_fallbacks = False
93
self.bzrdir = a_bzrdir
94
foreign.ForeignBranch.__init__(self,
105
self.controldir = a_controldir
106
foreign.ForeignBranch.__init__(self,
95
107
DummyForeignVcsMapping(DummyForeignVcs()))
96
branch.BzrBranch6.__init__(self, _format, _control_files, a_bzrdir,
108
bzrbranch.BzrBranch6.__init__(self, _format, _control_files, a_controldir,
100
class InterToDummyVcsBranch(branch.GenericInterBranch,
101
foreign.InterToForeignBranch):
111
def _get_checkout_format(self, lightweight=False):
112
"""Return the most suitable metadir for a checkout of this branch.
113
Weaves are used if this branch's repository uses weaves.
115
return self.controldir.checkout_metadir()
117
def import_last_revision_info_and_tags(self, source, revno, revid,
119
interbranch = InterToDummyVcsBranch(source, self)
120
result = interbranch.push(stop_revision=revid, lossy=True)
122
revid = result.revidmap[revid]
123
return (revno, revid)
126
class DummyForeignCommitBuilder(vf_repository.VersionedFileRootCommitBuilder):
128
def _generate_revision_if_needed(self):
129
mapping = DummyForeignVcsMapping(DummyForeignVcs())
131
self._new_revision_id = mapping.revision_id_foreign_to_bzr(
132
(str(self._timestamp), str(self._timezone), "UNKNOWN"))
133
self.random_revid = False
134
elif self._new_revision_id is not None:
135
self.random_revid = False
137
self._new_revision_id = self._gen_revision_id()
138
self.random_revid = True
141
class DummyForeignVcsRepository(groupcompress_repo.CHKInventoryRepository,
142
foreign.ForeignRepository):
143
"""Dummy foreign vcs repository."""
146
class DummyForeignVcsRepositoryFormat(groupcompress_repo.RepositoryFormat2a):
148
repository_class = DummyForeignVcsRepository
149
_commit_builder_class = DummyForeignCommitBuilder
152
def get_format_string(cls):
153
return "Dummy Foreign Vcs Repository"
155
def get_format_description(self):
156
return "Dummy Foreign Vcs Repository"
159
def branch_history(graph, revid):
160
ret = list(graph.iter_lefthand_ancestry(revid,
161
(revision.NULL_REVISION,)))
166
class InterToDummyVcsBranch(branch.GenericInterBranch):
104
169
def is_compatible(source, target):
105
170
return isinstance(target, DummyForeignVcsBranch)
107
def push(self, overwrite=False, stop_revision=None):
108
raise errors.NoRoundtrippingSupport(self.source, self.target)
110
def lossy_push(self, stop_revision=None):
172
def push(self, overwrite=False, stop_revision=None, lossy=False):
174
raise errors.NoRoundtrippingSupport(self.source, self.target)
111
175
result = branch.BranchPushResult()
112
176
result.source_branch = self.source
113
177
result.target_branch = self.target
114
178
result.old_revno, result.old_revid = self.target.last_revision_info()
115
179
self.source.lock_read()
181
graph = self.source.repository.get_graph()
117
182
# This just handles simple cases, but that's good enough for tests
118
my_history = self.target.revision_history()
119
their_history = self.source.revision_history()
183
my_history = branch_history(self.target.repository.get_graph(),
185
if stop_revision is None:
186
stop_revision = self.source.last_revision()
187
their_history = branch_history(graph, stop_revision)
120
188
if their_history[:min(len(my_history), len(their_history))] != my_history:
121
189
raise errors.DivergedBranches(self.target, self.source)
122
190
todo = their_history[len(my_history):]
137
205
parent_revids = [parent_revid]
138
206
builder = self.target.get_commit_builder(parent_revids,
139
self.target.get_config(), rev.timestamp,
207
self.target.get_config_stack(), rev.timestamp,
140
208
rev.timezone, rev.committer, rev.properties,
143
for path, ie in tree.inventory.iter_entries():
211
parent_tree = self.target.repository.revision_tree(
213
for path, ie in tree.iter_entries_by_dir():
144
214
new_ie = ie.copy()
145
215
new_ie.revision = None
146
216
builder.record_entry_contents(new_ie,
147
[self.target.repository.revision_tree(parent_revid).inventory],
217
[parent_tree.root_inventory],
149
219
(ie.kind, ie.text_size, ie.executable, ie.text_sha1))
150
220
builder.finish_inventory()
166
class DummyForeignVcsBranchFormat(branch.BzrBranchFormat6):
236
class DummyForeignVcsBranchFormat(bzrbranch.BzrBranchFormat6):
168
def get_format_string(self):
239
def get_format_string(cls):
169
240
return "Branch for Testing"
172
super(DummyForeignVcsBranchFormat, self).__init__()
173
self._matchingbzrdir = DummyForeignVcsDirFormat()
243
def _matchingbzrdir(self):
244
return DummyForeignVcsDirFormat()
175
def open(self, a_bzrdir, name=None, _found=False):
246
def open(self, a_controldir, name=None, _found=False, ignore_fallbacks=False,
247
found_repository=None):
249
name = a_controldir._get_selected_branch()
177
251
raise NotImplementedError
179
transport = a_bzrdir.get_branch_transport(None, name=name)
253
transport = a_controldir.get_branch_transport(None, name=name)
180
254
control_files = lockable_files.LockableFiles(transport, 'lock',
256
if found_repository is None:
257
found_repository = a_controldir.find_repository()
182
258
return DummyForeignVcsBranch(_format=self,
183
259
_control_files=control_files,
185
_repository=a_bzrdir.find_repository())
260
a_controldir=a_controldir,
261
_repository=found_repository,
186
263
except errors.NoSuchFile:
187
264
raise errors.NotBranchError(path=transport.base)
244
318
self._control_files = lockable_files.LockableFiles(self.transport,
245
319
"lock", lockable_files.TransportLock)
247
def open_branch(self, name=None, unsupported=False, ignore_fallbacks=True):
321
def create_workingtree(self):
322
# dirstate requires a ".bzr" entry to exist
323
self.root_transport.put_bytes(".bzr", "foo")
324
return super(DummyForeignVcsDir, self).create_workingtree()
326
def open_branch(self, name=None, unsupported=False, ignore_fallbacks=True,
327
possible_transports=None):
329
name = self._get_selected_branch()
249
331
raise errors.NoColocatedBranchSupport(self)
250
332
return self._format.get_branch_format().open(self, _found=True)
252
334
def cloning_metadir(self, stacked=False):
253
335
"""Produce a metadir suitable for cloning with."""
254
return bzrdir.format_registry.make_bzrdir("default")
336
return controldir.format_registry.make_controldir("default")
338
def checkout_metadir(self):
339
return self.cloning_metadir()
256
341
def sprout(self, url, revision_id=None, force_new_repo=False,
257
342
recurse='down', possible_transports=None,
258
343
accelerator_tree=None, hardlink=False, stacked=False,
259
344
source_branch=None):
260
# dirstate doesn't cope with accelerator_trees well
345
# dirstate doesn't cope with accelerator_trees well
261
346
# that have a different control dir
262
return super(DummyForeignVcsDir, self).sprout(url=url,
263
revision_id=revision_id, force_new_repo=force_new_repo,
264
recurse=recurse, possible_transports=possible_transports,
265
hardlink=hardlink, stacked=stacked, source_branch=source_branch)
347
return super(DummyForeignVcsDir, self).sprout(
349
revision_id=revision_id, force_new_repo=force_new_repo,
350
recurse=recurse, possible_transports=possible_transports,
351
hardlink=hardlink, stacked=stacked, source_branch=source_branch)
268
354
def register_dummy_foreign_for_test(testcase):
269
bzrdir.BzrDirFormat.register_control_format(DummyForeignVcsDirFormat)
270
testcase.addCleanup(bzrdir.BzrDirFormat.unregister_control_format,
271
DummyForeignVcsDirFormat)
355
controldir.ControlDirFormat.register_prober(DummyForeignProber)
356
testcase.addCleanup(controldir.ControlDirFormat.unregister_prober,
358
repository.format_registry.register(DummyForeignVcsRepositoryFormat())
359
testcase.addCleanup(repository.format_registry.remove,
360
DummyForeignVcsRepositoryFormat())
361
branch.format_registry.register(DummyForeignVcsBranchFormat())
362
testcase.addCleanup(branch.format_registry.remove,
363
DummyForeignVcsBranchFormat())
272
364
# We need to register the optimiser to make the dummy appears really
273
365
# different from a regular bzr repository.
274
366
branch.InterBranch.register_optimiser(InterToDummyVcsBranch)
304
411
mapp = DummyForeignVcsMapping(DummyForeignVcs())
305
412
rev = foreign.ForeignRevision(("a", "foreign", "revid"),
306
413
mapp, "roundtripped-revid")
307
self.assertEquals("", rev.inventory_sha1)
308
self.assertEquals(("a", "foreign", "revid"), rev.foreign_revid)
309
self.assertEquals(mapp, rev.mapping)
414
self.assertEqual("", rev.inventory_sha1)
415
self.assertEqual(("a", "foreign", "revid"), rev.foreign_revid)
416
self.assertEqual(mapp, rev.mapping)
312
419
class WorkingTreeFileUpdateTests(tests.TestCaseWithTransport):
352
459
def test_sprout(self):
353
460
"""Test we can clone dummies and that the format is not preserved."""
354
461
self.make_branch_and_tree("d", format=DummyForeignVcsDirFormat())
355
dir = bzrdir.BzrDir.open("d")
462
dir = controldir.ControlDir.open("d")
356
463
newdir = dir.sprout("e")
357
self.assertNotEquals("A Dummy VCS Dir",
358
newdir._format.get_format_string())
464
self.assertNotEqual("A Dummy VCS Dir",
465
newdir._format.get_format_string())
360
467
def test_push_not_supported(self):
361
468
source_tree = self.make_branch_and_tree("source")
362
target_tree = self.make_branch_and_tree("target",
363
format=DummyForeignVcsDirFormat())
364
self.assertRaises(errors.NoRoundtrippingSupport,
365
source_tree.branch.push, target_tree.branch)
469
target_tree = self.make_branch_and_tree(
470
"target", format=DummyForeignVcsDirFormat())
471
self.assertRaises(errors.NoRoundtrippingSupport,
472
source_tree.branch.push, target_tree.branch)
367
474
def test_lossy_push_empty(self):
368
475
source_tree = self.make_branch_and_tree("source")
369
target_tree = self.make_branch_and_tree("target",
370
format=DummyForeignVcsDirFormat())
371
pushresult = source_tree.branch.lossy_push(target_tree.branch)
372
self.assertEquals(revision.NULL_REVISION, pushresult.old_revid)
373
self.assertEquals(revision.NULL_REVISION, pushresult.new_revid)
374
self.assertEquals({}, pushresult.revidmap)
476
target_tree = self.make_branch_and_tree(
477
"target", format=DummyForeignVcsDirFormat())
478
pushresult = source_tree.branch.push(target_tree.branch, lossy=True)
479
self.assertEqual(revision.NULL_REVISION, pushresult.old_revid)
480
self.assertEqual(revision.NULL_REVISION, pushresult.new_revid)
481
self.assertEqual({}, pushresult.revidmap)
376
483
def test_lossy_push_simple(self):
377
484
source_tree = self.make_branch_and_tree("source")
378
485
self.build_tree(['source/a', 'source/b'])
379
486
source_tree.add(['a', 'b'])
380
487
revid1 = source_tree.commit("msg")
381
target_tree = self.make_branch_and_tree("target",
382
format=DummyForeignVcsDirFormat())
488
target_tree = self.make_branch_and_tree(
489
"target", format=DummyForeignVcsDirFormat())
383
490
target_tree.branch.lock_write()
385
pushresult = source_tree.branch.lossy_push(target_tree.branch)
492
pushresult = source_tree.branch.push(
493
target_tree.branch, lossy=True)
387
495
target_tree.branch.unlock()
388
self.assertEquals(revision.NULL_REVISION, pushresult.old_revid)
389
self.assertEquals({revid1:target_tree.branch.last_revision()},
391
self.assertEquals(pushresult.revidmap[revid1], pushresult.new_revid)
496
self.assertEqual(revision.NULL_REVISION, pushresult.old_revid)
497
self.assertEqual({revid1: target_tree.branch.last_revision()},
499
self.assertEqual(pushresult.revidmap[revid1], pushresult.new_revid)