1
1
# Copyright (C) 2007 Canonical Ltd
2
# Copyright (C) 2009 Jelmer Vernooij <jelmer@samba.org>
3
4
# This program is free software; you can redistribute it and/or modify
4
5
# it under the terms of the GNU General Public License as published by
17
18
"""An adapter between a Git Branch and a Bazaar Branch"""
20
from dulwich.objects import (
19
25
from bzrlib import (
25
from bzrlib.decorators import needs_read_lock
27
from bzrlib.plugins.git.foreign import ForeignBranch
28
from bzrlib.plugins.git.mapping import default_mapping
30
class GitTagDict(tag.BasicTags):
32
from bzrlib.decorators import (
35
from bzrlib.trace import (
39
from bzrlib.plugins.git.errors import (
42
from bzrlib.plugins.git.foreign import (
45
from bzrlib.plugins.git.errors import (
46
LightWeightCheckoutsNotSupported,
50
class LocalGitTagDict(tag.BasicTags):
51
"""Dictionary with tags in a local repository."""
32
53
def __init__(self, branch):
33
54
self.branch = branch
36
57
def get_tag_dict(self):
38
for tag in self.repository._git.tags:
39
ret[tag.name] = self.branch.mapping.revision_id_foreign_to_bzr(tag.ref)
59
for k,v in self.repository._git.tags.iteritems():
60
obj = self.repository._git.get_object(v)
61
while isinstance(obj, Tag):
63
obj = self.repository._git.get_object(v)
64
if not isinstance(obj, Commit):
65
mutter("Tag %s points at object %r that is not a commit, "
68
ret[k] = self.branch.mapping.revision_id_foreign_to_bzr(v)
42
71
def set_tag(self, name, revid):
43
raise NotImplementedError(self.set_tag)
72
self.repository._git.tags[name] = revid
46
75
class GitBranchConfig(config.BranchConfig):
51
80
# do not provide a BranchDataConfig
52
81
self.option_sources = self.option_sources[0], self.option_sources[2]
54
def set_user_option(self, name, value, local=False):
83
def set_user_option(self, name, value, store=config.STORE_BRANCH,
55
85
"""Force local to True"""
56
config.BranchConfig.set_user_option(self, name, value, local=True)
86
config.BranchConfig.set_user_option(self, name, value,
87
store=config.STORE_LOCATION, warn_masked=warn_masked)
59
90
class GitBranchFormat(branch.BranchFormat):
64
95
def supports_tags(self):
98
def make_tags(self, branch):
99
if getattr(branch.repository, "get_refs", None) is not None:
100
from bzrlib.plugins.git.remote import RemoteGitTagDict
101
return RemoteGitTagDict(branch)
103
return LocalGitTagDict(branch)
68
106
class GitBranch(ForeignBranch):
69
107
"""An adapter to git repositories for bzr Branch objects."""
71
109
def __init__(self, bzrdir, repository, name, head, lockfiles):
72
110
self.repository = repository
73
super(GitBranch, self).__init__(default_mapping)
111
self._format = GitBranchFormat()
112
super(GitBranch, self).__init__(repository.get_mapping())
74
113
self.control_files = lockfiles
75
114
self.bzrdir = bzrdir
78
117
self.base = bzrdir.transport.base
79
self._format = GitBranchFormat()
119
def _get_nick(self, local=False, possible_master_transports=None):
120
"""Find the nick name for this branch.
126
nick = property(_get_nick)
128
def dpull(self, source, stop_revision=None):
129
if stop_revision is None:
130
stop_revision = source.last_revision()
131
# FIXME: Check for diverged branches
132
revidmap = self.repository.dfetch(source.repository, stop_revision)
133
self.head, self.mapping = self.mapping.revision_id_bzr_to_foreign(
134
revidmap[stop_revision])
135
self.repository._git.set_ref(self.name, self.head)
81
138
def lock_write(self):
82
139
self.control_files.lock_write()
108
169
return revision.NULL_REVISION
109
170
return self.mapping.revision_id_foreign_to_bzr(self.head)
111
def _make_tags(self):
112
return GitTagDict(self)
172
def create_checkout(self, to_location, revision_id=None, lightweight=False,
173
accelerator_tree=None, hardlink=False):
175
raise LightWeightCheckoutsNotSupported()
176
return self._create_heavyweight_checkout(to_location, revision_id,
179
def _create_heavyweight_checkout(self, to_location, revision_id=None,
181
"""Create a new heavyweight checkout of this branch.
183
:param to_location: URL of location to create the new checkout in.
184
:param revision_id: Revision that should be the tip of the checkout.
185
:param hardlink: Whether to hardlink
186
:return: WorkingTree object of checkout.
188
checkout_branch = BzrDir.create_branch_convenience(
189
to_location, force_new_tree=False, format=get_rich_root_format())
190
checkout = checkout_branch.bzrdir
191
checkout_branch.bind(self)
192
# pull up to the specified revision_id to set the initial
193
# branch tip correctly, and seed it with history.
194
checkout_branch.pull(self, stop_revision=revision_id)
195
return checkout.create_workingtree(revision_id, hardlink=hardlink)
114
197
def _gen_revision_history(self):
115
198
if self.head is None:
117
ret = list(self.repository.iter_reverse_revision_history(self.last_revision()))
200
ret = list(self.repository.iter_reverse_revision_history(
201
self.last_revision()))
129
213
def set_push_location(self, location):
130
214
"""See Branch.set_push_location."""
131
215
self.get_config().set_user_option('push_location', location,
216
store=config.STORE_LOCATION)
134
218
def supports_tags(self):
137
def sprout(self, to_bzrdir, revision_id=None):
138
"""See Branch.sprout()."""
139
result = to_bzrdir.create_branch()
140
self.copy_content_into(result, revision_id=revision_id)
141
result.set_parent(self.bzrdir.root_transport.base)
222
class InterGitGenericBranch(branch.InterBranch):
223
"""InterBranch implementation that pulls from Git into bzr."""
226
def is_compatible(self, source, target):
227
return isinstance(source, GitBranch)
229
def update_revisions(self, stop_revision=None, overwrite=False,
231
"""See InterBranch.update_revisions()."""
232
# TODO: stop_revision, overwrite
233
interrepo = repository.InterRepository.get(self.source.repository,
234
self.target.repository)
235
self._last_revid = None
236
def determine_wants(heads):
237
if not self.source.name in heads:
238
raise NoSuchRef(self.source.name, heads.keys())
239
head = heads[self.source.name]
240
self._last_revid = self.source.mapping.revision_id_foreign_to_bzr(
242
if self.target.repository.has_revision(self._last_revid):
245
interrepo.fetch_objects(determine_wants, self.source.mapping)
246
# FIXME: Check that self._last_revid is a descendant of self.target.last_revision()
247
self.target.generate_revision_history(self._last_revid)
250
branch.InterBranch.register_optimiser(InterGitGenericBranch)