comparison contrib/hg-replay @ 721:2180358c32c4

Move some files to contrib
author Dongsheng Song <dongsheng.song@gmail.com>
date Thu, 12 Mar 2009 15:40:40 +0800
parents examples/hg-replay@d179d63ea018
children
comparison
equal deleted inserted replaced
720:1ef7708b3b7f 721:2180358c32c4
1 #!/usr/bin/env python
2 #
3 # Adapter for using interdiff with mercurial's extdiff extension.
4 #
5 # Copyright 2006 Bryan O'Sullivan <bos@serpentine.com>
6 #
7 # This software may be used and distributed according to the terms of
8 # the GNU General Public License, incorporated herein by reference.
9
10 import os
11 import shutil
12 import sys
13 import tempfile
14
15 if len(sys.argv) < 4:
16 print >> sys.stderr, ('usage: %s srcrepo destrepo cset-to-omit [...]' %
17 os.path.basename(sys.argv[0]))
18 sys.exit(1)
19
20 srcrepo, destrepo = sys.argv[1], sys.argv[2]
21 omit = sys.argv[3:]
22
23 changemap = {}
24 revs = []
25
26 parent = None
27
28 sys.stdout.write('gathering history...')
29 sys.stdout.flush()
30
31 for line in os.popen("hg --cwd %r log -r0:tip --template '{rev}:{node} {parents}\n'" % srcrepo):
32 changes = line.split()
33 cset = changes[0].split(':')[1]
34 rev = len(revs)
35 changemap[cset] = rev
36 if len(changes) >= 2:
37 p1 = int(changes[1].split(':', 1)[0])
38 if len(changes) == 3:
39 p2 = int(changes[2].split(':', 1)[0])
40 else:
41 p2 = None
42 if len(changes) == 1:
43 p1 = parent
44 revs.append((cset, p1, p2))
45 parent = rev
46
47 sys.stdout.write(' %d revs\n' % len(revs))
48
49 def findrev(r):
50 try:
51 i = int(r)
52 if str(i) == r:
53 rev = i
54 if rev < 0:
55 rev += len(revs)
56 if rev < 0 or rev > len(revs):
57 print >> sys.stderr, 'bad changeset: %r' % r
58 sys.exit(1)
59 cset = revs[rev][0]
60 except ValueError:
61 cset = r
62 matches = [changemap[c] for c in changemap if c.startswith(cset)]
63 if len(matches) != 1:
64 print >> sys.stderr, 'bad changeset: %r' % r
65 sys.exit(1)
66 rev = matches[0]
67 return rev
68
69 def run(cmd):
70 print cmd
71 ret = os.system(cmd)
72 if ret:
73 print >> sys.stderr, 'failure:', cmd
74 sys.exit(1)
75
76 omit = map(findrev, omit)
77 omit.sort()
78 newrevs = revs[:omit[0]]
79 tip = len(newrevs) - 1
80 run('hg clone -q -r%s %r %r' % (tip, srcrepo, destrepo))
81
82 os.environ['HGMERGE'] = 'true'
83
84 patchdir = tempfile.mkdtemp(prefix='replay.')
85 try:
86 run('hg --cwd %r export --git -o %r%s%%R %d:tip' %
87 (srcrepo, patchdir, os.sep, omit[0]+1))
88 for rev in xrange(omit[0], len(revs)):
89 if rev in omit:
90 print 'omit', rev
91 newrevs.append((None, revs[rev][1], None))
92 continue
93 _, p1, p2 = revs[rev]
94 np1 = newrevs[p1][1]
95 if tip != np1:
96 run('hg --cwd %r update -q -C %s' % (destrepo, np1))
97 np2 = None
98 if p2:
99 np2 = newrevs[p2][1]
100 run('hg --cwd %r merge -q %s' % (destrepo, np2))
101 print >> sys.stderr, 'XXX - cannot handle merges properly yet'
102 run('hg --cwd %r import -q -f %r%s%d' % (destrepo, patchdir, os.sep, rev))
103 tip = len(newrevs) - 1
104 newrevs.append((None, tip, np2))
105 finally:
106 print 'cleaning up ...'
107 #shutil.rmtree(patchdir)