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):
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."""
39
49
def __init__(self, branch):
40
50
self.branch = branch
49
59
obj = self.repository._git.get_object(v)
50
60
if not isinstance(obj, Commit):
51
mutter("Tag %s points at object %r that is not a commit, ignoring", k, obj)
61
mutter("Tag %s points at object %r that is not a commit, "
53
64
ret[k] = self.branch.mapping.revision_id_foreign_to_bzr(v)
65
76
# do not provide a BranchDataConfig
66
77
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):
79
def set_user_option(self, name, value, store=config.STORE_BRANCH,
69
81
"""Force local to True"""
70
config.BranchConfig.set_user_option(self, name, value, store=config.STORE_LOCATION, warn_masked=warn_masked)
82
config.BranchConfig.set_user_option(self, name, value,
83
store=config.STORE_LOCATION, warn_masked=warn_masked)
73
86
class GitBranchFormat(branch.BranchFormat):
78
91
def supports_tags(self):
82
class GitBranch(ForeignBranch):
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):
83
103
"""An adapter to git repositories for bzr Branch objects."""
85
105
def __init__(self, bzrdir, repository, name, head, lockfiles):
86
106
self.repository = repository
87
super(GitBranch, self).__init__(repository.get_mapping())
107
self._format = GitBranchFormat()
88
108
self.control_files = lockfiles
89
109
self.bzrdir = bzrdir
110
super(GitBranch, self).__init__(repository.get_mapping())
92
113
self.base = bzrdir.transport.base
93
self._format = GitBranchFormat()
115
def _get_nick(self, local=False, possible_master_transports=None):
116
"""Find the nick name for this branch.
122
nick = property(_get_nick)
95
124
def dpull(self, source, stop_revision=None):
96
125
if stop_revision is None:
97
126
stop_revision = source.last_revision()
98
127
# FIXME: Check for diverged branches
99
128
revidmap = self.repository.dfetch(source.repository, stop_revision)
100
self.head, self.mapping = self.mapping.revision_id_bzr_to_foreign(revidmap[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)
103
134
def lock_write(self):
110
141
def get_parent(self):
111
142
"""See Branch.get_parent()."""
143
# FIXME: Set "origin" url from .git/config ?
114
146
def set_parent(self, url):
147
# FIXME: Set "origin" url in .git/config ?
117
150
def lock_read(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):
200
244
def update_revisions(self, stop_revision=None, overwrite=False,
202
246
"""See InterBranch.update_revisions()."""
203
# TODO: stop_revision, overwrite
204
interrepo = repository.InterRepository.get(self.source.repository, self.target.repository)
247
interrepo = repository.InterRepository.get(self.source.repository,
248
self.target.repository)
205
249
self._last_revid = None
206
250
def determine_wants(heads):
207
251
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)
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)
212
261
if self.target.repository.has_revision(self._last_revid):
215
264
interrepo.fetch_objects(determine_wants, self.source.mapping)
216
self.target.generate_revision_history(self._last_revid)
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)
219
272
branch.InterBranch.register_optimiser(InterGitGenericBranch)