~bzr/ubuntu/karmic/bzr-git/bzr-ppa

« back to all changes in this revision

Viewing changes to branch.py

  • Committer: Jelmer Vernooij
  • Date: 2010-01-19 03:08:33 UTC
  • mfrom: (17.25.137 trunk)
  • Revision ID: jelmer@samba.org-20100119030833-5id45r3502agmfol
* New upstream release.
 + Supports 'bzr checkout'. Closes: #557295

Show diffs side-by-side

added added

removed removed

Lines of Context:
54
54
from bzrlib.foreign import ForeignBranch
55
55
 
56
56
 
57
 
def extract_tags(refs, mapping):
 
57
def extract_tags(refs):
58
58
    ret = {}
59
59
    for k,v in refs.iteritems():
60
60
        if k.startswith("refs/tags/") and not k.endswith("^{}"):
61
61
            v = refs.get(k+"^{}", v)
62
 
            ret[k[len("refs/tags/"):]] = mapping.revision_id_foreign_to_bzr(v)
 
62
            ret[k[len("refs/tags/"):]] = v
63
63
    return ret
64
64
 
65
65
 
88
88
 
89
89
    def get_tag_dict(self):
90
90
        ret = {}
91
 
        for k,v in self.repository._git.refs.as_dict("refs/tags").iteritems():
92
 
            obj = self.repository._git.get_object(v)
 
91
        for k,v in extract_tags(self.repository._git.get_refs()).iteritems():
 
92
            try:
 
93
                obj = self.repository._git[v]
 
94
            except KeyError:
 
95
                mutter("Tag %s points at unknown object %s, ignoring", v, obj)
 
96
                continue
93
97
            while isinstance(obj, Tag):
94
98
                v = obj.object[1]
95
 
                obj = self.repository._git.get_object(v)
 
99
                obj = self.repository._git[v]
96
100
            if not isinstance(obj, Commit):
97
101
                mutter("Tag %s points at object %r that is not a commit, "
98
102
                       "ignoring", k, obj)
122
126
    def get_format_description(self):
123
127
        return 'Git Branch'
124
128
 
 
129
    def network_name(self):
 
130
        return "git"
 
131
 
125
132
    def supports_tags(self):
126
133
        return True
127
134
 
 
135
    def get_foreign_tests_branch_factory(self):
 
136
        from bzrlib.plugins.git.tests.test_branch import ForeignTestsBranchFactory
 
137
        return ForeignTestsBranchFactory()
 
138
 
128
139
    def make_tags(self, branch):
129
140
        if getattr(branch.repository, "get_refs", None) is not None:
130
141
            from bzrlib.plugins.git.remote import RemoteGitTagDict
146
157
            self.tags = DictTagDict(self, tagsdict)
147
158
        self.name = name
148
159
        self._head = None
149
 
        self.base = bzrdir.transport.base
 
160
        self.base = bzrdir.root_transport.base
150
161
 
151
162
    def _get_checkout_format(self):
152
163
        """Return the most suitable metadir for a checkout of this branch.
186
197
 
187
198
    def get_stacked_on_url(self):
188
199
        # Git doesn't do stacking (yet...)
189
 
        return None
 
200
        raise errors.UnstackableBranchFormat(self._format, self.base)
190
201
 
191
202
    def get_parent(self):
192
203
        """See Branch.get_parent()."""
220
231
        return branch.InterBranch.get(self, target)._basic_push(
221
232
            overwrite, stop_revision)
222
233
 
223
 
 
 
234
 
224
235
class LocalGitBranch(GitBranch):
225
236
    """A local Git branch."""
226
237
 
231
242
            t.ensure_base()
232
243
            format = self._get_checkout_format()
233
244
            checkout = format.initialize_on_transport(t)
234
 
            from_branch = branch.BranchReferenceFormat().initialize(checkout, 
 
245
            from_branch = branch.BranchReferenceFormat().initialize(checkout,
235
246
                self)
236
247
            tree = checkout.create_workingtree(revision_id,
237
248
                from_branch=from_branch, hardlink=hardlink)
240
251
            return self._create_heavyweight_checkout(to_location, revision_id,
241
252
            hardlink)
242
253
 
243
 
    def _create_heavyweight_checkout(self, to_location, revision_id=None, 
 
254
    def _create_heavyweight_checkout(self, to_location, revision_id=None,
244
255
                                     hardlink=False):
245
256
        """Create a new heavyweight checkout of this branch.
