Differences between revisions 16 and 17
Revision 16 as of 2010-05-25 13:06:54
Size: 2764
Comment: Added emacs 'emerge' command
Revision 17 as of 2010-07-31 05:14:58
Size: 2751
Editor: MichaelErnst
Comment: Be more explicit about Mercurial 1.0 instructions; also some minor cleanups
Deletions are marked like this. Additions are marked like this.
Line 3: Line 3:
[[http://www.gnu.org/software/emacs/|Emacs]] is bundled with an elisp program called Ediff which purpose is to help developers to visually apply patches. One of the various ediff commands is well suited to three way merging and can be used as a [[MergeProgram|merger program]] with [[Mercurial]]. [[http://www.gnu.org/software/emacs/|Emacs]] is bundled with an elisp program called Ediff whose purpose is to help developers to visually apply patches. One of the ediff commands is well-suited to three-way merging and can be used as a [[MergeProgram|merger program]] with [[Mercurial]].
Line 5: Line 5:
As of Mercurial 1.0, you can configure Emacs as a merge tool (see MergeToolConfiguration): === Mercurial 1.0 and later ===

Add the following to
your ~/.hgrc file (see MergeToolConfiguration):
Line 7: Line 9:
[ui]
merge = emacs

[merge-tools]
Line 10: Line 16:
If you want to use the plain emerge tool in emacs, the following line works instead: If you want to use the plain emerge tool in emacs, the following line works instead in the merge-tools section:
Line 16: Line 22:
=== Wrapping Emacs+Ediff call in a script (Mercurial 0.9.5 and earlier) === === Mercurial 0.9.5 and earlier: Wrap Emacs+Ediff call in a script ===
Line 18: Line 24:
So to use [[http://www.gnu.org/software/emacs/|Emacs]] as [[Mercurial]] merger program, dump the following content into a file in your PATH (don't forget to turn on the execute bit): Dump the following content into a file in your PATH (don't forget to turn on the execute bit):
Line 72: Line 78:
=== How the script works === ==== How the script works ====
Line 76: Line 82:
=== Enabling the script usage === ==== Enabling the script usage ====
Line 82: Line 88:
}}}

Using Emacs as a merger program

Emacs is bundled with an elisp program called Ediff whose purpose is to help developers to visually apply patches. One of the ediff commands is well-suited to three-way merging and can be used as a merger program with Mercurial.

Mercurial 1.0 and later

Add the following to your ~/.hgrc file (see MergeToolConfiguration):

[ui]
merge = emacs

[merge-tools]
emacs.args = -q --eval "(ediff-merge-with-ancestor \"$local\" \"$other\" \"$base\" nil \"$output\")"

If you want to use the plain emerge tool in emacs, the following line works instead in the merge-tools section:

emacs.args = -q --eval "(emerge-files-with-ancestor nil \"$local\" \"$other\" \"$base\" \"$output\" nil 'kill-emacs)"

Mercurial 0.9.5 and earlier: Wrap Emacs+Ediff call in a script

Dump the following content into a file in your PATH (don't forget to turn on the execute bit):

   #!/bin/sh
   
   set -e # bail out quickly on failure
   
   LOCAL="$1"
   BASE="$2"
   OTHER="$3"

   BACKUP="$LOCAL.orig"
   
   Restore ()
   {
       cp "$BACKUP" "$LOCAL" 
   }

   ExitOK ()
   {
       exit $?
   }

   # Back up our file
   cp "$LOCAL" "$BACKUP"

   # Attempt to do a non-interactive merge
   if which merge > /dev/null 2>&1 ; then
       if merge "$LOCAL" "$BASE" "$OTHER" 2> /dev/null; then
           # success!
           ExitOK 
       fi
       Restore
   elif which diff3 > /dev/null 2>&1 ; then
       if diff3 -m "$BACKUP" "$BASE" "$OTHER" > "$LOCAL" ; then
           # success
           ExitOK
       fi
       Restore
   fi

   if emacs -q --no-site-file --eval "(ediff-merge-with-ancestor \"$BACKUP\" \"$OTHER\" \"$BASE\" nil \"$LOCAL\")" 
   then       
       ExitOK
   fi

   echo "emacs-merge: failed to merge files"
   exit 1

   # End of file

Considering Mercurial is now able to premerge before running the merge tool and even does it by default (see hgrc(5)), I feel that attempts of merge with merge and diff3 are not needed anymore.

1. How the script works

This script tries first to automatically merge the files using the RCS merge program or the diff3 program. If the automatic merger fails merging the files because of a conflict or, neither merge nor diff3 are available on the system, then emacs is launched to let the developer resolve the conflicts.

2. Enabling the script usage

Don't forget to add an entry in your hgrc file (either ~/.hgrc or the local working copy .hg/hgrc) to point Mercurial at your merge command (let's call it emacs-merge)

   [ui]
   merge = emacs-merge

MergingWithEmacs (last edited 2015-08-18 22:49:33 by mpm)