1
1
# Copyright (C) 2007 Canonical Ltd
2
# Copyright (C) 2009 Jelmer Vernooij <jelmer@samba.org>
4
3
# This program is free software; you can redistribute it and/or modify
5
4
# it under the terms of the GNU General Public License as published by
18
17
"""An adapter between a Git Branch and a Bazaar Branch"""
20
from dulwich.objects import (
25
19
from bzrlib import (
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."""
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):
51
39
def __init__(self, branch):
52
40
self.branch = branch
61
49
obj = self.repository._git.get_object(v)
62
50
if not isinstance(obj, Commit):
63
mutter("Tag %s points at object %r that is not a commit, "
51
mutter("Tag %s points at object %r that is not a commit, ignoring", k, obj)
66
53
ret[k] = self.branch.mapping.revision_id_foreign_to_bzr(v)
78
65
# do not provide a BranchDataConfig
79
66
self.option_sources = self.option_sources[0], self.option_sources[2]
81
def set_user_option(self, name, value, store=config.STORE_BRANCH,
68
def set_user_option(self, name, value, store=config.STORE_BRANCH, warn_masked=False):
83
69
"""Force local to True"""
84
config.BranchConfig.set_user_option(self, name, value,
85
store=config.STORE_LOCATION, warn_masked=warn_masked)
70
config.BranchConfig.set_user_option(self, name, value, store=config.STORE_LOCATION, warn_masked=warn_masked)
88
73
class GitBranchFormat(branch.BranchFormat):
93
78
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)
104
82
class GitBranch(ForeignBranch):
105
83
"""An adapter to git repositories for bzr Branch objects."""
107
85
def __init__(self, bzrdir, repository, name, head, lockfiles):
108
86
self.repository = repository
109
self._format = GitBranchFormat()
87
super(GitBranch, self).__init__(repository.get_mapping())
110
88
self.control_files = lockfiles
111
89
self.bzrdir = bzrdir
112
super(GitBranch, self).__init__(repository.get_mapping())
115
92
self.base = bzrdir.transport.base
117
def _get_nick(self, local=False, possible_master_transports=None):
118
"""Find the nick name for this branch.
124
nick = property(_get_nick)
93
self._format = GitBranchFormat()
126
95
def dpull(self, source, stop_revision=None):
127
96
if stop_revision is None:
128
97
stop_revision = source.last_revision()
129
98
# FIXME: Check for diverged branches
130
99
revidmap = self.repository.dfetch(source.repository, 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)
100
self.head, self.mapping = self.mapping.revision_id_bzr_to_foreign(revidmap[stop_revision])
136
103
def lock_write(self):
143
110
def get_parent(self):
144
111
"""See Branch.get_parent()."""
145
# FIXME: Set "origin" url from .git/config ?
148
114
def set_parent(self, url):
149
# FIXME: Set "origin" url in .git/config ?
152
117
def lock_read(self):
169
133
return revision.NULL_REVISION
170
134
return self.mapping.revision_id_foreign_to_bzr(self.head)
172
def _get_checkout_format(self):
173
"""Return the most suitable metadir for a checkout of this branch.
174
Weaves are used if this branch's repository uses weaves.
176
format = self.repository.bzrdir.checkout_metadir()
177
format.set_branch_format(self._format)
180
def create_checkout(self, to_location, revision_id=None, lightweight=False,
181
accelerator_tree=None, hardlink=False):
136
def create_checkout(self, to_location, revision_id=None,
137
lightweight=False, accelerator_tree=None, hardlink=False):
183
t = transport.get_transport(to_location)
185
format = self._get_checkout_format()
186
checkout = format.initialize_on_transport(t)
187
from_branch = branch.BranchReferenceFormat().initialize(checkout,
189
tree = checkout.create_workingtree(revision_id,
190
from_branch=from_branch, hardlink=hardlink)
193
return self._create_heavyweight_checkout(to_location, revision_id,
139
raise LightWeightCheckoutsNotSupported()
140
return self._create_heavyweight_checkout(to_location, revision_id, hardlink)
196
142
def _create_heavyweight_checkout(self, to_location, revision_id=None,
211
157
checkout_branch.pull(self, stop_revision=revision_id)
212
158
return checkout.create_workingtree(revision_id, hardlink=hardlink)
160
def _make_tags(self):
161
return GitTagDict(self)
214
163
def _gen_revision_history(self):
215
164
if self.head is None:
217
ret = list(self.repository.iter_reverse_revision_history(
218
self.last_revision()))
166
ret = list(self.repository.iter_reverse_revision_history(self.last_revision()))
235
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)
239
194
class InterGitGenericBranch(branch.InterBranch):
240
"""InterBranch implementation that pulls from Git into bzr."""
243
197
def is_compatible(self, source, target):
246
200
def update_revisions(self, stop_revision=None, overwrite=False,
248
202
"""See InterBranch.update_revisions()."""
249
interrepo = repository.InterRepository.get(self.source.repository,
250
self.target.repository)
203
# TODO: stop_revision, overwrite
204
interrepo = repository.InterRepository.get(self.source.repository, self.target.repository)
251
205
self._last_revid = None
252
206
def determine_wants(heads):
253
207
if not self.source.name in heads:
254
raise NoSuchRef(self.source.name, heads.keys())
255
if stop_revision is not None:
256
self._last_revid = stop_revision
257
head, mapping = self.source.repository.lookup_git_revid(
260
head = heads[self.source.name]
262
self.source.mapping.revision_id_foreign_to_bzr(head)
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)
263
212
if self.target.repository.has_revision(self._last_revid):
266
215
interrepo.fetch_objects(determine_wants, self.source.mapping)
268
prev_last_revid = None
270
prev_last_revid = self.target.last_revision()
271
self.target.generate_revision_history(self._last_revid, prev_last_revid)
216
self.target.set_last_revision(last_revid)
274
219
branch.InterBranch.register_optimiser(InterGitGenericBranch)