contrib/darcs2hg.py
author Alexis S. L. Carvalho <alexis@cecm.usp.br>
Sun, 22 Oct 2006 01:02:00 -0300
changeset 3484 a8823e6824fc
parent 2749 d13e4ffaa79d
child 3673 eb0b4a2d70a9
permissions -rwxr-xr-x
bash_completion: qfold, qrename, qheader, qclone and qselect
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2349
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
     1
#!/usr/bin/env python
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
     2
# Encoding: iso-8859-1
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
     3
# vim: tw=80 ts=4 sw=4 noet
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
     4
# -----------------------------------------------------------------------------
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
     5
# Project   : Basic Darcs to Mercurial conversion script
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
     6
# -----------------------------------------------------------------------------
2586
bb63d29ce03d darcs2hg: improved logging
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
     7
# Authors   : Sebastien Pierre                           <sebastien@xprima.com>
bb63d29ce03d darcs2hg: improved logging
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
     8
#             TK Soh                                      <teekaysoh@gmail.com>
bb63d29ce03d darcs2hg: improved logging
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
     9
# -----------------------------------------------------------------------------
2349
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    10
# Creation  : 24-May-2006
2587
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
    11
# Last mod  : 05-Jun-2006
2349
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    12
# -----------------------------------------------------------------------------
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    13
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    14
import os, sys
2352
61909dfb316d various fixes to darcs conversion script
TK Soh <teekaysoh@yahoo.com>
parents: 2349
diff changeset
    15
import tempfile
2349
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    16
import xml.dom.minidom as xml_dom
2352
61909dfb316d various fixes to darcs conversion script
TK Soh <teekaysoh@yahoo.com>
parents: 2349
diff changeset
    17
from time import strptime, mktime
2349
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    18
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    19
DARCS_REPO = None
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    20
HG_REPO    = None
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    21
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    22
USAGE = """\
2587
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
    23
%s DARCSREPO HGREPO [SKIP]
2349
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    24
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    25
    Converts the given Darcs repository to a new Mercurial repository. The given
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    26
    HGREPO must not exist, as it will be created and filled up (this will avoid
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    27
    overwriting valuable data.
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    28
2587
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
    29
    In case an error occurs within the process, you can resume the process by
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
    30
    giving the last successfuly applied change number.
2352
61909dfb316d various fixes to darcs conversion script
TK Soh <teekaysoh@yahoo.com>
parents: 2349
diff changeset
    31
""" % (os.path.basename(sys.argv[0]))
2349
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    32
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    33
# ------------------------------------------------------------------------------
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    34
#
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    35
# Utilities
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    36
#
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    37
# ------------------------------------------------------------------------------
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    38
2586
bb63d29ce03d darcs2hg: improved logging
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
    39
def cmd(text, path=None, silent=False):
2349
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    40
	"""Executes a command, in the given directory (if any), and returns the
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    41
	command result as a string."""
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    42
	cwd = None
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    43
	if path:
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    44
		path = os.path.abspath(path)
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    45
		cwd  = os.getcwd()
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    46
		os.chdir(path)
2586
bb63d29ce03d darcs2hg: improved logging
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
    47
	if not silent: print "> ", text
2349
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    48
	res = os.popen(text).read()
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    49
	if path:
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    50
		os.chdir(cwd)
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    51
	return res
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    52
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    53
def writefile(path, data):
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    54
	"""Writes the given data into the given file."""
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    55
	f = file(path, "w") ; f.write(data)  ; f.close()
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    56
2586
bb63d29ce03d darcs2hg: improved logging
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
    57
def error( *args ):
2587
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
    58
	sys.stderr.write("ERROR: ")
2586
bb63d29ce03d darcs2hg: improved logging
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
    59
	for a in args: sys.stderr.write(str(a))
bb63d29ce03d darcs2hg: improved logging
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
    60
	sys.stderr.write("\n")
2587
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
    61
	sys.stderr.write("You can make manual fixes if necessary and then resume by"
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
    62
	" giving the last changeset number")
