/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to branch.py

  • Committer: Robert Collins
  • Date: 2010-05-05 00:05:29 UTC
  • mto: This revision was merged to the branch mainline in revision 5206.
  • Revision ID: robertc@robertcollins.net-20100505000529-ltmllyms5watqj5u
Make 'pydoc bzrlib.tests.build_tree_shape' useful.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007 Canonical Ltd
2
 
# Copyright (C) 2009 Jelmer Vernooij <jelmer@samba.org>
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 dulwich.objects import (
21
 
    Commit,
22
 
    Tag,
23
 
    )
24
 
 
25
 
from bzrlib import (
26
 
    branch,
27
 
    config,
28
 
    foreign,
29
 
    repository,
30
 
    revision,
31
 
    tag,
32
 
    transport,
33
 
    )
34
 
from bzrlib.decorators import (
35
 
    needs_read_lock,
36
 
    )
37
 
from bzrlib.trace import (
38
 
    is_quiet,
39
 
    mutter,
40
 
    )
41
 
 
42
 
from bzrlib.plugins.git.config import (
43
 
    GitBranchConfig,
44
 
    )
45
 
from bzrlib.plugins.git.errors import (
46
 
    NoSuchRef,
47
 
    )
48
 
 
49
 
 
50
 
class GitPullResult(branch.PullResult):
51
 
 
52
 
    def _lookup_revno(self, revid):
53
 
        assert isinstance(revid, str), "was %r" % revid
54
 
        # Try in source branch first, it'll be faster
55
 
        return self.target_branch.revision_id_to_revno(revid)
56
 
 
57
 
    @property
58
 
    def old_revno(self):
59
 
        return self._lookup_revno(self.old_revid)
60
 
 
61
 
    @property
62
 
    def new_revno(self):
63
 
        return self._lookup_revno(self.new_revid)
64
 
 
65
 
 
66
 
class LocalGitTagDict(tag.BasicTags):
67
 
    """Dictionary with tags in a local repository."""
68
 
 
69
 
    def __init__(self, branch):
70
 
        self.branch = branch
71
 
        self.repository = branch.repository
72
 
 
73
 
    def get_tag_dict(self):
74
 
        ret = {}
75
 
        for k,v in self.repository._git.tags.iteritems():
76
 
            obj = self.repository._git.get_object(v)
77
 
            while isinstance(obj, Tag):
78
 
                v = obj.object[1]
79
 
                obj = self.repository._git.get_object(v)
80
 
            if not isinstance(obj, Commit):
81
 
                mutter("Tag %s points at object %r that is not a commit, "
82
 
                       "ignoring", k, obj)
83
 
                continue
84
 
            ret[k] = self.branch.mapping.revision_id_foreign_to_bzr(v)
85
 
        return ret
86
 
 
87
 
    def set_tag(self, name, revid):
88
 
        self.repository._git.tags[name] = revid
89
 
 
90
 
 
91
 
class GitBranchFormat(branch.BranchFormat):
92
 
 
93
 
    def get_format_description(self):
94
 
        return 'Git Branch'
95
 
 
96
 
    def supports_tags(self):
97
 
        return True
98
 
 
99
 
    def make_tags(self, branch):
100
 
        if getattr(branch.repository, "get_refs", None) is not None:
101
 
            from bzrlib.plugins.git.remote import RemoteGitTagDict
102
 
            return RemoteGitTagDict(branch)
103
 
        else:
104
 
            return LocalGitTagDict(branch)
105
 
 
106
 
 
107
 
class GitBranch(foreign.ForeignBranch):
108
 
    """An adapter to git repositories for bzr Branch objects."""
109
 
 
110
 
    def __init__(self, bzrdir, repository, name, head, lockfiles):
111
 
        self.repository = repository
112
 
        self._format = GitBranchFormat()
113
 
        self.control_files = lockfiles
114
 
        self.bzrdir = bzrdir
115
 
        super(GitBranch, self).__init__(repository.get_mapping())
116
 
        self.name = name
117
 
        self.head = head
118
 
        self.base = bzrdir.transport.base
119
 
 
120
 
    def _get_nick(self, local=False, possible_master_transports=None):
121
 
        """Find the nick name for this branch.
122
 
 
123
 
        :return: Branch nick
124
 
        """
125
 
        return self.name
126
 
 
127
 
    def _set_nick(self, nick):
128
 
        raise NotImplementedError
129
 
 
130
 
    nick = property(_get_nick, _set_nick)
131
 
 
132
 
    def dpull(self, source, stop_revision=None):
133
 
        if stop_revision is None:
134
 
            stop_revision = source.last_revision()
135
 
        # FIXME: Check for diverged branches
136
 
        revidmap = self.repository.dfetch(source.repository, stop_revision)
137
 
        if revidmap != {}:
138
 
            self.generate_revision_history(revidmap[stop_revision])
139
 
        return revidmap
140
 
 
141
 
    def generate_revision_history(self, revid, old_revid=None):
142
 
        # FIXME: Check that old_revid is in the ancestry of revid
