/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.327 by Jelmer Vernooij
merge new bzr-foreign.
28
    foreign,
0.200.225 by Jelmer Vernooij
Implement custom InterBranch to support fetching from remote git branches.
29
    repository,
0.200.19 by John Arbash Meinel
More refactoring. Add some direct tests for GitModel.
30
    revision,
0.200.82 by Jelmer Vernooij
Support listing tags.
31
    tag,
0.230.1 by Jelmer Vernooij
Support lightweight checkouts.
32
    transport,
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
33
    )
0.200.261 by Jelmer Vernooij
More formatting fixes.
34
from bzrlib.decorators import (
35
    needs_read_lock,
36
    )
37
from bzrlib.trace import (
38
    mutter,
39
    )
40
0.200.278 by Jelmer Vernooij
Update branch head appropriately during dpull.
41
from bzrlib.plugins.git.errors import (
42
    NoSuchRef,
43
    )
0.200.261 by Jelmer Vernooij
More formatting fixes.
44
45
46
class LocalGitTagDict(tag.BasicTags):
47
    """Dictionary with tags in a local repository."""
0.200.82 by Jelmer Vernooij
Support listing tags.
48
0.200.89 by Jelmer Vernooij
Support sprouting branches.
49
    def __init__(self, branch):
50
        self.branch = branch
51
        self.repository = branch.repository
0.200.82 by Jelmer Vernooij
Support listing tags.
52
53
    def get_tag_dict(self):
54
        ret = {}
0.200.180 by Jelmer Vernooij
Simplify tag handling.
55
        for k,v in self.repository._git.tags.iteritems():
0.200.194 by Jelmer Vernooij
Look for commit object in heavyweight tags.
56
            obj = self.repository._git.get_object(v)
57
            while isinstance(obj, Tag):
58
                v = obj.object[1]
59
                obj = self.repository._git.get_object(v)
60
            if not isinstance(obj, Commit):
0.200.261 by Jelmer Vernooij
More formatting fixes.
61
                mutter("Tag %s points at object %r that is not a commit, "
62
                       "ignoring", k, obj)
0.200.194 by Jelmer Vernooij
Look for commit object in heavyweight tags.
63
                continue
0.200.180 by Jelmer Vernooij
Simplify tag handling.
64
            ret[k] = self.branch.mapping.revision_id_foreign_to_bzr(v)
0.200.82 by Jelmer Vernooij
Support listing tags.
65
        return ret
66
0.200.86 by Jelmer Vernooij
Clearer error when setting tags.
67
    def set_tag(self, name, revid):
0.200.181 by Jelmer Vernooij
Support setting tags.
68
        self.repository._git.tags[name] = revid
0.200.86 by Jelmer Vernooij
Clearer error when setting tags.
69
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
70
71
class GitBranchConfig(config.BranchConfig):
72
    """BranchConfig that uses locations.conf in place of branch.conf"""
73
74
    def __init__(self, branch):
75
        config.BranchConfig.__init__(self, branch)
76
        # do not provide a BranchDataConfig
77
        self.option_sources = self.option_sources[0], self.option_sources[2]
78
0.200.261 by Jelmer Vernooij
More formatting fixes.
79
    def set_user_option(self, name, value, store=config.STORE_BRANCH,
80
            warn_masked=False):
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
81
        """Force local to True"""
