How does Mercurial merge internally?

2019-06-22 01:25发布

问题:

There is a nifty article on quora about how git does it's merging: http://www.quora.com/Git-revision-control/How-does-git-merge-work

And I was wondering if such a thing existed for Mercurial.

I'll admit that my motivation for this is that Mercurial seems really dumb about auto-resolving conflicts, but without concrete information about how mercurial does it's merging, I can't form an argument for Git. (Also maybe I'm crazy and the method is the same, regardless I can't find the information about how HG merges, anywhere.)

Also I hear HG is super configurable, so if there is a tool to make the merging less dumb, I would love to hear about it.

As an example of the silliness HG appeared to do, it gave a number of conflicts with other empty, such as:

<<<<<<<< local
[some new code...]
========
>>>>>>>> other

EDIT: So, I'm also looking for a merge tool that can intelligently auto-resolve conflicts. Like, it should be able to resolve the above example, which is painfully simple to resolve.

回答1:

Mercurial largely leaves merging to an external programs. See

https://www.mercurial-scm.org/wiki/MergeToolConfiguration

and

https://www.mercurial-scm.org/repo/hg/help/merge-tools

From the last page:

Mercurial uses these rules when deciding which merge tool to use:

  1. If a tool has been specified with the --tool option to merge or resolve, it is used. If it is the name of a tool in the merge-tools configuration, its configuration is used. Otherwise the specified tool must be executable by the shell.

  2. If the "HGMERGE" environment variable is present, its value is used and must be executable by the shell.

  3. If the filename of the file to be merged matches any of the patterns in the merge-patterns configuration section, the first usable merge tool corresponding to a matching pattern is used. Here, binary capabilities of the merge tool are not considered.

  4. If ui.merge is set it will be considered next. If the value is not the name of a configured tool, the specified value is used and must be executable by the shell. Otherwise the named tool is used if it is usable.

  5. If any usable merge tools are present in the merge-tools configuration section, the one with the highest priority is used.

  6. If a program named "hgmerge" can be found on the system, it is used - but it will by default not be used for symlinks and binary files.

  7. If the file to be merged is not binary and is not a symlink, then "internal:merge" is used.

8.The merge of the file fails and must be resolved before commit.

Note: After selecting a merge program, Mercurial will by default attempt to merge the files using a simple merge algorithm first. Only if it doesn't succeed because of conflicting changes Mercurial will actually execute the merge program. Whether to use the simple merge algorithm first can be controlled by the premerge setting of the merge tool. Premerge is enabled by default unless the file is binary or a symlink.