/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 progress reporting when creating index.

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,
22
29
    repository,
23
30
    revision,
24
31
    tag,
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):
 
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."""
38
48
 
39
49
    def __init__(self, branch):
40
50
        self.branch = branch
48
58
                v = obj.object[1]
49
59
                obj = self.repository._git.get_object(v)
50
60
            if not isinstance(obj, Commit):
51
 
                mutter("Tag %s points at object %r that is not a commit, ignoring", k, obj)
 
61
                mutter("Tag %s points at object %r that is not a commit, "
 
62
                       "ignoring", k, obj)
52
63
                continue
53
64
            ret[k] = self.branch.mapping.revision_id_foreign_to_bzr(v)
54
65
        return ret
65
76
        # do not provide a BranchDataConfig
66
77
        self.option_sources = self.option_sources[0], self.option_sources[2]
67
78
 
68
 
    def set_user_option(self, name, value, store=config.STORE_BRANCH, warn_masked=False):
 
79
    def set_user_option(self, name, value, store=config.STORE_BRANCH,
 
80
            warn_masked=False):
69
81
        """Force local to True"""
70
 
        config.BranchConfig.set_user_option(self, name, value, store=config.STORE_LOCATION, warn_masked=warn_masked)
 
82
        config.BranchConfig.set_user_option(self, name, value,
 
83
            store=config.STORE_LOCATION, warn_masked=warn_masked)
71
84
 
72
85
 
73
86
class GitBranchFormat(branch.BranchFormat):
78
91
    def supports_tags(self):
79
92
        return True
80
93
 
81
 
 
82
 
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):
83
103
    """An adapter to git repositories for bzr Branch objects."""
84
104
 
85
105
    def __init__(self, bzrdir, repository, name, head, lockfiles):
86
106
        self.repository = repository
87
 
        super(GitBranch, self).__init__(repository.get_mapping())
 
107
        self._format = GitBranchFormat()
88
108
        self.control_files = lockfiles
89
109
        self.bzrdir = bzrdir
 
110
        super(GitBranch, self).__init__(repository.get_mapping())
90
111
        self.name = name
91
112
        self.head = head
92
113
        self.base = bzrdir.transport.base
93
 
        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
    def _set_nick(self, nick):
 
123
        raise NotImplementedError
 
124
 
 
125
    nick = property(_get_nick, _set_nick)
94
126
 
95
127
    def dpull(self, source, stop_revision=None):
96
128
        if stop_revision is None:
97
129
            stop_revision = source.last_revision()
98
130
        # FIXME: Check for diverged branches
99
131
        revidmap = self.repository.dfetch(source.repository, stop_revision)
100
 
        self.head, self.mapping = self.mapping.revision_id_bzr_to_foreign(revidmap[stop_revision])
 
132
        self.head, self.mapping = self.mapping.revision_id_bzr_to_foreign(
 
133
            revidmap[stop_revision])
 
134
        self.repository._git.set_ref(self.name, self.head)
101
135
        return revidmap
102
136
 
103
137
    def lock_write(self):
109
143
 
110
144
    def get_parent(self):
111
145
        """See Branch.get_parent()."""
 
146
        # FIXME: Set "origin" url from .git/config ?
112
147
        return None
113
148
 
114
149
    def set_parent(self, url):
 
150
        # FIXME: Set "origin" url in .git/config ?
115
151
        pass
116
152
 
117
153
    def lock_read(self):
125
161
 
126
162
 
127
163
class LocalGitBranch(GitBranch):
 
164
    """A local Git branch."""
128
165
 
129
166
    @needs_read_lock
130
167
    def last_revision(self):
133
170
            return revision.NULL_REVISION
134
171
        return self.mapping.revision_id_foreign_to_bzr(self.head)
135
172
 
136
 
    def create_checkout(self, to_location, revision_id=None, 
137
 
                        lightweight=False, accelerator_tree=None, hardlink=False):
 
173
    def _get_checkout_format(self):
 
174
        """Return the most suitable metadir for a checkout of this branch.
 
