/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
20
from bzrlib import (
21
    branch,
22
    config,
0.200.225 by Jelmer Vernooij
Implement custom InterBranch to support fetching from remote git branches.
23
    repository,
0.200.19 by John Arbash Meinel
More refactoring. Add some direct tests for GitModel.
24
    revision,
0.200.82 by Jelmer Vernooij
Support listing tags.
25
    tag,
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
26
    )
27
from bzrlib.decorators import needs_read_lock
0.200.194 by Jelmer Vernooij
Look for commit object in heavyweight tags.
28
from bzrlib.trace import mutter
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
29
0.200.111 by Jelmer Vernooij
Merge bzr-foreign.
30
from bzrlib.plugins.git.foreign import ForeignBranch
0.200.210 by Jelmer Vernooij
properly error out about not support lightweight checkouts.
31
from bzrlib.plugins.git.errors import LightWeightCheckoutsNotSupported
0.200.20 by John Arbash Meinel
All tests are passing again
32
0.200.194 by Jelmer Vernooij
Look for commit object in heavyweight tags.
33
from dulwich.objects import (
34
        Commit,
35
        Tag,
36
        )
37
0.200.82 by Jelmer Vernooij
Support listing tags.
38
class GitTagDict(tag.BasicTags):
39
0.200.89 by Jelmer Vernooij
Support sprouting branches.
40
    def __init__(self, branch):
41
        self.branch = branch
42
        self.repository = branch.repository
0.200.82 by Jelmer Vernooij
Support listing tags.
43
44
    def get_tag_dict(self):
45
        ret = {}
0.200.180 by Jelmer Vernooij
Simplify tag handling.
46
        for k,v in self.repository._git.tags.iteritems():
0.200.194 by Jelmer Vernooij
Look for commit object in heavyweight tags.
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
0.200.180 by Jelmer Vernooij
Simplify tag handling.
54
            ret[k] = self.branch.mapping.revision_id_foreign_to_bzr(v)
0.200.82 by Jelmer Vernooij
Support listing tags.
55
        return ret
56
0.200.86 by Jelmer Vernooij
Clearer error when setting tags.
57
    def set_tag(self, name, revid):
0.200.181 by Jelmer Vernooij
Support setting tags.
58
        self.repository._git.tags[name] = revid
0.200.86 by Jelmer Vernooij
Clearer error when setting tags.
59
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
60
61
class GitBranchConfig(config.BranchConfig):
62
    """BranchConfig that uses locations.conf in place of branch.conf"""
63
64
    def __init__(self, branch):
65
        config.BranchConfig.__init__(self, branch)
66
        # do not provide a BranchDataConfig
67
        self.option_sources = self.option_sources[0], self.option_sources[2]
68
0.217.54 by John Carr
set_user_option breaks - doesnt have a local option in BranchConfig. Follow the bzr.dev syntax instead.
69
    def set_user_option(self, name, value, store=config.STORE_BRANCH, warn_masked=False):
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
70
        """Force local to True"""
