1
# Copyright (C) 2007 Canonical Ltd
2
# Copyright (C) 2009 Jelmer Vernooij <jelmer@samba.org>
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
"""An adapter between a Git Branch and a Bazaar Branch"""
20
from dulwich.objects import (
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."""
53
def __init__(self, branch):
55
self.repository = branch.repository
57
def get_tag_dict(self):
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)
71
def set_tag(self, name, revid):
72
self.repository._git.tags[name] = revid
75
class GitBranchConfig(config.BranchConfig):
76
"""BranchConfig that uses locations.conf in place of branch.conf"""
78
def __init__(self, branch):
79
config.BranchConfig.__init__(self, branch)
80
# do not provide a BranchDataConfig
81
self.option_sources = self.option_sources[0], self.option_sources[2]
83
def set_user_option(self, name, value, store=config.STORE_BRANCH,
85
"""Force local to True"""
86
config.BranchConfig.set_user_option(self, name, value,
87
store=config.STORE_LOCATION, warn_masked=warn_masked)
90
class GitBranchFormat(branch.BranchFormat):
92
def get_format_description(self):
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)
106
class GitBranch(ForeignBranch):
107
"""An adapter to git repositories for bzr Branch objects."""
109
def __init__(self, bzrdir, repository, name, head, lockfiles):
110
self.repository = repository
111
self._format = GitBranchFormat()
112
super(GitBranch, self).__init__(repository.get_mapping())
113
self.control_files = lockfiles
117
self.base = bzrdir.transport.base
119
def dpull(self, source, stop_revision=None):
120
if stop_revision is None:
121
stop_revision = source.last_revision()
122
# FIXME: Check for diverged branches
123
revidmap = self.repository.dfetch(source.repository, stop_revision)
124
self.head, self.mapping = self.mapping.revision_id_bzr_to_foreign(
125
revidmap[stop_revision])
126
self.repository._git.set_ref(self.name, self.head)
129
def lock_write(self):
130
self.control_files.lock_write()
132
def get_stacked_on_url(self):
133
# Git doesn't do stacking (yet...)
136
def get_parent(self):
137
"""See Branch.get_parent()."""
140
def set_parent(self, url):
144
self.control_files.lock_read()
147
self.control_files.unlock()
149
def get_physical_lock_status(self):
153
class LocalGitBranch(GitBranch):
154
"""A local Git branch."""
157
def last_revision(self):
158
# perhaps should escape this ?
159
if self.head is None:
160
return revision.NULL_REVISION
161
return self.mapping.revision_id_foreign_to_bzr(self.head)
163
def create_checkout(self, to_location, revision_id=None, lightweight=False,
164
accelerator_tree=None, hardlink=False):
166
raise LightWeightCheckoutsNotSupported()
167
return self._create_heavyweight_checkout(to_location, revision_id,
170
def _create_heavyweight_checkout(self, to_location, revision_id=None,
172
"""Create a new heavyweight checkout of this branch.
174
:param to_location: URL of location to create the new checkout in.
175
:param revision_id: Revision that should be the tip of the checkout.
176
:param hardlink: Whether to hardlink
177
:return: WorkingTree object of checkout.
179
checkout_branch = BzrDir.create_branch_convenience(
180
to_location, force_new_tree=False, format=get_rich_root_format())
181
checkout = checkout_branch.bzrdir
182
checkout_branch.bind(self)
183
# pull up to the specified revision_id to set the initial
184
# branch tip correctly, and seed it with history.
185
checkout_branch.pull(self, stop_revision=revision_id)
186
return checkout.create_workingtree(revision_id, hardlink=hardlink)
188
def _gen_revision_history(self):
189
if self.head is None:
191
ret = list(self.repository.iter_reverse_revision_history(
192
self.last_revision()))
196
def get_config(self):
197
return GitBranchConfig(self)
199
def get_push_location(self):
200
"""See Branch.get_push_location."""
201
push_loc = self.get_config().get_user_option('push_location')
204
def set_push_location(self, location):
205
"""See Branch.set_push_location."""
206
self.get_config().set_user_option('push_location', location,
207
store=config.STORE_LOCATION)
209
def supports_tags(self):
213
class InterGitGenericBranch(branch.InterBranch):
214
"""InterBranch implementation that pulls from Git into bzr."""
217
def is_compatible(self, source, target):
218
return isinstance(source, GitBranch)
220
def update_revisions(self, stop_revision=None, overwrite=False,
222
"""See InterBranch.update_revisions()."""
223
# TODO: stop_revision, overwrite
224
interrepo = repository.InterRepository.get(self.source.repository,
225
self.target.repository)
226
self._last_revid = None
227
def determine_wants(heads):
228
if not self.source.name in heads:
229
raise NoSuchRef(self.source.name, heads.keys())
230
head = heads[self.source.name]
231
self._last_revid = self.source.mapping.revision_id_foreign_to_bzr(
233
if self.target.repository.has_revision(self._last_revid):
236
interrepo.fetch_objects(determine_wants, self.source.mapping)
237
self.target.generate_revision_history(self._last_revid)
240
branch.InterBranch.register_optimiser(InterGitGenericBranch)