175
        Weaves are used if this branch's repository uses weaves.
 
176
        """
 
177
        format = self.repository.bzrdir.checkout_metadir()
 
178
        format.set_branch_format(self._format)
 
179
        return format
 
180
 
 
181
    def create_checkout(self, to_location, revision_id=None, lightweight=False,
 
182
        accelerator_tree=None, hardlink=False):
138
183
        if lightweight:
139
 
            raise LightWeightCheckoutsNotSupported()
140
 
        return self._create_heavyweight_checkout(to_location, revision_id, hardlink)
 
184
            t = transport.get_transport(to_location)
 
185
            t.ensure_base()
 
186
            format = self._get_checkout_format()
 
187
            checkout = format.initialize_on_transport(t)
 
188
            from_branch = branch.BranchReferenceFormat().initialize(checkout, 
 
189
                self)
 
190
            tree = checkout.create_workingtree(revision_id,
 
191
                from_branch=from_branch, hardlink=hardlink)
 
192
            return tree
 
193
        else:
 
194
            return self._create_heavyweight_checkout(to_location, revision_id,
 
195
            hardlink)
141
196
 
142
197
    def _create_heavyweight_checkout(self, to_location, revision_id=None, 
143
198
                                     hardlink=False):
157
212
        checkout_branch.pull(self, stop_revision=revision_id)
158
213
        return checkout.create_workingtree(revision_id, hardlink=hardlink)
159
214
 
160
 
    def _make_tags(self):
161
 
        return GitTagDict(self)
162
 
 
163
215
    def _gen_revision_history(self):
164
216
        if self.head is None:
165
217
            return []
166
 
        ret = list(self.repository.iter_reverse_revision_history(self.last_revision()))
 
218
        ret = list(self.repository.iter_reverse_revision_history(
 
219
            self.last_revision()))
167
220
        ret.reverse()
168
221
        return ret
169
222
 
183
236
    def supports_tags(self):
184
237
        return True
185
238
 
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
 
 
193
239
 
194
240
class InterGitGenericBranch(branch.InterBranch):
 
241
    """InterBranch implementation that pulls from Git into bzr."""
195
242
 
196
243
    @classmethod
197
244
    def is_compatible(self, source, target):
200
247
    def update_revisions(self, stop_revision=None, overwrite=False,
201
248
        graph=None):
202
249
        """See InterBranch.update_revisions()."""
203
 
        # TODO: stop_revision, overwrite
204
 
        interrepo = repository.InterRepository.get(self.source.repository, self.target.repository)
 
250
        interrepo = repository.InterRepository.get(self.source.repository, 
 
251
            self.target.repository)
205
252
        self._last_revid = None
206
253
        def determine_wants(heads):
207
254
            if not self.source.name in heads:
208
 
                raise BzrError("No such remote branch '%s', found: %r" % (
209
 
                    self.source.name, heads.keys()))
210
 
            head = heads[self.source.name]
211
 
            self._last_revid = self.source.mapping.revision_id_foreign_to_bzr(head)
 
255
                raise NoSuchRef(self.source.name, heads.keys())
 
256
            if stop_revision is not None:
 
257
                self._last_revid = stop_revision
 
258
                head, mapping = self.source.repository.lookup_git_revid(
 
259
                    stop_revision)
 
260
            else:
 
261
                head = heads[self.source.name]
 
262
                self._last_revid = \
 
263
                    self.source.mapping.revision_id_foreign_to_bzr(head)
212
264
            if self.target.repository.has_revision(self._last_revid):
213
265
                return []
214
266
            return [head]
215
267
        interrepo.fetch_objects(determine_wants, self.source.mapping)
216
 
        self.target.generate_revision_history(self._last_revid)
 
268
        if overwrite:
 
269
            prev_last_revid = None
 
270
        else:
 
271
            prev_last_revid = self.target.last_revision()
 
272
        self.target.generate_revision_history(self._last_revid, prev_last_revid)
217
273
 
218
274
 
219
275
branch.InterBranch.register_optimiser(InterGitGenericBranch)