/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to branch.py

Partially fix pull.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Copyright (C) 2007 Canonical Ltd
2
 
# Copyright (C) 2009 Jelmer Vernooij <jelmer@samba.org>
3
2
#
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
17
16
 
18
17
"""An adapter between a Git Branch and a Bazaar Branch"""
19
18
 
20
 
from dulwich.objects import (
21
 
    Commit,
22
 
    Tag,
23
 
    )
24
 
 
25
19
from bzrlib import (
26
20
    branch,
27
21
    config,
28
22
    repository,
29
23
    revision,
30
24
    tag,
31
 
    transport,
32
 
    )
33
 
from bzrlib.decorators import (
34
 
    needs_read_lock,
35
 
    )
36
 
from bzrlib.trace import (
37
 
    mutter,
38
 
    )
39
 
 
40
 
from bzrlib.plugins.git.errors import (
41
 
    NoSuchRef,
42
 
    )
43
 
from bzrlib.plugins.git.foreign import (
44
 
    ForeignBranch,
45
 
    )
46
 
 
47
 
 
48
 
class LocalGitTagDict(tag.BasicTags):
49
 
    """Dictionary with tags in a local repository."""
 
25
    )
 
26
from bzrlib.decorators import needs_read_lock
 
27
from bzrlib.trace import mutter
 
28
 
 
29
from bzrlib.plugins.git.foreign import ForeignBranch
 
30
from bzrlib.plugins.git.errors import LightWeightCheckoutsNotSupported
 
31
 
 
32
from dulwich.objects import (
 
33
        Commit,
 
34
        Tag,
 
35
        )
 
36
 
 
37
class GitTagDict(tag.BasicTags):
50
38
 
51
39
    def __init__(self, branch):
52
40
        self.branch = branch
60
48
                v = obj.object[1]
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, "
64
 
                       "ignoring", k, obj)
 
51
                mutter("Tag %s points at object %r that is not a commit, ignoring", k, obj)
65
52
                continue
66
53
            ret[k] = self.branch.mapping.revision_id_foreign_to_bzr(v)
67
54
        return ret
78
65
        # do not provide a BranchDataConfig
79
66
        self.option_sources = self.option_sources[0], self.option_sources[2]
80
67
 
81
 
    def set_user_option(self, name, value, store=config.STORE_BRANCH,
82
 
            warn_masked=False):
 
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)
86
71
 
87
72
 
88
73
class GitBranchFormat(branch.BranchFormat):
93
78
    def supports_tags(self):
94
79
        return True
95
80
 
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)
100
 
        else:
101
 
            return LocalGitTagDict(branch)
102
 
 
103
81
 
104
82
class GitBranch(ForeignBranch):
105
83
    """An adapter to git repositories for bzr Branch objects."""
106
84
 
107
85
    def __init__(self, bzrdir, repository, name, head, lockfiles):
108
86
        self.repository = repository
109
 
        self._format = GitBranchFormat()
110
87
        super(GitBranch, self).__init__(repository.get_mapping())
111
88
        self.control_files = lockfiles
112
89
        self.bzrdir = bzrdir
113
90
        self.name = name
114
91
        self.head = head
115
92
        self.base = bzrdir.transport.base
116
 
 
117
 
    def _get_nick(self, local=False, possible_master_transports=None):
118
 
        """Find the nick name for this branch.
119
 
 
120
 
        :return: Branch nick
121
 
        """
122
 
        return self.name
123
 
 
124
 
    nick = property(_get_nick)
 
93
        self._format = GitBranchFormat()
125
94
 
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])
134
101
        return revidmap
135
102
 
136
103
    def lock_write(self):
158
125
 
159
126
 
160
127
class LocalGitBranch(GitBranch):
161
 
    """A local Git branch."""
162
128
 
163
129
    @needs_read_lock
164
130
    def last_revision(self):
167
133
            return revision.NULL_REVISION
168
134
        return self.mapping.revision_id_foreign_to_bzr(self.head)
169
135
 
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.
173
 
        """
174
 
        format = self.repository.bzrdir.checkout_metadir()
175
 
        format.set_branch_format(self._format)
176
 
        return format
177
 
 
178
 
    def create_checkout(self, to_location, revision_id=None, lightweight=False,
179
 
        accelerator_tree=None, hardlink=False):
 
136
    def create_checkout(self, to_location, revision_id=None, 
 
137
                        lightweight=False, accelerator_tree=None, hardlink=False):
180
138
        if lightweight:
181
 
            t = transport.get_transport(to_location)
182
 
            t.ensure_base()
183
 
            format = self._get_checkout_format()
184
 
            checkout = format.initialize_on_transport(t)
185
 
            from_branch = branch.BranchReferenceFormat().initialize(checkout, 
186
 
                self)
187
 
            tree = checkout.create_workingtree(revision_id,
188
 
                from_branch=from_branch, hardlink=hardlink)
189
 
            return tree
190
 
        else:
191
 
            return self._create_heavyweight_checkout(to_location, revision_id,
192
 
            hardlink)
 
139
            raise LightWeightCheckoutsNotSupported()
 
140
        return self._create_heavyweight_checkout(to_location, revision_id, hardlink)
193
141
 
194
142
    def _create_heavyweight_checkout(self, to_location, revision_id=None, 
195
143
                                     hardlink=False):
209
157
        checkout_branch.pull(self, stop_revision=revision_id)
210
158
        return checkout.create_workingtree(revision_id, hardlink=hardlink)
211
159
 
 
160
    def _make_tags(self):
 
161
        return GitTagDict(self)
 
162
 
212
163
    def _gen_revision_history(self):
213
164
        if self.head is None:
214
165
            return []
215
 
        ret = list(self.repository.iter_reverse_revision_history(
216
 
            self.last_revision()))
 
166
        ret = list(self.repository.iter_reverse_revision_history(self.last_revision()))
217
167
        ret.reverse()
218
168
        return ret
219
169
 
233
183
    def supports_tags(self):
234
184
        return True
235
185
 
 
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)
 
191
        return result
 
192
 
236
193
 
237
194
class InterGitGenericBranch(branch.InterBranch):
238
 
    """InterBranch implementation that pulls from Git into bzr."""
239
195
 
240
196
    @classmethod
241
197
    def is_compatible(self, source, target):
245
201
        graph=None):
246
202
        """See InterBranch.update_revisions()."""
247
203
        # TODO: stop_revision, overwrite
248
 
        interrepo = repository.InterRepository.get(self.source.repository, 
249
 
            self.target.repository)
 
204
        interrepo = repository.InterRepository.get(self.source.repository, self.target.repository)
250
205
        self._last_revid = None
251
206
        def determine_wants(heads):
252
207
            if not self.source.name in heads:
253
 
                raise NoSuchRef(self.source.name, heads.keys())
 
208
                raise BzrError("No such remote branch '%s', found: %r" % (
 
209
                    self.source.name, heads.keys()))
254
210
            head = heads[self.source.name]
255
 
            self._last_revid = self.source.mapping.revision_id_foreign_to_bzr(
256
 
                head)
 
211
            self._last_revid = self.source.mapping.revision_id_foreign_to_bzr(head)
257
212
            if self.target.repository.has_revision(self._last_revid):
258
213
                return []
259
214
            return [head]
260
215
        interrepo.fetch_objects(determine_wants, self.source.mapping)
261
 
        # FIXME: Check that self._last_revid is a descendant of self.target.last_revision()
262
216
        self.target.generate_revision_history(self._last_revid)
263
217
 
264
218