0.200.261 by Jelmer Vernooij
More formatting fixes.
82
        config.BranchConfig.set_user_option(self, name, value,
83
            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.
84
85
86
class GitBranchFormat(branch.BranchFormat):
87
0.200.70 by Jelmer Vernooij
Implement GitBranchFormat.get_format_description.
88
    def get_format_description(self):
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
89
        return 'Git Branch'
90
0.200.82 by Jelmer Vernooij
Support listing tags.
91
    def supports_tags(self):
92
        return True
93
0.200.246 by Jelmer Vernooij
Cope with API changes in 1.13.
94
    def make_tags(self, branch):
0.228.3 by Jelmer Vernooij
Fix tags when fetching from remotes.
95
        if getattr(branch.repository, "get_refs", None) is not None:
96
            from bzrlib.plugins.git.remote import RemoteGitTagDict
97
            return RemoteGitTagDict(branch)
0.200.261 by Jelmer Vernooij
More formatting fixes.
98
        else:
99
            return LocalGitTagDict(branch)
0.200.246 by Jelmer Vernooij
Cope with API changes in 1.13.
100
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
101
0.200.327 by Jelmer Vernooij
merge new bzr-foreign.
102
class GitBranch(foreign.ForeignBranch):
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
103
    """An adapter to git repositories for bzr Branch objects."""
104
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
105
    def __init__(self, bzrdir, repository, name, head, lockfiles):
0.200.82 by Jelmer Vernooij
Support listing tags.
106
        self.repository = repository
0.200.246 by Jelmer Vernooij
Cope with API changes in 1.13.
107
        self._format = GitBranchFormat()
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
108
        self.control_files = lockfiles
0.200.59 by Jelmer Vernooij
Add more tests, fix revision history.
109
        self.bzrdir = bzrdir
0.231.1 by Jelmer Vernooij
Check that regenerated objects have the expected sha1.
110
        super(GitBranch, self).__init__(repository.get_mapping())
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
111
        self.name = name
0.200.57 by Jelmer Vernooij
Fix more tests.
112
        self.head = head
0.200.143 by Jelmer Vernooij
Reoncile InterGitRepository objects.
113
        self.base = bzrdir.transport.base
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
114
0.200.293 by Jelmer Vernooij
Fix branch nicks.
115
    def _get_nick(self, local=False, possible_master_transports=None):
116
        """Find the nick name for this branch.
117
118
        :return: Branch nick
119
        """
120
        return self.name
121
0.200.331 by Jelmer Vernooij
Add stub for setting nick function.
122
    def _set_nick(self, nick):
123
        raise NotImplementedError
124
125
    nick = property(_get_nick, _set_nick)
0.200.293 by Jelmer Vernooij
Fix branch nicks.
126
0.200.211 by Jelmer Vernooij
Add basic infrastructure for dpush.
127
    def dpull(self, source, stop_revision=None):
128
        if stop_revision is None:
129
            stop_revision = source.last_revision()
130
        # FIXME: Check for diverged branches
131
        revidmap = self.repository.dfetch(source.repository, stop_revision)
0.200.261 by Jelmer Vernooij
More formatting fixes.
132
        self.head, self.mapping = self.mapping.revision_id_bzr_to_foreign(
133
            revidmap[stop_revision])
0.200.278 by Jelmer Vernooij
Update branch head appropriately during dpull.
134
        self.repository._git.set_ref(self.name, self.head)
0.200.211 by Jelmer Vernooij
Add basic infrastructure for dpush.
135
        return revidmap
136
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
137
    def lock_write(self):
138
        self.control_files.lock_write()
139
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
140
    def get_stacked_on_url(self):
141
        # Git doesn't do stacking (yet...)
142
        return None
143
144
    def get_parent(self):
145
        """See Branch.get_parent()."""
0.200.312 by Jelmer Vernooij
Add notes about parent locations.
146
        # FIXME: Set "origin" url from .git/config ?
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
147
        return None
148
0.200.175 by Jelmer Vernooij
Add optimized handling when fetching from git to git.
149
    def set_parent(self, url):
0.200.312 by Jelmer Vernooij
Add notes about parent locations.
150
        # FIXME: Set "origin" url in .git/config ?
0.200.175 by Jelmer Vernooij
Add optimized handling when fetching from git to git.
151
        pass
152
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
153
    def lock_read(self):
154
        self.control_files.lock_read()
155
156
    def unlock(self):
157
        self.control_files.unlock()
158
159
    def get_physical_lock_status(self):
160
        return False
161
162
 
163
class LocalGitBranch(GitBranch):
0.200.261 by Jelmer Vernooij
More formatting fixes.
164
    """A local Git branch."""
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
165
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
166
    @needs_read_lock
167
    def last_revision(self):
168
        # perhaps should escape this ?
0.200.57 by Jelmer Vernooij
Fix more tests.
169
        if self.head is None:
0.200.19 by John Arbash Meinel
More refactoring. Add some direct tests for GitModel.
170
            return revision.NULL_REVISION
0.200.112 by Jelmer Vernooij
Fix the build.
171
        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.
172
0.230.1 by Jelmer Vernooij
Support lightweight checkouts.
173
    def _get_checkout_format(self):
174
        """Return the most suitable metadir for a checkout of this branch.
175
        Weaves are used if this branch's repository uses weaves.
176
        """
177
        format = self.repository.bzrdir.checkout_metadir()
178
        format.set_branch_format(self._format)
179
        return format
180
0.200.261 by Jelmer Vernooij
More formatting fixes.
181
    def create_checkout(self, to_location, revision_id=None, lightweight=False,
182
        accelerator_tree=None, hardlink=False):
0.200.210 by Jelmer Vernooij
properly error out about not support lightweight checkouts.
183
        if lightweight:
0.230.1 by Jelmer Vernooij
Support lightweight checkouts.
184
            t = transport.get_transport(to_location)
185
            t.ensure_base()
186
            format = self._get_checkout_format()
187
            checkout = format.initialize_on_transport(t)
188
            from_branch = branch.BranchReferenceFormat().initialize(checkout, 
189
                self)
190
            tree = checkout.create_workingtree(revision_id,
191
                from_branch=from_branch, hardlink=hardlink)
192
            return tree
193
        else:
194
            return self._create_heavyweight_checkout(to_location, revision_id,
0.200.261 by Jelmer Vernooij
More formatting fixes.
195
            hardlink)
0.200.210 by Jelmer Vernooij
properly error out about not support lightweight checkouts.
196
197
    def _create_heavyweight_checkout(self, to_location, revision_id=None, 
198
                                     hardlink=False):
199
        """Create a new heavyweight checkout of this branch.
200
201
        :param to_location: URL of location to create the new checkout in.
202
        :param revision_id: Revision that should be the tip of the checkout.
203
        :param hardlink: Whether to hardlink
204
        :return: WorkingTree object of checkout.
205
        """
206
        checkout_branch = BzrDir.create_branch_convenience(
207
            to_location, force_new_tree=False, format=get_rich_root_format())
208
        checkout = checkout_branch.bzrdir
209
        checkout_branch.bind(self)
210
        # pull up to the specified revision_id to set the initial 
211
        # branch tip correctly, and seed it with history.
212
        checkout_branch.pull(self, stop_revision=revision_id)
213
        return checkout.create_workingtree(revision_id, hardlink=hardlink)
214
0.200.57 by Jelmer Vernooij
Fix more tests.
215
    def _gen_revision_history(self):
0.200.58 by Jelmer Vernooij
Fix remaining tests.
216
        if self.head is None:
217
            return []
0.200.261 by Jelmer Vernooij
More formatting fixes.
218
        ret = list(self.repository.iter_reverse_revision_history(
219
            self.last_revision()))
0.200.59 by Jelmer Vernooij
Add more tests, fix revision history.
220
        ret.reverse()
0.200.57 by Jelmer Vernooij
Fix more tests.
221
        return ret
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
222
223
    def get_config(self):
224
        return GitBranchConfig(self)
225
226
    def get_push_location(self):
227
        """See Branch.get_push_location."""
228
        push_loc = self.get_config().get_user_option('push_location')
229
        return push_loc
230
231
    def set_push_location(self, location):
232
        """See Branch.set_push_location."""
0.200.19 by John Arbash Meinel
More refactoring. Add some direct tests for GitModel.
233
        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.
234
                                          store=config.STORE_LOCATION)
