From bffc5311bee634ed10c955bc184ef1c373b94028 Mon Sep 17 00:00:00 2001 From: Sean Whitton Date: Thu, 22 Dec 2022 12:10:13 -0700 Subject: mailscripts.el: add mailscripts-git-format-patch-append Signed-off-by: Sean Whitton --- debian/changelog | 2 +- mailscripts.el | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/debian/changelog b/debian/changelog index d0f983b..fc4a04a 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,7 +1,7 @@ mailscripts (28-1) UNRELEASED; urgency=medium * mailscripts.el: - - new commands: mailscripts-git-format-patch-{attach,drafts} + - new commands: mailscripts-git-format-patch-{attach,drafts,append} - new DWIM wrapper command: mailscripts-prepare-patch - notmuch-extract-{thread,message}-patches: add Gnus support diff --git a/mailscripts.el b/mailscripts.el index 6f49bd8..d152e92 100644 --- a/mailscripts.el +++ b/mailscripts.el @@ -384,6 +384,54 @@ See also the interactive wrapper command `mailscripts-prepare-patch'." (delete-directory temp t))) (notmuch-search (format "folder:%s" notmuch-draft-folder))) +(defun mailscripts-git-format-patch-append (args) + "Append a patch generated by git-format-patch(1) to an unsent message. +ARGS is a single string of arguments to git-format-patch(1). +The patch is formatted such that a recipient can use the --scissors option to +git-am(1) to apply the patch; see \"DISCUSSION\" in git-format-patch(1)." + (interactive (list (read-string "git format-patch " "-1 "))) + (let ((dir default-directory)) + (compose-mail nil nil nil t) + (save-excursion + (save-restriction + (message-narrow-to-headers-or-head) + (let ((unsent-buffer (current-buffer)) + (default-directory dir) + (args (split-string-and-unquote args)) + (unsent-from (message-fetch-field "from"))) + (widen) + (if (re-search-forward message-signature-separator nil t) + (progn (goto-char (pos-bol)) + (push "--no-signature" args)) + (goto-char (point-max))) + (if (fboundp 'ensure-empty-lines) + (ensure-empty-lines 1) + ;; This is only some of what (ensure-empty-lines 1) does. + (if (bolp) + (unless (save-excursion (goto-char (pos-bol 0)) (eolp)) + (newline)) + (newline 2))) + (insert "-- >8 --\n") + (with-temp-buffer + (apply #'call-process "git" nil t nil "format-patch" "--stdout" + args) + (when (bobp) + (user-error "git-format-patch(1) produced no output")) + (goto-char (point-min)) + (delete-line) ; drop "From $SHA1 $magic_timestamp" + (message-narrow-to-headers-or-head) + (when-let* ((unsent + (and unsent-from + (mail-header-parse-address-lax unsent-from))) + (patch-from (message-fetch-field "from")) + (patch (mail-header-parse-address-lax patch-from))) + (when (equal unsent patch) + (message-remove-header "^From:\\|^Date:" t))) + (widen) + (goto-char (point-max)) + (delete-blank-lines) + (append-to-buffer unsent-buffer 1 (point-max)))))))) + (defun mailscripts--gfp-addressee () "Try to find a recipient for the --to argument to git-format-patch(1)." (or (and (local-variable-p 'vc-default-patch-addressee) -- cgit v1.2.3