/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 thin-pack work.

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,
29
23
    revision,
30
24
    tag,
31
25
    )
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."""
 
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):
49
38
 
50
39
    def __init__(self, branch):
51
40
        self.branch = branch
59
48
                v = obj.object[1]
60
49
                obj = self.repository._git.get_object(v)
61
50
            if not isinstance(obj, Commit):
62
 
                mutter("Tag %s points at object %r that is not a commit, "
63
 
                       "ignoring", k, obj)
 
51
                mutter("Tag %s points at object %r that is not a commit, ignoring", k, obj)
64
52
                continue
65
53
            ret[k] = self.branch.mapping.revision_id_foreign_to_bzr(v)
66
54
        return ret
77
65
        # do not provide a BranchDataConfig
78
66
        self.option_sources = self.option_sources[0], self.option_sources[2]
79
67
 
80
 
    def set_user_option(self, name, value, store=config.STORE_BRANCH,
81
 
            warn_masked=False):
 
68
    def set_user_option(self, name, value, store=config.STORE_BRANCH, warn_masked=False):
82
69
        """Force local to True"""
83
 
        config.BranchConfig.set_user_option(self, name, value,
84
 
            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)
85
71
 
86
72
 
87
73
class GitBranchFormat(branch.BranchFormat):
92
78
    def supports_tags(self):
93
79
        return True
94
80
 
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
81
 
103
82
class GitBranch(ForeignBranch):
104
83
    """An adapter to git repositories for bzr Branch objects."""
105
84
 
106
85
    def __init__(self, bzrdir, repository, name, head, lockfiles):
107
86
        self.repository = repository
108
 
        self._format = GitBranchFormat()
109
87
        super(GitBranch, self).__init__(repository.get_mapping())
110
88
        self.control_files = lockfiles
111
89
        self.bzrdir = bzrdir
112
90
        self.name = name
113
91
        self.head = head
114
92
        self.base = bzrdir.transport.base
 
93
        self._format = GitBranchFormat()
115
94
 
116
95
    def dpull(self, source, stop_revision=None):
117
96
        if stop_revision is None:
118
97
            stop_revision = source.last_revision()
119
98
        # FIXME: Check for diverged branches
120
99
        revidmap = self.repository.dfetch(source.repository, stop_revision)
121
 
        self.head, self.mapping = self.mapping.revision_id_bzr_to_foreign(
122
 
            revidmap[stop_revision])
 
100
        self.head, self.mapping = self.mapping.revision_id_bzr_to_foreign(revidmap[stop_revision])
123
101
        return revidmap
124
102
 
125
103
    def lock_write(self):
147
125
 
148
126
 
149
127
class LocalGitBranch(GitBranch):
150
 
    """A local Git branch."""
151
128
 
152
129
    @needs_read_lock
153
130
    def last_revision(self):
156
133
            return revision.NULL_REVISION
157
134
        return self.mapping.revision_id_foreign_to_bzr(self.head)
158
135
 
159
 
    def create_checkout(self, to_location, revision_id=None, lightweight=False,
160
 
        accelerator_tree=None, hardlink=False):
 
136
    def create_checkout(self, to_location, revision_id=None, 
 
137
                        lightweight=False, accelerator_tree=None, hardlink=False):
161
138
        if lightweight:
162
139
            raise LightWeightCheckoutsNotSupported()
163
 
        return self._create_heavyweight_checkout(to_location, revision_id,
164
 
            hardlink)
 
140
        return self._create_heavyweight_checkout(to_location, revision_id, hardlink)
165
141
 
166
142
    def _create_heavyweight_checkout(self, to_location, revision_id=None, 
167
143
                                     hardlink=False):
181
157
        checkout_branch.pull(self, stop_revision=revision_id)
182
158
        return checkout.create_workingtree(revision_id, hardlink=hardlink)
183
159
 
 
160
    def _make_tags(self):
 
161
        return GitTagDict(self)
 
162
 
184
163
    def _gen_revision_history(self):
185
164
        if self.head is None:
186
165
            return []
187
 
        ret = list(self.repository.iter_reverse_revision_history(
188
 
            self.last_revision()))
 
166
        ret = list(self.repository.iter_reverse_revision_history(self.last_revision()))
189
167
        ret.reverse()
190
168
        return ret
191
169
 
205
183
    def supports_tags(self):
206
184
        return True
207
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
 
208
193
 
209
194
class InterGitGenericBranch(branch.InterBranch):
210
 
    """InterBranch implementation that pulls from Git into bzr."""
211
195
 
212
196
    @classmethod
213
197
    def is_compatible(self, source, target):
217
201
        graph=None):
218
202
        """See InterBranch.update_revisions()."""
219
203
        # TODO: stop_revision, overwrite
220
 
        interrepo = repository.InterRepository.get(self.source.repository, 
221
 
            self.target.repository)
 
204
        interrepo = repository.InterRepository.get(self.source.repository, self.target.repository)
222
205
        self._last_revid = None
223
206
        def determine_wants(heads):
224
207
            if not self.source.name in heads:
225
208
                raise BzrError("No such remote branch '%s', found: %r" % (
226
209
                    self.source.name, heads.keys()))
227
210
            head = heads[self.source.name]
228
 
            self._last_revid = self.source.mapping.revision_id_foreign_to_bzr(
229
 
                head)
 
211
            self._last_revid = self.source.mapping.revision_id_foreign_to_bzr(head)
230
212
            if self.target.repository.has_revision(self._last_revid):
231
213
                return []
232
214
            return [head]
233
215
        interrepo.fetch_objects(determine_wants, self.source.mapping)
234
 
        self.target.generate_revision_history(self._last_revid)
 
216
        self.target.set_last_revision(last_revid)
235
217
 
236
218
 
237
219
branch.InterBranch.register_optimiser(InterGitGenericBranch)