83
89
return "%s|%s|%s" % foreign_revid
86
class DummyForeignVcsBranch(branch.BzrBranch6,foreign.ForeignBranch):
92
class DummyForeignVcsBranch(bzrbranch.BzrBranch6,foreign.ForeignBranch):
87
93
"""A Dummy VCS Branch."""
96
def user_transport(self):
97
return self.bzrdir.user_transport
89
99
def __init__(self, _format, _control_files, a_bzrdir, *args, **kwargs):
90
100
self._format = _format
91
101
self._base = a_bzrdir.transport.base
92
102
self._ignore_fallbacks = False
93
103
self.bzrdir = a_bzrdir
94
foreign.ForeignBranch.__init__(self,
104
foreign.ForeignBranch.__init__(self,
95
105
DummyForeignVcsMapping(DummyForeignVcs()))
96
branch.BzrBranch6.__init__(self, _format, _control_files, a_bzrdir,
106
bzrbranch.BzrBranch6.__init__(self, _format, _control_files, a_bzrdir,
100
class InterToDummyVcsBranch(branch.GenericInterBranch,
101
foreign.InterToForeignBranch):
109
def _get_checkout_format(self, lightweight=False):
110
"""Return the most suitable metadir for a checkout of this branch.
111
Weaves are used if this branch's repository uses weaves.
113
return self.bzrdir.checkout_metadir()
115
def import_last_revision_info_and_tags(self, source, revno, revid,
117
interbranch = InterToDummyVcsBranch(source, self)
118
result = interbranch.push(stop_revision=revid, lossy=True)
120
revid = result.revidmap[revid]
121
return (revno, revid)
124
class DummyForeignCommitBuilder(vf_repository.VersionedFileRootCommitBuilder):
126
def _generate_revision_if_needed(self):
127
mapping = DummyForeignVcsMapping(DummyForeignVcs())
129
self._new_revision_id = mapping.revision_id_foreign_to_bzr(
130
(str(self._timestamp), str(self._timezone), "UNKNOWN"))
131
self.random_revid = False
132
elif self._new_revision_id is not None:
133
self.random_revid = False
135
self._new_revision_id = self._gen_revision_id()
136
self.random_revid = True
139
class DummyForeignVcsRepository(groupcompress_repo.CHKInventoryRepository,
140
foreign.ForeignRepository):
141
"""Dummy foreign vcs repository."""
144
class DummyForeignVcsRepositoryFormat(groupcompress_repo.RepositoryFormat2a):
146
repository_class = DummyForeignVcsRepository
147
_commit_builder_class = DummyForeignCommitBuilder
150
def get_format_string(cls):
151
return "Dummy Foreign Vcs Repository"
153
def get_format_description(self):
154
return "Dummy Foreign Vcs Repository"
157
def branch_history(graph, revid):
158
ret = list(graph.iter_lefthand_ancestry(revid,
159
(revision.NULL_REVISION,)))
164
class InterToDummyVcsBranch(branch.GenericInterBranch):
104
167
def is_compatible(source, target):
105
168
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):
170
def push(self, overwrite=False, stop_revision=None, lossy=False):
172
raise errors.NoRoundtrippingSupport(self.source, self.target)
111
173
result = branch.BranchPushResult()
112
174
result.source_branch = self.source
113
175
result.target_branch = self.target
114
176
result.old_revno, result.old_revid = self.target.last_revision_info()
115
177
self.source.lock_read()
179
graph = self.source.repository.get_graph()
117
180
# 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()
181
my_history = branch_history(self.target.repository.get_graph(),
183
if stop_revision is None:
184
stop_revision = self.source.last_revision()
185
their_history = branch_history(graph, stop_revision)
120
186
if their_history[:min(len(my_history), len(their_history))] != my_history:
121
187
raise errors.DivergedBranches(self.target, self.source)
122
188
todo = their_history[len(my_history):]
137
203
parent_revids = [parent_revid]
138
204
builder = self.target.get_commit_builder(parent_revids,
139
self.target.get_config(), rev.timestamp,
205
self.target.get_config_stack(), rev.timestamp,
140
206
rev.timezone, rev.committer, rev.properties,
143
for path, ie in tree.inventory.iter_entries():
209
parent_tree = self.target.repository.revision_tree(
211
for path, ie in tree.iter_entries_by_dir():
144
212
new_ie = ie.copy()
145
213
new_ie.revision = None
146
214
builder.record_entry_contents(new_ie,
147
[self.target.repository.revision_tree(parent_revid).inventory],
215
[parent_tree.root_inventory],
149
217
(ie.kind, ie.text_size, ie.executable, ie.text_sha1))
150
218
builder.finish_inventory()
166
class DummyForeignVcsBranchFormat(branch.BzrBranchFormat6):
234
class DummyForeignVcsBranchFormat(bzrbranch.BzrBranchFormat6):
168
def get_format_string(self):
237
def get_format_string(cls):
169
238
return "Branch for Testing"
172
super(DummyForeignVcsBranchFormat, self).__init__()
173
self._matchingbzrdir = DummyForeignVcsDirFormat()
241
def _matchingbzrdir(self):
242
return DummyForeignVcsDirFormat()
175
def open(self, a_bzrdir, name=None, _found=False):
244
def open(self, a_bzrdir, name=None, _found=False, ignore_fallbacks=False,
245
found_repository=None):
247
name = a_bzrdir._get_selected_branch()
177
249
raise NotImplementedError
179
251
transport = a_bzrdir.get_branch_transport(None, name=name)
180
252
control_files = lockable_files.LockableFiles(transport, 'lock',
254
if found_repository is None:
255
found_repository = a_bzrdir.find_repository()
182
256
return DummyForeignVcsBranch(_format=self,
183
257
_control_files=control_files,
184
258
a_bzrdir=a_bzrdir,
185
_repository=a_bzrdir.find_repository())
259
_repository=found_repository,
186
261
except errors.NoSuchFile:
187
262
raise errors.NotBranchError(path=transport.base)
244
316
self._control_files = lockable_files.LockableFiles(self.transport,
245
317
"lock", lockable_files.TransportLock)
247
def open_branch(self, name=None, unsupported=False, ignore_fallbacks=True):
319
def create_workingtree(self):
320
# dirstate requires a ".bzr" entry to exist
321
self.root_transport.put_bytes(".bzr", "foo")
322
return super(DummyForeignVcsDir, self).create_workingtree()
324
def open_branch(self, name=None, unsupported=False, ignore_fallbacks=True,
325
possible_transports=None):
327
name = self._get_selected_branch()
249
329
raise errors.NoColocatedBranchSupport(self)
250
330
return self._format.get_branch_format().open(self, _found=True)
252
332
def cloning_metadir(self, stacked=False):
253
333
"""Produce a metadir suitable for cloning with."""
254
return bzrdir.format_registry.make_bzrdir("default")
334
return controldir.format_registry.make_bzrdir("default")
336
def checkout_metadir(self):
337
return self.cloning_metadir()
256
339
def sprout(self, url, revision_id=None, force_new_repo=False,
257
340
recurse='down', possible_transports=None,
258
341
accelerator_tree=None, hardlink=False, stacked=False,
259
342
source_branch=None):
260
# dirstate doesn't cope with accelerator_trees well
343
# dirstate doesn't cope with accelerator_trees well
261
344
# 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)
345
return super(DummyForeignVcsDir, self).sprout(
347
revision_id=revision_id, force_new_repo=force_new_repo,
348
recurse=recurse, possible_transports=possible_transports,
349
hardlink=hardlink, stacked=stacked, source_branch=source_branch)
268
352
def register_dummy_foreign_for_test(testcase):
269
bzrdir.BzrDirFormat.register_control_format(DummyForeignVcsDirFormat)
270
testcase.addCleanup(bzrdir.BzrDirFormat.unregister_control_format,
271
DummyForeignVcsDirFormat)
353
controldir.ControlDirFormat.register_prober(DummyForeignProber)
354
testcase.addCleanup(controldir.ControlDirFormat.unregister_prober,
356
repository.format_registry.register(DummyForeignVcsRepositoryFormat())
357
testcase.addCleanup(repository.format_registry.remove,
358
DummyForeignVcsRepositoryFormat())
359
branch.format_registry.register(DummyForeignVcsBranchFormat())
360
testcase.addCleanup(branch.format_registry.remove,
361
DummyForeignVcsBranchFormat())
272
362
# We need to register the optimiser to make the dummy appears really
273
363
# different from a regular bzr repository.
274
364
branch.InterBranch.register_optimiser(InterToDummyVcsBranch)
304
409
mapp = DummyForeignVcsMapping(DummyForeignVcs())
305
410
rev = foreign.ForeignRevision(("a", "foreign", "revid"),
306
411
mapp, "roundtripped-revid")
307
self.assertEquals("", rev.inventory_sha1)
308
self.assertEquals(("a", "foreign", "revid"), rev.foreign_revid)
309
self.assertEquals(mapp, rev.mapping)
412
self.assertEqual("", rev.inventory_sha1)
413
self.assertEqual(("a", "foreign", "revid"), rev.foreign_revid)
414
self.assertEqual(mapp, rev.mapping)
312
417
class WorkingTreeFileUpdateTests(tests.TestCaseWithTransport):
352
457
def test_sprout(self):
353
458
"""Test we can clone dummies and that the format is not preserved."""
354
459
self.make_branch_and_tree("d", format=DummyForeignVcsDirFormat())
355
dir = bzrdir.BzrDir.open("d")
460
dir = controldir.ControlDir.open("d")
356
461
newdir = dir.sprout("e")
357
self.assertNotEquals("A Dummy VCS Dir",
358
newdir._format.get_format_string())
462
self.assertNotEqual("A Dummy VCS Dir",
463
newdir._format.get_format_string())
360
465
def test_push_not_supported(self):
361
466
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)
467
target_tree = self.make_branch_and_tree(
468
"target", format=DummyForeignVcsDirFormat())
469
self.assertRaises(errors.NoRoundtrippingSupport,
470
source_tree.branch.push, target_tree.branch)
367
472
def test_lossy_push_empty(self):
368
473
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)
474
target_tree = self.make_branch_and_tree(
475
"target", format=DummyForeignVcsDirFormat())
476
pushresult = source_tree.branch.push(target_tree.branch, lossy=True)
477
self.assertEqual(revision.NULL_REVISION, pushresult.old_revid)
478
self.assertEqual(revision.NULL_REVISION, pushresult.new_revid)
479
self.assertEqual({}, pushresult.revidmap)
376
481
def test_lossy_push_simple(self):
377
482
source_tree = self.make_branch_and_tree("source")
378
483
self.build_tree(['source/a', 'source/b'])
379
484
source_tree.add(['a', 'b'])
380
485
revid1 = source_tree.commit("msg")
381
target_tree = self.make_branch_and_tree("target",
382
format=DummyForeignVcsDirFormat())
486
target_tree = self.make_branch_and_tree(
487
"target", format=DummyForeignVcsDirFormat())
383
488
target_tree.branch.lock_write()
385
pushresult = source_tree.branch.lossy_push(target_tree.branch)
490
pushresult = source_tree.branch.push(
491
target_tree.branch, lossy=True)
387
493
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)
494
self.assertEqual(revision.NULL_REVISION, pushresult.old_revid)
495
self.assertEqual({revid1: target_tree.branch.last_revision()},
497
self.assertEqual(pushresult.revidmap[revid1], pushresult.new_revid)