/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 16:10:24 UTC
  • mto: (0.200.326 trunk)
  • mto: This revision was merged to the branch mainline in revision 6960.
  • Revision ID: jelmer@samba.org-20090402161024-ngoay3s4mpsq5pts
Add -Dverify flag (not fully implemented yet).

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,
22
28
    repository,
23
29
    revision,
24
30
    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):
 
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."""
38
50
 
39
51
    def __init__(self, branch):
40
52
        self.branch = branch
48
60
                v = obj.object[1]
49
61
                obj = self.repository._git.get_object(v)
50
62
            if not isinstance(obj, Commit):
51
 
                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)
52
65
                continue
53
66
            ret[k] = self.branch.mapping.revision_id_foreign_to_bzr(v)
54
67
        return ret
65
78
        # do not provide a BranchDataConfig
66
79
        self.option_sources = self.option_sources[0], self.option_sources[2]
67
80
 
68
 
    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):
69
83
        """Force local to True"""
70
 
        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)
71
86
 
72
87
 
73
88
class GitBranchFormat(branch.BranchFormat):
78
93
    def supports_tags(self):
79
94
        return True
80
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
 
81
103
 
82
104
class GitBranch(ForeignBranch):
83
105
    """An adapter to git repositories for bzr Branch objects."""
84
106
 
85
107
    def __init__(self, bzrdir, repository, name, head, lockfiles):
86
108
        self.repository = repository
87
 
        super(GitBranch, self).__init__(repository.get_mapping())
 
109
        self._format = GitBranchFormat()
88
110
        self.control_files = lockfiles
89
111
        self.bzrdir = bzrdir
 
112
        super(GitBranch, self).__init__(repository.get_mapping())
90
113
        self.name = name
91
114
        self.head = head
92
115
        self.base = bzrdir.transport.base
93
 
        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)
94
125
 
95
126
    def dpull(self, source, stop_revision=None):
96
127
        if stop_revision is None:
97
128
            stop_revision = source.last_revision()
98
129
        # FIXME: Check for diverged branches
99
130
        revidmap = self.repository.dfetch(source.repository, stop_revision)
100
 
        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)
101
134
        return revidmap
102
135
 
103
136
    def lock_write(self):
109
142
 
110
143
    def get_parent(self):
111
144
        """See Branch.get_parent()."""
 
145
        # FIXME: Set "origin" url from .git/config ?
112
146
        return None
113
147
 
114
148
    def set_parent(self, url):
 
149
        # FIXME: Set "origin" url in .git/config ?
115
150
        pass
116
151
 
117
152
    def lock_read(self):
125
160
 
126
161
 
127
162
class LocalGitBranch(GitBranch):
 
163
    """A local Git branch."""
128
164
 
129
165
    @needs_read_lock
130
166
    def last_revision(self):
133
169
            return revision.NULL_REVISION
134
170
        return self.mapping.revision_id_foreign_to_bzr(self.head)
135
171
 
136
 
    def create_checkout(self, to_location, revision_id=None, 
137
 
                        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):
138
182
        if lightweight:
139
 
            raise LightWeightCheckoutsNotSupported()
140
 
        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)
141
195
 
142
196
    def _create_heavyweight_checkout(self, to_location, revision_id=None, 
143
197
                                     hardlink=False):
157
211
        checkout_branch.pull(self, stop_revision=revision_id)
158
212
        return checkout.create_workingtree(revision_id, hardlink=hardlink)
159
213
 
160
 
    def _make_tags(self):
161
 
        return GitTagDict(self)
162
 
 
163
214
    def _gen_revision_history(self):
164
215
        if self.head is None:
165
216
            return []
166
 
        ret = list(self.repository.iter_reverse_revision_history(self.last_revision()))
 
217
        ret = list(self.repository.iter_reverse_revision_history(
 
218
            self.last_revision()))
167
219
        ret.reverse()
168
220
        return ret
169
221
 
183
235
    def supports_tags(self):
184
236
        return True
185
237
 
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
238
 
194
239
class InterGitGenericBranch(branch.InterBranch):
 
240
    """InterBranch implementation that pulls from Git into bzr."""
195
241
 
196
242
    @classmethod
197
243
    def is_compatible(self, source, target):
200
246
    def update_revisions(self, stop_revision=None, overwrite=False,
201
247
        graph=None):
202
248
        """See InterBranch.update_revisions()."""
203
 
        # TODO: stop_revision, overwrite
204
 
        interrepo = repository.InterRepository.get(self.source.repository, self.target.repository)
 
249
        interrepo = repository.InterRepository.get(self.source.repository, 
 
250
            self.target.repository)
205
251
        self._last_revid = None
206
252
        def determine_wants(heads):
207
253
            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)
 
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)
212
263
            if self.target.repository.has_revision(self._last_revid):
213
264
                return []
214
265
            return [head]
215
266
        interrepo.fetch_objects(determine_wants, self.source.mapping)
216
 
        self.target.generate_revision_history(self._last_revid)
 
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)
217
272
 
218
273
 
219
274
branch.InterBranch.register_optimiser(InterGitGenericBranch)