84
79
self.mapping_registry = DummyForeignVcsMappingRegistry()
85
80
self.mapping_registry.register("v1", DummyForeignVcsMapping(self),
82
self.abbreviation = "dummy"
88
84
def show_foreign_revid(self, foreign_revid):
89
85
return { "dummy ding": "%s/%s\\%s" % foreign_revid }
87
def serialize_foreign_revid(self, foreign_revid):
88
return "%s|%s|%s" % foreign_revid
92
91
class DummyForeignVcsBranch(branch.BzrBranch6,foreign.ForeignBranch):
93
92
"""A Dummy VCS Branch."""
95
def user_transport(self):
96
return self.bzrdir.user_transport
95
98
def __init__(self, _format, _control_files, a_bzrdir, *args, **kwargs):
96
99
self._format = _format
97
100
self._base = a_bzrdir.transport.base
98
101
self._ignore_fallbacks = False
99
foreign.ForeignBranch.__init__(self,
102
self.bzrdir = a_bzrdir
103
foreign.ForeignBranch.__init__(self,
100
104
DummyForeignVcsMapping(DummyForeignVcs()))
101
branch.BzrBranch6.__init__(self, _format, _control_files, a_bzrdir,
105
branch.BzrBranch6.__init__(self, _format, _control_files, a_bzrdir,
104
def dpull(self, source, stop_revision=None):
108
def _get_checkout_format(self, lightweight=False):
109
"""Return the most suitable metadir for a checkout of this branch.
110
Weaves are used if this branch's repository uses weaves.
112
return self.bzrdir.checkout_metadir()
114
def import_last_revision_info_and_tags(self, source, revno, revid,
116
interbranch = InterToDummyVcsBranch(source, self)
117
result = interbranch.push(stop_revision=revid, lossy=True)
119
revid = result.revidmap[revid]
120
return (revno, revid)
123
class DummyForeignCommitBuilder(vf_repository.VersionedFileRootCommitBuilder):
125
def _generate_revision_if_needed(self):
126
mapping = DummyForeignVcsMapping(DummyForeignVcs())
128
self._new_revision_id = mapping.revision_id_foreign_to_bzr(
129
(str(self._timestamp), str(self._timezone), "UNKNOWN"))
130
self.random_revid = False
131
elif self._new_revision_id is not None:
132
self.random_revid = False
134
self._new_revision_id = self._gen_revision_id()
135
self.random_revid = True
138
class DummyForeignVcsRepository(groupcompress_repo.CHKInventoryRepository,
139
foreign.ForeignRepository):
140
"""Dummy foreign vcs repository."""
143
class DummyForeignVcsRepositoryFormat(groupcompress_repo.RepositoryFormat2a):
145
repository_class = DummyForeignVcsRepository
146
_commit_builder_class = DummyForeignCommitBuilder
149
def get_format_string(cls):
150
return "Dummy Foreign Vcs Repository"
152
def get_format_description(self):
153
return "Dummy Foreign Vcs Repository"
156
def branch_history(graph, revid):
157
ret = list(graph.iter_lefthand_ancestry(revid,
158
(revision.NULL_REVISION,)))
163
class InterToDummyVcsBranch(branch.GenericInterBranch):
166
def is_compatible(source, target):
167
return isinstance(target, DummyForeignVcsBranch)
169
def push(self, overwrite=False, stop_revision=None, lossy=False):
171
raise errors.NoRoundtrippingSupport(self.source, self.target)
172
result = branch.BranchPushResult()
173
result.source_branch = self.source
174
result.target_branch = self.target
175
result.old_revno, result.old_revid = self.target.last_revision_info()
176
self.source.lock_read()
178
graph = self.source.repository.get_graph()
107
179
# This just handles simple cases, but that's good enough for tests
108
my_history = self.revision_history()
109
their_history = source.revision_history()
180
my_history = branch_history(self.target.repository.get_graph(),
182
if stop_revision is None:
183
stop_revision = self.source.last_revision()
184
their_history = branch_history(graph, stop_revision)
110
185
if their_history[:min(len(my_history), len(their_history))] != my_history:
111
raise errors.DivergedBranches(self, source)
186
raise errors.DivergedBranches(self.target, self.source)
112
187
todo = their_history[len(my_history):]
114
189
for revid in todo:
115
rev = source.repository.get_revision(revid)
116
tree = source.repository.revision_tree(revid)
190
rev = self.source.repository.get_revision(revid)
191
tree = self.source.repository.revision_tree(revid)
117
192
def get_file_with_stat(file_id, path=None):
118
193
return (tree.get_file(file_id), None)
119
194
tree.get_file_with_stat = get_file_with_stat
120
new_revid = self.mapping.revision_id_foreign_to_bzr(
121
(str(rev.timestamp), str(rev.timezone), str(self.revno())))
122
parent_revno, parent_revid= self.last_revision_info()
123
builder = self.get_commit_builder([parent_revid],
124
self.get_config(), rev.timestamp,
195
new_revid = self.target.mapping.revision_id_foreign_to_bzr(
196
(str(rev.timestamp), str(rev.timezone),
197
str(self.target.revno())))
198
parent_revno, parent_revid= self.target.last_revision_info()
199
if parent_revid == revision.NULL_REVISION:
202
parent_revids = [parent_revid]
203
builder = self.target.get_commit_builder(parent_revids,
204
self.target.get_config_stack(), rev.timestamp,
125
205
rev.timezone, rev.committer, rev.properties,
128
for path, ie in tree.inventory.iter_entries():
208
parent_tree = self.target.repository.revision_tree(
210
for path, ie in tree.iter_entries_by_dir():
129
211
new_ie = ie.copy()
130
212
new_ie.revision = None
131
213
builder.record_entry_contents(new_ie,
132
[self.repository.get_inventory(parent_revid)],
214
[parent_tree.root_inventory],
134
216
(ie.kind, ie.text_size, ie.executable, ie.text_sha1))
135
217
builder.finish_inventory()
139
221
revidmap[revid] = builder.commit(rev.message)
140
self.set_last_revision_info(parent_revno+1, revidmap[revid])
222
self.target.set_last_revision_info(parent_revno+1,
141
224
trace.mutter('lossily pushed revision %s -> %s',
142
225
revid, revidmap[revid])
228
result.new_revno, result.new_revid = self.target.last_revision_info()
229
result.revidmap = revidmap
148
233
class DummyForeignVcsBranchFormat(branch.BzrBranchFormat6):
150
def get_format_string(self):
236
def get_format_string(cls):
151
237
return "Branch for Testing"
154
super(DummyForeignVcsBranchFormat, self).__init__()
155
self._matchingbzrdir = DummyForeignVcsDirFormat()
240
def _matchingbzrdir(self):
241
return DummyForeignVcsDirFormat()
157
def open(self, a_bzrdir, _found=False):
243
def open(self, a_bzrdir, name=None, _found=False, ignore_fallbacks=False,
244
found_repository=None):
246
name = a_bzrdir._get_selected_branch()
159
248
raise NotImplementedError
161
transport = a_bzrdir.get_branch_transport(None)
250
transport = a_bzrdir.get_branch_transport(None, name=name)
162
251
control_files = lockable_files.LockableFiles(transport, 'lock',
253
if found_repository is None:
254
found_repository = a_bzrdir.find_repository()
164
255
return DummyForeignVcsBranch(_format=self,
165
256
_control_files=control_files,
166
257
a_bzrdir=a_bzrdir,
167
_repository=a_bzrdir.find_repository())
258
_repository=found_repository,
168
260
except errors.NoSuchFile:
169
261
raise errors.NotBranchError(path=transport.base)
172
class DummyForeignVcsDirFormat(BzrDirMetaFormat1):
264
class DummyForeignVcsDirFormat(bzrdir.BzrDirMetaFormat1):
173
265
"""BzrDirFormat for the dummy foreign VCS."""
226
315
self._control_files = lockable_files.LockableFiles(self.transport,
227
316
"lock", lockable_files.TransportLock)
229
def open_branch(self, ignore_fallbacks=True):
318
def create_workingtree(self):
319
# dirstate requires a ".bzr" entry to exist
320
self.root_transport.put_bytes(".bzr", "foo")
321
return super(DummyForeignVcsDir, self).create_workingtree()
323
def open_branch(self, name=None, unsupported=False, ignore_fallbacks=True,
324
possible_transports=None):
326
name = self._get_selected_branch()
328
raise errors.NoColocatedBranchSupport(self)
230
329
return self._format.get_branch_format().open(self, _found=True)
232
331
def cloning_metadir(self, stacked=False):
233
332
"""Produce a metadir suitable for cloning with."""
234
return format_registry.make_bzrdir("default")
333
return controldir.format_registry.make_bzrdir("default")
335
def checkout_metadir(self):
336
return self.cloning_metadir()
236
338
def sprout(self, url, revision_id=None, force_new_repo=False,
237
339
recurse='down', possible_transports=None,
245
347
hardlink=hardlink, stacked=stacked, source_branch=source_branch)
248
class ForeignVcsRegistryTests(TestCase):
350
def register_dummy_foreign_for_test(testcase):
351
controldir.ControlDirFormat.register_prober(DummyForeignProber)
352
testcase.addCleanup(controldir.ControlDirFormat.unregister_prober,
354
repository.format_registry.register(DummyForeignVcsRepositoryFormat())
355
testcase.addCleanup(repository.format_registry.remove,
356
DummyForeignVcsRepositoryFormat())
357
branch.format_registry.register(DummyForeignVcsBranchFormat())
358
testcase.addCleanup(branch.format_registry.remove,
359
DummyForeignVcsBranchFormat())
360
# We need to register the optimiser to make the dummy appears really
361
# different from a regular bzr repository.
362
branch.InterBranch.register_optimiser(InterToDummyVcsBranch)
363
testcase.addCleanup(branch.InterBranch.unregister_optimiser,
364
InterToDummyVcsBranch)
367
class DummyForeignProber(controldir.Prober):
370
def probe_transport(klass, transport):
371
"""Return the .bzrdir style format present in a directory."""
372
if not transport.has('.dummy'):
373
raise errors.NotBranchError(path=transport.base)
374
return DummyForeignVcsDirFormat()
377
def known_formats(cls):
378
return set([DummyForeignVcsDirFormat()])
381
class ForeignVcsRegistryTests(tests.TestCase):
249
382
"""Tests for the ForeignVcsRegistry class."""
251
384
def test_parse_revision_id_no_dash(self):
278
412
self.assertEquals(mapp, rev.mapping)
281
class ShowForeignPropertiesTests(TestCase):
282
"""Tests for the show_foreign_properties() function."""
285
super(ShowForeignPropertiesTests, self).setUp()
286
self.vcs = DummyForeignVcs()
287
foreign.foreign_vcs_registry.register("dummy",
288
self.vcs, "Dummy VCS")
291
super(ShowForeignPropertiesTests, self).tearDown()
292
foreign.foreign_vcs_registry.remove("dummy")
294
def test_show_non_foreign(self):
295
"""Test use with a native (non-foreign) bzr revision."""
296
self.assertEquals({}, foreign.show_foreign_properties(Revision("arevid")))
298
def test_show_imported(self):
299
rev = Revision("dummy-v1:my-foreign-revid")
300
self.assertEquals({ "dummy ding": "my/foreign\\revid" },
301
foreign.show_foreign_properties(rev))
303
def test_show_direct(self):
304
rev = foreign.ForeignRevision(("some", "foreign", "revid"),
305
DummyForeignVcsMapping(self.vcs),
307
self.assertEquals({ "dummy ding": "some/foreign\\revid" },
308
foreign.show_foreign_properties(rev))
311
class WorkingTreeFileUpdateTests(TestCaseWithTransport):
415
class WorkingTreeFileUpdateTests(tests.TestCaseWithTransport):
312
416
"""Tests for update_workingtree_fileids()."""
314
418
def test_update_workingtree(self):
357
455
def test_sprout(self):
358
456
"""Test we can clone dummies and that the format is not preserved."""
359
457
self.make_branch_and_tree("d", format=DummyForeignVcsDirFormat())
360
dir = BzrDir.open("d")
458
dir = controldir.ControlDir.open("d")
361
459
newdir = dir.sprout("e")
362
self.assertNotEquals("A Dummy VCS Dir", newdir._format.get_format_string())
460
self.assertNotEquals("A Dummy VCS Dir",
461
newdir._format.get_format_string())
463
def test_push_not_supported(self):
464
source_tree = self.make_branch_and_tree("source")
465
target_tree = self.make_branch_and_tree("target",
466
format=DummyForeignVcsDirFormat())
467
self.assertRaises(errors.NoRoundtrippingSupport,
468
source_tree.branch.push, target_tree.branch)
470
def test_lossy_push_empty(self):
471
source_tree = self.make_branch_and_tree("source")
472
target_tree = self.make_branch_and_tree("target",
473
format=DummyForeignVcsDirFormat())
474
pushresult = source_tree.branch.push(target_tree.branch, lossy=True)
475
self.assertEquals(revision.NULL_REVISION, pushresult.old_revid)
476
self.assertEquals(revision.NULL_REVISION, pushresult.new_revid)
477
self.assertEquals({}, pushresult.revidmap)
479
def test_lossy_push_simple(self):
480
source_tree = self.make_branch_and_tree("source")
481
self.build_tree(['source/a', 'source/b'])
482
source_tree.add(['a', 'b'])
483
revid1 = source_tree.commit("msg")
484
target_tree = self.make_branch_and_tree("target",
485
format=DummyForeignVcsDirFormat())
486
target_tree.branch.lock_write()
488
pushresult = source_tree.branch.push(
489
target_tree.branch, lossy=True)
491
target_tree.branch.unlock()
492
self.assertEquals(revision.NULL_REVISION, pushresult.old_revid)
493
self.assertEquals({revid1:target_tree.branch.last_revision()},
495
self.assertEquals(pushresult.revidmap[revid1], pushresult.new_revid)