634
640
class GenericBranchUpdater(object):
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.
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.
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
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
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]
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)
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)
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
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
707
dir_policy = dir_under_current
688
709
# Create/track missing branches
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]
696
# TODO: create the branch
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)
717
br = self.make_branch(location)
718
branch_tips.append((br,tip))
720
except errors.BzrError, ex:
721
error("ERROR: failed to create branch %s: %s",
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
728
def make_branch(self, location):
729
"""Create a branch in the repository."""
730
return bzrdir.BzrDir.create_branch_convenience(location)
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.
735
:return: a dictionary with Bazaar names as keys and
736
the original reference names as values.
706
738
bazaar_names = {}
707
739
for ref_name in sorted(ref_names):
708
740
parts = ref_name.split('/')