143
 
        newhead, self.mapping = self.mapping.revision_id_bzr_to_foreign(revid)
144
 
        self._set_head(newhead)
145
 
 
146
 
    def _set_head(self, head):
147
 
        self.head = head
148
 
        self.repository._git.set_ref(self.name, self.head)
149
 
 
150
 
    def lock_write(self):
151
 
        self.control_files.lock_write()
152
 
 
153
 
    def get_stacked_on_url(self):
154
 
        # Git doesn't do stacking (yet...)
155
 
        return None
156
 
 
157
 
    def get_parent(self):
158
 
        """See Branch.get_parent()."""
159
 
        # FIXME: Set "origin" url from .git/config ?
160
 
        return None
161
 
 
162
 
    def set_parent(self, url):
163
 
        # FIXME: Set "origin" url in .git/config ?
164
 
        pass
165
 
 
166
 
    def lock_read(self):
167
 
        self.control_files.lock_read()
168
 
 
169
 
    def unlock(self):
170
 
        self.control_files.unlock()
171
 
 
172
 
    def get_physical_lock_status(self):
173
 
        return False
174
 
 
175
 
 
176
 
class LocalGitBranch(GitBranch):
177
 
    """A local Git branch."""
178
 
 
179
 
    @needs_read_lock
180
 
    def last_revision(self):
181
 
        # perhaps should escape this ?
182
 
        if self.head is None:
183
 
            return revision.NULL_REVISION
184
 
        return self.mapping.revision_id_foreign_to_bzr(self.head)
185
 
 
186
 
    def _get_checkout_format(self):
187
 
        """Return the most suitable metadir for a checkout of this branch.
188
 
        Weaves are used if this branch's repository uses weaves.
189
 
        """
190
 
        format = self.repository.bzrdir.checkout_metadir()
191
 
        format.set_branch_format(self._format)
192
 
        return format
193
 
 
194
 
    def create_checkout(self, to_location, revision_id=None, lightweight=False,
195
 
        accelerator_tree=None, hardlink=False):
196
 
        if lightweight:
197
 
            t = transport.get_transport(to_location)
198
 
            t.ensure_base()
199
 
            format = self._get_checkout_format()
200
 
            checkout = format.initialize_on_transport(t)
201
 
            from_branch = branch.BranchReferenceFormat().initialize(checkout, 
202
 
                self)
203
 
            tree = checkout.create_workingtree(revision_id,
204
 
                from_branch=from_branch, hardlink=hardlink)
205
 
            return tree
206
 
        else:
207
 
            return self._create_heavyweight_checkout(to_location, revision_id,
208
 
            hardlink)
209
 
 
210
 
    def _create_heavyweight_checkout(self, to_location, revision_id=None, 
211
 
                                     hardlink=False):
212
 
        """Create a new heavyweight checkout of this branch.
213
 
 
214
 
        :param to_location: URL of location to create the new checkout in.
215
 
        :param revision_id: Revision that should be the tip of the checkout.
216
 
        :param hardlink: Whether to hardlink
217
 
        :return: WorkingTree object of checkout.
218
 
        """
219
 
        checkout_branch = BzrDir.create_branch_convenience(
220
 
            to_location, force_new_tree=False, format=get_rich_root_format())
221
 
        checkout = checkout_branch.bzrdir
222
 
        checkout_branch.bind(self)
223
 
        # pull up to the specified revision_id to set the initial 
224
 
        # branch tip correctly, and seed it with history.
225
 
        checkout_branch.pull(self, stop_revision=revision_id)
226
 
        return checkout.create_workingtree(revision_id, hardlink=hardlink)
227
 
 
228
 
    def _gen_revision_history(self):
229
 
        if self.head is None:
230
 
            return []
231
 
        ret = list(self.repository.iter_reverse_revision_history(
232
 
            self.last_revision()))
233
 
        ret.reverse()
234
 
        return ret
235
 
 
236
 
    def get_config(self):
237
 
        return GitBranchConfig(self)
238
 
 
239
 
    def get_push_location(self):
240
 
        """See Branch.get_push_location."""
241
 
        push_loc = self.get_config().get_user_option('push_location')
242
 
        return push_loc
243
 
 
244
 
    def set_push_location(self, location):
245
 
        """See Branch.set_push_location."""
246
 
        self.get_config().set_user_option('push_location', location,
247
 
                                          store=config.STORE_LOCATION)
248
 
 
249
 
    def supports_tags(self):
250
 
        return True
251
 
 
252
 
 
253
 
class GitBranchPullResult(branch.PullResult):
254
 
 
255
 
    def report(self, to_file):
256
 
        if not is_quiet():
257
 
            if self.old_revid == self.new_revid:
258
 
                to_file.write('No revisions to pull.\n')
259
 
            else:
260
 
                to_file.write('Now on revision %d (git sha: %s).\n' % 
261
 
                        (self.new_revno, self.new_git_head))
262
 
        self._show_tag_conficts(to_file)
263
 
 
264
 
 
265
 
