/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
1
# Copyright (C) 2007 Canonical Ltd
0.200.252 by Jelmer Vernooij
Clarify history, copyright.
2
# Copyright (C) 2009 Jelmer Vernooij <jelmer@samba.org>
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
3
#
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
18
"""An adapter between a Git Branch and a Bazaar Branch"""
19
0.200.261 by Jelmer Vernooij
More formatting fixes.
20
from dulwich.objects import (
21
    Commit,
22
    Tag,
23
    )
24
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
25
from bzrlib import (
26
    branch,
27
    config,
0.200.225 by Jelmer Vernooij
Implement custom InterBranch to support fetching from remote git branches.
28
    repository,
0.200.19 by John Arbash Meinel
More refactoring. Add some direct tests for GitModel.
29
    revision,
0.200.82 by Jelmer Vernooij
Support listing tags.
30
    tag,
0.230.1 by Jelmer Vernooij
Support lightweight checkouts.
31
    transport,
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
32
    )
0.200.261 by Jelmer Vernooij
More formatting fixes.
33
from bzrlib.decorators import (
34
    needs_read_lock,
35
    )
36
from bzrlib.trace import (
37
    mutter,
38
    )
39
0.200.278 by Jelmer Vernooij
Update branch head appropriately during dpull.
40
from bzrlib.plugins.git.errors import (
41
    NoSuchRef,
42
    )
