tamakisquare
tamakisquare

Reputation: 17077

How to diff two revisions of a file that got renamed in between and Mercurial doesn't know about the renaming?

I accidentally renamed a file outside of Mercurial. When I committed the change, Mercurial treated the change as two unrelated files (ie. a remove and a add). I need to go back to diff the two revisions but I don't know how to do so when Mercurial sees them as two respective files across different revisions. What can I do to diff the files?

Upvotes: 2

Views: 332

Answers (3)

davidmc24
davidmc24

Reputation: 2282

If you want to actually fix the history so that Mercurial is aware of the rename (and can use that information in future merges if needed), there's a way to do so documented on the Tips and Tricks page on the Mercurial wiki.

Current contents copied here for ease of use (and in case the link gets broken later):

Steps:

  1. Update your working directory to before you did the rename
  2. Do an actual "hg rename" which will create a new head
  3. Merge that new head into the revision where you did the "manual" rename (not the head revision!)
  4. Then finally merge the head revision into this merge result.

Advice:

  1. Make a clone first and work on that, just in case!
  2. After finishing the steps, use a file compare tool to check that the original and the clone are identical
  3. Check the file history of any moved file to make sure it is now restored

That being said, if all you want to do is compare the contents at the point in time, you can definitely accomplish that without making Mercurial aware of the rename (as mentioned in Stephen Rasku's answer). In fact, you can use a combination of "hg cat" and an external comparison tool to compare any files, not just ones that Mercurial knows about.

Upvotes: 3

Stephen Rasku
Stephen Rasku

Reputation: 2682

You didn't say what operating system you were using. The following will work with bash on Linux:

diff <(hg cat -r rev1 file1) <(hg cat -r rev2 file2)

You can replace diff with another program like vimdiff if you want a visual diff.

Upvotes: 2

Lazy Badger
Lazy Badger

Reputation: 97365

Fix history:

  • Update to first changeset with new-filename, save file outside WC
  • Update to parent of bad replacement changeset, replace file correctly (with rename tracking), commit, got second head
  • Rebase all changesets from old anonymous branch on top of fresh good changeset
  • --close-branch on bad-replacement changeset or delete this unwanted changeset or leave inactive head intact

Upvotes: 2

Related Questions