Fix escape in regular expression
[fast-export.git] / hg-reset.py
blob6aa88373867724c378fda7b542f2f58333b22cfc
1 #!/usr/bin/env python3
3 # Copyright (c) 2007, 2008 Rocco Rutte <pdmef@gmx.net> and others.
4 # License: GPLv2
6 from mercurial import node
7 from hg2git import setup_repo,load_cache,get_changeset,get_git_sha1
8 from optparse import OptionParser
9 import sys
10 from binascii import hexlify
12 def heads(ui,repo,start=None,stop=None,max=None):
13 # this is copied from mercurial/revlog.py and differs only in
14 # accepting a max argument for xrange(startrev+1,...) defaulting
15 # to the original repo.changelog.count()
16 if start is None:
17 start = node.nullid
18 if stop is None:
19 stop = []
20 if max is None:
21 max = repo.changelog.count()
22 stoprevs = dict.fromkeys([repo.changelog.rev(n) for n in stop])
23 startrev = repo.changelog.rev(start)
24 reachable = {startrev: 1}
25 heads = {startrev: 1}
27 parentrevs = repo.changelog.parentrevs
28 for r in range(startrev + 1, max):
29 for p in parentrevs(r):
30 if p in reachable:
31 if r not in stoprevs:
32 reachable[r] = 1
33 heads[r] = 1
34 if p in heads and p not in stoprevs:
35 del heads[p]
37 return [(repo.changelog.node(r), b"%d" % r) for r in heads]
39 def get_branches(ui,repo,heads_cache,marks_cache,mapping_cache,max):
40 h=heads(ui,repo,max=max)
41 stale=dict.fromkeys(heads_cache)
42 changed=[]
43 unchanged=[]
44 for node,rev in h:
45 _,_,user,(_,_),_,desc,branch,_=get_changeset(ui,repo,rev)
46 del stale[branch]
47 git_sha1=get_git_sha1(branch)
48 cache_sha1=marks_cache.get(b"%d" % (int(rev)+1))
49 if git_sha1!=None and git_sha1==cache_sha1:
50 unchanged.append([branch,cache_sha1,rev,desc.split(b'\n')[0],user])
51 else:
52 changed.append([branch,cache_sha1,rev,desc.split(b'\n')[0],user])
53 changed.sort()
54 unchanged.sort()
55 return stale,changed,unchanged
57 def get_tags(ui,repo,marks_cache,mapping_cache,max):
58 l=repo.tagslist()
59 good,bad=[],[]
60 for tag,node in l:
61 if tag==b'tip': continue
62 rev=int(mapping_cache[hexlify(node)])
63 cache_sha1=marks_cache.get(b"%d" % (int(rev)+1))
64 _,_,user,(_,_),_,desc,branch,_=get_changeset(ui,repo,rev)
65 if int(rev)>int(max):
66 bad.append([tag,branch,cache_sha1,rev,desc.split(b'\n')[0],user])
67 else:
68 good.append([tag,branch,cache_sha1,rev,desc.split(b'\n')[0],user])
69 good.sort()
70 bad.sort()
71 return good,bad
73 def mangle_mark(mark):
74 return b"%d" % (int(mark)-1)
76 if __name__=='__main__':
77 def bail(parser,opt):
78 sys.stderr.write('Error: No option %s given\n' % opt)
79 parser.print_help()
80 sys.exit(2)
82 parser=OptionParser()
84 parser.add_option("--marks",dest="marksfile",
85 help="File to read git-fast-import's marks from")
86 parser.add_option("--mapping",dest="mappingfile",
87 help="File to read last run's hg-to-git SHA1 mapping")
88 parser.add_option("--heads",dest="headsfile",
89 help="File to read last run's git heads from")
90 parser.add_option("--status",dest="statusfile",
91 help="File to read status from")
92 parser.add_option("-r","--repo",dest="repourl",
93 help="URL of repo to import")
94 parser.add_option("-R","--revision",type=int,dest="revision",
95 help="Revision to reset to")
97 (options,args)=parser.parse_args()
99 if options.marksfile==None: bail(parser,'--marks option')
100 if options.mappingfile==None: bail(parser,'--mapping option')
101 if options.headsfile==None: bail(parser,'--heads option')
102 if options.statusfile==None: bail(parser,'--status option')
103 if options.repourl==None: bail(parser,'--repo option')
104 if options.revision==None: bail(parser,'-R/--revision')
106 heads_cache=load_cache(options.headsfile)
107 marks_cache=load_cache(options.marksfile,mangle_mark)
108 state_cache=load_cache(options.statusfile)
109 mapping_cache = load_cache(options.mappingfile)
111 l=int(state_cache.get(b'tip',options.revision))
112 if options.revision+1>l:
113 sys.stderr.write('Revision is beyond last revision imported: %d>%d\n' % (options.revision,l))
114 sys.exit(1)
116 ui,repo=setup_repo(options.repourl)
118 stale,changed,unchanged=get_branches(ui,repo,heads_cache,marks_cache,mapping_cache,options.revision+1)
119 good,bad=get_tags(ui,repo,marks_cache,mapping_cache,options.revision+1)
121 print("Possibly stale branches:")
122 for b in stale:
123 sys.stdout.write('\t%s\n' % b.decode('utf8'))
125 print("Possibly stale tags:")
126 for b in bad:
127 sys.stdout.write(
128 '\t%s on %s (r%s)\n'
129 % (b[0].decode('utf8'), b[1].decode('utf8'), b[3].decode('utf8'))
132 print("Unchanged branches:")
133 for b in unchanged:
134 sys.stdout.write('\t%s (r%s)\n' % (b[0].decode('utf8'),b[2].decode('utf8')))
136 print("Unchanged tags:")
137 for b in good:
138 sys.stdout.write(
139 '\t%s on %s (r%s)\n'
140 % (b[0].decode('utf8'), b[1].decode('utf8'), b[3].decode('utf8'))
143 print("Reset branches in '%s' to:" % options.headsfile)
144 for b in changed:
145 sys.stdout.write(
146 '\t:%s %s\n\t\t(r%s: %s: %s)\n'
148 b[0].decode('utf8'),
149 b[1].decode('utf8'),
150 b[2].decode('utf8'),
151 b[4].decode('utf8'),
152 b[3].decode('utf8'),
156 print("Reset ':tip' in '%s' to '%d'" % (options.statusfile,options.revision))