/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

Fix tests.

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