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