contrib/darcs2hg.py
changeset 5341 bd706eb8bc25
parent 5340 ad8783fe20f7
child 5355 6b6104430964
equal deleted inserted replaced
5340:ad8783fe20f7 5341:bd706eb8bc25
   111 	doc = xml_dom.parseString(change)
   111 	doc = xml_dom.parseString(change)
   112 	for patch_node in doc.childNodes[0].childNodes:
   112 	for patch_node in doc.childNodes[0].childNodes:
   113 		summary_nodes = filter(lambda n: n.nodeName == "summary" and n.nodeType == n.ELEMENT_NODE, patch_node.childNodes)
   113 		summary_nodes = filter(lambda n: n.nodeName == "summary" and n.nodeType == n.ELEMENT_NODE, patch_node.childNodes)
   114 		for summary_node in summary_nodes:
   114 		for summary_node in summary_nodes:
   115 			change_nodes = filter(lambda n: n.nodeType == n.ELEMENT_NODE, summary_node.childNodes)
   115 			change_nodes = filter(lambda n: n.nodeType == n.ELEMENT_NODE, summary_node.childNodes)
       
   116 			if len(change_nodes) == 0:
       
   117 				name = filter(lambda n: n.nodeName == "name", patch_node.childNodes)
       
   118 				if not name:
       
   119 					error("Darcs patch has an empty summary node and no name: " + patch_node.toxml())
       
   120 				name = name[0].childNodes[0].data.strip()
       
   121 				(tag, sub_count) = re.subn('^TAG ', '', name, 1)
       
   122 				if sub_count != 1:
       
   123 					error("Darcs patch has an empty summary node but doesn't look like a tag: " + patch_node.toxml());
   116 			for change_node in change_nodes:
   124 			for change_node in change_nodes:
   117 				change = change_node.nodeName
   125 				change = change_node.nodeName
   118 				if change == 'modify_file':
   126 				if change == 'modify_file':
   119 					yield change, change_node.childNodes[0].data.strip()
   127 					yield change, change_node.childNodes[0].data.strip()
   120 				elif change == 'add_file':
   128 				elif change == 'add_file':
   160 	return int(tip)
   168 	return int(tip)
   161 
   169 
   162 def hg_rename( hg_repo, from_file, to_file ):
   170 def hg_rename( hg_repo, from_file, to_file ):
   163 	cmd("hg rename --after \"%s\" \"%s\"" % (from_file, to_file), hg_repo);
   171 	cmd("hg rename --after \"%s\" \"%s\"" % (from_file, to_file), hg_repo);
   164 	
   172 	
   165 def hg_handle_change( hg_repo, change, arg ):
   173 def hg_tag ( hg_repo, text, author, date ):
       
   174 	old_tip = hg_tip(hg_repo)
       
   175 	res = cmd("hg tag -u \"%s\" -d \"%s 0\" \"%s\""	 % (author, date, text), hg_repo)
       
   176 	new_tip = hg_tip(hg_repo)
       
   177 	if not new_tip == old_tip + 1:
       
   178 		error("Mercurial tag did not work as expected: " + res)
       
   179 
       
   180 def hg_handle_change( hg_repo, author, date, change, arg ):
   166 	"""Processes a change event as output by darcs_changes_summary. These
   181 	"""Processes a change event as output by darcs_changes_summary. These
   167 	consist of file move/rename/add/delete commands."""
   182 	consist of file move/rename/add/delete commands."""
   168 	if change == 'modify_file':
   183 	if change == 'modify_file':
   169 		pass
   184 		pass
   170 	elif change == 'add_file':
   185 	elif change == 'add_file':
   175 		pass
   190 		pass
   176 	elif change == 'remove_directory':
   191 	elif change == 'remove_directory':
   177 		pass
   192 		pass
   178 	elif change == 'move':
   193 	elif change == 'move':
   179 		hg_rename(hg_repo, arg[0], arg[1])
   194 		hg_rename(hg_repo, arg[0], arg[1])
       
   195 	elif change == 'tag':
       
   196 		hg_tag(hg_repo, arg, author, date)
   180 	else:
   197 	else:
   181 		error('Unknown change type ' + change + ': ' + arg)
   198 		error('Unknown change type ' + change + ': ' + arg)
   182 
   199 
   183 # ------------------------------------------------------------------------------
   200 # ------------------------------------------------------------------------------
   184 #
   201 #
   224 			# --------------------------------YYYYMMDDHHMMSS
   241 			# --------------------------------YYYYMMDDHHMMSS
   225 			date = chash.split("-")[0]
   242 			date = chash.split("-")[0]
   226 			epoch = int(mktime(strptime(date, '%Y%m%d%H%M%S')))
   243 			epoch = int(mktime(strptime(date, '%Y%m%d%H%M%S')))
   227 			darcs_pull(hg_repo, darcs_repo, chash)
   244 			darcs_pull(hg_repo, darcs_repo, chash)
   228 			for change, arg in darcs_changes_summary(darcs_repo, chash):
   245 			for change, arg in darcs_changes_summary(darcs_repo, chash):
   229 				hg_handle_change(hg_repo, change, arg)
   246 				hg_handle_change(hg_repo, author, epoch, change, arg)
   230 			hg_commit(hg_repo, text, author, epoch)
   247 			hg_commit(hg_repo, text, author, epoch)
   231 		change_number += 1
   248 		change_number += 1
   232 	print "Darcs repository (_darcs) was not deleted. You can keep or remove it."
   249 	print "Darcs repository (_darcs) was not deleted. You can keep or remove it."
   233 
   250 
   234 # EOF
   251 # EOF