contrib/darcs2hg.py
changeset 5339 b62a59fa9d26
parent 3673 eb0b4a2d70a9
child 5340 ad8783fe20f7
equal deleted inserted replaced
5276:aea35488ea66 5339:b62a59fa9d26
    96 	print res
    96 	print res
    97 	new_tip = darcs_tip(darcs_repo)
    97 	new_tip = darcs_tip(darcs_repo)
    98 	if not new_tip != old_tip + 1:
    98 	if not new_tip != old_tip + 1:
    99 		error("Darcs pull did not work as expected: " + res)
    99 		error("Darcs pull did not work as expected: " + res)
   100 
   100 
       
   101 def darcs_changes_summary(darcs_repo, chash):
       
   102 	"""Gets the changes from the darcs summary. This returns the chronological
       
   103 	list of changes as (change_type, args). Eg. ('add_file', 'foo.txt') or
       
   104 	('move', ['foo.txt','bar.txt'])."""
       
   105 	change = cmd("darcs changes --summary --xml-output --match=\"hash %s\"" % (chash), darcs_repo)
       
   106 	doc = xml_dom.parseString(change)
       
   107 	for patch_node in doc.childNodes[0].childNodes:
       
   108 		summary_nodes = filter(lambda n: n.nodeName == "summary" and n.nodeType == n.ELEMENT_NODE, patch_node.childNodes)
       
   109 		for summary_node in summary_nodes:
       
   110 			change_nodes = filter(lambda n: n.nodeType == n.ELEMENT_NODE, summary_node.childNodes)
       
   111 			for change_node in change_nodes:
       
   112 				change = change_node.nodeName
       
   113 				if change == 'modify_file':
       
   114 					yield change, change_node.childNodes[0].data.strip()
       
   115 				elif change == 'add_file':
       
   116 					yield change, change_node.childNodes[0].data.strip()
       
   117 				elif change == 'remove_file':
       
   118 					yield change, change_node.childNodes[0].data.strip()
       
   119 				elif change == 'add_directory':
       
   120 					yield change, change_node.childNodes[0].data.strip()
       
   121 				elif change == 'remove_directory':
       
   122 					yield change, change_node.childNodes[0].data.strip()
       
   123 				elif change == 'move':
       
   124 					yield change, (change_node.getAttribute('from'), change_node.getAttribute('to'))
       
   125 				else:
       
   126 					error('Problem parsing summary xml: Unexpected element: ' + change_node.toxml())
       
   127 
   101 # ------------------------------------------------------------------------------
   128 # ------------------------------------------------------------------------------
   102 #
   129 #
   103 # Mercurial interface
   130 # Mercurial interface
   104 #
   131 #
   105 # ------------------------------------------------------------------------------
   132 # ------------------------------------------------------------------------------
   124 def hg_tip( hg_repo ):
   151 def hg_tip( hg_repo ):
   125 	"""Returns the latest local revision number in the given repository."""
   152 	"""Returns the latest local revision number in the given repository."""
   126 	tip = cmd("hg tip", hg_repo, silent=True)
   153 	tip = cmd("hg tip", hg_repo, silent=True)
   127 	tip = tip.split("\n")[0].split(":")[1].strip()
   154 	tip = tip.split("\n")[0].split(":")[1].strip()
   128 	return int(tip)
   155 	return int(tip)
       
   156 
       
   157 def hg_rename( hg_repo, from_file, to_file ):
       
   158 	cmd("hg rename --after \"%s\" \"%s\"" % (from_file, to_file), hg_repo);
       
   159 	
       
   160 def hg_handle_change( hg_repo, change, arg ):
       
   161 	"""Processes a change event as output by darcs_changes_summary. These
       
   162 	consist of file move/rename/add/delete commands."""
       
   163 	if change == 'modify_file':
       
   164 		pass
       
   165 	elif change == 'add_file':
       
   166 		pass
       
   167 	elif change =='remove_file':
       
   168 		pass
       
   169 	elif change == 'add_directory':
       
   170 		pass
       
   171 	elif change == 'remove_directory':
       
   172 		pass
       
   173 	elif change == 'move':
       
   174 		hg_rename(hg_repo, arg[0], arg[1])
       
   175 	else:
       
   176 		error('Unknown change type ' + change + ': ' + arg)
   129 
   177 
   130 # ------------------------------------------------------------------------------
   178 # ------------------------------------------------------------------------------
   131 #
   179 #
   132 # Main
   180 # Main
   133 #
   181 #
   165 		print "== changeset", change_number,
   213 		print "== changeset", change_number,
   166 		if skip != None and change_number <= skip:
   214 		if skip != None and change_number <= skip:
   167 			print "(skipping)"
   215 			print "(skipping)"
   168 		else:
   216 		else:
   169 			text = summary + "\n" + description
   217 			text = summary + "\n" + description
   170 			darcs_pull(hg_repo, darcs_repo, chash)
       
   171 			# The commit hash has a date like 20021020201112
   218 			# The commit hash has a date like 20021020201112
   172 			# --------------------------------YYYYMMDDHHMMSS
   219 			# --------------------------------YYYYMMDDHHMMSS
   173 			date = chash.split("-")[0]
   220 			date = chash.split("-")[0]
   174 			epoch = int(mktime(strptime(date, '%Y%m%d%H%M%S')))
   221 			epoch = int(mktime(strptime(date, '%Y%m%d%H%M%S')))
       
   222 			darcs_pull(hg_repo, darcs_repo, chash)
       
   223 			for change, arg in darcs_changes_summary(darcs_repo, chash):
       
   224 				hg_handle_change(hg_repo, change, arg)
   175 			hg_commit(hg_repo, text, author, epoch)
   225 			hg_commit(hg_repo, text, author, epoch)
   176 		change_number += 1
   226 		change_number += 1
   177 	print "Darcs repository (_darcs) was not deleted. You can keep or remove it."
   227 	print "Darcs repository (_darcs) was not deleted. You can keep or remove it."
   178 
   228 
   179 # EOF
   229 # EOF