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):
34
from bzrlib.decorators import (
37
from bzrlib.trace import (
41
from bzrlib.plugins.git.errors import (
46
class LocalGitTagDict(tag.BasicTags):
47
"""Dictionary with tags in a local repository."""
32
49
def __init__(self, branch):
33
50
self.branch = branch
36
53
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)
55
for k,v in self.repository._git.tags.iteritems():
56
obj = self.repository._git.get_object(v)
57
while isinstance(obj, Tag):
59
obj = self.repository._git.get_object(v)
60
if not isinstance(obj, Commit):
61
mutter("Tag %s points at object %r that is not a commit, "
64
ret[k] = self.branch.mapping.revision_id_foreign_to_bzr(v)
42
67
def set_tag(self, name, revid):
43
raise NotImplementedError(self.set_tag)
68
self.repository._git.tags[name] = revid
46
71
class GitBranchConfig(config.BranchConfig):
51
76
# do not provide a BranchDataConfig
52
77
self.option_sources = self.option_sources[0], self.option_sources[2]
54
def set_user_option(self, name, value, local=False):
79
def set_user_option(self, name, value, store=config.STORE_BRANCH,
55
81
"""Force local to True"""
56
config.BranchConfig.set_user_option(self, name, value, local=True)
82
config.BranchConfig.set_user_option(self, name, value,
83
store=config.STORE_LOCATION, warn_masked=warn_masked)
59
86
class GitBranchFormat(branch.BranchFormat):
64
91
def supports_tags(self):
68
class GitBranch(ForeignBranch):
94
def make_tags(self, branch):
95
if getattr(branch.repository, "get_refs", None) is not None:
96
from bzrlib.plugins.git.remote import RemoteGitTagDict
97
return RemoteGitTagDict(branch)
99
return LocalGitTagDict(branch)
102
class GitBranch(foreign.ForeignBranch):
69
103
"""An adapter to git repositories for bzr Branch objects."""
71
105
def __init__(self, bzrdir, repository, name, head, lockfiles):
72
106
self.repository = repository
73
super(GitBranch, self).__init__(default_mapping)
107
self._format = GitBranchFormat()
74
108
self.control_files = lockfiles
75
109
self.bzrdir = bzrdir
110
super(GitBranch, self).__init__(repository.get_mapping())
78
113
self.base = bzrdir.transport.base
79
self._format = GitBranchFormat()
115
def _get_nick(self, local=False, possible_master_transports=None):
116
"""Find the nick name for this branch.
122
def _set_nick(self, nick):
123
raise NotImplementedError
125
nick = property(_get_nick, _set_nick)
127
def dpull(self, source, stop_revision=None):
128
if stop_revision is None:
129
stop_revision = source.last_revision()
130
# FIXME: Check for diverged branches
131
revidmap = self.repository.dfetch(source.repository, stop_revision)
132
self.head, self.mapping = self.mapping.revision_id_bzr_to_foreign(
133
revidmap[stop_revision])
134
self.repository._git.set_ref(self.name, self.head)
81
137
def lock_write(self):
82
138
self.control_files.lock_write()
88
144
def get_parent(self):
89
145
"""See Branch.get_parent()."""
146
# FIXME: Set "origin" url from .git/config ?
149
def set_parent(self, url):
150
# FIXME: Set "origin" url in .git/config ?
92
153
def lock_read(self):
93
154
self.control_files.lock_read()
108
170
return revision.NULL_REVISION
109
171
return self.mapping.revision_id_foreign_to_bzr(self.head)
111
def _make_tags(self):
112
return GitTagDict(self)
173
def _get_checkout_format(self):
174
"""Return the most suitable metadir for a checkout of this branch.
175
Weaves are used if this branch's repository uses weaves.
177
format = self.repository.bzrdir.checkout_metadir()
178
format.set_branch_format(self._format)
181
def create_checkout(self, to_location, revision_id=None, lightweight=False,
182
accelerator_tree=None, hardlink=False):
184
t = transport.get_transport(to_location)
186
format = self._get_checkout_format()
187
checkout = format.initialize_on_transport(t)
188
from_branch = branch.BranchReferenceFormat().initialize(checkout,
190
tree = checkout.create_workingtree(revision_id,
191
from_branch=from_branch, hardlink=hardlink)
194
return self._create_heavyweight_checkout(to_location, revision_id,
197
def _create_heavyweight_checkout(self, to_location, revision_id=None,
199
"""Create a new heavyweight checkout of this branch.
201
:param to_location: URL of location to create the new checkout in.
202
:param revision_id: Revision that should be the tip of the checkout.
203
:param hardlink: Whether to hardlink
204
:return: WorkingTree object of checkout.
206
checkout_branch = BzrDir.create_branch_convenience(
207
to_location, force_new_tree=False, format=get_rich_root_format())
208
checkout = checkout_branch.bzrdir
209
checkout_branch.bind(self)
210
# pull up to the specified revision_id to set the initial
211
# branch tip correctly, and seed it with history.
212
checkout_branch.pull(self, stop_revision=revision_id)
213
return checkout.create_workingtree(revision_id, hardlink=hardlink)
114
215
def _gen_revision_history(self):
115
216
if self.head is None:
117
ret = list(self.repository.iter_reverse_revision_history(self.last_revision()))
218
ret = list(self.repository.iter_reverse_revision_history(
219
self.last_revision()))
129
231
def set_push_location(self, location):
130
232
"""See Branch.set_push_location."""
131
233
self.get_config().set_user_option('push_location', location,
234
store=config.STORE_LOCATION)
134
236
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)
240
class InterGitGenericBranch(branch.InterBranch):
241
"""InterBranch implementation that pulls from Git into bzr."""
244
def is_compatible(self, source, target):
245
return isinstance(source, GitBranch)
247
def update_revisions(self, stop_revision=None, overwrite=False,
249
"""See InterBranch.update_revisions()."""
250
interrepo = repository.InterRepository.get(self.source.repository,
251
self.target.repository)
252
self._last_revid = None
253
def determine_wants(heads):
254
if not self.source.name in heads:
255
raise NoSuchRef(self.source.name, heads.keys())
256
if stop_revision is not None:
257
self._last_revid = stop_revision
258
head, mapping = self.source.repository.lookup_git_revid(
261
head = heads[self.source.name]
263
self.source.mapping.revision_id_foreign_to_bzr(head)
264
if self.target.repository.has_revision(self._last_revid):
267
interrepo.fetch_objects(determine_wants, self.source.mapping)
269
prev_last_revid = None
271
prev_last_revid = self.target.last_revision()
272
self.target.generate_revision_history(self._last_revid, prev_last_revid)
275
branch.InterBranch.register_optimiser(InterGitGenericBranch)