2586
bb63d29ce03d darcs2hg: improved logging
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
    63
	sys.exit(-1)
bb63d29ce03d darcs2hg: improved logging
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
    64
2349
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    65
# ------------------------------------------------------------------------------
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    66
#
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    67
# Darcs interface
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    68
#
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    69
# ------------------------------------------------------------------------------
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    70
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    71
def darcs_changes(darcsRepo):
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    72
	"""Gets the changes list from the given darcs repository. This returns the
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    73
	chronological list of changes as (change name, change summary)."""
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    74
	changes    = cmd("darcs changes --reverse --xml-output", darcsRepo)
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    75
	doc        = xml_dom.parseString(changes)
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    76
	for patch_node in doc.childNodes[0].childNodes:
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    77
		name = filter(lambda n:n.nodeName == "name", patch_node.childNodes)
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    78
		comm = filter(lambda n:n.nodeName == "comment", patch_node.childNodes)
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    79
		if not name:continue
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    80
		else: name = name[0].childNodes[0].data
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    81
		if not comm: comm = ""
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    82
		else: comm = comm[0].childNodes[0].data
2352
61909dfb316d various fixes to darcs conversion script
TK Soh <teekaysoh@yahoo.com>
parents: 2349
diff changeset
    83
		author = patch_node.getAttribute("author")
2587
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
    84
		date   = patch_node.getAttribute("date")
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
    85
		chash  = os.path.splitext(patch_node.getAttribute("hash"))[0]
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
    86
		yield author, date, name, chash, comm
2349
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
    87
2587
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
    88
def darcs_tip(darcs_repo):
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
    89
	changes = cmd("darcs changes",darcs_repo,silent=True)
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
    90
	changes = filter(lambda l:l.strip().startswith("* "), changes.split("\n"))
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
    91
	return len(changes)
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
    92
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
    93
def darcs_pull(hg_repo, darcs_repo, chash):
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
    94
	old_tip = darcs_tip(darcs_repo)
2749
d13e4ffaa79d [darcs2hg] Windows compatibilty patct
Sébastien Pierre <sebastien@xprima.com>
parents: 2588
diff changeset
    95
	res     = cmd("darcs pull \"%s\" --all --match=\"hash %s\"" % (darcs_repo, chash), hg_repo)
2587
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
    96
	print res
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
    97
	new_tip = darcs_tip(darcs_repo)
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
    98
	if not new_tip != old_tip + 1:
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
    99
		error("Darcs pull did not work as expected: " + res)
2349
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
   100
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
   101
# ------------------------------------------------------------------------------
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
   102
#
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
   103
# Mercurial interface
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
   104
#
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
   105
# ------------------------------------------------------------------------------
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
   106
2352
61909dfb316d various fixes to darcs conversion script
TK Soh <teekaysoh@yahoo.com>
parents: 2349
diff changeset
   107
def hg_commit( hg_repo, text, author, date ):
61909dfb316d various fixes to darcs conversion script
TK Soh <teekaysoh@yahoo.com>
parents: 2349
diff changeset
   108
	fd, tmpfile = tempfile.mkstemp(prefix="darcs2hg_")
61909dfb316d various fixes to darcs conversion script
TK Soh <teekaysoh@yahoo.com>
parents: 2349
diff changeset
   109
	writefile(tmpfile, text)
2587
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
   110
	old_tip = hg_tip(hg_repo)
2352
61909dfb316d various fixes to darcs conversion script
TK Soh <teekaysoh@yahoo.com>
parents: 2349
diff changeset
   111
	cmd("hg add -X _darcs", hg_repo)
61909dfb316d various fixes to darcs conversion script
TK Soh <teekaysoh@yahoo.com>
parents: 2349
diff changeset
   112
	cmd("hg remove -X _darcs --after", hg_repo)
