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
,fixup_user
,get_branch
,get_changeset
,load_cache
,save_cache
,get_git_sha1
8 from tempfile
import mkstemp
9 from optparse
import OptionParser
14 # silly regex to catch Signed-off-by lines in log message
15 sob_re
=re
.compile('^Signed-[Oo]ff-[Bb]y: (.+)$')
16 # insert 'checkpoint' command after this many commits or none at all if 0
17 cfg_checkpoint_count
=0
18 # write some progress message every this many file contents written
19 cfg_export_boundary
=1000
22 return x
and '100755' or '100644'
28 #map(lambda x: sys.stderr.write('\t[%s]\n' % x),msg.split('\n'))
30 def checkpoint(count
):
32 if cfg_checkpoint_count
>0 and count
%cfg_checkpoint
_count
==0:
33 sys
.stderr
.write("Checkpoint after %d commits\n" % count
)
38 def get_parent_mark(parent
,marks
):
39 """Get the mark for some parent.
40 If we saw it in the current session, return :%d syntax and
41 otherwise the SHA1 from the cache."""
42 return marks
.get(str(parent
),':%d' % (parent
+1))
44 def file_mismatch(f1
,f2
):
45 """See if two revisions of a file are not equal."""
46 return node
.hex(f1
)!=node
.hex(f2
)
48 def split_dict(dleft
,dright
,l
=[],c
=[],r
=[],match
=file_mismatch
):
49 """Loop over our repository and find all changed and missing files."""
50 for left
in dleft
.keys():
51 right
=dright
.get(left
,None)
53 # we have the file but our parent hasn't: add to left set
55 elif match(dleft
[left
],right
):
56 # we have it but checksums mismatch: add to center set
58 for right
in dright
.keys():
59 left
=dleft
.get(right
,None)
61 # if parent has file but we don't: add to right set
63 # change is already handled when comparing child against parent
66 def get_filechanges(repo
,revision
,parents
,mleft
):
67 """Given some repository and revision, find all changed/deleted files."""
71 mright
=repo
.changectx(p
).manifest()
72 l
,c
,r
=split_dict(mleft
,mright
,l
,c
,r
)
78 def get_author(logmessage
,committer
,authors
):
79 """As git distincts between author and committer of a patch, try to
80 extract author by detecting Signed-off-by lines.
82 This walks from the end of the log message towards the top skipping
83 empty lines. Upon the first non-empty line, it walks all Signed-off-by
84 lines upwards to find the first one. For that (if found), it extracts
85 authorship information the usual way (authors table, cleaning, etc.)
87 If no Signed-off-by line is found, this defaults to the committer.
89 This may sound stupid (and it somehow is), but in log messages we
90 accidentially may have lines in the middle starting with
91 "Signed-off-by: foo" and thus matching our detection regex. Prevent
94 loglines
=logmessage
.split('\n')
96 # from tail walk to top skipping empty lines
99 if len(loglines
[i
].strip())==0: continue
102 # walk further upwards to find first sob line, store in 'first'
105 m
=sob_re
.match(loglines
[i
])
109 # if the last non-empty line matches our Signed-Off-by regex: extract username
111 r
=fixup_user(first
.group(1),authors
)
115 def export_file_contents(ctx
,manifest
,files
):
119 d
=ctx
.filectx(file).data()
120 wr('M %s inline %s' % (gitmode(manifest
.execf(file)),file))
121 wr('data %d' % len(d
)) # had some trouble with size()
124 if count
%cfg_export
_boundary
==0:
125 sys
.stderr
.write('Exported %d/%d files\n' % (count
,max))
126 if max>cfg_export_boundary
:
127 sys
.stderr
.write('Exported %d/%d files\n' % (count
,max))
129 def is_merge(parents
):
131 for parent
in parents
:
136 def export_commit(ui
,repo
,revision
,marks
,heads
,last
,max,count
,authors
,sob
):
137 (revnode
,_
,user
,(time
,timezone
),files
,desc
,branch
,_
)=get_changeset(ui
,repo
,revision
,authors
)
138 parents
=repo
.changelog
.parentrevs(revision
)
140 wr('commit refs/heads/%s' % branch
)
141 wr('mark :%d' % (revision
+1))
143 wr('author %s %d %s' % (get_author(desc
,user
,authors
),time
,timezone
))
144 wr('committer %s %d %s' % (user
,time
,timezone
))
145 wr('data %d' % (len(desc
)+1)) # wtf?
150 if parents
[0] < parents
[1]:
153 src
=heads
.get(branch
,'')
156 # if we have a cached head, this is an incremental import: initialize it
157 # and kill reference so we won't init it again
160 sys
.stderr
.write('%s: Initializing to parent [%s]\n' %
162 link
=src
# avoid making a merge commit for incremental import
163 elif link
=='' and not heads
.has_key(branch
) and revision
>0:
164 # newly created branch and not the first one: connect to parent
165 tmp
=get_parent_mark(parents
[0],marks
)
167 sys
.stderr
.write('%s: Link new branch to parent [%s]\n' %
169 link
=tmp
# avoid making a merge commit for branch fork
170 elif last
.get(branch
,revision
) != parents
[pidx1
] and parents
[pidx1
] > 0 and revision
> 0:
171 pm
=get_parent_mark(parents
[pidx1
],marks
)
172 sys
.stderr
.write('%s: Placing commit [r%d] in branch [%s] on top of [r%d]\n' %
173 (branch
,revision
,branch
,parents
[pidx1
]));
176 if parents
[pidx2
] > 0:
177 pm
=get_parent_mark(parents
[pidx2
],marks
)
178 sys
.stderr
.write('%s: Merging with parent [%s] from [r%d]\n' %
179 (branch
,pm
,parents
[pidx2
]))
182 last
[branch
]=revision
184 # we need this later to write out tags
185 marks
[str(revision
)]=':%d'%(revision
+1)
187 ctx
=repo
.changectx(str(revision
))
189 added
,changed
,removed
,type=[],[],[],''
192 # first revision: feed in full manifest
196 elif is_merge(parents
):
197 # later merge revision: feed in changed manifest
198 # for many files comparing checksums is expensive so only do it for
199 # merges where we really need it due to hg's revlog logic
200 added
,changed
,removed
=get_filechanges(repo
,revision
,parents
,man
)
201 type='thorough delta'
203 # later non-merge revision: feed in changed manifest
204 # if we have exactly one parent, just take the changes from the
205 # manifest without expensively comparing checksums
206 f
=repo
.status(repo
.lookup(parents
[0]),revnode
)[:3]
207 added
,changed
,removed
=f
[1],f
[0],f
[2]
210 sys
.stderr
.write('%s: Exporting %s revision %d/%d with %d/%d/%d added/changed/removed files\n' %
211 (branch
,type,revision
+1,max,len(added
),len(changed
),len(removed
)))
213 map(lambda r
: wr('D %s' % r
),removed
)
214 export_file_contents(ctx
,man
,added
)
215 export_file_contents(ctx
,man
,changed
)
218 return checkpoint(count
)
220 def export_tags(ui
,repo
,marks_cache
,start
,end
,count
,authors
):
223 # ignore latest revision
224 if tag
=='tip': continue
225 rev
=repo
.changelog
.rev(node
)
226 # ignore those tags not in our import range
227 if rev
<start
or rev
>=end
: continue
229 ref
=get_parent_mark(rev
,marks_cache
)
231 sys
.stderr
.write('Failed to find reference for creating tag'
232 ' %s at r%d\n' % (tag
,rev
))
234 sys
.stderr
.write('Exporting tag [%s] at [hg r%d] [git %s]\n' % (tag
,rev
,ref
))
235 wr('reset refs/tags/%s' % tag
)
238 count
=checkpoint(count
)
241 def load_authors(filename
):
243 if not os
.path
.exists(filename
):
247 lre
=re
.compile('^([^=]+)[ ]*=[ ]*(.+)$')
248 for line
in f
.readlines():
252 sys
.stderr
.write('Invalid file format in [%s], line %d\n' % (filename
,l
))
254 # put key:value in cache, key without ^:
255 cache
[m
.group(1).strip()]=m
.group(2).strip()
257 sys
.stderr
.write('Loaded %d authors\n' % l
)
260 def verify_heads(ui
,repo
,cache
,force
):
261 branches
=repo
.branchtags()
262 l
=[(-repo
.changelog
.rev(n
), n
, t
) for t
, n
in branches
.items()]
265 # get list of hg's branches to verify, don't take all git has
270 if sha1
!=None and c
!=None:
271 sys
.stderr
.write('Verifying branch [%s]\n' % b
)
273 sys
.stderr
.write('Error: Branch [%s] modified outside hg-fast-export:'
274 '\n%s (repo) != %s (cache)\n' % (b
,sha1
,c
))
275 if not force
: return False
277 # verify that branch has exactly one head
279 for h
in repo
.heads():
280 (_
,_
,_
,_
,_
,_
,branch
,_
)=get_changeset(ui
,repo
,h
)
281 if t
.get(branch
,False):
282 sys
.stderr
.write('Error: repository has at least one unnamed head: hg r%s\n' %
283 repo
.changelog
.rev(h
))
284 if not force
: return False
289 def mangle_mark(mark
):
290 return str(int(mark
)-1)
292 def hg2git(repourl
,m
,marksfile
,headsfile
,tipfile
,authors
={},sob
=False,force
=False):
295 marks_cache
=load_cache(marksfile
,mangle_mark
)
296 heads_cache
=load_cache(headsfile
)
297 state_cache
=load_cache(tipfile
)
299 ui
,repo
=setup_repo(repourl
)
301 if not verify_heads(ui
,repo
,heads_cache
,force
):
304 tip
=repo
.changelog
.count()
306 min=int(state_cache
.get('tip',0))
313 for rev
in range(min,max):
314 c
=export_commit(ui
,repo
,rev
,marks_cache
,heads_cache
,last
,max,c
,authors
,sob
)
316 c
=export_tags(ui
,repo
,marks_cache
,min,max,c
,authors
)
318 sys
.stderr
.write('Issued %d commands\n' % c
)
320 state_cache
['tip']=max
321 state_cache
['repo']=repourl
322 save_cache(tipfile
,state_cache
)
326 if __name__
=='__main__':
327 def bail(parser
,opt
):
328 sys
.stderr
.write('Error: No %s option given\n' % opt
)
332 parser
=OptionParser()
334 parser
.add_option("-m","--max",type="int",dest
="max",
335 help="Maximum hg revision to import")
336 parser
.add_option("--marks",dest
="marksfile",
337 help="File to read git-fast-import's marks from")
338 parser
.add_option("--heads",dest
="headsfile",
339 help="File to read last run's git heads from")
340 parser
.add_option("--status",dest
="statusfile",
341 help="File to read status from")
342 parser
.add_option("-r","--repo",dest
="repourl",
343 help="URL of repo to import")
344 parser
.add_option("-s",action
="store_true",dest
="sob",
345 default
=False,help="Enable parsing Signed-off-by lines")
346 parser
.add_option("-A","--authors",dest
="authorfile",
347 help="Read authormap from AUTHORFILE")
348 parser
.add_option("-f","--force",action
="store_true",dest
="force",
349 default
=False,help="Ignore validation errors by force")
351 (options
,args
)=parser
.parse_args()
354 if options
.max!=None: m
=options
.max
356 if options
.marksfile
==None: bail(parser
,'--marks')
357 if options
.headsfile
==None: bail(parser
,'--heads')
358 if options
.statusfile
==None: bail(parser
,'--status')
359 if options
.repourl
==None: bail(parser
,'--repo')
362 if options
.authorfile
!=None:
363 a
=load_authors(options
.authorfile
)
365 sys
.exit(hg2git(options
.repourl
,m
,options
.marksfile
,options
.headsfile
,
366 options
.statusfile
,authors
=a
,sob
=options
.sob
,force
=options
.force
))