/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

merge versionedfiles support.

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