How to keep revision timestamps while converting a repository from Mercurial to Subversion?

Viewed 967

Because of work policies, I'm forced to use Subversion. Currently I've developed some code using Mercurial, and I have to convert this repository to a new Subversion repository. The current Mercurial repository also contains legacy code parts that I copied from another SVN repository (a HW driver), and this code contains keywords (like $Id: ...$) that were expanded while I copied the code.

My current approach to do the conversion is:

hg convert --dest-type svn repository.hg repository.svn

This works, unfortunately it does not preserve the timestamps of Mercurial revisions. Additionally, it expands tags in some legacy code parts where someone wrote $Id: ...$ stuff.

The questions:

  1. How can I export to Subversion, keeping the timestamps of revisions.
  2. How can I convert without expanding $Id:$ style keywords, or, even better, delete the offending lines while converting. Is this possible, or do I have to modify the history of the Mercurial dump before converting to make sure no keywords are present?

Sorry if this question has been answered already, I've searched intensely with Google and on here, but I cannot find an answer for this problem. Especially the timestamp problem must be something that others have already run into.

Cheers, H.

EDIT: I've since created a script that extracts timestamps from a Mercurial export file and updates a Subversion dumpfile, which is nothing more but a kludge, but it works. The $Id: problem is still unsolved, unfortunately.

4 Answers

I tried the keyword substitution once on SVN and I had to add a svn:keywords property to the working folder indicating which keyword(s) to be substituted. So, deleting this property should do the job.

This article on Stack Overflow is possibly related to this question.

As far as I understand your problem now: You have a Hg repository which was once converted from SVN, where keyword expansion had been used. Now the expanded keywords remain unchanged in the Hg repo. You now want to strip those keywords (or just their expanded values) from the Hg history, preserving author, date/time, commit message and of course changed files.

This is just an idea, I have never done this in practical, but ... Speaking just of mercurial, I think it's possible to do this by a combination of usual mercurial behaviour, just for one branch and without tags and other branches:

  1. Clone the intended source repository
  2. Prepare an empty Hg repository
  3. Perform hg log > logfile.txt over the source repo on that branch you want to change
  4. Parse the file getting a structured list of all necessary information like changeset number, user, date and summary, store this into a data structure that allows easy iteration
  5. Iterate through the list (or directly use the log file) and ...
    • delete all working files from the destination repo
    • export a changeset from the source repo to the working copy of the destination repo
    • change anything in the working files you want to do
    • hg addremove
    • hg commit -m ... -d ... -u ...
  6. Repeat step 5 as often as necessary

The drawback is that the changesets will get new hash values, so tags and bookmarks need to be updated.

If there are other branches that have to be migrated:

  1. Update the destination repo to that branch's starting point
  2. and the source repo, too
  3. insert steps 3 to 6 from the above list here

Perhaps there is some more work to be done, but I think this could work. As told before, I've never done this.

Related