/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

Add description of git-v1 mapping.

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
    foreign,
 
29
    repository,
22
30
    revision,
23
31
    tag,
24
 
    )
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
    transport,
 
33
    )
 
34
from bzrlib.decorators import (
 
35
    needs_read_lock,
 
36
    )
 
37
from bzrlib.trace import (
 
38
    mutter,
 
39
    )
 
40
 
 
41
from bzrlib.plugins.git.errors import (
 
42
    NoSuchRef,
 
43
    )
 
44
 
 
45
 
 
46
class LocalGitTagDict(tag.BasicTags):
 
47
    """Dictionary with tags in a local repository."""
31
48
 
32
49
    def __init__(self, branch):
33
50
        self.branch = branch
35
52
 
36
53
    def get_tag_dict(self):
37
54
        ret = {}
38
 
        for tag in self.repository._git.tags:
39
 
            ret[tag.name] = self.branch.mapping.revision_id_foreign_to_bzr(tag.ref)
 
55
        for k,v in self.repository._git.tags.iteritems():
 
56
            obj = self.repository._git.get_object(v)
 
57
            while isinstance(obj, Tag):
 
58
                v = obj.object[1]
 
59
                obj = self.repository._git.get_object(v)
 
60
            if not isinstance(obj, Commit):
 
61
                mutter("Tag %s points at object %r that is not a commit, "
 
62
                       "ignoring", k, obj)
 
63
                continue
 
64
            ret[k] = self.branch.mapping.revision_id_foreign_to_bzr(v)
40
65
        return ret
41
66
 
42
67
    def set_tag(self, name, revid):
43
 
        raise NotImplementedError(self.set_tag)
 
68
        self.repository._git.tags[name] = revid
44
69
 
45
70
 
46
71
class GitBranchConfig(config.BranchConfig):
51
76
        # do not provide a BranchDataConfig
52
77
        self.option_sources = self.option_sources[0], self.option_sources[2]
53
78
 
54
 
    def set_user_option(self, name, value, local=False):
 
79
    def set_user_option(self, name, value, store=config.STORE_BRANCH,
 
80
            warn_masked=False):
55
81
        """Force local to True"""
56
 
        config.BranchConfig.set_user_option(self, name, value, local=True)
 
82
        config.BranchConfig.set_user_option(self, name, value,
 
83
            store=config.STORE_LOCATION, warn_masked=warn_masked)
57
84
 
58
85
 
59
86
class GitBranchFormat(branch.BranchFormat):
64
91
    def supports_tags(self):
65
92
        return True
66
93
 
67
 
 
68
 
class GitBranch(ForeignBranch):
 
94
    def make_tags(self, branch):
 
95
        if getattr(branch.repository, "get_refs", None) is not None:
 
96
            from bzrlib.plugins.git.remote import RemoteGitTagDict
 
97
            return RemoteGitTagDict(branch)
 
98
        else:
 
99
            return LocalGitTagDict(branch)
 
100
 
 
101
 
 
102
class GitBranch(foreign.ForeignBranch):
69
103
    """An adapter to git repositories for bzr Branch objects."""
70
104
 
71
105
    def __init__(self, bzrdir, repository, name, head, lockfiles):
72
106
        self.repository = repository
73
 
        super(GitBranch, self).__init__(default_mapping)
 
107
        self._format = GitBranchFormat()
74
108
        self.control_files = lockfiles
75
109
        self.bzrdir = bzrdir
 
110
        super(GitBranch, self).__init__(repository.get_mapping())
76
111
        self.name = name
77
112
        self.head = head
78
113
        self.base = bzrdir.transport.base
79
 
        self._format = GitBranchFormat()
 
114
 
 
115
    def _get_nick(self, local=False, possible_master_transports=None):
 
116
        """Find the nick name for this branch.
 
117
 
 
118
        :return: Branch nick
 
119
        """
 
120
        return self.name
 
121
 
 
122
    nick = property(_get_nick)
 
123
 
 
124
    def dpull(self, source, stop_revision=None):
 
125
        if stop_revision is None:
 
126
            stop_revision = source.last_revision()
 
127
        # FIXME: Check for diverged branches
 
128
        revidmap = self.repository.dfetch(source.repository, stop_revision)
 
129
        self.head, self.mapping = self.mapping.revision_id_bzr_to_foreign(
 
130
            revidmap[stop_revision])
 
131
        self.repository._git.set_ref(self.name, self.head)
 
