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 (
34
from bzrlib.decorators import (
37
from bzrlib.trace import (
42
from bzrlib.plugins.git.config import (
45
from bzrlib.plugins.git.errors import (
50
class GitPullResult(branch.PullResult):
52
def _lookup_revno(self, revid):
53
assert isinstance(revid, str), "was %r" % revid
54
# Try in source branch first, it'll be faster
55
return self.target_branch.revision_id_to_revno(revid)
59
return self._lookup_revno(self.old_revid)
63
return self._lookup_revno(self.new_revid)
66
class LocalGitTagDict(tag.BasicTags):
67
"""Dictionary with tags in a local repository."""
69
def __init__(self, branch):
71
self.repository = branch.repository
73
def get_tag_dict(self):
75
for k,v in self.repository._git.tags.iteritems():
76
obj = self.repository._git.get_object(v)
77
while isinstance(obj, Tag):
79
obj = self.repository._git.get_object(v)
80
if not isinstance(obj, Commit):
81
mutter("Tag %s points at object %r that is not a commit, "
84
ret[k] = self.branch.mapping.revision_id_foreign_to_bzr(v)
87
def set_tag(self, name, revid):
88
self.repository._git.tags[name] = revid
91
class GitBranchFormat(branch.BranchFormat):
93
def get_format_description(self):
96
def supports_tags(self):
99
def make_tags(self, branch):
100
if getattr(branch.repository, "get_refs", None) is not None:
101
from bzrlib.plugins.git.remote import RemoteGitTagDict
102
return RemoteGitTagDict(branch)
104
return LocalGitTagDict(branch)
107
class GitBranch(foreign.ForeignBranch):
108
"""An adapter to git repositories for bzr Branch objects."""
110
def __init__(self, bzrdir, repository, name, head, lockfiles):
111
self.repository = repository
112
self._format = GitBranchFormat()
113
self.control_files = lockfiles
115
super(GitBranch, self).__init__(repository.get_mapping())
118
self.base = bzrdir.transport.base
120
def _get_nick(self, local=False, possible_master_transports=None):
121
"""Find the nick name for this branch.
127
def _set_nick(self, nick):
128
raise NotImplementedError
130
nick = property(_get_nick, _set_nick)
132
def dpull(self, source, stop_revision=None):
133
if stop_revision is None:
134
stop_revision = source.last_revision()
135
# FIXME: Check for diverged branches
136
revidmap = self.repository.dfetch(source.repository, stop_revision)
138
self.generate_revision_history(revidmap[stop_revision])
141
def generate_revision_history(self, revid, old_revid=None):
142
# FIXME: Check that old_revid is in the ancestry of revid
143
newhead, self.mapping = self.mapping.revision_id_bzr_to_foreign(revid)
144
self._set_head(newhead)
146
def _set_head(self, head):
148
self.repository._git.set_ref(self.name, self.head)
150
def lock_write(self):
151
self.control_files.lock_write()
153
def get_stacked_on_url(self):
154
# Git doesn't do stacking (yet...)
157
def get_parent(self):
158
"""See Branch.get_parent()."""
159
# FIXME: Set "origin" url from .git/config ?
162
def set_parent(self, url):
163
# FIXME: Set "origin" url in .git/config ?
167
self.control_files.lock_read()
170
self.control_files.unlock()
172
def get_physical_lock_status(self):
176
class LocalGitBranch(GitBranch):
177
"""A local Git branch."""
180
def last_revision(self):
181
# perhaps should escape this ?
182
if self.head is None:
183
return revision.NULL_REVISION
184
return self.mapping.revision_id_foreign_to_bzr(self.head)
186
def _get_checkout_format(self):
187
"""Return the most suitable metadir for a checkout of this branch.
188
Weaves are used if this branch's repository uses weaves.
190
format = self.repository.bzrdir.checkout_metadir()
191
format.set_branch_format(self._format)
194
def create_checkout(self, to_location, revision_id=None, lightweight=False,
195
accelerator_tree=None, hardlink=False):
197
t = transport.get_transport(to_location)
199
format = self._get_checkout_format()
200
checkout = format.initialize_on_transport(t)
201
from_branch = branch.BranchReferenceFormat().initialize(checkout,
203
tree = checkout.create_workingtree(revision_id,
204
from_branch=from_branch, hardlink=hardlink)
207
return self._create_heavyweight_checkout(to_location, revision_id,
210
def _create_heavyweight_checkout(self, to_location, revision_id=None,
212
"""Create a new heavyweight checkout of this branch.
214
:param to_location: URL of location to create the new checkout in.
215
:param revision_id: Revision that should be the tip of the checkout.
216
:param hardlink: Whether to hardlink
217
:return: WorkingTree object of checkout.
219
checkout_branch = BzrDir.create_branch_convenience(
220
to_location, force_new_tree=False, format=get_rich_root_format())
221
checkout = checkout_branch.bzrdir
222
checkout_branch.bind(self)
223
# pull up to the specified revision_id to set the initial
224
# branch tip correctly, and seed it with history.
225
checkout_branch.pull(self, stop_revision=revision_id)
226
return checkout.create_workingtree(revision_id, hardlink=hardlink)
228
def _gen_revision_history(self):
229
if self.head is None:
231
ret = list(self.repository.iter_reverse_revision_history(
232
self.last_revision()))
236
def get_config(self):
237
return GitBranchConfig(self)
239
def get_push_location(self):
240
"""See Branch.get_push_location."""
241
push_loc = self.get_config().get_user_option('push_location')
244
def set_push_location(self, location):
245
"""See Branch.set_push_location."""
246
self.get_config().set_user_option('push_location', location,
247
store=config.STORE_LOCATION)
249
def supports_tags(self):
253
class GitBranchPullResult(branch.PullResult):
255
def report(self, to_file):
257
if self.old_revid == self.new_revid:
258
to_file.write('No revisions to pull.\n')
260
to_file.write('Now on revision %d (git sha: %s).\n' %
261
(self.new_revno, self.new_git_head))
262
self._show_tag_conficts(to_file)
265
class InterGitGenericBranch(branch.InterBranch):
266
"""InterBranch implementation that pulls from Git into bzr."""
269
def is_compatible(self, source, target):
270
return (isinstance(source, GitBranch) and
271
not isinstance(target, GitBranch))
273
def update_revisions(self, stop_revision=None, overwrite=False,
275
"""See InterBranch.update_revisions()."""
276
interrepo = repository.InterRepository.get(self.source.repository,
277
self.target.repository)
279
self._last_revid = None
280
def determine_wants(heads):
281
if not self.source.name in heads:
282
raise NoSuchRef(self.source.name, heads.keys())
283
if stop_revision is not None:
284
self._last_revid = stop_revision
285
self._head, mapping = self.source.repository.lookup_git_revid(
288
self._head = heads[self.source.name]
290
self.source.mapping.revision_id_foreign_to_bzr(self._head)
291
if self.target.repository.has_revision(self._last_revid):
294
interrepo.fetch_objects(determine_wants, self.source.mapping)
296
prev_last_revid = None
298
prev_last_revid = self.target.last_revision()
299
self.target.generate_revision_history(self._last_revid, prev_last_revid)
301
def pull(self, overwrite=False, stop_revision=None,
302
possible_transports=None, _hook_master=None, run_hooks=True,
303
_override_hook_target=None):
306
:param _hook_master: Private parameter - set the branch to
307
be supplied as the master to pull hooks.
308
:param run_hooks: Private parameter - if false, this branch
309
is being called because it's the master of the primary branch,
310
so it should not run its hooks.
311
:param _override_hook_target: Private parameter - set the branch to be
312
supplied as the target_branch to pull hooks.
314
result = GitBranchPullResult()
315
result.source_branch = self.source
316
if _override_hook_target is None:
317
result.target_branch = self.target
319
result.target_branch = _override_hook_target
320
self.source.lock_read()
322
# We assume that during 'pull' the target repository is closer than
324
graph = self.target.repository.get_graph(self.source.repository)
325
result.old_revno, result.old_revid = \
326
self.target.last_revision_info()
327
self.update_revisions(stop_revision, overwrite=overwrite,
329
result.new_git_head = self._head
330
result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
332
result.new_revno, result.new_revid = self.target.last_revision_info()
334
result.master_branch = _hook_master
335
result.local_branch = result.target_branch
337
result.master_branch = result.target_branch
338
result.local_branch = None
340
for hook in branch.Branch.hooks['post_pull']:
349
branch.InterBranch.register_optimiser(InterGitGenericBranch)
352
class InterGitRemoteLocalBranch(branch.InterBranch):
353
"""InterBranch implementation that pulls between Git branches."""
356
def is_compatible(self, source, target):
357
from bzrlib.plugins.git.remote import RemoteGitBranch
358
return (isinstance(source, RemoteGitBranch) and
359
isinstance(target, LocalGitBranch))
361
def pull(self, stop_revision=None, overwrite=False,
362
possible_transports=None):
363
result = GitPullResult()
364
result.source_branch = self.source
365
result.target_branch = self.target
366
interrepo = repository.InterRepository.get(self.source.repository,
367
self.target.repository)
368
result.old_revid = self.target.last_revision()
369
if stop_revision is None:
370
stop_revision = self.source.last_revision()
371
interrepo.fetch(revision_id=stop_revision)
372
self.target.generate_revision_history(stop_revision, result.old_revid)
373
result.new_revid = self.target.last_revision()
377
branch.InterBranch.register_optimiser(InterGitRemoteLocalBranch)