discussion and development of piem
 help / color / mirror / code / Atom feed
* [PATCH] Support adding Message-Id to attached patches
@ 2020-06-12  4:55 Kyle Meyer
  2020-06-13  3:21 ` Kyle Meyer
  0 siblings, 1 reply; 3+ messages in thread
From: Kyle Meyer @ 2020-06-12  4:55 UTC (permalink / raw)
  To: piem; +Cc: Kyle Meyer

With an inline patch that has a Message-Id, this information can be
linked up to the patch when applied (e.g., with git-am's --message-id
flag or using a post-applypatch hook [1]).  Unfortunately, this method
fails for projects where it is common to attach patches, as there is
no Message-Id within the patch.

Teach piem-am-ready-mbox how to insert the Message-Id that piem-mid
reports, which should always correspond to the message that contains
the patch attachments.

[1] Here's an example that used to keep the commit->Message-Id mapping
    in git.git:
    https://lore.kernel.org/git/xmqq7e5ag4g5.fsf@gitster-ct.c.googlers.com/
---
 piem.el | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)

diff --git a/piem.el b/piem.el
index a757092..fcd5d8c 100644
--- a/piem.el
+++ b/piem.el
@@ -101,6 +101,14 @@ (defcustom piem-am-ready-mbox-functions nil
 arguments and inserts the mbox's contents in the current buffer."
   :type 'hook)
 
+(defcustom piem-add-message-id-header nil
+  "Whether to add Message-Id header to non-mail patches.
+If this value is non-nil and a patch returned by a function in
+`piem-am-ready-mbox-functions' looks like a patch that was
+attached rather than sent inline, add a Message-Id header with
+the value of `piem-mid'."
+  :type 'boolean)
+
 (defcustom piem-git-executable
   (or (and (boundp 'magit-git-executable) magit-git-executable)
       "git")
@@ -300,10 +308,19 @@ (defun piem-am-ready-mbox ()
   (when-let ((fn (run-hook-with-args-until-success
                   'piem-am-ready-mbox-functions)))
     (let ((buffer (generate-new-buffer " *piem am-ready mbox*"))
+          (mid (and piem-add-message-id-header (piem-mid)))
           has-content)
       (with-current-buffer buffer
         (funcall fn)
-        (setq has-content (< 1 (point-max))))
+        (setq has-content (< 1 (point-max)))
+        (when (and has-content mid)
+          (goto-char (point-min))
+          (while (re-search-forward
+                  (rx "From " (>= 40 hex-digit) " Mon Sep 17 00:00:00 2001")
+                  nil t)
+            (search-forward "\n\n")
+            (forward-line -1)
+            (insert (format "Message-Id: <%s>\n" mid)))))
       (if has-content
           buffer
         (kill-buffer buffer)

base-commit: 54ea4ed7d1bc64222cf837ecc046f7aa4f42c769
-- 
2.26.2


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] Support adding Message-Id to attached patches
  2020-06-12  4:55 [PATCH] Support adding Message-Id to attached patches Kyle Meyer
@ 2020-06-13  3:21 ` Kyle Meyer
  2020-06-13  4:49   ` [PATCH v2] " Kyle Meyer
  0 siblings, 1 reply; 3+ messages in thread
From: Kyle Meyer @ 2020-06-13  3:21 UTC (permalink / raw)
  To: piem

Kyle Meyer writes:
> +(defcustom piem-add-message-id-header nil
> +  "Whether to add Message-Id header to non-mail patches.
> +If this value is non-nil and a patch returned by a function in
> +`piem-am-ready-mbox-functions' looks like a patch that was
> +attached rather than sent inline, add a Message-Id header with
> +the value of `piem-mid'."

s/value of/value returned by/

> +          (while (re-search-forward
> +                  (rx "From " (>= 40 hex-digit) " Mon Sep 17 00:00:00 2001")

This regexp should be anchored at the start of the line.

> +                  nil t)
> +            (search-forward "\n\n")
> +            (forward-line -1)
> +            (insert (format "Message-Id: <%s>\n" mid)))))

The From line above is very unlikely to be in the commit message and
can't be in a diff line if the regexp is anchored.  Still it's probably
worth trying a little harder to check the surroundings.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH v2] Support adding Message-Id to attached patches
  2020-06-13  3:21 ` Kyle Meyer
