/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 processors/generic_processor.py

create branches as required

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
import re
21
21
import time
22
22
from bzrlib import (
 
23
    builtins,
 
24
    bzrdir,
23
25
    delta,
24
26
    errors,
25
27
    generate_ids,
29
31
    progress,
30
32
    revision,
31
33
    revisiontree,
 
34
    transport,
32
35
    )
33
36
from bzrlib.trace import (
34
37
    note,
35
38
    warning,
 
39
    error,
36
40
    )
37
41
import bzrlib.util.configobj.configobj as configobj
38
42
from bzrlib.plugins.fastimport import (
154
158
 
155
159
        # Update the branches
156
160
        self.note("Updating branch information ...")
157
 
        updater = GenericBranchUpdater(self.branch, self.cache_mgr,
 
161
        updater = GenericBranchUpdater(self.repo, self.branch, self.cache_mgr,
158
162
            helpers.invert_dict(self.cache_mgr.heads),
159
163
            self.cache_mgr.last_ref)
160
164
        branches_updated, branches_lost = updater.update()
162
166
 
163
167
        # Tell the user about branches that were not created
164
168
        if branches_lost:
165
 
            self.warning("Unshared repository - not creating branches for "
166
 
                "these head revisions:")
 
169
            if not self.repo.is_shared():
 
170
                self.warning("Cannot import multiple branches into "
 
171
                    "an unshared repository")
 
172
            self.warning("Not creating branches for these head revisions:")
167
173
            for lost_info in branches_lost:
168
174
                head_revision = lost_info[1]
169
175
                branch_name = lost_info[0]
633
639
 
634
640
class GenericBranchUpdater(object):
635
641
 
636
 
    def __init__(self, branch, cache_mgr, heads_by_ref, last_ref):
 
642
    def __init__(self, repo, branch, cache_mgr, heads_by_ref, last_ref):
637
643
        """Create an object responsible for updating branches.
638
644
 
639
645
        :param heads_by_ref: a dictionary where
640
646
          names are git-style references like refs/heads/master;
641
647
          values are one item lists of commits marks.
642
648
        """
 
649
        self.repo = repo
643
650
        self.branch = branch
644
 
        self.repo = branch.repository
645
651
        self.cache_mgr = cache_mgr
646
652
        self.heads_by_ref = heads_by_ref
647
653
        self.last_ref = last_ref
659
665
            would have been created had the repository been shared
660
666
        """
661
667
        updated = []
662
 
        default_tip, branch_tips, lost_heads = self._get_matching_branches()
663
 
        self._update_branch(self.branch, default_tip)
664
 
        updated.append(self.branch)
 
668
        branch_tips, lost_heads = self._get_matching_branches()
665
669
        for br, tip in branch_tips:
666
670
            self._update_branch(br, tip)
667
671
            updated.append(br)
674
678
          default_tip = the last commit mark for the default branch
675
679
          branch_tips = a list of (branch,tip) tuples for other branches.
676
680
          lost_heads = a list of (bazaar-name,revision) for branches that
677
 
            would have been created had the repository been shared
 
681
            would have been created had the repository been shared and
 
682
            everything succeeded
678
683
        """
679
 
        # Until there's a good reason to be more sellective,
680
 
        # use the last imported revision as the tip of the default branch
681
 
        default_tip = self.heads_by_ref[self.last_ref][0]
 
684
        branch_tips = []
 
685
        lost_heads = []
 
686
        ref_names = self.heads_by_ref.keys()
 
687
        if self.branch is not None:
 
688
            # Until there's a good reason to be more selective,
 
689
            # use the last imported revision as the tip of the default branch
 
690
            default_tip = self.heads_by_ref[self.last_ref][0]
 
691
            branch_tips.append((self.branch, default_tip))
 
692
            ref_names.remove(self.last_ref)
682
693
 
683
694
        # Convert the reference names into Bazaar speak
684
 
        ref_names = self.heads_by_ref.keys()
685
 
        ref_names.remove(self.last_ref)
686
695
        bzr_names = self._get_bzr_names_from_ref_names(ref_names)
687
696
 
 
697
        # Policy for locating branches
 
698
        def dir_under_current(name, ref_name):
 
699
            # Using the Bazaar name, get a directory under the current one
 
700
            return name
 
701
        def dir_sister_branch(name, ref_name):
 
702
            # Using the Bazaar name, get a sister directory to the branch
 
703
            return osutils.pathjoin(self.branch.base, "..", name)
 
704
        if self.branch is not None:
 
705
            dir_policy = dir_sister_branch
 
706
        else:
 
707
            dir_policy = dir_under_current
 
708
 
688
709
        # Create/track missing branches
689
 
        branch_tips = []
690
 
        lost_heads = []
691
710
        shared_repo = self.repo.is_shared()
692
711
        for name in sorted(bzr_names.keys()):
693
712
            ref_name = bzr_names[name]
694
713
            tip = self.heads_by_ref[ref_name][0]
695
714
            if shared_repo:
696
 
                # TODO: create the branch
697
 
                pass
698
 
            else:
699
 
                lost_head = self.cache_mgr.revision_ids[tip]
700
 
                lost_info = (name, lost_head)
701
 
                lost_heads.append(lost_info)
702
 
        return default_tip, branch_tips, lost_heads
 
715
                location = dir_policy(name, ref_name)
 
716
                try:
 
717
                    br = self.make_branch(location)
 
718
                    branch_tips.append((br,tip))
 
719
                    continue
 
720
                except errors.BzrError, ex:
 
721
                    error("ERROR: failed to create branch %s: %s",
 
722
                        location, ex)
 
723
            lost_head = self.cache_mgr.revision_ids[tip]
 
724
            lost_info = (name, lost_head)
 
725
            lost_heads.append(lost_info)
 
726
        return branch_tips, lost_heads
 
727
 
 
728
    def make_branch(self, location):
 
729
        """Create a branch in the repository."""
 
730
        return bzrdir.BzrDir.create_branch_convenience(location)
703
731
 
704
732
    def _get_bzr_names_from_ref_names(self, ref_names):
705
 
        """Map reference names to Bazaar branch names."""
 
733
        """Generate Bazaar branch names from import ref names.
 
734
        
 
735
        :return: a dictionary with Bazaar names as keys and
 
736
          the original reference names as values.
 
737
        """
706
738
        bazaar_names = {}
707
739
        for ref_name in sorted(ref_names):
708
740
            parts = ref_name.split('/')