Commits

Takafumi Arakaki  committed 2601834

Refactor & fix hucb.py

It failed with one open "default" head and one closed "default"
head.

  • Participants
  • Parent commits 3a2d6e8

Comments (0)

Files changed (1)

          "[HUCB]\n\nAdd new branch '{0}' to uniquify heads.".format(name)])
 
 
+def append_branch(branch_heads, heads, sep):
+    """Parse strings in `heads` and add them to `branch_heads`."""
+    for head in heads:
+        (branch, node) = head.split(sep, 1)
+        branch_heads.setdefault(branch, [])
+        branch_heads[branch].append(node)
+
+
+def sort_branches(branch_heads):
+    """Make sure 'default' branch comes first (for cosmetic reason)."""
+    brancheset = set(branch_heads)
+    if 'default' in brancheset:
+        brancheset.remove('default')
+        return ['default'] + list(brancheset)
+    else:
+        return list(brancheset)
+
+
 def hucb(dry_run, sep, branch_temp):
     heads_active = hg_heads(sep).splitlines()
     heads_all = hg_heads(sep, '--closed').splitlines()
     heads_closed = set(heads_all) - set(heads_active)
 
     heads_branch = {}
-    for head in heads_closed:
-        (branch, node) = head.split(sep, 1)
-        heads_branch.setdefault(branch, [])
-        heads_branch[branch].append(node)
-
-    brancheset = set(heads_branch)
-    if 'default' in brancheset:
-        brancheset.remove('default')
-        branchlist = ['default'] + list(brancheset)
-    else:
-        branchlist = list(brancheset)
+    append_branch(heads_branch, heads_active, sep)
+    append_branch(heads_branch, heads_closed, sep)
 
     i = 0
-    for br in branchlist:
+    for br in sort_branches(heads_branch):
         heads = heads_branch[br]
         if len(heads) > 1:
-            for node in heads:
+            for node in heads[1:]:
                 newbranch = branch_temp.format(i=i)
                 print 'node:{node} | branch: {branch} -> {newbranch}'.format(
-                    node=node, branch=branch, newbranch=newbranch)
+                    node=node, branch=br, newbranch=newbranch)
                 if not dry_run:
                     hg_branch(node, newbranch)
                 i += 1