1
# Copyright (C) 2007 Canonical Ltd
2
# Copyright (C) 2009 Jelmer Vernooij <jelmer@samba.org>
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
"""An adapter between a Git Branch and a Bazaar Branch"""
20
from dulwich.objects import (
35
from bzrlib.decorators import (
38
from bzrlib.trace import (
43
from bzrlib.plugins.git import (
46
from bzrlib.plugins.git.config import (
49
from bzrlib.plugins.git.errors import (
54
from bzrlib.foreign import ForeignBranch
57
def extract_tags(refs):
59
for k,v in refs.iteritems():
60
if k.startswith("refs/tags/") and not k.endswith("^{}"):
61
v = refs.get(k+"^{}", v)
62
ret[k[len("refs/tags/"):]] = v
66
class GitPullResult(branch.PullResult):
68
def _lookup_revno(self, revid):
69
assert isinstance(revid, str), "was %r" % revid
70
# Try in source branch first, it'll be faster
71
return self.target_branch.revision_id_to_revno(revid)
75
return self._lookup_revno(self.old_revid)
79
return self._lookup_revno(self.new_revid)
82
class LocalGitTagDict(tag.BasicTags):
83
"""Dictionary with tags in a local repository."""
85
def __init__(self, branch):
87
self.repository = branch.repository
89
def get_tag_dict(self):
91
for k,v in extract_tags(self.repository._git.get_refs()).iteritems():
93
obj = self.repository._git[v]
95
mutter("Tag %s points at unknown object %s, ignoring", v, obj)
97
while isinstance(obj, Tag):
99
obj = self.repository._git[v]
100
if not isinstance(obj, Commit):
101
mutter("Tag %s points at object %r that is not a commit, "
104
ret[k] = self.branch.mapping.revision_id_foreign_to_bzr(v)
107
def _set_tag_dict(self, to_dict):
108
extra = set(self.repository._git.get_refs().keys())
109
for k, revid in to_dict.iteritems():
110
name = "refs/tags/%s" % k
113
self.set_tag(k, revid)
115
if name.startswith("refs/tags/"):
116
del self.repository._git[name]
118
def set_tag(self, name, revid):
119
self.repository._git.refs["refs/tags/%s" % name], _ = \
120
self.branch.mapping.revision_id_bzr_to_foreign(revid)
123
class DictTagDict(LocalGitTagDict):
126
def __init__(self, branch, tags):
127
super(DictTagDict, self).__init__(branch)
130
def get_tag_dict(self):
135
class GitBranchFormat(branch.BranchFormat):
137
def get_format_description(self):
140
def network_name(self):
143
def supports_tags(self):
146
def get_foreign_tests_branch_factory(self):
147
from bzrlib.plugins.git.tests.test_branch import ForeignTestsBranchFactory
148
return ForeignTestsBranchFactory()
150
def make_tags(self, branch):
151
if getattr(branch.repository, "get_refs", None) is not None:
152
from bzrlib.plugins.git.remote import RemoteGitTagDict
153
return RemoteGitTagDict(branch)
155
return LocalGitTagDict(branch)
158
class GitBranch(ForeignBranch):
159
"""An adapter to git repositories for bzr Branch objects."""
161
def __init__(self, bzrdir, repository, name, lockfiles, tagsdict=None):
162
self.repository = repository
163
self._format = GitBranchFormat()
164
self.control_files = lockfiles
166
super(GitBranch, self).__init__(repository.get_mapping())
167
if tagsdict is not None:
168
self.tags = DictTagDict(self, tagsdict)
171
self.base = bzrdir.root_transport.base
172
if self.name != "HEAD":
173
self.base += ",%s" % self.name
175
def _get_checkout_format(self):
176
"""Return the most suitable metadir for a checkout of this branch.
177
Weaves are used if this branch's repository uses weaves.
179
return get_rich_root_format()
181
def get_child_submit_format(self):
182
"""Return the preferred format of submissions to this branch."""
183
ret = self.get_config().get_user_option("child_submit_format")
188
def _get_nick(self, local=False, possible_master_transports=None):
189
"""Find the nick name for this branch.
195
def _set_nick(self, nick):
196
raise NotImplementedError
198
nick = property(_get_nick, _set_nick)
201
return "%s(%r, %r)" % (self.__class__.__name__, self.repository.base, self.name)
203
def generate_revision_history(self, revid, old_revid=None):
204
# FIXME: Check that old_revid is in the ancestry of revid
205
newhead, self.mapping = self.mapping.revision_id_bzr_to_foreign(revid)
206
self._set_head(newhead)
208
def lock_write(self):
209
self.control_files.lock_write()
211
def get_stacked_on_url(self):
212
# Git doesn't do stacking (yet...)
213
raise errors.UnstackableBranchFormat(self._format, self.base)
215
def get_parent(self):
216
"""See Branch.get_parent()."""
217
# FIXME: Set "origin" url from .git/config ?
220
def set_parent(self, url):
221
# FIXME: Set "origin" url in .git/config ?
225
self.control_files.lock_read()
228
return self.control_files.is_locked()
231
self.control_files.unlock()
233
def get_physical_lock_status(self):
237
def last_revision(self):
238
# perhaps should escape this ?
239
if self.head is None:
240
return revision.NULL_REVISION
241
return self.mapping.revision_id_foreign_to_bzr(self.head)
243
def _basic_push(self, target, overwrite=False, stop_revision=None):
244
return branch.InterBranch.get(self, target)._basic_push(
245
overwrite, stop_revision)
248
class LocalGitBranch(GitBranch):
249
"""A local Git branch."""
251
def create_checkout(self, to_location, revision_id=None, lightweight=False,
252
accelerator_tree=None, hardlink=False):
254
t = transport.get_transport(to_location)
256
format = self._get_checkout_format()
257
checkout = format.initialize_on_transport(t)
258
from_branch = branch.BranchReferenceFormat().initialize(checkout,
260
tree = checkout.create_workingtree(revision_id,
261
from_branch=from_branch, hardlink=hardlink)
264
return self._create_heavyweight_checkout(to_location, revision_id,
267
def _create_heavyweight_checkout(self, to_location, revision_id=None,
269
"""Create a new heavyweight checkout of this branch.
271
:param to_location: URL of location to create the new checkout in.
272
:param revision_id: Revision that should be the tip of the checkout.
273
:param hardlink: Whether to hardlink
274
:return: WorkingTree object of checkout.
276
checkout_branch = bzrdir.BzrDir.create_branch_convenience(
277
to_location, force_new_tree=False, format=get_rich_root_format())
278
checkout = checkout_branch.bzrdir
279
checkout_branch.bind(self)
280
# pull up to the specified revision_id to set the initial
281
# branch tip correctly, and seed it with history.
282
checkout_branch.pull(self, stop_revision=revision_id)
283
return checkout.create_workingtree(revision_id, hardlink=hardlink)
285
def _gen_revision_history(self):
286
if self.head is None:
288
ret = list(self.repository.iter_reverse_revision_history(
289
self.last_revision()))
295
return self.repository._git.ref(self.name)
299
def set_last_revision_info(self, revno, revid):
300
self.set_last_revision(revid)
302
def set_last_revision(self, revid):
303
(newhead, self.mapping) = self.mapping.revision_id_bzr_to_foreign(
307
def _set_head(self, value):
309
self.repository._git.refs[self.name] = self._head
310
self._clear_cached_state()
312
head = property(_get_head, _set_head)
314
def get_config(self):
315
return GitBranchConfig(self)
317
def get_push_location(self):
318
"""See Branch.get_push_location."""
319
push_loc = self.get_config().get_user_option('push_location')
322
def set_push_location(self, location):
323
"""See Branch.set_push_location."""
324
self.get_config().set_user_option('push_location', location,
325
store=config.STORE_LOCATION)
327
def supports_tags(self):
331
class GitBranchPullResult(branch.PullResult):
333
def report(self, to_file):
335
if self.old_revid == self.new_revid:
336
to_file.write('No revisions to pull.\n')
337
elif self.new_git_head is not None:
338
to_file.write('Now on revision %d (git sha: %s).\n' %
339
(self.new_revno, self.new_git_head))
341
to_file.write('Now on revision %d.\n' % (self.new_revno,))
342
self._show_tag_conficts(to_file)
345
class GitBranchPushResult(branch.BranchPushResult):
347
def _lookup_revno(self, revid):
348
assert isinstance(revid, str), "was %r" % revid
349
# Try in source branch first, it'll be faster
351
return self.source_branch.revision_id_to_revno(revid)
352
except errors.NoSuchRevision:
353
# FIXME: Check using graph.find_distance_to_null() ?
354
return self.target_branch.revision_id_to_revno(revid)
358
return self._lookup_revno(self.old_revid)
362
return self._lookup_revno(self.new_revid)
365
class InterFromGitBranch(branch.GenericInterBranch):
366
"""InterBranch implementation that pulls from Git into bzr."""
369
def _get_interrepo(self, source, target):
370
return repository.InterRepository.get(source.repository,
374
def is_compatible(cls, source, target):
375
return (isinstance(source, GitBranch) and
376
not isinstance(target, GitBranch) and
377
(getattr(cls._get_interrepo(source, target), "fetch_objects", None) is not None))
379
def _update_revisions(self, stop_revision=None, overwrite=False,
380
graph=None, limit=None):
381
"""Like InterBranch.update_revisions(), but with additions.
383
Compared to the `update_revisions()` below, this function takes a
384
`limit` argument that limits how many git commits will be converted
385
and returns the new git head.
387
interrepo = self._get_interrepo(self.source, self.target)
388
def determine_wants(heads):
389
if not self.source.name in heads:
390
raise NoSuchRef(self.source.name, heads.keys())
391
if stop_revision is not None:
392
self._last_revid = stop_revision
393
head, mapping = self.source.repository.lookup_bzr_revision_id(
396
head = heads[self.source.name]
397
self._last_revid = self.source.mapping.revision_id_foreign_to_bzr(
399
if self.target.repository.has_revision(self._last_revid):
402
_, head = interrepo.fetch_objects(
403
determine_wants, self.source.mapping, limit=limit)
405
self._last_revid = self.source.mapping.revision_id_foreign_to_bzr(head)
407
prev_last_revid = None
409
prev_last_revid = self.target.last_revision()
410
self.target.generate_revision_history(self._last_revid, prev_last_revid)
413
def update_revisions(self, stop_revision=None, overwrite=False,
415
"""See InterBranch.update_revisions()."""
416
self._update_revisions(stop_revision, overwrite, graph)
418
def pull(self, overwrite=False, stop_revision=None,
419
possible_transports=None, _hook_master=None, run_hooks=True,
420
_override_hook_target=None, local=False, limit=None):
423
:param _hook_master: Private parameter - set the branch to
424
be supplied as the master to pull hooks.
425
:param run_hooks: Private parameter - if false, this branch
426
is being called because it's the master of the primary branch,
427
so it should not run its hooks.
428
:param _override_hook_target: Private parameter - set the branch to be
429
supplied as the target_branch to pull hooks.
430
:param limit: Only import this many revisons. `None`, the default,
431
means import all revisions.
433
# This type of branch can't be bound.
435
raise errors.LocalRequiresBoundBranch()
436
result = GitBranchPullResult()
437
result.source_branch = self.source
438
if _override_hook_target is None:
439
result.target_branch = self.target
441
result.target_branch = _override_hook_target
442
self.source.lock_read()
444
# We assume that during 'pull' the target repository is closer than
446
graph = self.target.repository.get_graph(self.source.repository)
447
(result.old_revno, result.old_revid) = \
448
self.target.last_revision_info()
449
result.new_git_head = self._update_revisions(
450
stop_revision, overwrite=overwrite, graph=graph, limit=limit)
451
result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
453
(result.new_revno, result.new_revid) = \
454
self.target.last_revision_info()
456
result.master_branch = _hook_master
457
result.local_branch = result.target_branch
459
result.master_branch = result.target_branch
460
result.local_branch = None
462
for hook in branch.Branch.hooks['post_pull']:
468
def _basic_push(self, overwrite=False, stop_revision=None):
469
result = branch.BranchPushResult()
470
result.source_branch = self.source
471
result.target_branch = self.target
472
graph = self.target.repository.get_graph(self.source.repository)
473
result.old_revno, result.old_revid = self.target.last_revision_info()
474
result.new_git_head = self._update_revisions(
475
stop_revision, overwrite=overwrite, graph=graph)
476
result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
478
result.new_revno, result.new_revid = self.target.last_revision_info()
482
class InterGitBranch(branch.GenericInterBranch):
483
"""InterBranch implementation that pulls between Git branches."""
486
class InterGitLocalRemoteBranch(InterGitBranch):
487
"""InterBranch that copies from a local to a remote git branch."""
490
def is_compatible(self, source, target):
491
from bzrlib.plugins.git.remote import RemoteGitBranch
492
return (isinstance(source, LocalGitBranch) and
493
isinstance(target, RemoteGitBranch))
495
def _basic_push(self, overwrite=False, stop_revision=None):
496
result = GitBranchPushResult()
497
result.source_branch = self.source
498
result.target_branch = self.target
499
if stop_revision is None:
500
stop_revision = self.source.last_revision()
501
# FIXME: Check for diverged branches
502
def get_changed_refs(old_refs):
503
result.old_revid = self.target.mapping.revision_id_foreign_to_bzr(old_refs.get("refs/heads/master", "0" * 40))
504
refs = { "refs/heads/master": self.source.repository.lookup_bzr_revision_id(stop_revision)[0] }
505
result.new_revid = stop_revision
506
for name, sha in self.source.repository._git.refs.as_dict("refs/tags").iteritems():
507
refs["refs/tags/%s" % name] = sha
509
self.target.repository.send_pack(get_changed_refs,
510
self.source.repository._git.object_store.generate_pack_contents)
514
class InterGitRemoteLocalBranch(InterGitBranch):
515
"""InterBranch that copies from a remote to a local git branch."""
518
def is_compatible(self, source, target):
519
from bzrlib.plugins.git.remote import RemoteGitBranch
520
return (isinstance(source, RemoteGitBranch) and
521
isinstance(target, LocalGitBranch))
523
def _basic_push(self, overwrite=False, stop_revision=None):
524
result = branch.BranchPushResult()
525
result.source_branch = self.source
526
result.target_branch = self.target
527
result.old_revid = self.target.last_revision()
528
refs, stop_revision = self.update_refs(stop_revision)
529
self.target.generate_revision_history(stop_revision, result.old_revid)
530
self.update_tags(refs)
531
result.new_revid = self.target.last_revision()
534
def update_tags(self, refs):
535
for name, v in extract_tags(refs).iteritems():
536
revid = self.target.mapping.revision_id_foreign_to_bzr(v)
537
self.target.tags.set_tag(name, revid)
539
def update_refs(self, stop_revision=None):
540
interrepo = repository.InterRepository.get(self.source.repository,
541
self.target.repository)
542
if stop_revision is None:
543
refs = interrepo.fetch_refs(branches=["HEAD"])
544
stop_revision = self.target.mapping.revision_id_foreign_to_bzr(refs["HEAD"])
546
refs = interrepo.fetch_refs(revision_id=stop_revision)
547
return refs, stop_revision
549
def pull(self, stop_revision=None, overwrite=False,
550
possible_transports=None, run_hooks=True,local=False):
551
# This type of branch can't be bound.
553
raise errors.LocalRequiresBoundBranch()
554
result = GitPullResult()
555
result.source_branch = self.source
556
result.target_branch = self.target
557
result.old_revid = self.target.last_revision()
558
refs, stop_revision = self.update_refs(stop_revision)
559
self.target.generate_revision_history(stop_revision, result.old_revid)
560
self.update_tags(refs)
561
result.new_revid = self.target.last_revision()
565
class InterToGitBranch(branch.InterBranch):
566
"""InterBranch implementation that pulls from Git into bzr."""
569
def _get_branch_formats_to_test():
573
def is_compatible(self, source, target):
574
return (not isinstance(source, GitBranch) and
575
isinstance(target, GitBranch))
577
def update_revisions(self, *args, **kwargs):
578
raise NoPushSupport()
580
def push(self, overwrite=True, stop_revision=None,
581
_override_hook_source_branch=None):
582
raise NoPushSupport()
584
def lossy_push(self, stop_revision=None):
585
result = GitBranchPushResult()
586
result.source_branch = self.source
587
result.target_branch = self.target
589
result.old_revid = self.target.last_revision()
591
result.old_revid = revision.NULL_REVISION
592
if stop_revision is None:
593
stop_revision = self.source.last_revision()
594
# FIXME: Check for diverged branches
595
refs = { "refs/heads/master": stop_revision }
596
for name, revid in self.source.tags.get_tag_dict().iteritems():
597
if self.source.repository.has_revision(revid):
598
refs["refs/tags/%s" % name] = revid
599
revidmap, new_refs = self.target.repository.dfetch_refs(
600
self.source.repository, refs)
602
self.target.generate_revision_history(revidmap[stop_revision])
603
result.new_revid = revidmap[stop_revision]
605
result.new_revid = result.old_revid
606
result.revidmap = revidmap
610
branch.InterBranch.register_optimiser(InterGitRemoteLocalBranch)
611
branch.InterBranch.register_optimiser(InterFromGitBranch)
612
branch.InterBranch.register_optimiser(InterToGitBranch)
613
branch.InterBranch.register_optimiser(InterGitLocalRemoteBranch)