hg-fast-export.py: Fix option presence checking
[fast-export/rorcz.git] / hg-reset.py
blob8e1fa87edebb0a97cb5bb23b610a111cee44308d
1 #!/usr/bin/env python
3 # Copyright (c) 2007 Rocco Rutte <pdmef@gmx.net>
4 # License: MIT <http://www.opensource.org/licenses/mit-license.php>
6 from mercurial import repo,hg,cmdutil,util,ui,revlog,node
7 from hg2git import setup_repo,load_cache,get_changeset
8 from optparse import OptionParser
9 import sys
11 def heads(ui,repo,start=None,stop=None,max=None):
12 if start is None:
13 start = node.nullid
14 if stop is None:
15 stop = []
16 if max is None:
17 max = repo.changelog.count()
18 stoprevs = dict.fromkeys([repo.changelog.rev(n) for n in stop])
19 startrev = repo.changelog.rev(start)
20 reachable = {startrev: 1}
21 heads = {startrev: 1}
23 parentrevs = repo.changelog.parentrevs
24 for r in xrange(startrev + 1, max):
25 for p in parentrevs(r):
26 if p in reachable:
27 if r not in stoprevs:
28 reachable[r] = 1
29 heads[r] = 1
30 if p in heads and p not in stoprevs:
31 del heads[p]
33 return [(repo.changelog.node(r),str(r)) for r in heads]
35 def get_branches(ui,repo,heads_cache,marks_cache,max):
36 h=heads(ui,repo,max=max)
37 old=dict.fromkeys(heads_cache)
38 r=[]
39 for node,rev in h:
40 _,_,user,(_,_),_,desc,branch,_=get_changeset(ui,repo,rev)
41 del old[branch]
42 r.append([branch,marks_cache.get(str(int(rev)+1)),rev,desc.split('\n')[0],user])
43 r.sort()
44 return old,r
46 if __name__=='__main__':
47 def bail(parser,opt):
48 sys.stderr.write('Error: No option %s given\n' % opt)
49 parser.print_help()
50 sys.exit(2)
52 parser=OptionParser()
54 parser.add_option("--marks",dest="marksfile",
55 help="File to read git-fast-import's marks from")
56 parser.add_option("--heads",dest="headsfile",
57 help="File to read last run's git heads from")
58 parser.add_option("--status",dest="statusfile",
59 help="File to read status from")
60 parser.add_option("-r","--repo",dest="repourl",
61 help="URL of repo to import")
62 parser.add_option("-R","--revision",type=int,dest="revision",
63 help="Revision to reset to")
65 (options,args)=parser.parse_args()
67 if options.marksfile==None: bail(parser,'--marks option')
68 if options.headsfile==None: bail(parser,'--heads option')
69 if options.statusfile==None: bail(parser,'--status option')
70 if options.repourl==None: bail(parser,'--repo option')
71 if options.revision==None: bail(parser,'-R/--revision')
73 heads_cache=load_cache(options.headsfile)
74 marks_cache=load_cache(options.marksfile)
75 state_cache=load_cache(options.statusfile)
77 l=int(state_cache.get('tip',options.revision))
78 if options.revision+1>l:
79 sys.stderr.write('Revision is beyond last revision imported: %d>%d\n' % (options.revision,l))
80 sys.exit(1)
82 ui,repo=setup_repo(options.repourl)
83 stale,changed=get_branches(ui,repo,heads_cache,marks_cache,options.revision+1)
85 print "Possibly stale branches:"
86 map(lambda b: sys.stdout.write('\t%s\n' % b),stale.keys())
88 print "Reset branches in '%s' to:" % options.headsfile
89 map(lambda b: sys.stdout.write('\t:%s %s\n\t\t(r%s: %s: %s)\n' % (b[0],b[1],b[2],b[4],b[3])),changed)
91 print "Reset ':tip' in '%s' to '%d'" % (options.statusfile,options.revision)