hg-uniquify-closed-branch /

#!/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 =
    if ret != 0:
        print stdout
        import sys
    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])
    date = command(['hg', 'log', '--rev', revision,
                    '--template', '{date|hgdate}'])
    (ts, tz) = date.split(' ', 1)
    newts = int(ts) + 10
    newdate = '{0} {1}'.format(newts, tz)
    command(['hg', 'commit', '--date', newdate, '--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, [])

    brancheset = set(heads_branch)
    if 'default' in brancheset:
        branchlist = ['default'] + list(brancheset)
        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()

if __name__ == '__main__':
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.