tags:

views:

55

answers:

2

I had 2 Python similar scripts, that I've since merged into one (and now takes some parameters to differ the behaviour appropriately). Both of the previous files are in the tip of my Mercurial repository. How can I indicate that the new file, is a combination of the 2 older files that I intend to remove?

Also note, that 1 file has been chosen in favor of the other, and some code moved across, so if it's not possible to create a version controlled file with a new name, then assimilating one file's history into the other will suffice.

+1  A: 

What follows is an example of a merge between two unrelated files "a" and "b".

Please note that I'm abusing a list format because I can't figure out how to use this system. Lines that appear to be headers but start as "summary:" are actually output from hg and happen to be the last line before a blank line. All other headers are my own comments explaining a command/sequence and its output. Commands appear on line that have 'timeless-mbp:...$'. All other lines are output.

Setup

timeless-mbp:~ timeless$ cd /tmp
timeless-mbp:tmp timeless$ rm -rf q
timeless-mbp:tmp timeless$ hg init q
timeless-mbp:tmp timeless$ cd q

Define my username

timeless-mbp:q timeless$ echo '[ui]' > .hg/hgrc
timeless-mbp:q timeless$ echo 'username = timeless' >> .hg/hgrc

First create, add and commit file a

timeless-mbp:q timeless$ echo a > a
timeless-mbp:q timeless$ hg commit -Am a
adding a

Start at a new scratch revision (null)

timeless-mbp:q timeless$ hg up -r null
0 files updated, 0 files merged, 1 files removed, 0 files unresolved

Create, add and commit file b

timeless-mbp:q timeless$ echo b > b
timeless-mbp:q timeless$ hg commit -Am b
adding b
created new head

Switch back to our first file, this is just to create an interleaved version history

timeless-mbp:q timeless$ hg up -r 0
1 files updated, 0 files merged, 1 files removed, 0 files unresolved

Rename a to file because we want to merge it with b under the name "file"

timeless-mbp:q timeless$ hg mv a file
timeless-mbp:q timeless$ hg commit -m 'rename a to file'

Switch back to our second file, this is just to create an interleaved version history

timeless-mbp:q timeless$ hg up -r 1
1 files updated, 0 files merged, 1 files removed, 0 files unresolved

Rename b to file because we want to merge it with a under the name "file"

timeless-mbp:q timeless$ hg mv b file
timeless-mbp:q timeless$ hg commit -m 'rename b to file'

I'm forcing a merge using internal because I don't want a merge tool to pop up, there are other merge tools you could use (e.g. on OS X with modern versions of Mercurial, it tends to pop up FileMerge.app, but that's hard to show in a log).

timeless-mbp:q timeless$ hg --config ui.merge=internal:local merge 2
0 files updated, 1 files merged, 0 files removed, 0 files unresolved
(branch merge, don't forget to commit)

Replace the content with what I want

timeless-mbp:q timeless$ hg cat -r 2 file > file
timeless-mbp:q timeless$ hg cat -r 3 file >> file

Verify that it now has the content I want

timeless-mbp:q timeless$ cat file
a
b

Commit the "merged" file

timeless-mbp:q timeless$ hg commit -m merging

Review the repository history

timeless-mbp:q timeless$ hg log
changeset:   4:d9d3d23ec1cc
tag:         tip
parent:      3:01c6a61cbe04
parent:      2:3de70df713a7
user:        timeless
date:        Wed Jun 02 16:10:08 2010 +0300
summary:     merging

changeset:   3:01c6a61cbe04
parent:      1:e6b8058965b4
user:        timeless
date:        Wed Jun 02 16:09:43 2010 +0300
summary:     rename b to file

changeset:   2:3de70df713a7
parent:      0:635f50240100
user:        timeless
date:        Wed Jun 02 16:09:30 2010 +0300
summary:     rename a to file

changeset:   1:e6b8058965b4
parent:      -1:000000000000
user:        timeless
date:        Wed Jun 02 16:09:17 2010 +0300
summary:     b

changeset:   0:635f50240100
user:        timeless
date:        Wed Jun 02 16:09:01 2010 +0300
summary:     a

Check annotate output

timeless-mbp:q timeless$ hg ann -f -c file
635f50240100 a: a
e6b8058965b4 b: b
timeless
A very arduous solution, I'll see if any others are better first.
Matt Joiner
+1  A: 

It's not a perfect solution, and it might be too late to do it this time, but I think hg rename would get you where you want to go. Here's a sequence:

ry4an@hail [~/hg] % mkdir test
ry4an@hail [~/hg] % cd test/
ry4an@hail [~/hg/test] % hg init
ry4an@hail [~/hg/test] % echo stuff > file1
ry4an@hail [~/hg/test] % echo different > file2
ry4an@hail [~/hg/test] % hg commit --addremove -m 'adding both separately'
adding file1
adding file2
ry4an@hail [~/hg/test] % hg rename --force file1 file2
ry4an@hail [~/hg/test] % hg commit -m 'overwrote 2 with 1'
ry4an@hail [~/hg/test] % hg grep --follow different file2
file2:0:different
ry4an@hail [~/hg/test] % hg grep --follow stuff file2 
file2:1:stuff

Notice that the file 'grep' find that 'stuff' was in file2 revision 1 and 'different' was in file two revision 0, so both histories are now in file2.

Ry4an
What command shows that the new file is derived from both earlier files?
Matt Joiner
I don't know that one can easily do so from the command line. Possibly some invocation of 'hg log --follow' could do it. The --follow argument says "follow renames back in time" which is why those hggreps found stuff from both file1 and file2.
Ry4an
Well, while I couldn't work `hg` to actually show the merging or renaming in its log, this is the easy solution I was after.
Matt Joiner