1
1
# Copyright (C) 2007 Canonical Ltd
2
# Copyright (C) 2009 Jelmer Vernooij <jelmer@samba.org>
3
4
# This program is free software; you can redistribute it and/or modify
4
5
# it under the terms of the GNU General Public License as published by
17
18
"""An adapter between a Git Branch and a Bazaar Branch"""
20
from dulwich.objects import (
19
25
from bzrlib import (
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):
33
from bzrlib.decorators import (
36
from bzrlib.trace import (
40
from bzrlib.plugins.git.errors import (
43
from bzrlib.plugins.git.foreign import (
48
class LocalGitTagDict(tag.BasicTags):
49
"""Dictionary with tags in a local repository."""
39
51
def __init__(self, branch):
40
52
self.branch = branch
49
61
obj = self.repository._git.get_object(v)
50
62
if not isinstance(obj, Commit):
51
mutter("Tag %s points at object %r that is not a commit, ignoring", k, obj)
63
mutter("Tag %s points at object %r that is not a commit, "
53
66
ret[k] = self.branch.mapping.revision_id_foreign_to_bzr(v)
65
78
# do not provide a BranchDataConfig
66
79
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):
81
def set_user_option(self, name, value, store=config.STORE_BRANCH,
69
83
"""Force local to True"""
70
config.BranchConfig.set_user_option(self, name, value, store=config.STORE_LOCATION, warn_masked=warn_masked)
84
config.BranchConfig.set_user_option(self, name, value,
85
store=config.STORE_LOCATION, warn_masked=warn_masked)
73
88
class GitBranchFormat(branch.BranchFormat):
78
93
def supports_tags(self):
96
def make_tags(self, branch):
97
if getattr(branch.repository, "get_refs", None) is not None:
98
from bzrlib.plugins.git.remote import RemoteGitTagDict
99
return RemoteGitTagDict(branch)
101
return LocalGitTagDict(branch)
82
104
class GitBranch(ForeignBranch):
83
105
"""An adapter to git repositories for bzr Branch objects."""
85
107
def __init__(self, bzrdir, repository, name, head, lockfiles):
86
108
self.repository = repository
109
self._format = GitBranchFormat()
87
110
super(GitBranch, self).__init__(repository.get_mapping())
88
111
self.control_files = lockfiles
89
112
self.bzrdir = bzrdir
92
115
self.base = bzrdir.transport.base
93
self._format = GitBranchFormat()
117
def _get_nick(self, local=False, possible_master_transports=None):
118
"""Find the nick name for this branch.
124
nick = property(_get_nick)
95
126
def dpull(self, source, stop_revision=None):
96
127
if stop_revision is None:
97
128
stop_revision = source.last_revision()
98
129
# FIXME: Check for diverged branches
99
130
revidmap = self.repository.dfetch(source.repository, stop_revision)
100
self.head, self.mapping = self.mapping.revision_id_bzr_to_foreign(revidmap[stop_revision])
131
self.head, self.mapping = self.mapping.revision_id_bzr_to_foreign(
132
revidmap[stop_revision])
133
self.repository._git.set_ref(self.name, self.head)
103
136
def lock_write(self):
133
167
return revision.NULL_REVISION
134
168
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):
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):
139
raise LightWeightCheckoutsNotSupported()
140
return self._create_heavyweight_checkout(to_location, revision_id, hardlink)
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,
142
194
def _create_heavyweight_checkout(self, to_location, revision_id=None,
157
209
checkout_branch.pull(self, stop_revision=revision_id)
158
210
return checkout.create_workingtree(revision_id, hardlink=hardlink)
160
def _make_tags(self):
161
return GitTagDict(self)
163
212
def _gen_revision_history(self):
164
213
if self.head is None:
166
ret = list(self.repository.iter_reverse_revision_history(self.last_revision()))
215
ret = list(self.repository.iter_reverse_revision_history(
216
self.last_revision()))
183
233
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
237
class InterGitGenericBranch(branch.InterBranch):
238
"""InterBranch implementation that pulls from Git into bzr."""
197
241
def is_compatible(self, source, target):
202
246
"""See InterBranch.update_revisions()."""
203
247
# TODO: stop_revision, overwrite
204
interrepo = repository.InterRepository.get(self.source.repository, self.target.repository)
248
interrepo = repository.InterRepository.get(self.source.repository,
249
self.target.repository)
205
250
self._last_revid = None
206
251
def determine_wants(heads):
207
252
if not self.source.name in heads:
208
raise BzrError("No such remote branch '%s', found: %r" % (
209
self.source.name, heads.keys()))
253
raise NoSuchRef(self.source.name, heads.keys())
210
254
head = heads[self.source.name]
211
self._last_revid = self.source.mapping.revision_id_foreign_to_bzr(head)
255
self._last_revid = self.source.mapping.revision_id_foreign_to_bzr(
212
257
if self.target.repository.has_revision(self._last_revid):
215
260
interrepo.fetch_objects(determine_wants, self.source.mapping)
261
# FIXME: Check that self._last_revid is a descendant of self.target.last_revision()
216
262
self.target.generate_revision_history(self._last_revid)