17
18
"""An adapter between a Git Branch and a Bazaar Branch"""
20
from dulwich.objects import (
19
25
from bzrlib import (
24
from bzrlib.decorators import needs_read_lock
26
from bzrlib.plugins.git import ids
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."""
49
def __init__(self, branch):
51
self.repository = branch.repository
53
def get_tag_dict(self):
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)
67
def set_tag(self, name, revid):
68
self.repository._git.tags[name] = revid
29
71
class GitBranchConfig(config.BranchConfig):
44
88
def get_format_description(self):
45
89
return 'Git Branch'
48
class GitBranch(branch.Branch):
91
def supports_tags(self):
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):
49
103
"""An adapter to git repositories for bzr Branch objects."""
51
def __init__(self, bzrdir, repository, head, base, lockfiles):
52
super(GitBranch, self).__init__()
105
def __init__(self, bzrdir, repository, name, head, lockfiles):
106
self.repository = repository
107
self._format = GitBranchFormat()
53
108
self.control_files = lockfiles
54
109
self.bzrdir = bzrdir
55
self.repository = repository
110
super(GitBranch, self).__init__(repository.get_mapping())
58
self._format = GitBranchFormat()
113
self.base = bzrdir.transport.base
115
def _get_nick(self, local=False, possible_master_transports=None):
116
"""Find the nick name for this branch.
122
nick = property(_get_nick)
124
def dpull(self, source, stop_revision=None):
125
if stop_revision is None:
126
stop_revision = source.last_revision()
127
# FIXME: Check for diverged branches
128
revidmap = self.repository.dfetch(source.repository, stop_revision)
129
self.head, self.mapping = self.mapping.revision_id_bzr_to_foreign(
130
revidmap[stop_revision])
131
self.repository._git.set_ref(self.name, self.head)
60
134
def lock_write(self):
61
135
self.control_files.lock_write()
137
def get_stacked_on_url(self):
138
# Git doesn't do stacking (yet...)
141
def get_parent(self):
142
"""See Branch.get_parent()."""
143
# FIXME: Set "origin" url from .git/config ?
146
def set_parent(self, url):
147
# FIXME: Set "origin" url in .git/config ?
151
self.control_files.lock_read()
154
self.control_files.unlock()
156
def get_physical_lock_status(self):
160
class LocalGitBranch(GitBranch):
161
"""A local Git branch."""
64
164
def last_revision(self):
65
165
# perhaps should escape this ?
66
166
if self.head is None:
67
167
return revision.NULL_REVISION
68
return ids.convert_revision_id_git_to_bzr(self.head)
71
"""See Branch.get_parent()."""
74
def get_stacked_on_url(self):
168
return self.mapping.revision_id_foreign_to_bzr(self.head)
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)
77
212
def _gen_revision_history(self):
78
213
if self.head is None:
86
cms = self.repository._git.commits(self.head, max_count=max_count, skip=skip)
90
ret.append(ids.convert_revision_id_git_to_bzr(cm.id))
94
nextid = cm.parents[0].id
215
ret = list(self.repository.iter_reverse_revision_history(
216
self.last_revision()))
98
220
def get_config(self):
99
221
return GitBranchConfig(self)
102
self.control_files.lock_read()
105
self.control_files.unlock()
107
def get_physical_lock_status(self):
110
223
def get_push_location(self):
111
224
"""See Branch.get_push_location."""
112
225
push_loc = self.get_config().get_user_option('push_location')
115
228
def set_push_location(self, location):
116
229
"""See Branch.set_push_location."""
117
230
self.get_config().set_user_option('push_location', location,
231
store=config.STORE_LOCATION)
120
233
def supports_tags(self):
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
interrepo = repository.InterRepository.get(self.source.repository,
248
self.target.repository)
249
self._last_revid = None
250
def determine_wants(heads):
251
if not self.source.name in heads:
252
raise NoSuchRef(self.source.name, heads.keys())
253
if stop_revision is not None:
254
self._last_revid = stop_revision
255
head, mapping = self.source.repository.lookup_git_revid(
258
head = heads[self.source.name]
260
self.source.mapping.revision_id_foreign_to_bzr(head)
261
if self.target.repository.has_revision(self._last_revid):
264
interrepo.fetch_objects(determine_wants, self.source.mapping)
266
prev_last_revid = None
268
prev_last_revid = self.target.last_revision()
269
self.target.generate_revision_history(self._last_revid, prev_last_revid)
272
branch.InterBranch.register_optimiser(InterGitGenericBranch)