3 # Copyright (c) 2007, 2008 Rocco Rutte <pdmef@gmx.net> and others.
6 from mercurial
import node
7 from hg2git
import setup_repo
,load_cache
,get_changeset
,get_git_sha1
8 from optparse
import OptionParser
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()
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}
27 parentrevs
= repo
.changelog
.parentrevs
28 for r
in range(startrev
+ 1, max):
29 for p
in parentrevs(r
):
34 if p
in heads
and p
not in stoprevs
:
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
)
45 _
,_
,user
,(_
,_
),_
,desc
,branch
,_
=get_changeset(ui
,repo
,rev
)
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
])
52 changed
.append([branch
,cache_sha1
,rev
,desc
.split(b
'\n')[0],user
])
55 return stale
,changed
,unchanged
57 def get_tags(ui
,repo
,marks_cache
,mapping_cache
,max):
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
)
66 bad
.append([tag
,branch
,cache_sha1
,rev
,desc
.split(b
'\n')[0],user
])
68 good
.append([tag
,branch
,cache_sha1
,rev
,desc
.split(b
'\n')[0],user
])
73 def mangle_mark(mark
):
74 return b
"%d" % (int(mark
)-1)
76 if __name__
=='__main__':
78 sys
.stderr
.write('Error: No option %s given\n' % opt
)
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
))
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:")
123 sys
.stdout
.write('\t%s\n' % b
.decode('utf8'))
125 print("Possibly stale tags:")
129 % (b
[0].decode('utf8'), b
[1].decode('utf8'), b
[3].decode('utf8'))
132 print("Unchanged branches:")
134 sys
.stdout
.write('\t%s (r%s)\n' % (b
[0].decode('utf8'),b
[2].decode('utf8')))
136 print("Unchanged tags:")
140 % (b
[0].decode('utf8'), b
[1].decode('utf8'), b
[3].decode('utf8'))
143 print("Reset branches in '%s' to:" % options
.headsfile
)
146 '\t:%s %s\n\t\t(r%s: %s: %s)\n'
156 print("Reset ':tip' in '%s' to '%d'" % (options
.statusfile
,options
.revision
))