Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Mercurial, can I apply changes from one file to another file in the same branch?

Tags:

mercurial

In the good old days of Subversion, I would sometimes derive a new file from an existing one using svn copy. Then if something changed in sections they had in common, I could still use svn merge to update the derived version.

To use the example from hginit.com, say the "guac" recipe already exists, and I want to create a "superguac" that includes instructions on how to serve guacamole to 1000 raving soccer fans. Using the process I just described, I could:

svn cp guac superguac
svn ci -m "Created superguac by copying guac"
(edit superguac)
svn ci -m "Added instructions for serving 1000 raving soccer fans to superguac"
(edit guac)
svn ci -m "Fixed a typo in guac"
svn merge -r3:4 guac superguac

and thus the typo fix would be applied to superguac.

Mercurial provides an hg copy command that marks a file as a copy of the original, but I'm not sure the repository structure supports a similar workflow. Here's the same example, and I carefully only edit a single file in the commit I want to use in the merge:

hg cp guac superguac
hg ci -m "Created superguac by copying guac"
(edit superguac)
hg ci -m "Added instructions for serving 1000 raving soccer fans to superguac"
(edit guac)
hg ci -m "Fixed a typo in guac"

I now want to apply the change in guac to superguac. Is that possible? If so, what's the right command? Is there a different workflow in Mercurial that achieves the same results (limited to a single branch)?

like image 500
Stephen Avatar asked Apr 23 '10 18:04

Stephen


People also ask

How do I merge two branches in mercurial?

First, we need to move back into the main branch, which is accomplished using the “hg update” command. Then, we run the “merge”command, specifying the name of the branch we'd like to merge from. Finally, we commit the changes. The changes are now present in the main branch.

How do I switch between branches in mercurial?

Quickly switch to another branch or bookmarkIn the Status bar, click the Mercurial Branch widget to open the Branches popup. Select the branch or bookmark to which you want to switch and in the menu that opens, click Update.

What is a branch in Mercurial?

Branches occur if lines of development diverge. The term "branch" may thus refer to a "diverged line of development". For Mercurial, a "line of development" is a linear sequence of consecutive changesets. Combining a line of development into an existing one is called merging. Creating a branch.

How do I revert a file in Mercurial?

To revert a file to a specific changeset, use hg revert -r CHANGESET FILENAME . This will revert the file without committing it.


1 Answers

There's no pure-mercurial way to go cross-file with your patches, but if patch is installed on your system you could achieve essentially the same thing by following up your series of mercurial commands with:

hg log -p -r tip -I quac | patch superquac

That's essentially saying: "take the diff (-p) that was applied to file quac (-I quac) in the most recent changeset (-r tip) send it to standard output (hg log), and use that as the input to the patch (| patch) command acting on file superquac (superquac).

like image 119
Ry4an Brase Avatar answered Nov 13 '22 20:11

Ry4an Brase