Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I combine the history of two files in Mercurial?

Tags:

mercurial

I need to combine the functionality of two classes into one, but I am wondering can I combine their change history as well?

Both classes contains lots of previous changes, so I want to keep their change history in one place for reference. I can make a comment in the combined class, but in an IDE, it's not very easy to get the history of a deleted file.

like image 584
billc.cn Avatar asked May 23 '12 09:05

billc.cn


People also ask

How do you combine two Mercurial heads?

To start a merge between the two heads, we use the hg merge command. We resolve the contents of hello. c This updates the working directory so that it contains changes from both heads, which is reflected in both the output of hg parents and the contents of hello.

What is hg status?

hg status shows the status of a repository. Files are stored in a project's working directory (which users see), and the local repository (where committed snapshots are permanently recorded). hg add tells Mercurial to track files. hg commit creates a snapshot of the changes to 1 or more files in the local repository.


1 Answers

To do this, you need to record the file merging as a merge changeset.

Say changeset 0 is your current head with files A and B which you want to merge into file C. To do this, rename your file A to C and commit as changeset 1, then update back to changeset 0, rename file B to C and commit this as changeset 2. Next, you merge these two changesets, combining their content and removing the remaining files A and B. This causes the file history of C to be registered as descending from both A and B. Finally you can rename file C back to A.

The resulting tree looks as follows:

0 [A, B] --- 1 [A, C] \
          \            \
           \ 2 [C, B] --- 3 [C] --- 4 [A] 

Example command line flow:

$ hg init
$ echo a > a
$ echo b > b
$ hg add a b
$ hg commit -m "add files a and b"
$ hg mv a c
$ hg commit -m "rename a to c"
$ hg update 0
1 files updated, 0 files merged, 1 files removed, 0 files unresolved
$ hg mv b c
$ hg commit -m "rename b to c"
created new head
$ hg merge
merging c
warning: conflicts during merge.
merging c incomplete! (edit conflicts, then use 'hg resolve --mark')
0 files updated, 0 files merged, 1 files removed, 1 files unresolved
use 'hg resolve' to retry unresolved file merges or 'hg update -C .' to abandon
$ ls
c      c.orig
$ cat c
<<<<<<< local
b
=======
a
>>>>>>> other
$ echo ab > c
$ hg resolve --mark c
$ hg commit -m "merge a and b into c"
$ hg mv c a
$ hg commit -m "rename c back to a"

And if you now type hg log -f a you will notice that the history of both files is shown.

like image 61
Laurens Holst Avatar answered Sep 23 '22 23:09

Laurens Holst