3 # Copyright (c) 2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
5 # Distributed under terms of the GNU General Public License
6 # This program comes with NO WARRANTY.
17 # - use binary search to find from-patch ica. conflict.
21 import distutils
.version
26 import xml
.dom
.minidom
28 import gdbm
as dbmodule
31 from email
.utils
import parsedate_tz
33 ################################################################
39 mail_to_name_dict
= {}
44 ################################################################
47 class PullConflict (Exception):
49 class CommandFailed (Exception):
53 sys
.stderr
.write (s
+ '\n')
55 def get_cli_options ():
56 class MyOP(optparse
.OptionParser
):
58 optparse
.OptionParser
.print_help (self
)
62 This tool is a conversion utility for Darcs repositories, importing
63 them in chronological order. It requires a Git version that has
64 git-fast-import. It does not support incremental updating.
68 * repositories with skewed timestamps, or different patches with
69 equal timestamps will confuse darcs2git.
70 * does not respect file modes or time zones.
71 * too slow. See source code for instructions to speed it up.
72 * probably doesn\'t work on partial repositories
74 Report new bugs to hanwen@xs4all.nl
78 Copyright (c) 2007 Han-Wen Nienhuys <hanwen@xs4all.nl>.
79 Distributed under terms of the GNU General Public License
80 This program comes with NO WARRANTY.
85 p
.usage
='''darcs2git [OPTIONS] DARCS-REPO'''
86 p
.description
='''Convert darcs repo to git.'''
88 def update_map (option
, opt
, value
, parser
):
89 for l
in open (value
).readlines ():
90 (mail
, name
) = tuple (l
.strip ().split ('='))
91 mail_to_name_dict
[mail
] = name
93 p
.add_option ('-a', '--authors', action
='callback',
97 help='read a text file, containing EMAIL=NAME lines')
99 p
.add_option ('--checkpoint-frequency', action
='store',
100 dest
='checkpoint_frequency',
103 help='how often should the git importer be synced?\n'
104 'Default is 0 (no limit)'
107 p
.add_option ('-d', '--destination', action
='store',
110 dest
='target_git_repo',
111 help='where to put the resulting Git repo.')
113 p
.add_option ('--verbose', action
='store_true',
116 help='show commands as they are invoked')
118 p
.add_option ('--history-window', action
='store',
119 dest
='history_window',
122 help='Look back this many patches as conflict ancestors.\n'
123 'Default is 0 (no limit)'
126 p
.add_option ('--debug', action
='store_true',
129 help="""add patch numbers to commit messages;
130 don\'t clean conversion repo;
134 options
, args
= p
.parse_args ()
139 if len(urlparse
.urlparse(args
[0])) == 0:
140 raise NotImplementedError,"We support local DARCS repos only."
142 git_version
= distutils
.version
.LooseVersion(os
.popen("git --version","r").read().strip().split(" ")[-1])
143 ideal_version
= distutils
.version
.LooseVersion("1.5.0")
144 if git_version
<ideal_version
:
145 raise RuntimeError,"You need git >= 1.5.0 for this."
147 options
.basename
= os
.path
.basename (os
.path
.normpath (args
[0])).replace ('.darcs', '')
148 if not options
.target_git_repo
:
149 options
.target_git_repo
= options
.basename
+ '.git'
153 name
= options
.target_git_repo
.replace ('.git', '.log')
154 if name
== options
.target_git_repo
:
157 progress ("Shell log to %s" % name
)
158 log_file
= open (name
, 'w')
160 return (options
, args
)
162 def read_pipe (cmd
, ignore_errors
=False):
164 progress ('pipe %s' % cmd
)
165 pipe
= os
.popen (cmd
)
168 if pipe
.close () and not ignore_errors
:
169 raise CommandFailed ("Pipe failed: %s" % cmd
)
173 def system (c
, ignore_error
=0, timed
=0):
180 log_file
.write ('%s\n' % c
)
183 if os
.system (c
) and not ignore_error
:
184 raise CommandFailed ("Command failed: %s" % c
)
186 def darcs_date_to_git (x
):
187 t
= time
.strptime (x
, '%Y%m%d%H%M%S')
188 return '%d' % int (time
.mktime (t
))
190 def darcs_timezone (x
) :
191 tz
= parsedate_tz(x
)[9] / 60
192 return "%+03d%02d" % (tz
/ 60, tz
% 60)
194 ################################################################
197 class DarcsConversionRepo
:
198 """Representation of a Darcs repo.
200 The repo is thought to be ordered, and supports methods for
201 going back (obliterate) and forward (pull).
205 def __init__ (self
, dir, patches
):
206 self
.dir = os
.path
.abspath (dir)
207 self
.patches
= patches
208 self
._current
_number
= -1
210 self
._inventory
_dict
= None
212 self
._short
_id
_dict
= dict ((p
.short_id (), p
) for p
in patches
)
214 def is_contiguous (self
):
215 return (len (self
.inventory_dict ()) == self
._current
_number
+1
216 and self
.contains_contiguous (self
._current
_number
))
218 def contains_contiguous (self
, num
):
219 if not self
._is
_valid
:
222 darcs_dir
= self
.dir + '/_darcs'
223 if not os
.path
.exists (darcs_dir
):
226 for p
in self
.patches
[:num
+ 1]:
227 if not self
.has_patch (p
):
232 def has_patch (self
, p
):
233 assert self
._is
_valid
235 return self
.inventory_dict ().has_key (p
.short_id ())
237 def pristine_tree (self
):
238 return self
.dir + '/_darcs/pristine'
240 def go_back_to (self
, dest
):
242 # at 4, len = 5, go to 2: count == 2
243 count
= len (self
.inventory_dict()) - dest
- 1
245 assert self
._is
_valid
251 progress ('Rewinding %d patches' % count
)
252 system ('cd %(dir)s && echo ay|darcs obliterate --ignore-times --last %(count)d' % locals ())
253 d
= self
.inventory_dict ()
254 for p
in self
.patches
[dest
+1:self
._current
_number
+1]:
260 self
._current
_number
= dest
263 system ('rm -rf %s' % self
.dir)
267 system ('rsync -a %(dir)s/_darcs/pristine/ %(dir)s/' % locals ())
269 def pull (self
, patch
):
270 id = patch
.attributes
['hash']
271 source_repo
= patch
.dir
274 progress ('Pull patch %d' % patch
.number
)
275 system ('cd %(dir)s && darcs pull --ignore-times --quiet --all --match "hash %(id)s" %(source_repo)s ' % locals ())
277 self
._current
_number
= patch
.number
279 ## must reread: the pull may have pulled in others.
280 self
._inventory
_dict
= None
282 def go_forward_to (self
, num
):
283 d
= self
.inventory_dict ()
288 for p
in self
.patches
[0:num
+1]:
289 if not d
.has_key (p
.short_id ()):
293 pull_str
= ' || '.join (['hash %s' % p
.id () for p
in pull_me
])
295 src
= self
.patches
[0].dir
297 progress ('Pulling %d patches to go to %d' % (len (pull_me
), num
))
298 system ('darcs pull --all --repo %(dir)s --match "%(pull_str)s" %(src)s' % locals ())
300 def create_fresh (self
):
302 system ('rm -rf %(dir)s && mkdir %(dir)s && darcs init --repo %(dir)s'
304 self
._is
_valid
= True
305 self
._current
_number
= -1
306 self
._inventory
_dict
= {}
308 def inventory (self
):
309 darcs_dir
= self
.dir + '/_darcs'
311 for f
in [darcs_dir
+ '/inventory'] + glob
.glob (darcs_dir
+ '/inventories/*'):
312 i
+= open (f
).read ()
315 def inventory_dict (self
):
316 if type (self
._inventory
_dict
) != type ({}):
317 self
._inventory
_dict
= {}
320 self
._inventory
_dict
[m
.group (1)] = self
._short
_id
_dict
[m
.group(1)]
322 re
.sub (r
'\n([^*\n]+\*[*-][0-9]+)', note_patch
, self
.inventory ())
323 return self
._inventory
_dict
325 def start_at (self
, num
):
327 """Move the repo to NUM.
329 This uses the fishy technique of writing the inventory and
330 constructing the pristine tree with 'darcs repair'
332 progress ('Starting afresh at %d' % num
)
336 iv
= open (dir + '/_darcs/inventory', 'w')
338 log_file
.write ("# messing with _darcs/inventory")
340 for p
in self
.patches
[:num
+1]:
341 os
.link (p
.filename (), dir + '/_darcs/patches/' + os
.path
.basename (p
.filename ()))
342 iv
.write (p
.header ())
343 self
._inventory
_dict
[p
.short_id ()] = p
346 system ('darcs repair --repo %(dir)s --quiet' % locals ())
348 self
._current
_number
= num
349 self
._is
_valid
= True
351 def go_to (self
, dest
):
352 contiguous
= self
.is_contiguous ()
354 if not self
._is
_valid
:
356 elif dest
== self
._current
_number
and contiguous
:
358 elif (self
.contains_contiguous (dest
)):
359 self
.go_back_to (dest
)
360 elif dest
- len (self
.inventory_dict ()) < dest
/ 100:
361 self
.go_forward_to (dest
)
366 def go_from_to (self
, from_patch
, to_patch
):
368 """Move the repo to FROM_PATCH, then go to TO_PATCH. Raise
369 PullConflict if conflict is detected
372 progress ('Trying %s -> %s' % (from_patch
, to_patch
))
374 source
= to_patch
.dir
377 self
.go_to (from_patch
.number
)
383 success
= 'No conflicts to resolve' in read_pipe ('cd %(dir)s && echo y|darcs resolve' % locals ())
384 except CommandFailed
:
385 self
._is
_valid
= False
386 raise PullConflict ()
389 raise PullConflict ()
393 return 'patch %d' % self
.number
395 def __init__ (self
, xml
, dir):
400 self
._contents
= None
401 for (nm
, value
) in xml
.attributes
.items():
402 self
.attributes
[nm
] = value
404 # fixme: ugh attributes vs. methods.
405 self
.extract_author ()
406 self
.extract_message ()
410 return self
.attributes
['hash']
414 if self
.attributes
['inverted'] == 'True':
417 return '%s*%s%s' % (self
.attributes
['author'], inv
, self
.attributes
['hash'].split ('-')[0])
420 return self
.dir + '/_darcs/patches/' + self
.attributes
['hash']
423 if type (self
._contents
) != type (''):
424 f
= gzip
.open (self
.filename ())
425 self
._contents
= f
.read ()
427 return self
._contents
430 lines
= self
.contents ().split ('\n')
433 committer
= lines
[1] + '\n'
434 committer
= re
.sub ('] {\n$', ']\n', committer
)
435 committer
= re
.sub ('] *\n$', ']\n', committer
)
437 if not committer
.endswith (']\n'):
444 header
= name
+ '\n' + committer
448 assert header
[-1] == '\n'
451 def extract_author (self
):
452 mail
= self
.attributes
['author']
454 m
= re
.search ("^(.*) <(.*)>$", mail
)
461 name
= mail_to_name_dict
[mail
]
463 name
= mail
.split ('@')[0]
465 self
.author_name
= name
466 self
.author_mail
= mail
468 def extract_time (self
):
469 self
.date
= darcs_date_to_git (self
.attributes
['date']) + ' ' + darcs_timezone (self
.attributes
['local_date'])
472 patch_name
= '(no comment)'
474 name_elt
= self
.xml
.getElementsByTagName ('name')[0]
475 patch_name
= name_elt
.childNodes
[0].data
480 def extract_message (self
):
481 patch_name
= self
.name ()
482 comment_elts
= self
.xml
.getElementsByTagName ('comment')
485 comment
= comment_elts
[0].childNodes
[0].data
487 if self
.attributes
['inverted'] == 'True':
488 patch_name
= 'UNDO: ' + patch_name
490 self
.message
= '%s\n\n%s' % (patch_name
, comment
)
493 patch_name
= self
.name ()
494 if patch_name
.startswith ("TAG "):
496 tag
= re
.sub (r
'\s', '_', tag
).strip ()
497 tag
= re
.sub (r
':', '_', tag
).strip ()
501 def get_darcs_patches (darcs_repo
):
502 progress ('reading patches.')
504 xml_string
= read_pipe ('darcs changes --xml --reverse --repo ' + darcs_repo
)
506 dom
= xml
.dom
.minidom
.parseString(xml_string
)
507 xmls
= dom
.documentElement
.getElementsByTagName('patch')
509 patches
= [DarcsPatch (x
, darcs_repo
) for x
in xmls
]
518 ################################################################
522 def __init__ (self
, parent
, darcs_patch
):
524 self
.darcs_patch
= darcs_patch
526 self
.depth
= parent
.depth
+ 1
531 return self
.darcs_patch
.number
533 def parent_patch (self
):
535 return self
.parent
.darcs_patch
539 def common_ancestor (a
, b
):
541 if a
.depth
< b
.depth
:
543 elif a
.depth
> b
.depth
:
557 def export_checkpoint (gfi
):
558 gfi
.write ('checkpoint\n\n')
560 def export_tree (tree
, gfi
):
561 tree
= os
.path
.normpath (tree
)
562 gfi
.write ('deleteall\n')
563 for (root
, dirs
, files
) in os
.walk (tree
):
565 rf
= os
.path
.normpath (os
.path
.join (root
, f
))
566 s
= open (rf
).read ()
567 rf
= rf
.replace (tree
+ '/', '')
569 gfi
.write ('M 644 inline %s\n' % rf
)
570 gfi
.write ('data %d\n%s\n' % (len (s
), s
))
574 def export_commit (repo
, patch
, last_patch
, gfi
):
575 gfi
.write ('commit refs/heads/darcstmp%d\n' % patch
.number
)
576 gfi
.write ('mark :%d\n' % (patch
.number
+ 1))
577 gfi
.write ('committer %s <%s> %s\n' % (patch
.author_name
,
583 msg
+= '\n\n#%d\n' % patch
.number
585 gfi
.write ('data %d\n%s\n' % (len (msg
), msg
))
589 for (n
, p
) in pending_patches
.items ():
590 if repo
.has_patch (p
):
592 del pending_patches
[n
]
596 and git_commits
.has_key (last_patch
.number
)):
597 mergers
= [last_patch
.number
]
600 gfi
.write ('from :%d\n' % (mergers
[0] + 1))
601 for m
in mergers
[1:]:
602 gfi
.write ('merge :%d\n' % (m
+ 1))
604 pending_patches
[patch
.number
] = patch
605 export_tree (repo
.pristine_tree (), gfi
)
610 n
= last_patch
.number
611 git_commits
[patch
.number
] = GitCommit (git_commits
.get (n
, None),
614 def export_pending (gfi
):
615 if len (pending_patches
.items ()) == 1:
616 gfi
.write ('reset refs/heads/master\n')
617 gfi
.write ('from :%d\n\n' % (pending_patches
.values()[0].number
+1))
619 progress ("Creating branch master")
622 for (n
, p
) in pending_patches
.items ():
623 gfi
.write ('reset refs/heads/master%d\n' % n
)
624 gfi
.write ('from :%d\n\n' % (n
+1))
626 progress ("Creating branch master%d" % n
)
628 patches
= pending_patches
.values()
630 gfi
.write ('commit refs/heads/master\n')
631 gfi
.write ('committer %s <%s> %s\n' % (patch
.author_name
,
635 gfi
.write ('data %d\n%s\n' % (len(msg
), msg
))
636 gfi
.write ('from :%d\n' % (patch
.number
+ 1))
637 for p
in patches
[1:]:
638 gfi
.write ('merge :%d\n' % (p
.number
+ 1))
641 def export_tag (patch
, gfi
):
642 gfi
.write ('tag %s\n' % patch
.tag_name ())
643 gfi
.write ('from :%d\n' % (patch
.number
+ 1))
644 gfi
.write ('tagger %s <%s> %s\n' % (patch
.author_name
,
647 gfi
.write ('data %d\n%s\n' % (len (patch
.message
),
650 ################################################################
652 def test_conversion (darcs_repo
, git_repo
):
653 gd
= options
.basename
+ '.checkouttmp.git'
654 system ('rm -rf %(gd)s && git clone %(git_repo)s %(gd)s' % locals ())
655 diff
= read_pipe ('diff --exclude .git -urN %(gd)s %(darcs_repo)s/_darcs/pristine' % locals (), ignore_errors
=True)
656 system ('rm -rf %(gd)s' % locals ())
658 progress ("Conversion introduced changes: %s" % diff
)
661 progress ("Checkout matches pristine darcs tree.")
664 (options
, args
) = get_cli_options ()
666 darcs_repo
= os
.path
.abspath (args
[0])
667 git_repo
= os
.path
.abspath (options
.target_git_repo
)
669 if os
.path
.exists (git_repo
):
670 system ('rm -rf %(git_repo)s' % locals ())
672 system ('mkdir %(git_repo)s && cd %(git_repo)s && git --bare init' % locals ())
673 system ('git --git-dir %(git_repo)s repo-config core.logAllRefUpdates false' % locals ())
675 os
.environ
['GIT_DIR'] = git_repo
677 gfi
= os
.popen ('git-fast-import --quiet', 'w')
679 patches
= get_darcs_patches (darcs_repo
)
680 conv_repo
= DarcsConversionRepo (options
.basename
+ ".tmpdarcs", patches
)
681 conv_repo
.start_at (-1)
688 combinations
= [(v
, w
) for v
in pending_patches
.values ()
689 for w
in pending_patches
.values ()]
690 candidates
= [common_ancestor (git_commits
[c
[0].number
], git_commits
[c
[1].number
]) for c
in combinations
]
691 candidates
= sorted ([(-a
.darcs_patch
.number
, a
) for a
in candidates
])
692 for (depth
, c
) in candidates
:
695 conv_repo
.go_from_to (q
, p
)
698 parent_number
= q
.number
699 progress ('Found existing common parent as predecessor')
705 ## no branches found where we could attach.
706 ## try previous commits one by one.
708 parent_number
= p
.number
- 2
710 if parent_number
>= 0:
711 parent_patch
= patches
[parent_number
]
714 conv_repo
.go_from_to (parent_patch
, p
)
717 ## simplistic, may not be enough.
718 progress ('conflict, going one back')
721 if parent_number
< 0:
724 if (options
.history_window
725 and parent_number
< p
.number
- options
.history_window
):
730 if parent_number
>= 0 or p
.number
== 0:
731 progress ('Export %d -> %d (total %d)' % (parent_number
,
732 p
.number
, len (patches
)))
733 export_commit (conv_repo
, p
, parent_patch
, gfi
)
737 if options
.checkpoint_frequency
and p
.number
% options
.checkpoint_frequency
== 0:
738 export_checkpoint (gfi
)
740 progress ("Can't import patch %d, need conflict resolution patch?" % p
.number
)
745 for f
in glob
.glob ('%(git_repo)s/refs/heads/darcstmp*' % locals ()):
748 test_conversion (darcs_repo
, git_repo
)
750 if not options
.debug
: