hgbook
view examples/hg-replay @ 280:4700dd38384c
Bring the comparison section up to date and more into balance.
author | Bryan O'Sullivan <bos@serpentine.com> |
---|---|
date | Fri Dec 14 11:08:44 2007 -0800 (2007-12-14) |
parents | |
children |
line source
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.
10 import os
11 import shutil
12 import sys
13 import tempfile
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)
20 srcrepo, destrepo = sys.argv[1], sys.argv[2]
21 omit = sys.argv[3:]
23 changemap = {}
24 revs = []
26 parent = None
28 sys.stdout.write('gathering history...')
29 sys.stdout.flush()
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
47 sys.stdout.write(' %d revs\n' % len(revs))
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
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)
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))
82 os.environ['HGMERGE'] = 'true'
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)