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