Commits

Takafumi Arakaki  committed cddd2ae

Add hucb.py

  • Participants

Comments (0)

Files changed (1)

+#!/usr/bin/env python
+
+import subprocess
+
+
+def command(cmds, *args, **kwds):
+    cmds = list(a for a in cmds if a)  # exclude empty string
+    proc = subprocess.Popen(
+        cmds, *args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, **kwds)
+    ret = proc.wait()
+    stdout = proc.stdout.read()
+    if ret != 0:
+        print stdout
+        import sys
+        sys.exit(1)
+    return stdout
+
+
+def hg_heads(sep, *args):
+    return command(
+        ['hg', 'heads', '--template', sep.join(['{branch}', '{node}\n'])]
+        + list(args))
+
+
+def hg_branch(revision, name):
+    command(['hg', 'update', revision])
+    command(['hg', 'branch', name])
+    command(['hg', 'commit', '--message',
+             "[HUCB] Add new branch '{0}' to uniquify heads.".format(name)])
+
+
+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)
+
+    i = 0
+    for br in branchlist:
+        heads = heads_branch[br]
+        if len(heads) > 1:
+            for node in heads:
+                newbranch = branch_temp.format(i=i)
+                print 'node:{node} | branch: {branch} -> {newbranch}'.format(
+                    node=node, branch=branch, newbranch=newbranch)
+                if not dry_run:
+                    hg_branch(node, newbranch)
+                i += 1
+
+
+def main():
+    from argparse import ArgumentParser
+    parser = ArgumentParser(description=__doc__)
+    parser.add_argument('--dry-run', '-n', default=False, action='store_true')
+    parser.add_argument('--sep', default='#')
+    parser.add_argument('--branch-temp', default='closed-{i}')
+    args = parser.parse_args()
+    hucb(**vars(args))
+
+
+if __name__ == '__main__':
+    main()