132
        return revidmap
80
133
 
81
134
    def lock_write(self):
82
135
        self.control_files.lock_write()
87
140
 
88
141
    def get_parent(self):
89
142
        """See Branch.get_parent()."""
 
143
        # FIXME: Set "origin" url from .git/config ?
90
144
        return None
91
145
 
 
146
    def set_parent(self, url):
 
147
        # FIXME: Set "origin" url in .git/config ?
 
148
        pass
 
149
 
92
150
    def lock_read(self):
93
151
        self.control_files.lock_read()
94
152
 
100
158
 
101
159
 
102
160
class LocalGitBranch(GitBranch):
 
161
    """A local Git branch."""
103
162
 
104
163
    @needs_read_lock
105
164
    def last_revision(self):
108
167
            return revision.NULL_REVISION
109
168
        return self.mapping.revision_id_foreign_to_bzr(self.head)
110
169
 
111
 
    def _make_tags(self):
112
 
        return GitTagDict(self)
 
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):
 
180
        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)
 
193
 
 
194
    def _create_heavyweight_checkout(self, to_location, revision_id=None, 
 
195
                                     hardlink=False):
 
196
        """Create a new heavyweight checkout of this branch.
 
197
 
 
198
        :param to_location: URL of location to create the new checkout in.
 
199
        :param revision_id: Revision that should be the tip of the checkout.
 
200
        :param hardlink: Whether to hardlink
 
201
        :return: WorkingTree object of checkout.
 
202
        """
 
203
        checkout_branch = BzrDir.create_branch_convenience(
 
204
            to_location, force_new_tree=False, format=get_rich_root_format())
 
205
        checkout = checkout_branch.bzrdir
 
206
        checkout_branch.bind(self)
 
207
        # pull up to the specified revision_id to set the initial 
 
208
        # branch tip correctly, and seed it with history.
 
209
        checkout_branch.pull(self, stop_revision=revision_id)
 
210
        return checkout.create_workingtree(revision_id, hardlink=hardlink)
113
211
 
114
212
    def _gen_revision_history(self):
115
213
        if self.head is None:
116
214
            return []
117
 
        ret = list(self.repository.iter_reverse_revision_history(self.last_revision()))
 
215
        ret = list(self.repository.iter_reverse_revision_history(
 
216
            self.last_revision()))
118
217
        ret.reverse()
119
218
        return ret
120
219
 
129
228
    def set_push_location(self, location):
130
229
        """See Branch.set_push_location."""
131
230
        self.get_config().set_user_option('push_location', location,
132
 
                                          local=True)
 
231
                                          store=config.STORE_LOCATION)
133
232
 
134
233
    def supports_tags(self):
135
234
        return True
136
235
 
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
 
 
 
236
 
 
237
class InterGitGenericBranch(branch.InterBranch):
 
238
    """InterBranch implementation that pulls from Git into bzr."""
 
239
 
 
240
    @classmethod
 
241
    def is_compatible(self, source, target):
 
242
        return isinstance(source, GitBranch)
 
243
 
 
244
    def update_revisions(self, stop_revision=None, overwrite=False,
 
245
        graph=None):
 
246
        """See InterBranch.update_revisions()."""
 
247
        interrepo = repository.InterRepository.get(self.source.repository, 
 
248
            self.target.repository)
 
249
        self._last_revid = None
 
250
        def determine_wants(heads):
 
251
            if not self.source.name in heads:
 
252
                raise NoSuchRef(self.source.name, heads.keys())
 
253
            if stop_revision is not None:
 
254
                self._last_revid = stop_revision
 
255
                head, mapping = self.source.repository.lookup_git_revid(
 
256
                    stop_revision)
 
257
            else:
 
258
                head = heads[self.source.name]
 
259
                self._last_revid = \
 
260
                    self.source.mapping.revision_id_foreign_to_bzr(head)
 
261
            if self.target.repository.has_revision(self._last_revid):
 
262
                return []
 
263
            return [head]
 
264
        interrepo.fetch_objects(determine_wants, self.source.mapping)
 
265
        if overwrite:
 
266
            prev_last_revid = None
 
267
        else:
 
268
            prev_last_revid = self.target.last_revision()
 
269
        self.target.generate_revision_history(self._last_revid, prev_last_revid)
 
270
 
 
271
 
 
272
branch.InterBranch.register_optimiser(InterGitGenericBranch)