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):
33
from bzrlib.decorators import (
36
from bzrlib.trace import (
40
from bzrlib.plugins.git.errors import (
43
from bzrlib.plugins.git.foreign import (
48
class LocalGitTagDict(tag.BasicTags):
49
"""Dictionary with tags in a local repository."""
32
51
def __init__(self, branch):
33
52
self.branch = branch
36
55
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)
57
for k,v in self.repository._git.tags.iteritems():
58
obj = self.repository._git.get_object(v)
59
while isinstance(obj, Tag):
61
obj = self.repository._git.get_object(v)
62
if not isinstance(obj, Commit):
63
mutter("Tag %s points at object %r that is not a commit, "
66
ret[k] = self.branch.mapping.revision_id_foreign_to_bzr(v)
42
69
def set_tag(self, name, revid):
43
raise NotImplementedError(self.set_tag)
70
self.repository._git.tags[name] = revid
46
73
class GitBranchConfig(config.BranchConfig):
51
78
# do not provide a BranchDataConfig
52
79
self.option_sources = self.option_sources[0], self.option_sources[2]
54
def set_user_option(self, name, value, local=False):
81
def set_user_option(self, name, value, store=config.STORE_BRANCH,
55
83
"""Force local to True"""
56
config.BranchConfig.set_user_option(self, name, value, local=True)
84
config.BranchConfig.set_user_option(self, name, value,
85
store=config.STORE_LOCATION, warn_masked=warn_masked)
59
88
class GitBranchFormat(branch.BranchFormat):
64
93
def supports_tags(self):
96
def make_tags(self, branch):
97
if getattr(branch.repository, "get_refs", None) is not None:
98
from bzrlib.plugins.git.remote import RemoteGitTagDict
99
return RemoteGitTagDict(branch)
101
return LocalGitTagDict(branch)
68
104
class GitBranch(ForeignBranch):
69
105
"""An adapter to git repositories for bzr Branch objects."""
71
107
def __init__(self, bzrdir, repository, name, head, lockfiles):
72
108
self.repository = repository
73
super(GitBranch, self).__init__(default_mapping)
109
self._format = GitBranchFormat()
110
super(GitBranch, self).__init__(repository.get_mapping())
74
111
self.control_files = lockfiles
75
112
self.bzrdir = bzrdir
78
115
self.base = bzrdir.transport.base
79
self._format = GitBranchFormat()
117
def _get_nick(self, local=False, possible_master_transports=None):
118
"""Find the nick name for this branch.
124
nick = property(_get_nick)
126
def dpull(self, source, stop_revision=None):
127
if stop_revision is None:
128
stop_revision = source.last_revision()
129
# FIXME: Check for diverged branches
130
revidmap = self.repository.dfetch(source.repository, stop_revision)
131
self.head, self.mapping = self.mapping.revision_id_bzr_to_foreign(
132
revidmap[stop_revision])
133
self.repository._git.set_ref(self.name, self.head)
81
136
def lock_write(self):
82
137
self.control_files.lock_write()
108
167
return revision.NULL_REVISION
109
168
return self.mapping.revision_id_foreign_to_bzr(self.head)
111
def _make_tags(self):
112
return GitTagDict(self)
170
def _get_checkout_format(self):
171
"""Return the most suitable metadir for a checkout of this branch.
172
Weaves are used if this branch's repository uses weaves.
174
format = self.repository.bzrdir.checkout_metadir()
175
format.set_branch_format(self._format)
178
def create_checkout(self, to_location, revision_id=None, lightweight=False,
179
accelerator_tree=None, hardlink=False):
181
t = transport.get_transport(to_location)
183
format = self._get_checkout_format()
184
checkout = format.initialize_on_transport(t)
185
from_branch = branch.BranchReferenceFormat().initialize(checkout,
187
tree = checkout.create_workingtree(revision_id,
188
from_branch=from_branch, hardlink=hardlink)
191
return self._create_heavyweight_checkout(to_location, revision_id,
194
def _create_heavyweight_checkout(self, to_location, revision_id=None,
196
"""Create a new heavyweight checkout of this branch.
198
:param to_location: URL of location to create the new checkout in.
199
:param revision_id: Revision that should be the tip of the checkout.
200
:param hardlink: Whether to hardlink
201
:return: WorkingTree object of checkout.
203
checkout_branch = BzrDir.create_branch_convenience(
204
to_location, force_new_tree=False, format=get_rich_root_format())
205
checkout = checkout_branch.bzrdir
206
checkout_branch.bind(self)
207
# pull up to the specified revision_id to set the initial
208
# branch tip correctly, and seed it with history.
209
checkout_branch.pull(self, stop_revision=revision_id)
210
return checkout.create_workingtree(revision_id, hardlink=hardlink)
114
212
def _gen_revision_history(self):
115
213
if self.head is None:
117
ret = list(self.repository.iter_reverse_revision_history(self.last_revision()))
215
ret = list(self.repository.iter_reverse_revision_history(
216
self.last_revision()))
129
228
def set_push_location(self, location):
130
229
"""See Branch.set_push_location."""
131
230
self.get_config().set_user_option('push_location', location,
231
store=config.STORE_LOCATION)
134
233
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)
237
class InterGitGenericBranch(branch.InterBranch):
238
"""InterBranch implementation that pulls from Git into bzr."""
241
def is_compatible(self, source, target):
242
return isinstance(source, GitBranch)
244
def update_revisions(self, stop_revision=None, overwrite=False,
246
"""See InterBranch.update_revisions()."""
247
# TODO: stop_revision, overwrite
248
interrepo = repository.InterRepository.get(self.source.repository,
249
self.target.repository)
250
self._last_revid = None
251
def determine_wants(heads):
252
if not self.source.name in heads:
253
raise NoSuchRef(self.source.name, heads.keys())
254
head = heads[self.source.name]
255
self._last_revid = self.source.mapping.revision_id_foreign_to_bzr(
257
if self.target.repository.has_revision(self._last_revid):
260
interrepo.fetch_objects(determine_wants, self.source.mapping)
261
self.target.generate_revision_history(self._last_revid)
264
branch.InterBranch.register_optimiser(InterGitGenericBranch)