0.200.261 by Jelmer Vernooij
More formatting fixes.
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."""
0.200.82 by Jelmer Vernooij
Support listing tags.
50
0.200.89 by Jelmer Vernooij
Support sprouting branches.
51
    def __init__(self, branch):
52
        self.branch = branch
53
        self.repository = branch.repository
0.200.82 by Jelmer Vernooij
Support listing tags.
54
55
    def get_tag_dict(self):
56
        ret = {}
0.200.180 by Jelmer Vernooij
Simplify tag handling.
57
        for k,v in self.repository._git.tags.iteritems():
0.200.194 by Jelmer Vernooij
Look for commit object in heavyweight tags.
58
            obj = self.repository._git.get_object(v)
59
            while isinstance(obj, Tag):
60
                v = obj.object[1]
61
                obj = self.repository._git.get_object(v)
62
            if not isinstance(obj, Commit):
0.200.261 by Jelmer Vernooij
More formatting fixes.
63
                mutter("Tag %s points at object %r that is not a commit, "
64
                       "ignoring", k, obj)
0.200.194 by Jelmer Vernooij
Look for commit object in heavyweight tags.
65
                continue
0.200.180 by Jelmer Vernooij
Simplify tag handling.
66
            ret[k] = self.branch.mapping.revision_id_foreign_to_bzr(v)
0.200.82 by Jelmer Vernooij
Support listing tags.
67
        return ret
68
0.200.86 by Jelmer Vernooij
Clearer error when setting tags.
69
    def set_tag(self, name, revid):
0.200.181 by Jelmer Vernooij
Support setting tags.
70
        self.repository._git.tags[name] = revid
0.200.86 by Jelmer Vernooij
Clearer error when setting tags.
71
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
72
73
class GitBranchConfig(config.BranchConfig):
74
    """BranchConfig that uses locations.conf in place of branch.conf"""
75
76
    def __init__(self, branch):
77
        config.BranchConfig.__init__(self, branch)
78
        # do not provide a BranchDataConfig
79
        self.option_sources = self.option_sources[0], self.option_sources[2]
80
0.200.261 by Jelmer Vernooij
More formatting fixes.
81
    def set_user_option(self, name, value, store=config.STORE_BRANCH,
82
            warn_masked=False):
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
83
        """Force local to True"""
0.200.261 by Jelmer Vernooij
More formatting fixes.
84
        config.BranchConfig.set_user_option(self, name, value,
85
            store=config.STORE_LOCATION, warn_masked=warn_masked)
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
86
87
88
class GitBranchFormat(branch.BranchFormat):
89
0.200.70 by Jelmer Vernooij
Implement GitBranchFormat.get_format_description.
90
    def get_format_description(self):
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
91
        return 'Git Branch'
92
0.200.82 by Jelmer Vernooij
Support listing tags.
93
    def supports_tags(self):
94
        return True
95
0.200.246 by Jelmer Vernooij
Cope with API changes in 1.13.
96
    def make_tags(self, branch):
0.228.3 by Jelmer Vernooij
Fix tags when fetching from remotes.
97
        if getattr(branch.repository, "get_refs", None) is not None:
98
            from bzrlib.plugins.git.remote import RemoteGitTagDict
99
            return RemoteGitTagDict(branch)
0.200.261 by Jelmer Vernooij
More formatting fixes.
100
        else:
101
            return LocalGitTagDict(branch)
0.200.246 by Jelmer Vernooij
Cope with API changes in 1.13.
102
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
103
0.200.111 by Jelmer Vernooij
Merge bzr-foreign.
104
class GitBranch(ForeignBranch):
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
105
    """An adapter to git repositories for bzr Branch objects."""
106
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
107
    def __init__(self, bzrdir, repository, name, head, lockfiles):
0.200.82 by Jelmer Vernooij
Support listing tags.
108
        self.repository = repository
0.200.246 by Jelmer Vernooij
Cope with API changes in 1.13.
109
        self._format = GitBranchFormat()
0.222.1 by Jelmer Vernooij
Retrieve mapping from repository rather than just using the default mapping.
110
        super(GitBranch, self).__init__(repository.get_mapping())
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
111
        self.control_files = lockfiles
0.200.59 by Jelmer Vernooij
Add more tests, fix revision history.
112
        self.bzrdir = bzrdir
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
113
        self.name = name
0.200.57 by Jelmer Vernooij
Fix more tests.
114
        self.head = head
0.200.143 by Jelmer Vernooij
Reoncile InterGitRepository objects.
115
        self.base = bzrdir.transport.base
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
116
0.200.293 by Jelmer Vernooij
Fix branch nicks.
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)
125
0.200.211 by Jelmer Vernooij
Add basic infrastructure for dpush.
126
    def dpull(self, source, stop_revision=None):
127
        if stop_revision is None:
128
            stop_revision = source.last_revision()
129
        # FIXME: Check for diverged branches
130
        revidmap = self.repository.dfetch(source.repository, stop_revision)
0.200.261 by Jelmer Vernooij
More formatting fixes.
131
        self.head, self.mapping = self.mapping.revision_id_bzr_to_foreign(
132
            revidmap[stop_revision])
0.200.278 by Jelmer Vernooij
Update branch head appropriately during dpull.
133
        self.repository._git.set_ref(self.name, self.head)
0.200.211 by Jelmer Vernooij
Add basic infrastructure for dpush.
134
        return revidmap
135
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
136
    def lock_write(self):
137
        self.control_files.lock_write()
138
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
139
    def get_stacked_on_url(self):
140
        # Git doesn't do stacking (yet...)
141
        return None
142
143
    def get_parent(self):
144
        """See Branch.get_parent()."""
145
        return None
146
0.200.175 by Jelmer Vernooij
Add optimized handling when fetching from git to git.
147
    def set_parent(self, url):
148
        pass
149
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
150
    def lock_read(self):
151
        self.control_files.lock_read()
152
153
    def unlock(self):
154
        self.control_files.unlock()
155
156
    def get_physical_lock_status(self):
157
        return False
158
159
 
160
class LocalGitBranch(GitBranch):
0.200.261 by Jelmer Vernooij
More formatting fixes.
161
    """A local Git branch."""
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
162
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
163
    @needs_read_lock
164
    def last_revision(self):
165
        # perhaps should escape this ?
0.200.57 by Jelmer Vernooij
Fix more tests.
166
        if self.head is None:
0.200.19 by John Arbash Meinel
More refactoring. Add some direct tests for GitModel.
167
            return revision.NULL_REVISION
0.200.112 by Jelmer Vernooij
Fix the build.
168
        return self.mapping.revision_id_foreign_to_bzr(self.head)
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
169
0.230.1 by Jelmer Vernooij
Support lightweight checkouts.
170
    def _get_checkout_format(self):
171
        """Return the most suitable metadir for a checkout of this branch.
172
        Weaves are used if this branch's repository uses weaves.
173
        """
174
        format = self.repository.bzrdir.checkout_metadir()
175
        format.set_branch_format(self._format)
176
        return format
177
0.200.261 by Jelmer Vernooij
More formatting fixes.
178
    def create_checkout(self, to_location, revision_id=None, lightweight=False,
179
        accelerator_tree=None, hardlink=False):
0.200.210 by Jelmer Vernooij
properly error out about not support lightweight checkouts.
180
        if lightweight:
0.230.1 by Jelmer Vernooij
Support lightweight checkouts.
181
            t = transport.get_transport(to_location)
182
            t.ensure_base()
183
            format = self._get_checkout_format()
184
            checkout = format.initialize_on_transport(t)
185
            from_branch = branch.BranchReferenceFormat().initialize(checkout, 
186
                self)
187
            tree = checkout.create_workingtree(revision_id,
188
                from_branch=from_branch, hardlink=hardlink)
189
            return tree
190
        else:
191
            return self._create_heavyweight_checkout(to_location, revision_id,
0.200.261 by Jelmer Vernooij
More formatting fixes.
192
            hardlink)
0.200.210 by Jelmer Vernooij
properly error out about not support lightweight checkouts.
193
194
    def _create_heavyweight_checkout(self, to_location, revision_id=None, 
195
                                     hardlink=False):
196
        """Create a new heavyweight checkout of this branch.
