How to mark file as resolved using local/remote version - mercurial

I pulled one revision and tried to merge but hg couldn't make the merge. I know the right version is the local (or the remote...), so I do this:
hg heads
hg revert file_path --rev right_rev
hg resolve -m file_path
...
Is there an easier way to do this?
Yes I know I should open the file, verify, manually resolve, bla bla bla

With newer versions of Mercurial (1.7.0 and later), you can use hg merge --tool internal:local to keep the local version (i.e. the one that's in your working directory), or hg merge --tool internal:other to keep the other version. The --tool option was introduced as a shorthand for --config ui.merge=internal:local, which was how you did it in older versions of Mercurial.
See the merge-tools online help or use hg help merge-tools at the command-line for more information.

If you know before you're going to merge you can use this quick trick:
to auto select local (or remote) like this:
hg --config ui.merge=internal:local merge

Related

Resolving conflicts: how to accept "their" changes automatically?

When merging conflicting changes using hg merge, Mercurial inserts a set of markers into the files to be merged in my working copy like this:
<<<<<<< local
version = 0.2
=======
version = 0.1
>>>>>>> other
Then I manually edit all files marked as U from a list produced by hg resolve --all -l and then I tell mercurial I have resolved them by hg resolve -m file1 file2 file3 ...
In many situations I would like however accept either my-only or their-only changes on some conflicting files. I am thinking to create two simple sed/awk/whatever scripts named accept-theirs.sh and accept-my.sh or is there any "proper" way to do it?
Use
hg resolve -t internal:other --all
to accept theirs and
hg resolve -t internal:local --all
to accept yours
Try this:
hg merge --tool internal:other
See also hg help merge-tools for more information.

Generating patches in Mercurial

I've looked for that in the manual, but I can't generate a patch for the last commit.
I tried
hg qnew patch_name
but it does only file with
# HG changeset patch
# Parent a6a8e225d16ff5970a8926ee8d24272a1c099f9c
I also tried
hg export tip
but it doesn't do anything. I committed the changes exactly.
How to generate a patch file with the last commit in?
The command to do this is export:
$ hg export -o FILE -r REV
It doesn't require redirection and will thus work correctly on any platform/shell.
Your hg export tip is the best way to do it, and the hg diff and hg log based answers are just lesser versions of the same. What exactly do you see/get when you type hg export tip? What does the output of hg log -p -r tip show?
The changeset tip is just means "the changeset that most recently arrived in my repository" which isn't as useful a concept as you might think, since hg pull and hg tag all create changesets too. If you really want the last thing you committed you'll need a more precise revspec.
Like so:
hg diff -r tip > tip.patch
You can use this command:
hg log -r tip -p > tip.patch
this will generate a patch for just that revision.
If you want to convert the latest commit to a patch file, use
hg qimport -r tip
This will replace the topmost regular commit with an applied MQ patch file.
To generate patches using "mq extensions" in mercurial, you can follow the below given steps. This will create a patch using mercurial:
1) Enabling mq extensions: Add the following lines to your hgrc file and save it.
[extensions]
mq =
2) Creating a patch using mq extensions: To create a patch using mq extensions you can do the following.
hg qnew -e -m "comment you want to enter" bug_name.patch
In the above command, -e flag is for editing the patch and -m flag is for adding a message to the patch.
3) Updating the patch: For updating the patch, you can use the following command when a patch is already applied.
hg qrefresh

Accidentally Working on the Wrong Named Branch in Mercurial

I have been making some changes to my working directory, and noticed that I have accidentally been working on the wrong branch. I have not committed anything yet, and I would like my next commit to go against another branch. What is the best way to do this?
The Shelve extension can give you grief, and this can be done entirely with Mercurial commands. Krtek almost had it but he used export instead of diff. Try this:
hg diff --git > ~/saved-work.patch
hg update --clean desiredbranch
hg import --no-commit ~/saved-work.patch
You should be able to just hg up otherbranch. It is important that you do not use the --clean option to hg up, either directly or via an alias as that will discard your uncommitted changes.
Another option is to use one of the extensions that provides hg shelve. The process would then be:
$ hg shelve --all
$ hg up otherbranch
$ hg unshelve
That will create a patch of your changes within the .hg directory, returning your working directory to a clean state, switch to the 'otherbranch', and then apply the saved patch.
I don't know if it is the best solution, but you can follow these steps :
1° hg diff --git > modifications.patch
2° hg update -C the_right_branch
3° hg patch modifications.patch
Maybe it's better to copy modifications.patch somewhere safe, just in case.
edit: update with diff instead of export. Thanks to the commenters.

hg diff local to remote file

This may be a silly question but when comparing a local to remote file, what is the path to the remote file?
Does hg want you to provide the head/revision you are referring to or something?
ie:
hg diff /local/file /remote?/file?
Mercurial doesn't do this. The only comparison with other repositories is hg incoming and hg outgoning which show which changesets differ between repositories. You can add the --patch option to either of those to see the patches that are the meat of those changesets, but you can't compare two versions of a file without having them in the same local clone.
From Hg man
hg diff [OPTION]... ([-c REV] | [-r REV1 [-r REV2]]) [FILE]...
I am not sure if you can speak about a "remote file" in a DVCS: you need to fetch or clone a remote repo in order to be able to make any hg diff.
hg fetch, for instance, is described here.

In Mercurial how do I extract a single file's changes from a Changeset to apply to another branch?

I have a large commit of many files on one branch, I need to transfer the modifications of a single file in that changeset to another branch. How can I do this? I am mostly using TortoiseHg but commandline solutions are also fine.
If I go to the changeset in TortoiseHg and select the file I can see the diffs I want to transfer, but not a way to actually apply them.
You can get the patch for just that file using:
hg log -r THEREVISIONWITHLOTSOFCHANGES -p -I path/to/justthatfile > justthatfile.patch
which you can then import on whatever branch you want by doing:
hg update anotherbranch
hg import --no-commit justthatfile.patch
hg commit
The most basic solution is to dump the patch of the file, apply it to the current working revision, and commit it (assuming you're at the root of the repository):
$ hg up <revision-to-apply-the-patch-to>
$ hg diff -c <revision-containing-the-patch> <files-to-include> | patch -p0
$ hg ci -m "Transplanting selected changes from <revision-contain...>"
The drawback of this method is that it isn't very obvious what you've done from a revision history perspective. A good commit message helps here, but the history graph gives no hint about the process of transplanting some changes. In that case merging and reverting may be a better solution:
$ hg up <revision-to-apply-the-patch-to>
$ hg merge -r <revision-containing-the-patch>
$ hg revert --no-backup <files-to-exclude>
$ hg ci -m "Merge in changes of <files-to-include>"
Probably there are more solutions to do this -- these two came to my mind first.