Is there any way to use hg copy from a specific revision?

52 Views Asked by At

Is there any way to use hg copy from a specific revision?

I want to copy an earlier version of a file and work on it while preserving the history of the copied file at the point it was copied.

In other words if I have some program foo.py at rev 23 but I want to fork it into bar.py from rev 6 of foo.py, is there a way to specify:

hg copy foo.py@6 bar.py
1

There are 1 best solutions below

0
StayOnTarget On

I can't think of a single command that would do this because you have to add a commit as a child of the older revision. But it can certainly be done in a series of commands.

Example:

hg up 6
hg copy foo.py bar.py
hg commit -m "Copied foo to bar because reasons"
hg up tip
hg merge

This will create file bar.py which would be the contents of foo.py at revision 6.

(For private/unpublished changes you could also use rebase and/or histedit but that seems out of scope for this question).

bar.py may receive merges intended for foo.py which can be undesirable, especially if a lot of changes have occurred since rev 6. You may just have to discard conflicts.


If it is acceptable to have the copied file's lifetime start from the current revision its simpler:

Example:

hg cat --rev=6 --output bar.py foo.py
hg copy -A foo.py bar.py
hg commit -m "Create bar.py from earlier revision of foo.py"

In many cases this would be OK - for instance if there were no content changes to foo.py in all that time, but maybe only its location or filename changed.


FYI the reason hg copy by itself can't be used for this is its basic definition:

mark files as copied for the next commit

(according to hg cp --help) so that is not what you should use; its just not designed for what you had in mind.