/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

Support bzr.dev.

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>
2
3
#
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
16
17
 
17
18
"""An adapter between a Git Branch and a Bazaar Branch"""
18
19
 
 
20
from dulwich.objects import (
 
21
    Commit,
 
22
    Tag,
 
23
    )
 
24
 
19
25
from bzrlib import (
20
26
    branch,
21
27
    config,
 
28
    repository,
22
29
    revision,
23
30
    tag,
24
31
    )
25
 
from bzrlib.decorators import needs_read_lock
26
 
 
27
 
from bzrlib.plugins.git.foreign import ForeignBranch
28
 
from bzrlib.plugins.git.mapping import default_mapping
29
 
 
30
 
class GitTagDict(tag.BasicTags):
 
32
from bzrlib.decorators import (
 
33
    needs_read_lock,
 
34
    )
 
35
from bzrlib.trace import (
 
36
    mutter,
 
37
    )
 
38
 
 
39
from bzrlib.plugins.git.errors import (
 
40
    NoSuchRef,
 
41
    )
 
42
from bzrlib.plugins.git.foreign import (
 
43
    ForeignBranch,
 
44
    )
 
45
from bzrlib.plugins.git.errors import (
 
46
    LightWeightCheckoutsNotSupported,
 
47
    )
 
48
 
 
49
 
 
50
class LocalGitTagDict(tag.BasicTags):
 
51
    """Dictionary with tags in a local repository."""
31
52
 
32
53
    def __init__(self, branch):
33
54
        self.branch = branch
35
56
 
36
57
    def get_tag_dict(self):
37
58
        ret = {}
38
 
        for tag in self.repository._git.tags:
39
 
            ret[tag.name] = self.branch.mapping.revision_id_foreign_to_bzr(tag.ref)
 
59
        for k,v in self.repository._git.tags.iteritems():
 
60
            obj = self.repository._git.get_object(v)
 
61
            while isinstance(obj, Tag):
 
62
                v = obj.object[1]
 
63
                obj = self.repository._git.get_object(v)
 
64
            if not isinstance(obj, Commit):
 
65
                mutter("Tag %s points at object %r that is not a commit, "
 
66
                       "ignoring", k, obj)
 
67
                continue
 
68
            ret[k] = self.branch.mapping.revision_id_foreign_to_bzr(v)
40
69
        return ret
41
70
 
42
71
    def set_tag(self, name, revid):
43
 
        raise NotImplementedError(self.set_tag)
 
72
        self.repository._git.tags[name] = revid
44
73
 
45
74
 
46
75
class GitBranchConfig(config.BranchConfig):
51
80
        # do not provide a BranchDataConfig
52
81
        self.option_sources = self.option_sources[0], self.option_sources[2]
53
82
 
54
 
    def set_user_option(self, name, value, local=False):
 
83
    def set_user_option(self, name, value, store=config.STORE_BRANCH,
 
84
            warn_masked=False):
55
85
        """Force local to True"""
56
 
        config.BranchConfig.set_user_option(self, name, value, local=True)
 
86
        config.BranchConfig.set_user_option(self, name, value,
 
87
            store=config.STORE_LOCATION, warn_masked=warn_masked)
57
88
 
58
89
 
59
90
class GitBranchFormat(branch.BranchFormat):
64
95
    def supports_tags(self):
65
96
        return True
66
97
 
 
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)
 
102
        else:
 
103
            return LocalGitTagDict(branch)
 
104
 
67
105
 
68
106
class GitBranch(ForeignBranch):
69
107
    """An adapter to git repositories for bzr Branch objects."""
70
108
 
71
109
    def __init__(self, bzrdir, repository, name, head, lockfiles):
72
110
        self.repository = repository
73
 
        super(GitBranch, self).__init__(default_mapping)
 
111
        self._format = GitBranchFormat()
 
112
        super(GitBranch, self).__init__(repository.get_mapping())
74
113
        self.control_files = lockfiles
75
114
        self.bzrdir = bzrdir
76
115
        self.name = name
77
116
        self.head = head
78
117
        self.base = bzrdir.transport.base
79
 
        self._format = GitBranchFormat()
 
118
 
 
119
    def dpull(self, source, stop_revision=None):
 
120
        if stop_revision is None:
 
121
            stop_revision = source.last_revision()
 
122
        # FIXME: Check for diverged branches
 
123
        revidmap = self.repository.dfetch(source.repository, 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)
 
127
        return revidmap
80
128
 
