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.foreign import (
42
from bzrlib.plugins.git.errors import (
43
LightWeightCheckoutsNotSupported,
47
class LocalGitTagDict(tag.BasicTags):
48
"""Dictionary with tags in a local repository."""
32
50
def __init__(self, branch):
33
51
self.branch = branch
36
54
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.commit.id)
56
for k,v in self.repository._git.tags.iteritems():
57
obj = self.repository._git.get_object(v)
58
while isinstance(obj, Tag):
60
obj = self.repository._git.get_object(v)
61
if not isinstance(obj, Commit):
62
mutter("Tag %s points at object %r that is not a commit, "
65
ret[k] = self.branch.mapping.revision_id_foreign_to_bzr(v)
42
68
def set_tag(self, name, revid):
43
raise NotImplementedError(self.set_tag)
69
self.repository._git.tags[name] = revid
46
72
class GitBranchConfig(config.BranchConfig):
51
77
# do not provide a BranchDataConfig
52
78
self.option_sources = self.option_sources[0], self.option_sources[2]
54
def set_user_option(self, name, value, local=False):
80
def set_user_option(self, name, value, store=config.STORE_BRANCH,
55
82
"""Force local to True"""
56
config.BranchConfig.set_user_option(self, name, value, local=True)
83
config.BranchConfig.set_user_option(self, name, value,
84
store=config.STORE_LOCATION, warn_masked=warn_masked)
59
87
class GitBranchFormat(branch.BranchFormat):
64
92
def supports_tags(self):
95
def make_tags(self, branch):
96
if getattr(branch.repository, "get_refs", None) is not None:
97
from bzrlib.plugins.git.remote import RemoteGitTagDict
98
return RemoteGitTagDict(branch)
100
return LocalGitTagDict(branch)
68
103
class GitBranch(ForeignBranch):
69
104
"""An adapter to git repositories for bzr Branch objects."""
71
def __init__(self, bzrdir, repository, head, base, lockfiles):
106
def __init__(self, bzrdir, repository, name, head, lockfiles):
72
107
self.repository = repository
73
super(GitBranch, self).__init__(default_mapping)
108
self._format = GitBranchFormat()
109
super(GitBranch, self).__init__(repository.get_mapping())
74
110
self.control_files = lockfiles
75
111
self.bzrdir = bzrdir
78
self._format = GitBranchFormat()
114
self.base = bzrdir.transport.base
116
def dpull(self, source, stop_revision=None):
117
if stop_revision is None:
118
stop_revision = source.last_revision()
119
# FIXME: Check for diverged branches
120
revidmap = self.repository.dfetch(source.repository, stop_revision)
121
self.head, self.mapping = self.mapping.revision_id_bzr_to_foreign(
122
revidmap[stop_revision])
80
125
def lock_write(self):
81
126
self.control_files.lock_write()
128
def get_stacked_on_url(self):
129
# Git doesn't do stacking (yet...)
132
def get_parent(self):
133
"""See Branch.get_parent()."""
136
def set_parent(self, url):
140
self.control_files.lock_read()
143
self.control_files.unlock()
145
def get_physical_lock_status(self):
149
class LocalGitBranch(GitBranch):
150
"""A local Git branch."""
84
153
def last_revision(self):
85
154
# perhaps should escape this ?
87
156
return revision.NULL_REVISION
88
157
return self.mapping.revision_id_foreign_to_bzr(self.head)
91
return GitTagDict(self)
94
"""See Branch.get_parent()."""
97
def get_stacked_on_url(self):
159
def create_checkout(self, to_location, revision_id=None, lightweight=False,
160
accelerator_tree=None, hardlink=False):
162
raise LightWeightCheckoutsNotSupported()
163
return self._create_heavyweight_checkout(to_location, revision_id,
166
def _create_heavyweight_checkout(self, to_location, revision_id=None,
168
"""Create a new heavyweight checkout of this branch.
170
:param to_location: URL of location to create the new checkout in.
171
:param revision_id: Revision that should be the tip of the checkout.
172
:param hardlink: Whether to hardlink
173
:return: WorkingTree object of checkout.
175
checkout_branch = BzrDir.create_branch_convenience(
176
to_location, force_new_tree=False, format=get_rich_root_format())
177
checkout = checkout_branch.bzrdir
178
checkout_branch.bind(self)
179
# pull up to the specified revision_id to set the initial
180
# branch tip correctly, and seed it with history.
181
checkout_branch.pull(self, stop_revision=revision_id)
182
return checkout.create_workingtree(revision_id, hardlink=hardlink)
100
184
def _gen_revision_history(self):
101
185
if self.head is None:
109
cms = self.repository._git.commits(self.head, max_count=max_count, skip=skip)
113
ret.append(self.mapping.revision_id_foreign_to_bzr(cm.id))
117
nextid = cm.parents[0].id
187
ret = list(self.repository.iter_reverse_revision_history(
188
self.last_revision()))
121
192
def get_config(self):
122
193
return GitBranchConfig(self)
125
self.control_files.lock_read()
128
self.control_files.unlock()
130
def get_physical_lock_status(self):
133
195
def get_push_location(self):
134
196
"""See Branch.get_push_location."""
135
197
push_loc = self.get_config().get_user_option('push_location')
138
200
def set_push_location(self, location):
139
201
"""See Branch.set_push_location."""
140
202
self.get_config().set_user_option('push_location', location,
203
store=config.STORE_LOCATION)
143
205
def supports_tags(self):
146
def sprout(self, to_bzrdir, revision_id=None):
147
"""See Branch.sprout()."""
148
result = to_bzrdir.create_branch()
149
self.copy_content_into(result, revision_id=revision_id)
150
result.set_parent(self.bzrdir.root_transport.base)
209
class InterGitGenericBranch(branch.InterBranch):
210
"""InterBranch implementation that pulls from Git into bzr."""
213
def is_compatible(self, source, target):
214
return isinstance(source, GitBranch)
216
def update_revisions(self, stop_revision=None, overwrite=False,
218
"""See InterBranch.update_revisions()."""
219
# TODO: stop_revision, overwrite
220
interrepo = repository.InterRepository.get(self.source.repository,
221
self.target.repository)
222
self._last_revid = None
223
def determine_wants(heads):
224
if not self.source.name in heads:
225
raise BzrError("No such remote branch '%s', found: %r" % (
226
self.source.name, heads.keys()))
227
head = heads[self.source.name]
228
self._last_revid = self.source.mapping.revision_id_foreign_to_bzr(
230
if self.target.repository.has_revision(self._last_revid):
233
interrepo.fetch_objects(determine_wants, self.source.mapping)
234
self.target.generate_revision_history(self._last_revid)
237
branch.InterBranch.register_optimiser(InterGitGenericBranch)