remote-bzr: simplify get_remote_branch()
[gitweb.git] / contrib / remote-helpers / git-remote-bzr
index ad42317ca095c960f5a8d87aee768d9690ef6eaa..5c4201ac310958a62e63d30f4f2e04ca9b42495b 100755 (executable)
@@ -31,6 +31,7 @@ import bzrlib.transport
 import bzrlib.errors
 import bzrlib.ui
 import bzrlib.urlutils
+import bzrlib.branch
 
 import sys
 import os
@@ -620,7 +621,7 @@ def parse_commit(parser):
             mark = int(mark_ref[1:])
             f = { 'mode' : m, 'mark' : mark }
         elif parser.check('D'):
-            t, path = line.split(' ')
+            t, path = line.split(' ', 1)
             f = { 'deleted' : True }
         else:
             die('Unknown file command: %s' % line)
@@ -764,31 +765,34 @@ def do_list(parser):
     print "@refs/heads/%s HEAD" % master_branch
     print
 
-def get_remote_branch(origin, remote_branch, name):
+def clone(path, remote_branch):
+    bdir = bzrlib.bzrdir.BzrDir.create(path)
+    repo = bdir.find_repository()
+    repo.fetch(remote_branch.repository)
+    return remote_branch.sprout(bdir, repository=repo)
+
+def get_remote_branch(remote_branch, name):
     global dirname, peers
 
     branch_path = os.path.join(dirname, 'clone', name)
-    if os.path.exists(branch_path):
+
+    try:
+        branch = bzrlib.branch.Branch.open(branch_path)
+    except bzrlib.errors.NotBranchError:
+        # clone
+        branch = clone(branch_path, remote_branch)
+    else:
         # pull
-        d = bzrlib.bzrdir.BzrDir.open(branch_path)
-        branch = d.open_branch()
         try:
-            branch.pull(remote_branch, [], None, False)
+            branch.pull(remote_branch, overwrite=True)
         except bzrlib.errors.DivergedBranches:
             # use remote branch for now
             return remote_branch
-    else:
-        # clone
-        d = origin.sprout(branch_path, None,
-                hardlink=True, create_tree_if_local=False,
-                force_new_repo=False,
-                source_branch=remote_branch)
-        branch = d.open_branch()
 
     return branch
 
 def find_branches(repo, wanted):
-    transport = repo.user_transport
+    transport = repo.bzrdir.root_transport
 
     for fn in transport.iter_files_recursive():
         if not fn.endswith('.bzr/branch-format'):
@@ -837,6 +841,8 @@ def get_repo(url, alias):
                 bdir.destroy_repository()
             except bzrlib.errors.NotBranchError:
                 pass
+            except bzrlib.errors.NoRepositoryPresent:
+                pass
 
     try:
         repo = origin.open_repository()
@@ -851,7 +857,7 @@ def get_repo(url, alias):
 
         if not is_local:
             peers[name] = remote_branch.base
-            branch = get_remote_branch(origin, remote_branch, name)
+            branch = get_remote_branch(remote_branch, name)
         else:
             branch = remote_branch
 
@@ -869,7 +875,7 @@ def get_repo(url, alias):
 
             if not is_local:
                 peers[name] = remote_branch.base
-                branch = get_remote_branch(origin, remote_branch, name)
+                branch = get_remote_branch(remote_branch, name)
             else:
                 branch = remote_branch
 
@@ -922,7 +928,8 @@ def main(args):
     if not os.path.exists(dirname):
         os.makedirs(dirname)
 
-    bzrlib.ui.ui_factory.be_quiet(True)
+    if hasattr(bzrlib.ui.ui_factory, 'be_quiet'):
+        bzrlib.ui.ui_factory.be_quiet(True)
 
     repo = get_repo(url, alias)