0.217.54 by John Carr
set_user_option breaks - doesnt have a local option in BranchConfig. Follow the bzr.dev syntax instead.
71
        config.BranchConfig.set_user_option(self, name, value, 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.
72
73
74
class GitBranchFormat(branch.BranchFormat):
75
0.200.70 by Jelmer Vernooij
Implement GitBranchFormat.get_format_description.
76
    def get_format_description(self):
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
77
        return 'Git Branch'
78
0.200.82 by Jelmer Vernooij
Support listing tags.
79
    def supports_tags(self):
80
        return True
81
0.200.246 by Jelmer Vernooij
Cope with API changes in 1.13.
82
    def make_tags(self, branch):
0.228.3 by Jelmer Vernooij
Fix tags when fetching from remotes.
83
        if getattr(branch.repository, "get_refs", None) is not None:
84
            from bzrlib.plugins.git.remote import RemoteGitTagDict
85
            return RemoteGitTagDict(branch)
0.200.246 by Jelmer Vernooij
Cope with API changes in 1.13.
86
        return GitTagDict(branch)
87
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
88
0.200.111 by Jelmer Vernooij
Merge bzr-foreign.
89
class GitBranch(ForeignBranch):
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
90
    """An adapter to git repositories for bzr Branch objects."""
91
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
92
    def __init__(self, bzrdir, repository, name, head, lockfiles):
0.200.82 by Jelmer Vernooij
Support listing tags.
93
        self.repository = repository
0.200.246 by Jelmer Vernooij
Cope with API changes in 1.13.
94
        self._format = GitBranchFormat()
0.222.1 by Jelmer Vernooij
Retrieve mapping from repository rather than just using the default mapping.
95
        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.
96
        self.control_files = lockfiles
0.200.59 by Jelmer Vernooij
Add more tests, fix revision history.
97
        self.bzrdir = bzrdir
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
98
        self.name = name
0.200.57 by Jelmer Vernooij
Fix more tests.
99
        self.head = head
0.200.143 by Jelmer Vernooij
Reoncile InterGitRepository objects.
100
        self.base = bzrdir.transport.base
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
101
0.200.211 by Jelmer Vernooij
Add basic infrastructure for dpush.
102
    def dpull(self, source, stop_revision=None):
103
        if stop_revision is None:
104
            stop_revision = source.last_revision()
105
        # FIXME: Check for diverged branches
106
        revidmap = self.repository.dfetch(source.repository, stop_revision)
107
        self.head, self.mapping = self.mapping.revision_id_bzr_to_foreign(revidmap[stop_revision])
108
        return revidmap
109
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
110
    def lock_write(self):
111
        self.control_files.lock_write()
112
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
113
    def get_stacked_on_url(self):
114
        # Git doesn't do stacking (yet...)
115
        return None
116
117
    def get_parent(self):
118
        """See Branch.get_parent()."""
119
        return None
120
0.200.175 by Jelmer Vernooij
Add optimized handling when fetching from git to git.
121
    def set_parent(self, url):
122
        pass
123
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
124
    def lock_read(self):
125
        self.control_files.lock_read()
126
127
    def unlock(self):
128
        self.control_files.unlock()
129
130
    def get_physical_lock_status(self):
131
        return False
132
133
 
134
class LocalGitBranch(GitBranch):
135
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
136
    @needs_read_lock
137
    def last_revision(self):
138
        # perhaps should escape this ?
0.200.57 by Jelmer Vernooij
Fix more tests.
139
        if self.head is None:
0.200.19 by John Arbash Meinel
More refactoring. Add some direct tests for GitModel.
140
            return revision.NULL_REVISION
0.200.112 by Jelmer Vernooij
Fix the build.
141
        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.
142
0.200.210 by Jelmer Vernooij
properly error out about not support lightweight checkouts.
143
    def create_checkout(self, to_location, revision_id=None, 
144
                        lightweight=False, accelerator_tree=None, hardlink=False):
145
        if lightweight:
146
            raise LightWeightCheckoutsNotSupported()
147
        return self._create_heavyweight_checkout(to_location, revision_id, hardlink)
148
149
    def _create_heavyweight_checkout(self, to_location, revision_id=None, 
150
                                     hardlink=False):
151
        """Create a new heavyweight checkout of this branch.
152
153
        :param to_location: URL of location to create the new checkout in.
154
        :param revision_id: Revision that should be the tip of the checkout.
155
        :param hardlink: Whether to hardlink
156
        :return: WorkingTree object of checkout.
157
        """
158
        checkout_branch = BzrDir.create_branch_convenience(
159
            to_location, force_new_tree=False, format=get_rich_root_format())
160
        checkout = checkout_branch.bzrdir
161
        checkout_branch.bind(self)
162
        # pull up to the specified revision_id to set the initial 
163
        # branch tip correctly, and seed it with history.
164
        checkout_branch.pull(self, stop_revision=revision_id)
165
        return checkout.create_workingtree(revision_id, hardlink=hardlink)
166
0.200.57 by Jelmer Vernooij
Fix more tests.
167
    def _gen_revision_history(self):
0.200.58 by Jelmer Vernooij
Fix remaining tests.
168
        if self.head is None:
169
            return []
0.200.131 by Jelmer Vernooij
Fix all tests but two, use rich roots by default.
170
        ret = list(self.repository.iter_reverse_revision_history(self.last_revision()))
0.200.59 by Jelmer Vernooij
Add more tests, fix revision history.
171
        ret.reverse()
0.200.57 by Jelmer Vernooij
Fix more tests.
172
        return ret
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
173
174
    def get_config(self):
175
        return GitBranchConfig(self)
176
177
    def get_push_location(self):
178
        """See Branch.get_push_location."""
179
        push_loc = self.get_config().get_user_option('push_location')
180
        return push_loc
181
182
    def set_push_location(self, location):
183
        """See Branch.set_push_location."""
0.200.19 by John Arbash Meinel
More refactoring. Add some direct tests for GitModel.
184
        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.
185
                                          store=config.STORE_LOCATION)
0.200.43 by David Allouche
Ultra-experimental support for "bzr pull". No test. No sanity.
186
187
    def supports_tags(self):
0.200.82 by Jelmer Vernooij
Support listing tags.
188
        return True
0.200.96 by Jelmer Vernooij
Fix branch.
189
0.200.225 by Jelmer Vernooij
Implement custom InterBranch to support fetching from remote git branches.
190
191
class InterGitGenericBranch(branch.InterBranch):
192
193
    @classmethod
194
    def is_compatible(self, source, target):
195
        return isinstance(source, GitBranch)
196
197
    def update_revisions(self, stop_revision=None, overwrite=False,
198
        graph=None):
199
        """See InterBranch.update_revisions()."""
200
        # TODO: stop_revision, overwrite
0.226.2 by Jelmer Vernooij
Cope with new fetch_spec argument.
201
        interrepo = repository.InterRepository.get(self.source.repository, 
202
            self.target.repository)
0.200.225 by Jelmer Vernooij
Implement custom InterBranch to support fetching from remote git branches.
203
        self._last_revid = None
204
        def determine_wants(heads):
205
            if not self.source.name in heads:
206
                raise BzrError("No such remote branch '%s', found: %r" % (
207
                    self.source.name, heads.keys()))
208
            head = heads[self.source.name]
209
            self._last_revid = self.source.mapping.revision_id_foreign_to_bzr(head)
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
210
            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.
211
                return []
212
            return [head]
213
        interrepo.fetch_objects(determine_wants, self.source.mapping)
0.225.2 by Jelmer Vernooij
Handle situation when repository is already up to date during pull.
214
        self.target.generate_revision_history(self._last_revid)
0.200.225 by Jelmer Vernooij
Implement custom InterBranch to support fetching from remote git branches.
215
216
217
branch.InterBranch.register_optimiser(InterGitGenericBranch)