I would like to create a patch (git diff) with custom name -- e.g., hello-world.diff. Emacs doesn't seem to be able to handle git diff > hello-world.diff, perhaps because of the greater-than sign that works in regular terminals.

I have tried using start-process, but it fails:

(defun create-diff-patch ()
    (let ((name-of-patch
           (read-string "Name of Patch:  " nil nil "patch.diff" nil)))
      (start-process "process-name" "*output-buffer*"
                     "git" "diff" ">" name-of-patch)))

Here is the error message:

fatal: ambiguous argument '>': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

Process process-name exited abnormally with code 128

The following two examples create automatic names, but I'd like to use my own name instead:

(apply #'magit-call-git
       (magit-process-quote-arguments '("format-patch" "-1" "SHA1")))

(apply #'magit-call-git
       (magit-process-quote-arguments '("format-patch" "HEAD~~")))
  • 1
    tarsius will probably give you some way to do that with magit, but regardless of that: when you call a program form shell, > isn't an argument to the program, it's a shell's command. What you could've done instead is to collect program's output into a buffer and then write-region that buffer to whatever file you want. – wvxvw Jan 20 '16 at 20:02
  • So one solution is to build a shell command and pass it to /bin/sh. – Harald Hanche-Olsen Jan 20 '16 at 20:03

One possibility (not tested, but the principle should be sound) is something like this:

(defun create-diff-patch (filename)
  (start-process "git-diff" nil "/bin/sh" "-c"
         (format "git diff > %s" (shell-quote-argument filename))))

A more elaborate solution would be to use make-process to collect the git output in a temporary buffer, then use a process sentinel to save the buffer in the file you want when the process finishes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.