chiark / gitweb /
emerge-hack: A new script for Git merging.
authorMark Wooding <mdw@distorted.org.uk>
Tue, 26 Feb 2008 17:32:04 +0000 (17:32 +0000)
committerMark Wooding <mdw@distorted.org.uk>
Tue, 26 Feb 2008 17:36:45 +0000 (17:36 +0000)
emerge-hack pretends to be an Emacs and takes arguments as for emerge;
but instead it runs ediff, which is much nicer.

emerge-hack [new file with mode: 0755]
gitconfig
setup

diff --git a/emerge-hack b/emerge-hack
new file mode 100755 (executable)
index 0000000..dfbaaa1
--- /dev/null
@@ -0,0 +1,84 @@
+#! /bin/sh
+set -e
+
+### Huh?
+###
+### This script is an adaptor for git mergetool so that, while /it/ thinks
+### it's using emerge, it's really using ediff, which is much better, and
+### maybe a long-running Emacs via gnuserv.
+
+## Find out what we're supposed to be doing.  The command line looks like
+##
+## emerge-hack -f emerge-mumble FILE FILE ... OUTPUT
+
+dashf=$1
+command=$2
+shift 2
+case "$dashf" in
+  -f) ;;
+  *) echo >&2 "$0: expected -f emerge-mumble"; exit 1;;
+esac
+
+case "$command" in
+  emerge-files-with-ancestor-command)
+    func=ediff-merge-files-with-ancestor
+    ;;
+  emerge-merge-files-command)
+    func=ediff-files
+    ;;
+  *)
+    echo >&2 "$0: unexpected command $command"
+    exit 1
+    ;;
+esac
+
+## Find out whether there's a plausible-looking gnuserv.
+
+if gnuclient -batch -eval t >/dev/null 2>&1; then
+  emacs="gnuclient"
+else
+  emacs="emacs"
+fi
+
+## Now build the command line.
+##
+## The hairy-looking seddery is there to quotify file names properly for
+## Lisp.  We also need to make sure that the filenames are absolute.
+## Finally, there's a little tweak to separate off the last name because of
+## the strange argument order ediff uses.
+
+expr=""
+sep=""
+last="$func"
+
+for i; do
+  name=$i
+  case "$name" in
+    /*);;
+    *) name=`pwd`/$name;;
+  esac
+  quot=$(echo -n "$name" |
+    sed -n '
+    1 h
+    2,$ H
+    $ {
+      g
+      s/["\\]/\\&/
+      s/^/"/
+      s/$/"/
+      s/\
+/\\n/
+      p
+    }
+')
+  expr="$expr$sep$last"
+  sep=" "
+  last=$quot
+  output=$name
+done
+
+## Done.  Note that we pass the output filename again, as an explicit
+## argument.  This will make gnuclient wait until we've finished with the
+## merge.
+
+exec "$emacs" -eval "($expr${sep}nil $last)" "$output"
index 4f6359249bdeb579e8f70c1f74342bea7ba51a06..4d8eb77a2fc89cbadf9d2282268090e24a9f6a8a 100644 (file)
--- a/gitconfig
+++ b/gitconfig
@@ -7,6 +7,10 @@
 
 [merge]
        summary = true
+       tool = emerge
+
+[mergetool "emerge"]
+       path = emerge-hack
 
 [diff]
        renames = copies
@@ -54,6 +58,7 @@
 [mail "alias"]
        git = git@vger.kernel.org
        mdw = mdw@distorted.org.uk
+
 [gui]
        fontui = -family helvetica -size 11 -weight normal -slant roman -underline 0 -overstrike 0
        fontdiff = -family fixed -size 10 -weight normal -slant roman -underline 0 -overstrike 0
diff --git a/setup b/setup
index 47eac252703a5b645e6f9f24ca4ca3db65a06867..8ca3066ee490b670659fc7df16081bfe74cb5510 100755 (executable)
--- a/setup
+++ b/setup
@@ -179,7 +179,8 @@ echo "      all done."
 
 ### Install useful scripts included in this package
 scripts="
-  mdw-editor"
+  mdw-editor
+  emerge-hack"
 echo "Installing scripts..."
 mkdir -p $HOME$sub/bin
 for s in $scripts; do