197
198
        :param to_location: URL of location to create the new checkout in.
199
        :param revision_id: Revision that should be the tip of the checkout.
200
        :param hardlink: Whether to hardlink
201
        :return: WorkingTree object of checkout.
202
        """
203
        checkout_branch = BzrDir.create_branch_convenience(
204
            to_location, force_new_tree=False, format=get_rich_root_format())
205
        checkout = checkout_branch.bzrdir
206
        checkout_branch.bind(self)
207
        # pull up to the specified revision_id to set the initial 
208
        # branch tip correctly, and seed it with history.
209
        checkout_branch.pull(self, stop_revision=revision_id)
210
        return checkout.create_workingtree(revision_id, hardlink=hardlink)
211
0.200.57 by Jelmer Vernooij
Fix more tests.
212
    def _gen_revision_history(self):
0.200.58 by Jelmer Vernooij
Fix remaining tests.
213
        if self.head is None:
214
            return []
0.200.261 by Jelmer Vernooij
More formatting fixes.
215
        ret = list(self.repository.iter_reverse_revision_history(
216
            self.last_revision()))
0.200.59 by Jelmer Vernooij
Add more tests, fix revision history.
217
        ret.reverse()
0.200.57 by Jelmer Vernooij
Fix more tests.
218
        return ret
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
219
220
    def get_config(self):
221
        return GitBranchConfig(self)
222
223
    def get_push_location(self):
224
        """See Branch.get_push_location."""
225
        push_loc = self.get_config().get_user_option('push_location')
226
        return push_loc
227
228
    def set_push_location(self, location):
229
        """See Branch.set_push_location."""
0.200.19 by John Arbash Meinel
More refactoring. Add some direct tests for GitModel.
230
        self.get_config().set_user_option('push_location', location,
0.217.54 by John Carr
set_user_option breaks - doesnt have a local option in BranchConfig. Follow the bzr.dev syntax instead.
231
                                          store=config.STORE_LOCATION)
0.200.43 by David Allouche
Ultra-experimental support for "bzr pull". No test. No sanity.
232
233
    def supports_tags(self):
0.200.82 by Jelmer Vernooij
Support listing tags.
234
        return True
0.200.96 by Jelmer Vernooij
Fix branch.
235
0.200.225 by Jelmer Vernooij
Implement custom InterBranch to support fetching from remote git branches.
236
237
class InterGitGenericBranch(branch.InterBranch):
0.200.261 by Jelmer Vernooij
More formatting fixes.
238
    """InterBranch implementation that pulls from Git into bzr."""
0.200.225 by Jelmer Vernooij
Implement custom InterBranch to support fetching from remote git branches.
239
240
    @classmethod
241
    def is_compatible(self, source, target):
242
        return isinstance(source, GitBranch)
243
244
    def update_revisions(self, stop_revision=None, overwrite=False,
245
        graph=None):
246
        """See InterBranch.update_revisions()."""
247
        # TODO: stop_revision, overwrite
0.226.2 by Jelmer Vernooij
Cope with new fetch_spec argument.
248
        interrepo = repository.InterRepository.get(self.source.repository, 
249
            self.target.repository)
0.200.225 by Jelmer Vernooij
Implement custom InterBranch to support fetching from remote git branches.
250
        self._last_revid = None
251
        def determine_wants(heads):
252
            if not self.source.name in heads:
0.200.278 by Jelmer Vernooij
Update branch head appropriately during dpull.
253
                raise NoSuchRef(self.source.name, heads.keys())
0.200.225 by Jelmer Vernooij
Implement custom InterBranch to support fetching from remote git branches.
254
            head = heads[self.source.name]
0.200.261 by Jelmer Vernooij
More formatting fixes.
255
            self._last_revid = self.source.mapping.revision_id_foreign_to_bzr(
256
                head)
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
257
            if self.target.repository.has_revision(self._last_revid):
0.200.225 by Jelmer Vernooij
Implement custom InterBranch to support fetching from remote git branches.
258
                return []
259
            return [head]
260
        interrepo.fetch_objects(determine_wants, self.source.mapping)
0.200.293 by Jelmer Vernooij
Fix branch nicks.
261
        # FIXME: Check that self._last_revid is a descendant of self.target.last_revision()
0.225.2 by Jelmer Vernooij
Handle situation when repository is already up to date during pull.
262
        self.target.generate_revision_history(self._last_revid)
0.200.225 by Jelmer Vernooij
Implement custom InterBranch to support fetching from remote git branches.
263
264
265
branch.InterBranch.register_optimiser(InterGitGenericBranch)