1
# Copyright (C) 2007 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""An adapter between a Git Branch and a Bazaar Branch"""
26
from bzrlib.decorators import needs_read_lock
27
from bzrlib.trace import mutter
29
from bzrlib.plugins.git.foreign import ForeignBranch
30
from bzrlib.plugins.git.errors import LightWeightCheckoutsNotSupported
32
from dulwich.objects import (
37
class GitTagDict(tag.BasicTags):
39
def __init__(self, branch):
41
self.repository = branch.repository
43
def get_tag_dict(self):
45
for k,v in self.repository._git.tags.iteritems():
46
obj = self.repository._git.get_object(v)
47
while isinstance(obj, Tag):
49
obj = self.repository._git.get_object(v)
50
if not isinstance(obj, Commit):
51
mutter("Tag %s points at object %r that is not a commit, ignoring", k, obj)
53
ret[k] = self.branch.mapping.revision_id_foreign_to_bzr(v)
56
def set_tag(self, name, revid):
57
self.repository._git.tags[name] = revid
60
class GitBranchConfig(config.BranchConfig):
61
"""BranchConfig that uses locations.conf in place of branch.conf"""
63
def __init__(self, branch):
64
config.BranchConfig.__init__(self, branch)
65
# do not provide a BranchDataConfig
66
self.option_sources = self.option_sources[0], self.option_sources[2]
68
def set_user_option(self, name, value, store=config.STORE_BRANCH, warn_masked=False):
69
"""Force local to True"""
70
config.BranchConfig.set_user_option(self, name, value, store=config.STORE_LOCATION, warn_masked=warn_masked)
73
class GitBranchFormat(branch.BranchFormat):
75
def get_format_description(self):
78
def supports_tags(self):
82
class GitBranch(ForeignBranch):
83
"""An adapter to git repositories for bzr Branch objects."""
85
def __init__(self, bzrdir, repository, name, head, lockfiles):
86
self.repository = repository
87
super(GitBranch, self).__init__(repository.get_mapping())
88
self.control_files = lockfiles
92
self.base = bzrdir.transport.base
93
self._format = GitBranchFormat()
95
def dpull(self, source, stop_revision=None):
96
if stop_revision is None:
97
stop_revision = source.last_revision()
98
# FIXME: Check for diverged branches
99
revidmap = self.repository.dfetch(source.repository, stop_revision)
100
self.head, self.mapping = self.mapping.revision_id_bzr_to_foreign(revidmap[stop_revision])
103
def lock_write(self):
104
self.control_files.lock_write()
106
def get_stacked_on_url(self):
107
# Git doesn't do stacking (yet...)
110
def get_parent(self):
111
"""See Branch.get_parent()."""
114
def set_parent(self, url):
118
self.control_files.lock_read()
121
self.control_files.unlock()
123
def get_physical_lock_status(self):
127
class LocalGitBranch(GitBranch):
130
def last_revision(self):
131
# perhaps should escape this ?
132
if self.head is None:
133
return revision.NULL_REVISION
134
return self.mapping.revision_id_foreign_to_bzr(self.head)
136
def create_checkout(self, to_location, revision_id=None,
137
lightweight=False, accelerator_tree=None, hardlink=False):
139
raise LightWeightCheckoutsNotSupported()
140
return self._create_heavyweight_checkout(to_location, revision_id, hardlink)
142
def _create_heavyweight_checkout(self, to_location, revision_id=None,
144
"""Create a new heavyweight checkout of this branch.
146
:param to_location: URL of location to create the new checkout in.
147
:param revision_id: Revision that should be the tip of the checkout.
148
:param hardlink: Whether to hardlink
149
:return: WorkingTree object of checkout.
151
checkout_branch = BzrDir.create_branch_convenience(
152
to_location, force_new_tree=False, format=get_rich_root_format())
153
checkout = checkout_branch.bzrdir
154
checkout_branch.bind(self)
155
# pull up to the specified revision_id to set the initial
156
# branch tip correctly, and seed it with history.
157
checkout_branch.pull(self, stop_revision=revision_id)
158
return checkout.create_workingtree(revision_id, hardlink=hardlink)
160
def _make_tags(self):
161
return GitTagDict(self)
163
def _gen_revision_history(self):
164
if self.head is None:
166
ret = list(self.repository.iter_reverse_revision_history(self.last_revision()))
170
def get_config(self):
171
return GitBranchConfig(self)
173
def get_push_location(self):
174
"""See Branch.get_push_location."""
175
push_loc = self.get_config().get_user_option('push_location')
178
def set_push_location(self, location):
179
"""See Branch.set_push_location."""
180
self.get_config().set_user_option('push_location', location,
181
store=config.STORE_LOCATION)
183
def supports_tags(self):
186
def sprout(self, to_bzrdir, revision_id=None):
187
"""See Branch.sprout()."""
188
result = to_bzrdir.create_branch()
189
self.copy_content_into(result, revision_id=revision_id)
190
result.set_parent(self.bzrdir.root_transport.base)
194
class InterGitGenericBranch(branch.InterBranch):
197
def is_compatible(self, source, target):
198
return isinstance(source, GitBranch)
200
def update_revisions(self, stop_revision=None, overwrite=False,
202
"""See InterBranch.update_revisions()."""
203
# TODO: stop_revision, overwrite
204
interrepo = repository.InterRepository.get(self.source.repository, self.target.repository)
205
self._last_revid = None
206
def determine_wants(heads):
207
if not self.source.name in heads:
208
raise BzrError("No such remote branch '%s', found: %r" % (
209
self.source.name, heads.keys()))
210
head = heads[self.source.name]
211
self._last_revid = self.source.mapping.revision_id_foreign_to_bzr(head)
212
if self.target.repository.has_revision(self._last_revid):
215
interrepo.fetch_objects(determine_wants, self.source.mapping)
216
self.target.set_last_revision(last_revid)
219
branch.InterBranch.register_optimiser(InterGitGenericBranch)