In Mercurial, is it possible to apply changes from one file to another file in the same branch?

In the good old days of Subversion, I sometimes outputted a new file from an existing one using svn copy . Then, if something changed in the partitions that they shared, I could use svn merge to update the derived version.

To use the hginit.com example, let's say that the โ€œguacโ€ recipe already exists, and I want to create a โ€œsuperguacโ€ that includes instructions on how to serve guacamole to thousands of crazy football fans. Using the process 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 correction will be applied to superguac.

Mercurial provides an hg copy command that marks the file as a copy of the original, but I'm not sure if the repository structure supports a similar workflow. Here is the same example, and I carefully edit only one file in the commit that 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" 

Now I want to apply the changes in guac to superguac. Is it possible? If so, what is the right team? Is there any other workflow in Mercurial that achieves the same results (limited to one branch)?

+7
mercurial
source share
2 answers

You can do it with

 hg cp guac superguac hg ci -m "Created superguac by copying guac" # CS1 (edit superguac) hg ci -m "Added instructions for serving 1000 raving soccer fans to superguac" # CS2 hg up -r revision-before-copy (edit guac) hg ci -m "Fixed a typo in guac" #CS3 hg merge # this will transfer the typo-fix both to guac and superguac hg ci -m "merged typo-fix from guac" # CS4 

After that, the repository looks like this:

 CS1 <--- CS2 <--------- CS4 \ / \--<-------- CS3 -<-/ 
+6
source share

There is no pure mercury way to cross-file with your patches, but if patch installed on your system, you could achieve almost the same thing by running a series of Mercury commands:

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

Basically saying: "take the diff ( -p ) that was applied to the quac file ( -I quac ) in the last change set ( -r tip ), send it to standard output ( hg log ) and use this as input to the patch ( | patch ) acting on the superquac ( superquac ) file.

+9
source share

All Articles