19
20
from bzrlib import (
24
27
from bzrlib.decorators import needs_read_lock
26
from bzrlib.plugins.git import ids
28
from bzrlib.trace import mutter
30
from bzrlib.plugins.git.foreign import ForeignBranch
31
from bzrlib.plugins.git.errors import LightWeightCheckoutsNotSupported
33
from dulwich.objects import (
38
class GitTagDict(tag.BasicTags):
40
def __init__(self, branch):
42
self.repository = branch.repository
44
def get_tag_dict(self):
46
for k,v in self.repository._git.tags.iteritems():
47
obj = self.repository._git.get_object(v)
48
while isinstance(obj, Tag):
50
obj = self.repository._git.get_object(v)
51
if not isinstance(obj, Commit):
52
mutter("Tag %s points at object %r that is not a commit, ignoring", k, obj)
54
ret[k] = self.branch.mapping.revision_id_foreign_to_bzr(v)
57
def set_tag(self, name, revid):
58
self.repository._git.tags[name] = revid
29
61
class GitBranchConfig(config.BranchConfig):
34
66
# do not provide a BranchDataConfig
35
67
self.option_sources = self.option_sources[0], self.option_sources[2]
37
def set_user_option(self, name, value, local=False):
69
def set_user_option(self, name, value, store=config.STORE_BRANCH, warn_masked=False):
38
70
"""Force local to True"""
39
config.BranchConfig.set_user_option(self, name, value, local=True)
71
config.BranchConfig.set_user_option(self, name, value, store=config.STORE_LOCATION, warn_masked=warn_masked)
42
74
class GitBranchFormat(branch.BranchFormat):
44
76
def get_format_description(self):
45
77
return 'Git Branch'
48
class GitBranch(branch.Branch):
79
def supports_tags(self):
82
def make_tags(self, branch):
83
return GitTagDict(branch)
86
class GitBranch(ForeignBranch):
49
87
"""An adapter to git repositories for bzr Branch objects."""
51
def __init__(self, bzrdir, repository, head, base, lockfiles):
52
super(GitBranch, self).__init__()
89
def __init__(self, bzrdir, repository, name, head, lockfiles):
90
self.repository = repository
91
self._format = GitBranchFormat()
92
super(GitBranch, self).__init__(repository.get_mapping())
53
93
self.control_files = lockfiles
54
94
self.bzrdir = bzrdir
55
self.repository = repository
58
self._format = GitBranchFormat()
97
self.base = bzrdir.transport.base
99
def dpull(self, source, stop_revision=None):
100
if stop_revision is None:
101
stop_revision = source.last_revision()
102
# FIXME: Check for diverged branches
103
revidmap = self.repository.dfetch(source.repository, stop_revision)
104
self.head, self.mapping = self.mapping.revision_id_bzr_to_foreign(revidmap[stop_revision])
60
107
def lock_write(self):
61
108
self.control_files.lock_write()
110
def get_stacked_on_url(self):
111
# Git doesn't do stacking (yet...)
114
def get_parent(self):
115
"""See Branch.get_parent()."""
118
def set_parent(self, url):
122
self.control_files.lock_read()
125
self.control_files.unlock()
127
def get_physical_lock_status(self):
131
class LocalGitBranch(GitBranch):
64
134
def last_revision(self):
65
135
# perhaps should escape this ?
66
136
if self.head is None:
67
137
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):
138
return self.mapping.revision_id_foreign_to_bzr(self.head)
140
def create_checkout(self, to_location, revision_id=None,
141
lightweight=False, accelerator_tree=None, hardlink=False):
143
raise LightWeightCheckoutsNotSupported()
144
return self._create_heavyweight_checkout(to_location, revision_id, hardlink)
146
def _create_heavyweight_checkout(self, to_location, revision_id=None,
148
"""Create a new heavyweight checkout of this branch.
150
:param to_location: URL of location to create the new checkout in.
151
:param revision_id: Revision that should be the tip of the checkout.
152
:param hardlink: Whether to hardlink
153
:return: WorkingTree object of checkout.
155
checkout_branch = BzrDir.create_branch_convenience(
156
to_location, force_new_tree=False, format=get_rich_root_format())
157
checkout = checkout_branch.bzrdir
158
checkout_branch.bind(self)
159
# pull up to the specified revision_id to set the initial
160
# branch tip correctly, and seed it with history.
161
checkout_branch.pull(self, stop_revision=revision_id)
162
return checkout.create_workingtree(revision_id, hardlink=hardlink)
77
164
def _gen_revision_history(self):
78
165
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
167
ret = list(self.repository.iter_reverse_revision_history(self.last_revision()))
98
171
def get_config(self):
99
172
return GitBranchConfig(self)
102
self.control_files.lock_read()
105
self.control_files.unlock()
107
def get_physical_lock_status(self):
110
174
def get_push_location(self):
111
175
"""See Branch.get_push_location."""
112
176
push_loc = self.get_config().get_user_option('push_location')
115
179
def set_push_location(self, location):
116
180
"""See Branch.set_push_location."""
117
181
self.get_config().set_user_option('push_location', location,
182
store=config.STORE_LOCATION)
120
184
def supports_tags(self):
188
class InterGitGenericBranch(branch.InterBranch):
191
def is_compatible(self, source, target):
192
return isinstance(source, GitBranch)
194
def update_revisions(self, stop_revision=None, overwrite=False,
196
"""See InterBranch.update_revisions()."""
197
# TODO: stop_revision, overwrite
198
interrepo = repository.InterRepository.get(self.source.repository,
199
self.target.repository)
200
self._last_revid = None
201
def determine_wants(heads):
202
if not self.source.name in heads:
203
raise BzrError("No such remote branch '%s', found: %r" % (
204
self.source.name, heads.keys()))
205
head = heads[self.source.name]
206
self._last_revid = self.source.mapping.revision_id_foreign_to_bzr(head)
207
if self.target.repository.has_revision(self._last_revid):
210
interrepo.fetch_objects(determine_wants, self.source.mapping)
211
self.target.generate_revision_history(self._last_revid)
214
branch.InterBranch.register_optimiser(InterGitGenericBranch)