@@ -281,7 +281,7 @@ def export_branch(repo, name):
281281 ref = '%s/heads/%s' % (prefix , name )
282282 tip = marks .get_tip (name )
283283
284- branch = bzrlib . branch . Branch . open ( branches [ name ] )
284+ branch = get_remote_branch ( name )
285285 repo = branch .repository
286286
287287 branch .lock_read ()
@@ -593,7 +593,7 @@ def parse_commit(parser):
593593
594594 if ref .startswith ('refs/heads/' ):
595595 name = ref [len ('refs/heads/' ):]
596- branch = bzrlib . branch . Branch . open ( branches [ name ] )
596+ branch = get_remote_branch ( name )
597597 else :
598598 die ('unknown ref' )
599599
@@ -624,7 +624,7 @@ def parse_commit(parser):
624624 mark = int (mark_ref [1 :])
625625 f = { 'mode' : m , 'mark' : mark }
626626 elif parser .check ('D' ):
627- t , path = line .split (' ' )
627+ t , path = line .split (' ' , 1 )
628628 f = { 'deleted' : True }
629629 else :
630630 die ('Unknown file command: %s' % line )
@@ -695,7 +695,7 @@ def do_export(parser):
695695 for ref , revid in parsed_refs .iteritems ():
696696 if ref .startswith ('refs/heads/' ):
697697 name = ref [len ('refs/heads/' ):]
698- branch = bzrlib . branch . Branch . open ( branches [ name ] )
698+ branch = get_remote_branch ( name )
699699 branch .generate_revision_history (revid , marks .get_tip (name ))
700700
701701 if name in peers :
@@ -752,7 +752,7 @@ def do_list(parser):
752752 master_branch = name
753753 print "? refs/heads/%s" % name
754754
755- branch = bzrlib . branch . Branch . open ( branches [ master_branch ] )
755+ branch = get_remote_branch ( master_branch )
756756 branch .lock_read ()
757757 for tag , revid in branch .tags .get_tag_dict ().items ():
758758 try :
@@ -768,30 +768,40 @@ def do_list(parser):
768768 print "@refs/heads/%s HEAD" % master_branch
769769 print
770770
771- def get_remote_branch (origin , remote_branch , name ):
772- global dirname , peers
771+ def clone (path , remote_branch ):
772+ try :
773+ bdir = bzrlib .bzrdir .BzrDir .create (path )
774+ except bzrlib .errors .AlreadyControlDirError :
775+ bdir = bzrlib .bzrdir .BzrDir .open (path )
776+ repo = bdir .find_repository ()
777+ repo .fetch (remote_branch .repository )
778+ return remote_branch .sprout (bdir , repository = repo )
779+
780+ def get_remote_branch (name ):
781+ global dirname , branches
782+
783+ remote_branch = bzrlib .branch .Branch .open (branches [name ])
784+ if isinstance (remote_branch .user_transport , bzrlib .transport .local .LocalTransport ):
785+ return remote_branch
773786
774787 branch_path = os .path .join (dirname , 'clone' , name )
775- if os .path .exists (branch_path ):
788+
789+ try :
790+ branch = bzrlib .branch .Branch .open (branch_path )
791+ except bzrlib .errors .NotBranchError :
792+ # clone
793+ branch = clone (branch_path , remote_branch )
794+ else :
776795 # pull
777- d = bzrlib .bzrdir .BzrDir .open (branch_path )
778- branch = d .open_branch ()
779796 try :
780- branch .pull (remote_branch , [], None , False )
797+ branch .pull (remote_branch , overwrite = True )
781798 except bzrlib .errors .DivergedBranches :
782799 # use remote branch for now
783800 return remote_branch
784- else :
785- # clone
786- d = origin .sprout (branch_path , None ,
787- hardlink = True , create_tree_if_local = False ,
788- force_new_repo = False ,
789- source_branch = remote_branch )
790- branch = d .open_branch ()
791801
792802 return branch
793803
794- def find_branches (repo , wanted ):
804+ def find_branches (repo ):
795805 transport = repo .bzrdir .root_transport
796806
797807 for fn in transport .iter_files_recursive ():
@@ -802,16 +812,13 @@ def find_branches(repo, wanted):
802812 name = name if name != '' else 'master'
803813 name = name .replace ('/' , '+' )
804814
805- if wanted and not name in wanted :
806- continue
807-
808815 try :
809816 cur = transport .clone (subdir )
810817 branch = bzrlib .branch .Branch .open_from_transport (cur )
811818 except bzrlib .errors .NotBranchError :
812819 continue
813820 else :
814- yield name , branch
821+ yield name , branch . base
815822
816823def get_repo (url , alias ):
817824 global dirname , peer , branches
@@ -844,44 +851,35 @@ def get_repo(url, alias):
844851 except bzrlib .errors .NoRepositoryPresent :
845852 pass
846853
847- try :
848- repo = origin .open_repository ()
849- if not repo .user_transport .listable ():
850- # this repository is not usable for us
851- raise bzrlib .errors .NoRepositoryPresent (repo .bzrdir )
852- except bzrlib .errors .NoRepositoryPresent :
853- # branch
854-
855- name = 'master'
856- remote_branch = origin .open_branch ()
857-
858- if not is_local :
859- peers [name ] = remote_branch .base
860- branch = get_remote_branch (origin , remote_branch , name )
861- else :
862- branch = remote_branch
863-
864- branches [name ] = branch .base
854+ wanted = get_config ('remote-bzr.branches' ).rstrip ().split (', ' )
855+ # stupid python
856+ wanted = [e for e in wanted if e ]
865857
866- return branch .repository
858+ if not wanted :
859+ try :
860+ repo = origin .open_repository ()
861+ if not repo .user_transport .listable ():
862+ # this repository is not usable for us
863+ raise bzrlib .errors .NoRepositoryPresent (repo .bzrdir )
864+ except bzrlib .errors .NoRepositoryPresent :
865+ wanted = ['master' ]
866+
867+ if wanted :
868+ def list_wanted (url , wanted ):
869+ for name in wanted :
870+ subdir = name if name != 'master' else ''
871+ yield name , bzrlib .urlutils .join (url , subdir )
872+
873+ branch_list = list_wanted (url , wanted )
867874 else :
868- # repository
869-
870- wanted = get_config ('remote-bzr.branches' ).rstrip ().split (', ' )
871- # stupid python
872- wanted = [e for e in wanted if e ]
873-
874- for name , remote_branch in find_branches (repo , wanted ):
875+ branch_list = find_branches (repo )
875876
876- if not is_local :
877- peers [name ] = remote_branch .base
878- branch = get_remote_branch (origin , remote_branch , name )
879- else :
880- branch = remote_branch
881-
882- branches [name ] = branch .base
877+ for name , url in branch_list :
878+ if not is_local :
879+ peers [name ] = url
880+ branches [name ] = url
883881
884- return repo
882+ return origin
885883
886884def fix_path (alias , orig_url ):
887885 url = urlparse .urlparse (orig_url , 'file' )
0 commit comments