/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

Add docstring.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Copyright (C) 2007 Canonical Ltd
2
 
# Copyright (C) 2009-2010 Jelmer Vernooij <jelmer@samba.org>
 
2
# Copyright (C) 2009 Jelmer Vernooij <jelmer@samba.org>
3
3
#
4
4
# This program is free software; you can redistribute it and/or modify
5
5
# it under the terms of the GNU General Public License as published by
50
50
    NoPushSupport,
51
51
    NoSuchRef,
52
52
    )
53
 
from bzrlib.plugins.git.refs import (
54
 
    ref_to_branch_name,
55
 
    extract_tags,
56
 
    tag_name_to_ref,
57
 
    )
58
53
 
59
54
from bzrlib.foreign import ForeignBranch
60
55
 
61
56
 
 
57
def extract_tags(refs):
 
58
    """Extract the tags from a refs dictionary.
 
59
 
 
60
    :param refs: Refs to extract the tags from.
 
61
    :return: Dictionary mapping tag names to SHA1s.
 
62
    """
 
63
    ret = {}
 
64
    for k,v in refs.iteritems():
 
65
        if k.startswith("refs/tags/") and not k.endswith("^{}"):
 
66
            v = refs.get(k+"^{}", v)
 
67
            ret[k[len("refs/tags/"):]] = v
 
68
    return ret
 
69
 
 
70
 
 
71
def branch_name_to_ref(name):
 
72
    """Map a branch name to a ref.
 
73
 
 
74
    :param name: Branch name
 
75
    :return: ref string
 
76
    """
 
77
    if name is None or name == "HEAD":
 
78
        return "HEAD"
 
79
    if not "/" in name:
 
80
        return "refs/heads/%s" % name
 
81
    else:
 
82
        return name
 
83
 
 
84
 
 
85
def ref_to_branch_name(ref):
 
86
    """Map a ref to a branch name
 
87
 
 
88
    :param ref: Ref
 
89
    :return: A branch name
 
90
    """
 
91
    if ref == "HEAD":
 
92
        return "HEAD"
 
93
    if ref.startswith("refs/heads/"):
 
94
        return ref[len("refs/heads/"):]
 
95
    raise ValueError("unable to map ref %s back to branch name")
 
96
 
 
97
 
62
98
class GitPullResult(branch.PullResult):
63
99
 
64
100
    def _lookup_revno(self, revid):
103
139
    def _set_tag_dict(self, to_dict):
104
140
        extra = set(self.repository._git.get_refs().keys())
105
141
        for k, revid in to_dict.iteritems():
106
 
            name = tag_name_to_ref(k)
 
142
            name = "refs/tags/%s" % k
107
143
            if name in extra:
108
144
                extra.remove(name)
109
145
            self.set_tag(k, revid)
112
148
                del self.repository._git[name]
113
149
        
114
150
    def set_tag(self, name, revid):
115
 
        self.repository._git.refs[tag_name_to_ref(name)], _ = \
 
151
        self.repository._git.refs["refs/tags/%s" % name], _ = \
116
152
            self.branch.mapping.revision_id_bzr_to_foreign(revid)
117
153
 
118
154
 
300
336
        self.set_last_revision(revid)
301
337
 
302
338
    def set_last_revision(self, revid):
303
 
        (newhead, self.mapping) = self.repository.lookup_bzr_revision_id(revid)
 
339
        (newhead, self.mapping) = self.mapping.revision_id_bzr_to_foreign(
 
340
                revid)
304
341
        self.head = newhead
305
342
 
306
343
    def _set_head(self, value):
