3 # Copyright (c) 2007 Rocco Rutte <pdmef@gmx.net>
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
11 def heads(ui,repo,start=None,stop=None,max=None):
12 # this is copied from mercurial/revlog.py and differs only in
13 # accepting a max argument for xrange(startrev+1,...) defaulting
14 # to the original repo.changelog.count()
20 max = repo.changelog.count()
21 stoprevs = dict.fromkeys([repo.changelog.rev(n) for n in stop])
22 startrev = repo.changelog.rev(start)
23 reachable = {startrev: 1}
26 parentrevs = repo.changelog.parentrevs
27 for r in xrange(startrev + 1, max):
28 for p in parentrevs(r):
33 if p in heads and p not in stoprevs:
36 return [(repo.changelog.node(r),str(r)) for r in heads]
38 def get_branches(ui,repo,heads_cache,marks_cache,max):
39 h=heads(ui,repo,max=max)
40 old=dict.fromkeys(heads_cache)
43 _,_,user,(_,_),_,desc,branch,_=get_changeset(ui,repo,rev)
45 r.append([branch,marks_cache.get(str(int(rev)+1)),rev,desc.split('\n')[0],user])
49 if __name__=='__main__':
51 sys.stderr.write('Error: No option %s given\n' % opt)
57 parser.add_option("--marks",dest="marksfile",
58 help="File to read git-fast-import's marks from")
59 parser.add_option("--heads",dest="headsfile",
60 help="File to read last run's git heads from")
61 parser.add_option("--status",dest="statusfile",
62 help="File to read status from")
63 parser.add_option("-r","--repo",dest="repourl",
64 help="URL of repo to import")
65 parser.add_option("-R","--revision",type=int,dest="revision",
66 help="Revision to reset to")
68 (options,args)=parser.parse_args()
70 if options.marksfile==None: bail(parser,'--marks option')
71 if options.headsfile==None: bail(parser,'--heads option')
72 if options.statusfile==None: bail(parser,'--status option')
73 if options.repourl==None: bail(parser,'--repo option')
74 if options.revision==None: bail(parser,'-R/--revision')
76 heads_cache=load_cache(options.headsfile)
77 marks_cache=load_cache(options.marksfile)
78 state_cache=load_cache(options.statusfile)
80 l=int(state_cache.get('tip',options.revision))
81 if options.revision+1>l:
82 sys.stderr.write('Revision is beyond last revision imported: %d>%d\n' % (options.revision,l))
85 ui,repo=setup_repo(options.repourl)
86 stale,changed=get_branches(ui,repo,heads_cache,marks_cache,options.revision+1)
88 print "Possibly stale branches:"
89 map(lambda b: sys.stdout.write('\t%s\n' % b),stale.keys())
91 print "Reset branches in '%s' to:" % options.headsfile
92 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)
94 print "Reset ':tip' in '%s' to '%d'" % (options.statusfile,options.revision)