81
129
    def lock_write(self):
82
130
        self.control_files.lock_write()
89
137
        """See Branch.get_parent()."""
90
138
        return None
91
139
 
 
140
    def set_parent(self, url):
 
141
        pass
 
142
 
92
143
    def lock_read(self):
93
144
        self.control_files.lock_read()
94
145
 
100
151
 
101
152
 
102
153
class LocalGitBranch(GitBranch):
 
154
    """A local Git branch."""
103
155
 
104
156
    @needs_read_lock
105
157
    def last_revision(self):
108
160
            return revision.NULL_REVISION
109
161
        return self.mapping.revision_id_foreign_to_bzr(self.head)
110
162
 
111
 
    def _make_tags(self):
112
 
        return GitTagDict(self)
 
163
    def create_checkout(self, to_location, revision_id=None, lightweight=False,
 
164
        accelerator_tree=None, hardlink=False):
 
165
        if lightweight:
 
166
            raise LightWeightCheckoutsNotSupported()
 
167
        return self._create_heavyweight_checkout(to_location, revision_id,
 
168
            hardlink)
 
169
 
 
170
    def _create_heavyweight_checkout(self, to_location, revision_id=None, 
 
171
                                     hardlink=False):
 
172
        """Create a new heavyweight checkout of this branch.
 
173
 
 
174
        :param to_location: URL of location to create the new checkout in.
 
175
        :param revision_id: Revision that should be the tip of the checkout.
 
176
        :param hardlink: Whether to hardlink
 
177
        :return: WorkingTree object of checkout.
 
178
        """
 
179
        checkout_branch = BzrDir.create_branch_convenience(
 
180
            to_location, force_new_tree=False, format=get_rich_root_format())
 
181
        checkout = checkout_branch.bzrdir
 
182
        checkout_branch.bind(self)
 
183
        # pull up to the specified revision_id to set the initial 
 
184
        # branch tip correctly, and seed it with history.
 
185
        checkout_branch.pull(self, stop_revision=revision_id)
 
186
        return checkout.create_workingtree(revision_id, hardlink=hardlink)
113
187
 
114
188
    def _gen_revision_history(self):
115
189
        if self.head is None:
116
190
            return []
117
 
        ret = list(self.repository.iter_reverse_revision_history(self.last_revision()))
 
191
        ret = list(self.repository.iter_reverse_revision_history(
 
192
            self.last_revision()))
118
193
        ret.reverse()
119
194
        return ret
120
195
 
129
204
    def set_push_location(self, location):
130
205
        """See Branch.set_push_location."""
131
206
        self.get_config().set_user_option('push_location', location,
132
 
                                          local=True)
 
207
                                          store=config.STORE_LOCATION)
133
208
 
134
209
    def supports_tags(self):
135
210
        return True
136
211
 
137
 
    def sprout(self, to_bzrdir, revision_id=None):
138
 
        """See Branch.sprout()."""
139
 
        result = to_bzrdir.create_branch()
140
 
        self.copy_content_into(result, revision_id=revision_id)
141
 
        result.set_parent(self.bzrdir.root_transport.base)
142
 
        return result
143
 
 
 
212
 
 
213
class InterGitGenericBranch(branch.InterBranch):
 
214
    """InterBranch implementation that pulls from Git into bzr."""
 
215
 
 
216
    @classmethod
 
217
    def is_compatible(self, source, target):
 
218
        return isinstance(source, GitBranch)
 
219
 
 
220
    def update_revisions(self, stop_revision=None, overwrite=False,
 
221
        graph=None):
 
222
        """See InterBranch.update_revisions()."""
 
223
        # TODO: stop_revision, overwrite
 
224
        interrepo = repository.InterRepository.get(self.source.repository, 
 
225
            self.target.repository)
 
226
        self._last_revid = None
 
227
        def determine_wants(heads):
 
228
            if not self.source.name in heads:
 
229
                raise NoSuchRef(self.source.name, heads.keys())
 
230
            head = heads[self.source.name]
 
231
            self._last_revid = self.source.mapping.revision_id_foreign_to_bzr(
 
232
                head)
 
233
            if self.target.repository.has_revision(self._last_revid):
 
234
                return []
 
235
            return [head]
 
236
        interrepo.fetch_objects(determine_wants, self.source.mapping)
 
237
        self.target.generate_revision_history(self._last_revid)
 
238
 
 
239
 
 
240
branch.InterBranch.register_optimiser(InterGitGenericBranch)