help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to use emacs as a "diff3 -m" compatible merge tool?


From: Tassilo Horn
Subject: Re: How to use emacs as a "diff3 -m" compatible merge tool?
Date: Mon, 13 Jun 2022 15:34:24 +0200
User-agent: mu4e 1.7.27; emacs 29.0.50

Hi all,

> pacdiff calls the user-defined mergeprog like so:
>
>       if $mergeprog "$file" "$base" "$pacfile" >"$merged"; then
>               msg2 "Merged without conflicts."
>       fi
>
>
> I'd like to use emacs as mergetool so need to support these calling
> convention (which are the ones of "diff3 -m"), i.e.,
>
>   1. args are MY-VERSION BASE THEIR-VERSION
>   2. a successful merge exits zero, an aborted merge exits non-zero
>   3. the merge result is printed to stdout

Here's a version which works for me (on GNU/Linux where /dev/stdout is a
thing) and fulfills all requirements except for 2 which turns out not to
be important at least in my pacdiff use-case.  It'll ask if I want to
use the merge result anyhow so exiting successfully even when I abort
the merge is no big deal.

--8<---------------cut here---------------start------------->8---
(defun th/ediff-spit-merge-result-and-kill-emacs ()
  (if-let ((merge-buf (ediff-get-buffer 'C)))
      (with-current-buffer merge-buf
        (append-to-file (point-min) (point-max)
                        "/dev/stdout")
        (kill-emacs))
    (error "There is no merge result buffer")))

(defun th/command-line-ediff-merge3 (_switch)
  "Do a 3-way merge using `ediff-merge-files-with-ancestor'.
Assumes the command line args were: my-version base-version their-version"
  (setq ediff-quit-merge-hook (list 
#'th/ediff-spit-merge-result-and-kill-emacs))
  (let ((make-backup-files nil)
        (my-version    (pop command-line-args-left))
        (base-version  (pop command-line-args-left))
        (their-version (pop command-line-args-left)))
    (ediff-merge-files-with-ancestor
     my-version their-version base-version)))

(add-to-list 'command-switch-alist
             '("--ediff-merge3" . th/command-line-ediff-merge3))
--8<---------------cut here---------------end--------------->8---

Bye,
Tassilo



reply via email to

[Prev in Thread] Current Thread [Next in Thread]