File: clean_old_branches.py

package info (click to toggle)
pypy3 7.0.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 111,848 kB
  • sloc: python: 1,291,746; ansic: 74,281; asm: 5,187; cpp: 3,017; sh: 2,533; makefile: 544; xml: 243; lisp: 45; csh: 21; awk: 4
file content (70 lines) | stat: -rw-r--r-- 2,032 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""
For branches that have been closed but still have a dangling head
in 'hg heads --topo --closed', force them to join with the branch
called 'closed-branch'.  It reduces the number of heads.
"""

import os
import sys
import commands

if not os.path.isdir('.hg'):
    print 'Must run this script from the top-level directory.'
    sys.exit(1)

def heads():
    result = commands.getoutput(
        "hg heads --topo --closed --template '{node|short}:{branches}:{extras}\n'")
    result = result.splitlines(False)
    result = [s.split(':', 2) for s in result]
    for line in result:
        if len(line) != 3:
            raise ValueError("'result' contains: %r" % line)
    result = [(head, branch or 'default') for (head, branch, extra) in result
                if branch != 'closed-branches' and 'close=1' in extra]
    return result


closed_heads = heads()

if not closed_heads:
    print >> sys.stderr, 'no dangling closed heads.'
    sys.exit()

# ____________________________________________________________

closed_heads.reverse()

for head, branch in closed_heads:
    print '\t', head, '\t', branch
print
print 'The %d branches listed above will be merged to "closed-branches".' % (
    len(closed_heads),)
print 'You need to run this script in a clean working copy where you'
print 'don''t mind all files being removed.'
print
if raw_input('Continue? [y/n] ').upper() != 'Y':
    sys.exit(1)

# ____________________________________________________________

def do(cmd):
    print cmd
    err = os.system(cmd)
    if err != 0:
        print '*** error %r' % (err,)
        sys.exit(1)

print '*** switching to closed branches *** '
do("hg up --clean closed-branches")
do("hg --config extensions.purge= purge --all")

for head, branch in closed_heads:
    print
    print '***** %s ***** %s *****' % (branch, head)
    do("hg debugsetparents closed-branches %s" % head)
    do("hg ci -m'Merge closed head %s on branch %s'" % (head, branch))

print
do("hg ci --close-branch -m're-close this branch'")
do("hg up default")