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
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):
32
from bzrlib.decorators import (
35
from bzrlib.trace import (
39
from bzrlib.plugins.git.errors import (
42
from bzrlib.plugins.git.foreign import (
45
from bzrlib.plugins.git.errors import (
46
LightWeightCheckoutsNotSupported,
50
class LocalGitTagDict(tag.BasicTags):
51
"""Dictionary with tags in a local repository."""
39
53
def __init__(self, branch):
40
54
self.branch = branch
49
63
obj = self.repository._git.get_object(v)
50
64
if not isinstance(obj, Commit):
51
mutter("Tag %s points at object %r that is not a commit, ignoring", k, obj)
65
mutter("Tag %s points at object %r that is not a commit, "
53
68
ret[k] = self.branch.mapping.revision_id_foreign_to_bzr(v)
65
80
# do not provide a BranchDataConfig
66
81
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):
83
def set_user_option(self, name, value, store=config.STORE_BRANCH,
69
85
"""Force local to True"""
70
config.BranchConfig.set_user_option(self, name, value, store=config.STORE_LOCATION, warn_masked=warn_masked)
86
config.BranchConfig.set_user_option(self, name, value,
87
store=config.STORE_LOCATION, warn_masked=warn_masked)
73
90
class GitBranchFormat(branch.BranchFormat):
78
95
def supports_tags(self):
98
def make_tags(self, branch):
99
if getattr(branch.repository, "get_refs", None) is not None:
100
from bzrlib.plugins.git.remote import RemoteGitTagDict
101
return RemoteGitTagDict(branch)
103
return LocalGitTagDict(branch)
82
106
class GitBranch(ForeignBranch):
83
107
"""An adapter to git repositories for bzr Branch objects."""
85
109
def __init__(self, bzrdir, repository, name, head, lockfiles):
86
110
self.repository = repository
111
self._format = GitBranchFormat()
87
112
super(GitBranch, self).__init__(repository.get_mapping())
88
113
self.control_files = lockfiles
89
114
self.bzrdir = bzrdir
92
117
self.base = bzrdir.transport.base
93
self._format = GitBranchFormat()
95
119
def dpull(self, source, stop_revision=None):
96
120
if stop_revision is None:
97
121
stop_revision = source.last_revision()
98
122
# FIXME: Check for diverged branches
99
123
revidmap = self.repository.dfetch(source.repository, stop_revision)
100
self.head, self.mapping = self.mapping.revision_id_bzr_to_foreign(revidmap[stop_revision])
124
self.head, self.mapping = self.mapping.revision_id_bzr_to_foreign(
125
revidmap[stop_revision])
126
self.repository._git.set_ref(self.name, self.head)
103
129
def lock_write(self):
133
160
return revision.NULL_REVISION
134
161
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):
163
def create_checkout(self, to_location, revision_id=None, lightweight=False,
164
accelerator_tree=None, hardlink=False):
139
166
raise LightWeightCheckoutsNotSupported()
140
return self._create_heavyweight_checkout(to_location, revision_id, hardlink)
167
return self._create_heavyweight_checkout(to_location, revision_id,
142
170
def _create_heavyweight_checkout(self, to_location, revision_id=None,
157
185
checkout_branch.pull(self, stop_revision=revision_id)
158
186
return checkout.create_workingtree(revision_id, hardlink=hardlink)
160
def _make_tags(self):
161
return GitTagDict(self)
163
188
def _gen_revision_history(self):
164
189
if self.head is None:
166
ret = list(self.repository.iter_reverse_revision_history(self.last_revision()))
191
ret = list(self.repository.iter_reverse_revision_history(
192
self.last_revision()))
183
209
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
213
class InterGitGenericBranch(branch.InterBranch):
214
"""InterBranch implementation that pulls from Git into bzr."""
197
217
def is_compatible(self, source, target):
202
222
"""See InterBranch.update_revisions()."""
203
223
# TODO: stop_revision, overwrite
204
interrepo = repository.InterRepository.get(self.source.repository, self.target.repository)
224
interrepo = repository.InterRepository.get(self.source.repository,
225
self.target.repository)
205
226
self._last_revid = None
206
227
def determine_wants(heads):
207
228
if not self.source.name in heads:
208
raise BzrError("No such remote branch '%s', found: %r" % (
209
self.source.name, heads.keys()))
229
raise NoSuchRef(self.source.name, heads.keys())
210
230
head = heads[self.source.name]
211
self._last_revid = self.source.mapping.revision_id_foreign_to_bzr(head)
231
self._last_revid = self.source.mapping.revision_id_foreign_to_bzr(
212
233
if self.target.repository.has_revision(self._last_revid):