Mercurial Patch Creation and Usage

25,060

Solution 1

I would first make copies of everything so you have a way of backtracking.

Then, in the working copy with the changes, I would first delete the .hg directory, then copy in the .hg directory from the new repo. This basically transfers all of the changed files into the new repo without the need to delete any files and directories.

You will still need to tell the repo about whether to remove any files marked as missing. You will also have to handle renames manually. If this is a small number of operations, it's easier than trying to use the patch method.

Once this is done, commit your changes and push, if necessary.

Solution 2

You're correct — a patch is what you need to transfer the information from one repository to another (unrelated) repository. This will work since the files are the same, as you note.

So, to transfer your uncommitted changes from your old clone, you do

$ hg diff -g > uncommited.patch
$ cd ../new
$ hg patch --no-commit ../old/uncomitted.patch

That will restore the information saved in the patch. This includes information about files that are added or renamed in the old clone.

Solution 3

The following steps can be performed with a standard Mercurial install:

  1. Commit the changes in your local repository. Note the revision number.
  2. Use "hg export -r REV >patch.diff" to create a patch.
  3. Clone the new repository.
  4. Use "hg import patch.diff" to apply the patch to the new repository.

Example

C:\>hg init example
C:\>cd example
C:\example>echo >file1
C:\example>hg ci -Am file1
adding file1

C:\example>hg clone . ..\example2
updating to branch default
1 files updated, 0 files merged, 0 files removed, 0 files unresolved

C:\example>rd /s/q .hg
C:\example>hg init
C:\example>hg ci -Am same-but-different
adding file1

At this point example and example2 have identical contents, but the repositories are unrelated to each other due to deleting and reinitializing the .hg folder.

Now make some changes and commit them in one of the repositories, then export them as a patch:

C:\example>echo >>file1
C:\example>echo >file2
C:\example>hg ci -Am changes
adding file2

C:\example>hg export -r 1 >patch.diff

Below shows that the other repository can't pull the changes, because of the reinitialization. It can, however, apply the patch successfully:

C:\example>cd ..\example2
C:\example2>hg pull
pulling from c:\example
searching for changes
abort: repository is unrelated

C:\example2>hg import ..\example\patch.diff
applying ..\example\patch.diff

Solution 4

seems like what you want is patch queues. In that you have uncommitted changes, and you want to pull from the new repo before committing them....

$ hg qinit -c # initialize mq for your repo containing the uncommitted changes 
$ hg qnew name_of_patch # create patch that contains your uncommitted changes
$ hg qpop # resets your working dir back to the parent changeset

no worries though, your changes are safe and sound in .hg/patches/name_of_patch to see for yourself.....

$ cat .hg/patches/name_of_patch

now pull in the new repo

$ hg pull -u http://location.of.new/repo # pull in changes from new repo update working dir

$ hg qpush # apply your uncommitted changes to new repo

If you are lucky you will have no merge conflicts and you can go ahead and commit the patch by....

$ hg qfinish -a # change all applied patches to changeset

And then if you want....

$ hg push http://location.of.new/repo

If the repos are unrelated, just init a patch repo on your new repo. and manually copy the patch in and add it to .hg/patches/series file.

assuming patch was created. clone new repo

$ hg clone http://location.of.new/repo ./new_repo

init patch repo

$ cd ./new_repo && hg qinit -c

copy patch

$ cp ../old_repo/.hg/patches/name_of_patch .hg/patches/

edit series file using an editor of some sort

$ your_favorite_editor .hg/patches/series

name_of_patch   # <---put this in the series file

apply your patch to new repo

$ hg qpush

if no merge conflicts and you are convinced it works

$ hg qfinish -a
Share:
25,060
Abidi
Author by

Abidi

Updated on December 15, 2020

Comments

  • Abidi
    Abidi over 3 years

    I have come across a problem that I "think" can only be resolved using patches.

    I cloned a project from our main repository, made quite a few changes (updates, deletion of files & directory and additions) to it. These changes are not even committed. The problem is, project from the main repository has been deleted/removed and recreated as a new project (name is same, all the directory structures everything is same as before). I cloned that project again from the main repository and would like to transfer all my uncommitted changes to it.

    I am still exploring the hg patch to resolve that. It would be helpful if someone could confirm that creating and adding a patch IS the right approach to this, any resources explaining the process would be of great help.