billc.cn
billc.cn

Reputation: 7317

Can I combine the history of two files in 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.

Upvotes: 15

Views: 2002

Answers (1)

Laurens Holst
Laurens Holst

Reputation: 21026

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.

Upvotes: 23

Related Questions