Online Book Reader

Home Category

Mercurial_ The Definitive Guide - Bryan O'Sullivan [24]

By Root 954 0

$ cd scam-merge

$ hg pull -u ../scam-son

pulling from ../scam-son

searching for changes

adding changesets

adding manifests

adding file changes

added 1 changesets with 1 changes to 1 files (+1 heads)

not updating, since new heads added

(run 'hg heads' to see heads, 'hg merge' to merge)

In this example, I’ll set HGMERGE to tell Mercurial to use the non-interactive merge command. This is bundled with many Unix-like systems. (If you’re following this example on your computer, don’t bother setting HGMERGE. You’ll get dropped into a GUI file merge tool instead, which is much preferable.)

$ export HGMERGE=merge

$ hg merge

merging letter.txt

merge: warning: conflicts during merge

merging letter.txt failed!

0 files updated, 0 files merged, 0 files removed, 1 files unresolved

use 'hg resolve' to retry unresolved file merges

$ cat letter.txt

Greetings!

<<<<<<< /tmp/tour-merge-conflict_ULNXe/scam-merge/letter.txt

I am Shehu Musa Abacha, cousin to the former

=======

I am Alhaji Abba Abacha, son of the former

>>>>>>> /tmp/letter.txt~other.9ZxyYj

Nigerian dictator Sani Abacha.

Because merge can’t resolve the conflicting changes, it leaves merge markers inside the file that has conflicts, indicating which lines have conflicts, and whether they came from our version of the file or theirs.

Mercurial can tell from the way merge exits that it wasn’t able to merge successfully, so it tells us what commands we’ll need to run if we want to redo the merging operation. This could be useful if, for example, we were running a graphical merge tool and quit because we were confused or realized we had made a mistake.

If automatic or manual merges fail, there’s nothing to prevent us from “fixing up” the affected files ourselves, and committing the results of our merge:

$ cat > letter.txt <> Greetings!

> I am Bryan O'Sullivan, no relation of the former

> Nigerian dictator Sani Abacha.

> EOF

$ hg resolve -m letter.txt

$ hg commit -m 'Send me your money'

$ hg tip

changeset: 3:cef275730d6e

tag: tip

parent: 1:4a31891298cb

parent: 2:44d8436f9b83

user: Bryan O'Sullivan

date: Tue May 05 06:44:53 2009 +0000

summary: Send me your money

* * *

Where is the hg resolve command?


The hg resolve command was introduced in Mercurial 1.1, which was released in December 2008. If you are using an older version of Mercurial (run hg version to see), this command will not be present. If your version of Mercurial is older than 1.1, you should strongly consider upgrading to a newer version before trying to tackle complicated merges.

* * *

Simplifying the Pull-Merge-Commit Sequence

The process of merging changes as outlined above is straightforward, but requires running three commands in sequence:

hg pull -u

hg merge

hg commit -m 'Merged remote changes'

In the case of the final commit, you also need to enter a commit message, which is almost always going to be a piece of uninteresting “boilerplate” text.

It would be nice to reduce the number of steps needed, if this were possible. Indeed, Mercurial is distributed with an extension called fetch that does just this.

Mercurial provides a flexible extension mechanism that lets people extend its functionality, while keeping the core of Mercurial small and easy to deal with. Some extensions add new commands that you can use from the command line, while others work “behind the scenes,” for example adding capabilities to Mercurial’s built-in server mode.

The fetch extension adds a new command called, not surprisingly, hg fetch. This extension acts as a combination of hg pull -u, hg merge, and hg commit. It begins by pulling changes from another repository into the current repository. If it finds that the changes added a new head to the repository, it updates to the new head, begins a merge, then (if the merge succeeded) commits the result of the merge with an automatically generated commit message. If no new heads were added, it updates the working directory to the new tip changeset.

Enabling the fetch extension is easy. Edit the

Return Main Page Previous Page Next Page

®Online Book Reader