385
422
        """
386
423
        interrepo = self._get_interrepo(self.source, self.target)
387
424
        def determine_wants(heads):
388
 
            if not self.source.ref in heads:
389
 
                raise NoSuchRef(self.source.ref, heads.keys())
 
425
            if not self.source.name in heads:
 
426
                raise NoSuchRef(self.source.name, heads.keys())
390
427
            if stop_revision is not None:
391
428
                self._last_revid = stop_revision
392
429
                head, mapping = self.source.repository.lookup_bzr_revision_id(
393
430
                    stop_revision)
394
431
            else:
395
 
                head = heads[self.source.ref]
 
432
                head = heads[self.source.name]
396
433
                self._last_revid = self.source.mapping.revision_id_foreign_to_bzr(
397
434
                    head)
398
435
            if self.target.repository.has_revision(self._last_revid):
495
532
                isinstance(target, RemoteGitBranch))
496
533
 
497
534
    def _basic_push(self, overwrite=False, stop_revision=None):
498
 
        from dulwich.protocol import ZERO_SHA
499
535
        result = GitBranchPushResult()
500
536
        result.source_branch = self.source
501
537
        result.target_branch = self.target
503
539
            stop_revision = self.source.last_revision()
504
540
        # FIXME: Check for diverged branches
505
541
        def get_changed_refs(old_refs):
506
 
            result.old_revid = self.target.mapping.revision_id_foreign_to_bzr(old_refs.get(self.target.ref, ZERO_SHA))
507
 
            refs = { self.target.ref: self.source.repository.lookup_bzr_revision_id(stop_revision)[0] }
 
542
            result.old_revid = self.target.mapping.revision_id_foreign_to_bzr(old_refs.get("refs/heads/master", "0" * 40))
 
543
            refs = { "refs/heads/master": self.source.repository.lookup_bzr_revision_id(stop_revision)[0] }
508
544
            result.new_revid = stop_revision
509
545
            for name, sha in self.source.repository._git.refs.as_dict("refs/tags").iteritems():
510
 
                refs[tag_name_to_ref(name)] = sha
 
546
                refs["refs/tags/%s" % name] = sha
511
547
            return refs
512
548
        self.target.repository.send_pack(get_changed_refs,
513
549
            self.source.repository._git.object_store.generate_pack_contents)
582
618
 
583
619
    def push(self, overwrite=True, stop_revision=None,
584
620
             _override_hook_source_branch=None):
585
 
        from dulwich.protocol import ZERO_SHA
586
 
        result = GitBranchPushResult()
587
 
        result.source_branch = self.source
588
 
        result.target_branch = self.target
589
 
        if stop_revision is None:
590
 
            stop_revision = self.source.last_revision()
591
 
        # FIXME: Check for diverged branches
592
 
        refs = { self.target.ref: stop_revision }
593
 
        for name, revid in self.source.tags.get_tag_dict().iteritems():
594
 
            if self.source.repository.has_revision(revid):
595
 
                refs[tag_name_to_ref(name)] = revid
596
 
        old_refs = self.target.repository._git.get_refs()
597
 
        self.target.repository.fetch_refs(self.source.repository, refs)
598
 
        result.old_revid = self.target.mapping.revision_id_foreign_to_bzr(
599
 
            old_refs.get(self.target.ref, ZERO_SHA))
600
 
        result.new_revid = refs[self.target.ref]
601
 
        return result
 
621
        raise NoPushSupport()
602
622
 
603
623
    def lossy_push(self, stop_revision=None):
604
 
        return self._push(stop_revision=stop_revision, roundtrip=False)
605
 
        from dulwich.protocol import ZERO_SHA
606
624
        result = GitBranchPushResult()
607
625
        result.source_branch = self.source
608
626
        result.target_branch = self.target
 
627
        try:
 
628
            result.old_revid = self.target.last_revision()
 
629
        except NoSuchRef:
 
630
            result.old_revid = revision.NULL_REVISION
609
631
        if stop_revision is None:
610
632
            stop_revision = self.source.last_revision()
611
633
        # FIXME: Check for diverged branches
612
 
        refs = { self.target.ref: stop_revision }
 
634
        refs = { "refs/heads/master": stop_revision }
613
635
        for name, revid in self.source.tags.get_tag_dict().iteritems():
614
636
            if self.source.repository.has_revision(revid):
615
 
                refs[tag_name_to_ref(name)] = revid
616
 
        revidmap, old_refs, new_refs = self.target.repository.dfetch_refs(
 
637
                refs["refs/tags/%s" % name] = revid
 
638
        revidmap, new_refs = self.target.repository.dfetch_refs(
617
639
            self.source.repository, refs)
 
640
        if revidmap != {}:
 
641
            self.target.generate_revision_history(revidmap[stop_revision])
 
642
            result.new_revid = revidmap[stop_revision]
 
643
        else:
 
644
            result.new_revid = result.old_revid
618
645
        result.revidmap = revidmap
619
 
        result.old_revid = self.target.mapping.revision_id_foreign_to_bzr(
620
 
            old_refs.get(self.target.ref, ZERO_SHA))
621
 
        result.new_revid = self.target.mapping.revision_id_foreign_to_bzr(
622
 
            new_refs[self.target.ref])
623
646
        return result
624
647
 
625
648