0.200.43 by David Allouche
Ultra-experimental support for "bzr pull". No test. No sanity.
235
236
    def supports_tags(self):
0.200.82 by Jelmer Vernooij
Support listing tags.
237
        return True
0.200.96 by Jelmer Vernooij
Fix branch.
238
0.200.225 by Jelmer Vernooij
Implement custom InterBranch to support fetching from remote git branches.
239
240
class InterGitGenericBranch(branch.InterBranch):
0.200.261 by Jelmer Vernooij
More formatting fixes.
241
    """InterBranch implementation that pulls from Git into bzr."""
0.200.225 by Jelmer Vernooij
Implement custom InterBranch to support fetching from remote git branches.
242
243
    @classmethod
244
    def is_compatible(self, source, target):
245
        return isinstance(source, GitBranch)
246
247
    def update_revisions(self, stop_revision=None, overwrite=False,
248
        graph=None):
249
        """See InterBranch.update_revisions()."""
0.226.2 by Jelmer Vernooij
Cope with new fetch_spec argument.
250
        interrepo = repository.InterRepository.get(self.source.repository, 
251
            self.target.repository)
0.200.225 by Jelmer Vernooij
Implement custom InterBranch to support fetching from remote git branches.
252
        self._last_revid = None
253
        def determine_wants(heads):
254
            if not self.source.name in heads:
0.200.278 by Jelmer Vernooij
Update branch head appropriately during dpull.
255
                raise NoSuchRef(self.source.name, heads.keys())
0.200.314 by Jelmer Vernooij
Support stop_revision.
256
            if stop_revision is not None:
257
                self._last_revid = stop_revision
0.200.316 by Jelmer Vernooij
Fix formatting.
258
                head, mapping = self.source.repository.lookup_git_revid(
259
                    stop_revision)
0.200.314 by Jelmer Vernooij
Support stop_revision.
260
            else:
261
                head = heads[self.source.name]
0.200.316 by Jelmer Vernooij
Fix formatting.
262
                self._last_revid = \
263
                    self.source.mapping.revision_id_foreign_to_bzr(head)
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
264
            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.
265
                return []
266
            return [head]
267
        interrepo.fetch_objects(determine_wants, self.source.mapping)
0.200.313 by Jelmer Vernooij
Support overwrite parameter.
268
        if overwrite:
0.200.314 by Jelmer Vernooij
Support stop_revision.
269
            prev_last_revid = None
0.200.313 by Jelmer Vernooij
Support overwrite parameter.
270
        else:
0.200.314 by Jelmer Vernooij
Support stop_revision.
271
            prev_last_revid = self.target.last_revision()
272
        self.target.generate_revision_history(self._last_revid, prev_last_revid)
0.200.225 by Jelmer Vernooij
Implement custom InterBranch to support fetching from remote git branches.
273
274
275
branch.InterBranch.register_optimiser(InterGitGenericBranch)