/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

  • Committer: Jelmer Vernooij
  • Date: 2009-04-02 15:54:49 UTC
  • mto: (0.200.326 trunk)
  • mto: This revision was merged to the branch mainline in revision 6960.
  • Revision ID: jelmer@samba.org-20090402155449-nuqhu1fsnqk6bt0g
Check that regenerated objects have the expected sha1.

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