@ 2020-06-13  4:49   ` Kyle Meyer
  0 siblings, 0 replies; 3+ messages in thread
From: Kyle Meyer @ 2020-06-13  4:49 UTC (permalink / raw)
  To: piem; +Cc: Kyle Meyer

With an inline patch that has a Message-Id, this information can be
linked up to the patch when applied (e.g., with git-am's --message-id
flag or using a post-applypatch hook [1]).  Unfortunately, this method
fails for projects where it is common to attach patches, as there is
no Message-Id within the patch.

Teach piem-am-ready-mbox how to insert the Message-Id that piem-mid
reports, which should always correspond to the message that contains
the patch attachments.

[1] Here's an example that used to keep the commit->Message-Id mapping
    in git.git:
    https://lore.kernel.org/git/xmqq7e5ag4g5.fsf@gitster-ct.c.googlers.com/
---
 piem.el | 43 ++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 42 insertions(+), 1 deletion(-)

diff --git a/piem.el b/piem.el
index a757092..6e13cc9 100644
--- a/piem.el
+++ b/piem.el
@@ -101,6 +101,14 @@ (defcustom piem-am-ready-mbox-functions nil
 arguments and inserts the mbox's contents in the current buffer."
   :type 'hook)
 
+(defcustom piem-add-message-id-header nil
+  "Whether to add Message-Id header to non-mail patches.
+If this value is non-nil and a patch returned by a function in
+`piem-am-ready-mbox-functions' looks like a patch that was
+attached rather than sent inline, add a Message-Id header with
+the return value of `piem-mid'."
+  :type 'boolean)
+
 (defcustom piem-git-executable
   (or (and (boundp 'magit-git-executable) magit-git-executable)
       "git")
@@ -294,16 +302,49 @@ (defun piem-mid ()
   "Return the current buffer's message ID."
   (run-hook-with-args-until-success 'piem-get-mid-functions))
 
+(defun piem--insert-message-id-header (mid)
+  ;; Be strict about case because this is coming from
+  ;; git-format-patch.
+  (let ((case-fold-search nil))
+    (while (re-search-forward
+            (rx line-start
+                "From " (>= 40 hex-digit) " Mon Sep 17 00:00:00 2001"
+                line-end)
+            nil t)
+      (catch 'has-message-id
+        (let ((header-count 0))
+          (while (and (= (forward-line) 0)
+                      (not (looking-at-p
+                            (rx line-start (zero-or-one space) line-end))))
+            (cond
+             ((looking-at-p
+               (rx line-start
+                   "Message-Id: <" (one-or-more not-newline) ">"
+                   line-end))
+              (throw 'has-message-id nil))
+             ((looking-at-p
+               (rx line-start (or "From" "Date" "Subject") ": "
+                   not-newline))
+              (cl-incf header-count))))
+          (when (= header-count 3)
+            ;; Found all the expected headers before hitting a
+            ;; blank line.  Assume we're in a header.
+            (insert (format "Message-Id: <%s>\n" mid))))))))
+
 (defun piem-am-ready-mbox ()
   "Return buffer containing an am-ready mbox.
 Callers are responsible for killing the buffer."
   (when-let ((fn (run-hook-with-args-until-success
                   'piem-am-ready-mbox-functions)))
     (let ((buffer (generate-new-buffer " *piem am-ready mbox*"))
+          (mid (and piem-add-message-id-header (piem-mid)))
           has-content)
       (with-current-buffer buffer
         (funcall fn)
-        (setq has-content (< 1 (point-max))))
+        (setq has-content (< 1 (point-max)))
+        (when (and has-content mid)
+          (goto-char (point-min))
+          (piem--insert-message-id-header mid)))
       (if has-content
           buffer
         (kill-buffer buffer)
-- 
2.26.2


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-06-13  4:49 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-06-12  4:55 [PATCH] Support adding Message-Id to attached patches Kyle Meyer
2020-06-13  3:21 ` Kyle Meyer
2020-06-13  4:49   ` [PATCH v2] " Kyle Meyer

Code repositories for project(s) associated with this public inbox

	https://git.kyleam.com/piem/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).