class InterGitGenericBranch(branch.InterBranch):
266
 
    """InterBranch implementation that pulls from Git into bzr."""
267
 
 
268
 
    @classmethod
269
 
    def is_compatible(self, source, target):
270
 
        return (isinstance(source, GitBranch) and 
271
 
                not isinstance(target, GitBranch))
272
 
 
273
 
    def update_revisions(self, stop_revision=None, overwrite=False,
274
 
        graph=None):
275
 
        """See InterBranch.update_revisions()."""
276
 
        interrepo = repository.InterRepository.get(self.source.repository, 
277
 
            self.target.repository)
278
 
        self._head = None
279
 
        self._last_revid = None
280
 
        def determine_wants(heads):
281
 
            if not self.source.name in heads:
282
 
                raise NoSuchRef(self.source.name, heads.keys())
283
 
            if stop_revision is not None:
284
 
                self._last_revid = stop_revision
285
 
                self._head, mapping = self.source.repository.lookup_git_revid(
286
 
                    stop_revision)
287
 
            else:
288
 
                self._head = heads[self.source.name]
289
 
                self._last_revid = \
290
 
                    self.source.mapping.revision_id_foreign_to_bzr(self._head)
291
 
            if self.target.repository.has_revision(self._last_revid):
292
 
                return []
293
 
            return [self._head]
294
 
        interrepo.fetch_objects(determine_wants, self.source.mapping)
295
 
        if overwrite:
296
 
            prev_last_revid = None
297
 
        else:
298
 
            prev_last_revid = self.target.last_revision()
299
 
        self.target.generate_revision_history(self._last_revid, prev_last_revid)
300
 
 
301
 
    def pull(self, overwrite=False, stop_revision=None,
302
 
             possible_transports=None, _hook_master=None, run_hooks=True,
303
 
             _override_hook_target=None):
304
 
        """See Branch.pull.
305
 
 
306
 
        :param _hook_master: Private parameter - set the branch to
307
 
            be supplied as the master to pull hooks.
308
 
        :param run_hooks: Private parameter - if false, this branch
309
 
            is being called because it's the master of the primary branch,
310
 
            so it should not run its hooks.
311
 
        :param _override_hook_target: Private parameter - set the branch to be
312
 
            supplied as the target_branch to pull hooks.
313
 
        """
314
 
        result = GitBranchPullResult()
315
 
        result.source_branch = self.source
316
 
        if _override_hook_target is None:
317
 
            result.target_branch = self.target
318
 
        else:
319
 
            result.target_branch = _override_hook_target
320
 
        self.source.lock_read()
321
 
        try:
322
 
            # We assume that during 'pull' the target repository is closer than
323
 
            # the source one.
324
 
            graph = self.target.repository.get_graph(self.source.repository)
325
 
            result.old_revno, result.old_revid = \
326
 
                self.target.last_revision_info()
327
 
            self.update_revisions(stop_revision, overwrite=overwrite, 
328
 
                graph=graph)
329
 
            result.new_git_head = self._head
330
 
            result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
331
 
                overwrite)
332
 
            result.new_revno, result.new_revid = self.target.last_revision_info()
333
 
            if _hook_master:
334
 
                result.master_branch = _hook_master
335
 
                result.local_branch = result.target_branch
336
 
            else:
337
 
                result.master_branch = result.target_branch
338
 
                result.local_branch = None
339
 
            if run_hooks:
340
 
                for hook in branch.Branch.hooks['post_pull']:
341
 
                    hook(result)
342
 
        finally:
343
 
            self.source.unlock()
344
 
        return result
345
 
 
346
 
 
347
 
 
348
 
 
349
 
branch.InterBranch.register_optimiser(InterGitGenericBranch)
350
 
 
351
 
 
352
 
class InterGitRemoteLocalBranch(branch.InterBranch):
353
 
    """InterBranch implementation that pulls between Git branches."""
354
 
 
355
 
    @classmethod
356
 
    def is_compatible(self, source, target):
357
 
        from bzrlib.plugins.git.remote import RemoteGitBranch
358
 
        return (isinstance(source, RemoteGitBranch) and 
359
 
                isinstance(target, LocalGitBranch))
360
 
 
361
 
    def pull(self, stop_revision=None, overwrite=False, 
362
 
        possible_transports=None):
363
 
        result = GitPullResult()
364
 
        result.source_branch = self.source
365
 
        result.target_branch = self.target
366
 
        interrepo = repository.InterRepository.get(self.source.repository, 
367
 
            self.target.repository)
368
 
        result.old_revid = self.target.last_revision()
369
 
        if stop_revision is None:
370
 
            stop_revision = self.source.last_revision()
371
 
        interrepo.fetch(revision_id=stop_revision)
372
 
        self.target.generate_revision_history(stop_revision, result.old_revid)
373
 
        result.new_revid = self.target.last_revision()
374
 
        return result
375
 
 
376
 
 
377
 
branch.InterBranch.register_optimiser(InterGitRemoteLocalBranch)