246
257
 
253
264
            to_location, force_new_tree=False, format=get_rich_root_format())
254
265
        checkout = checkout_branch.bzrdir
255
266
        checkout_branch.bind(self)
256
 
        # pull up to the specified revision_id to set the initial 
 
267
        # pull up to the specified revision_id to set the initial
257
268
        # branch tip correctly, and seed it with history.
258
269
        checkout_branch.pull(self, stop_revision=revision_id)
259
270
        return checkout.create_workingtree(revision_id, hardlink=hardlink)
311
322
            if self.old_revid == self.new_revid:
312
323
                to_file.write('No revisions to pull.\n')
313
324
            else:
314
 
                to_file.write('Now on revision %d (git sha: %s).\n' % 
 
325
                to_file.write('Now on revision %d (git sha: %s).\n' %
315
326
                        (self.new_revno, self.new_git_head))
316
327
        self._show_tag_conficts(to_file)
317
328
 
340
351
    """InterBranch implementation that pulls from Git into bzr."""
341
352
 
342
353
    @classmethod
343
 
    def is_compatible(self, source, target):
344
 
        return (isinstance(source, GitBranch) and 
345
 
                not isinstance(target, GitBranch))
 
354
    def _get_interrepo(self, source, target):
 
355
        return repository.InterRepository.get(source.repository,
 
356
            target.repository)
 
357
 
 
358
    @classmethod
 
359
    def is_compatible(cls, source, target):
 
360
        return (isinstance(source, GitBranch) and
 
361
                not isinstance(target, GitBranch) and
 
362
                (getattr(cls._get_interrepo(source, target), "fetch_objects", None) is not None))
346
363
 
347
364
    def update_revisions(self, stop_revision=None, overwrite=False,
348
365
        graph=None):
349
366
        """See InterBranch.update_revisions()."""
350
 
        interrepo = repository.InterRepository.get(self.source.repository, 
351
 
            self.target.repository)
 
367
        interrepo = self._get_interrepo(self.source, self.target)
352
368
        self._head = None
353
369
        self._last_revid = None
354
370
        def determine_wants(heads):
356
372
                raise NoSuchRef(self.source.name, heads.keys())
357
373
            if stop_revision is not None:
358
374
                self._last_revid = stop_revision
359
 
                self._head, mapping = self.source.repository.lookup_git_revid(
 
375
                self._head, mapping = self.source.repository.lookup_bzr_revision_id(
360
376
                    stop_revision)
361
377
            else:
362
378
                self._head = heads[self.source.name]
401
417
            graph = self.target.repository.get_graph(self.source.repository)
402
418
            result.old_revno, result.old_revid = \
403
419
                self.target.last_revision_info()
404
 
            self.update_revisions(stop_revision, overwrite=overwrite, 
 
420
            self.update_revisions(stop_revision, overwrite=overwrite,
405
421
                graph=graph)
406
422
            result.new_git_head = self._head
407
423
            result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
426
442
        result.target_branch = self.target
427
443
        graph = self.target.repository.get_graph(self.source.repository)
428
444
        result.old_revno, result.old_revid = self.target.last_revision_info()
429
 
        self.update_revisions(stop_revision, overwrite=overwrite, 
430
 
            graph=graph)
 
445
        self.update_revisions(stop_revision, overwrite=overwrite, graph=graph)
431
446
        result.new_git_head = self._head
432
447
        result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
433
448
            overwrite)
445
460
    @classmethod
446
461
    def is_compatible(self, source, target):
447
462
        from bzrlib.plugins.git.remote import RemoteGitBranch
448
 
        return (isinstance(source, LocalGitBranch) and 
 
463
        return (isinstance(source, LocalGitBranch) and
449
464
                isinstance(target, RemoteGitBranch))
450
465
 
451
466
    def _basic_push(self, overwrite=False, stop_revision=None):
457
472
        # FIXME: Check for diverged branches
458
473
        def get_changed_refs(old_refs):
459
474
            result.old_revid = self.target.mapping.revision_id_foreign_to_bzr(old_refs.get("refs/heads/master", "0" * 40))
460
 
            refs = { "refs/heads/master": self.source.repository.lookup_git_revid(stop_revision)[0] }
 
475
            refs = { "refs/heads/master": self.source.repository.lookup_bzr_revision_id(stop_revision)[0] }
461
476
            result.new_revid = stop_revision
462
477
            for name, sha in self.source.repository._git.refs.as_dict("refs/tags").iteritems():
463
478
                refs["refs/tags/%s" % name] = sha
464
479
            return refs
465
 
        self.target.repository.send_pack(get_changed_refs, 
 
480
        self.target.repository.send_pack(get_changed_refs,
466
481
                self.source.repository._git.object_store.generate_pack_contents)
467
482
        return result
468
483
 
473
488
    @classmethod
474
489
    def is_compatible(self, source, target):
475
490
        from bzrlib.plugins.git.remote import RemoteGitBranch
476
 
        return (isinstance(source, RemoteGitBranch) and 
 
491
        return (isinstance(source, RemoteGitBranch) and
477
492
                isinstance(target, LocalGitBranch))
478
493
 
479
494
    def _basic_push(self, overwrite=False, stop_revision=None):
488
503
        return result
489
504
 
490
505
    def update_tags(self, refs):
491
 
        for name, revid in extract_tags(refs, self.target.mapping).iteritems():
 
506
        for name, v in extract_tags(refs).iteritems():
 
507
            revid = self.target.mapping.revision_id_foreign_to_bzr(v)
492
508
            self.target.tags.set_tag(name, revid)
493
509
 
494
510
    def update_refs(self, stop_revision=None):
495
 
        interrepo = repository.InterRepository.get(self.source.repository, 
 
511
        interrepo = repository.InterRepository.get(self.source.repository,
496
512
            self.target.repository)
497
513
        if stop_revision is None:
498
514
            refs = interrepo.fetch_refs(branches=["HEAD"])
501
517
            refs = interrepo.fetch_refs(revision_id=stop_revision)
502
518
        return refs, stop_revision
503
519
 
504
 
    def pull(self, stop_revision=None, overwrite=False, 
 
520
    def pull(self, stop_revision=None, overwrite=False,
505
521
        possible_transports=None, local=False):
506
522
        # This type of branch can't be bound.
507
523
        if local:
516
532
        result.new_revid = self.target.last_revision()
517
533
        return result
518
534
 
519
 
    
 
535
 
520
536
class InterToGitBranch(branch.InterBranch):
521
537
    """InterBranch implementation that pulls from Git into bzr."""
522
538
 
 
539
    @staticmethod
 
540
    def _get_branch_formats_to_test():
 
541
        return None, None
 
542
 
523
543
    @classmethod
524
544
    def is_compatible(self, source, target):
525
 
        return (not isinstance(source, GitBranch) and 
 
545
        return (not isinstance(source, GitBranch) and
526
546
                isinstance(target, GitBranch))
527
547
 
528
548
    def update_revisions(self, *args, **kwargs):
529
549
        raise NoPushSupport()
530
550
 
531
 
    def push(self, overwrite=True, stop_revision=None, 
 
551
    def push(self, overwrite=True, stop_revision=None,
532
552
             _override_hook_source_branch=None):
533
553
        raise NoPushSupport()
534
554