2749
d13e4ffaa79d [darcs2hg] Windows compatibilty patct
Sébastien Pierre <sebastien@xprima.com>
parents: 2588
diff changeset
   113
	res = cmd("hg commit -l %s -u \"%s\" -d \"%s 0\""  % (tmpfile, author, date), hg_repo)
d13e4ffaa79d [darcs2hg] Windows compatibilty patct
Sébastien Pierre <sebastien@xprima.com>
parents: 2588
diff changeset
   114
	os.close(fd)
2352
61909dfb316d various fixes to darcs conversion script
TK Soh <teekaysoh@yahoo.com>
parents: 2349
diff changeset
   115
	os.unlink(tmpfile)
2587
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
   116
	new_tip = hg_tip(hg_repo)
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
   117
	if not new_tip == old_tip + 1:
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
   118
		# Sometimes we may have empty commits, we simply skip them
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
   119
		if res.strip().lower().find("nothing changed") != -1:
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
   120
			pass
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
   121
		else:
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
   122
			error("Mercurial commit did not work as expected: " + res)
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
   123
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
   124
def hg_tip( hg_repo ):
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
   125
	"""Returns the latest local revision number in the given repository."""
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
   126
	tip = cmd("hg tip", hg_repo, silent=True)
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
   127
	tip = tip.split("\n")[0].split(":")[1].strip()
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
   128
	return int(tip)
2349
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
   129
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
   130
# ------------------------------------------------------------------------------
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
   131
#
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
   132
# Main
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
   133
#
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
   134
# ------------------------------------------------------------------------------
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
   135
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
   136
if __name__ == "__main__":
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
   137
	args = sys.argv[1:]
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
   138
	# We parse the arguments
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
   139
	if len(args)   == 2:
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
   140
		darcs_repo = os.path.abspath(args[0])
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
   141
		hg_repo    = os.path.abspath(args[1])
2587
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
   142
		skip       = None
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
   143
	elif len(args) == 3:
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
   144
		darcs_repo = os.path.abspath(args[0])
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
   145
		hg_repo    = os.path.abspath(args[1])
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
   146
		skip       = int(args[2])
2349
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
   147
	else:
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
   148
		print USAGE
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
   149
		sys.exit(-1)
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
   150
	# Initializes the target repo
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
   151
	if not os.path.isdir(darcs_repo + "/_darcs"):
2586
bb63d29ce03d darcs2hg: improved logging
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
   152
		print "No darcs directory found at: " + darcs_repo
2349
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
   153
		sys.exit(-1)
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
   154
	if not os.path.isdir(hg_repo):
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
   155
		os.mkdir(hg_repo)
2587
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
   156
	elif skip == None:
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
   157
		print "Given HG repository must not exist when no SKIP is specified."
2349
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
   158
		sys.exit(-1)
2587
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
   159
	if skip == None:
2749
d13e4ffaa79d [darcs2hg] Windows compatibilty patct
Sébastien Pierre <sebastien@xprima.com>
parents: 2588
diff changeset
   160
		cmd("hg init \"%s\"" % (hg_repo))
2587
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
   161
		cmd("darcs initialize", hg_repo)
2349
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
   162
	# Get the changes from the Darcs repository
2587
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
   163
	change_number = 0
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
   164
	for author, date, summary, chash, description in darcs_changes(darcs_repo):
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
   165
		print "== changeset", change_number,
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
   166
		if skip != None and change_number <= skip:
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
   167
			print "(skipping)"
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
   168
		else:
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
   169
			text = summary + "\n" + description
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
   170
			darcs_pull(hg_repo, darcs_repo, chash)
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
   171
			# The commit hash has a date like 20021020201112
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
   172
			# --------------------------------YYYYMMDDHHMMSS
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
   173
			date = chash.split("-")[0]
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
   174
			epoch = int(mktime(strptime(date, '%Y%m%d%H%M%S')))
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
   175
			hg_commit(hg_repo, text, author, epoch)
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
   176
		change_number += 1
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
   177
	print "Darcs repository (_darcs) was not deleted. You can keep or remove it."
2349
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
   178
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
   179
# EOF