| 1 |
""" |
| 2 |
For branches that have been closed but still have a dangling head |
| 3 |
in 'hg heads --topo --closed', force them to join with the branch |
| 4 |
called 'closed-branch'. It reduces the number of heads. |
| 5 |
""" |
| 6 |
|
| 7 |
import os |
| 8 |
import sys |
| 9 |
import commands |
| 10 |
|
| 11 |
if not os.path.isdir('.hg'): |
| 12 |
print 'Must run this script from the top-level directory.' |
| 13 |
sys.exit(1) |
| 14 |
|
| 15 |
def heads(): |
| 16 |
result = commands.getoutput( |
| 17 |
"hg heads --topo --closed --template '{node|short}:{branches}:{extras}\n'") |
| 18 |
result = result.splitlines(False) |
| 19 |
result = [s.split(':', 2) for s in result] |
| 20 |
for line in result: |
| 21 |
if len(line) != 3: |
| 22 |
raise ValueError("'result' contains: %r" % line) |
| 23 |
result = [(head, branch or 'default') for (head, branch, extra) in result |
| 24 |
if branch != 'closed-branches' and 'close=1' in extra] |
| 25 |
return result |
| 26 |
|
| 27 |
|
| 28 |
closed_heads = heads() |
| 29 |
|
| 30 |
if not closed_heads: |
| 31 |
print >> sys.stderr, 'no dangling closed heads.' |
| 32 |
sys.exit() |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
closed_heads.reverse() |
| 37 |
|
| 38 |
for head, branch in closed_heads: |
| 39 |
print '\t', head, '\t', branch |
| 40 |
print |
| 41 |
print 'The %d branches listed above will be merged to "closed-branches".' % ( |
| 42 |
len(closed_heads),) |
| 43 |
print 'You need to run this script in a clean working copy where you' |
| 44 |
print 'don''t mind all files being removed.' |
| 45 |
print |
| 46 |
if raw_input('Continue? [y/n] ').upper() != 'Y': |
| 47 |
sys.exit(1) |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
def do(cmd): |
| 52 |
print cmd |
| 53 |
err = os.system(cmd) |
| 54 |
if err != 0: |
| 55 |
print '*** error %r' % (err,) |
| 56 |
sys.exit(1) |
| 57 |
|
| 58 |
print '*** switching to closed branches *** ' |
| 59 |
do("hg up --clean closed-branches") |
| 60 |
do("hg --config extensions.purge= purge --all") |
| 61 |
|
| 62 |
for head, branch in closed_heads: |
| 63 |
print |
| 64 |
print '***** %s ***** %s *****' % (branch, head) |
| 65 |
do("hg debugsetparents closed-branches %s" % head) |
| 66 |
do("hg ci -m'Merge closed head %s on branch %s'" % (head, branch)) |
| 67 |
|
| 68 |
print |
| 69 |
do("hg ci --close-branch -m're-close this branch'") |
| 70 |
do("hg up default") |