tools.linux.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Philippe Blain <levraiphilippeblain@gmail.com>
To: tools@linux.kernel.org
Subject: [PATCH b4] Reorder headers for `git imap-send` compatibility
Date: Tue, 10 Aug 2021 22:41:32 -0400	[thread overview]
Message-ID: <20210811024132.8496-1-levraiphilippeblain@gmail.com> (raw)

When downloading messages from a public-inbox instance, reorder the
"From", "Date" and "Subject" headers in that specific order. This allows
the output from 'b4 mbox -o- $url' to be piped to 'git imap-send', which
expects these headers in that specific order.

An easy place to implement that is in 'mailsplit_bytes', which is called
by 'get_pi_thread_by_url' and already loops over the messages in the 'git
mailsplit'-ed mbox file to populate the message list.

Note that 'mailsplit_bytes' is also called by 'get_msgs' directly when
piping in a local mailbox, i.e. when '-' is used as the value of
'--use-local-mbox', but it does no harm to also reorder the headers in
that case.

Signed-off-by: Philippe Blain <levraiphilippeblain@gmail.com>
---

Notes:
    Hi, I suggested that feature in [1] last December and just recently took the
    time to implement it.
    
    Konstantin suggested in his response to [1] to try using isync/mbsync instead,
    but I would prefer not having to change my workflow.  I've been using 'git
    imap-send' for a while with my custom script [2] when hitting emails that were
    not created with 'git send-email'/'git format-patch' and I did not hit any
    corner case where 'git imap-send' was not able to send the email.
    
    [1] https://lore.kernel.org/tools/7e6329fa-e5fa-c5af-002d-a8fbb60e0724@gmail.com/
    [2] https://gist.github.com/phil-blain/d350e91959efa6e7afce60e74bf7e4a8

 b4/__init__.py | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/b4/__init__.py b/b4/__init__.py
index b45e26f..baea003 100644
--- a/b4/__init__.py
+++ b/b4/__init__.py
@@ -2162,6 +2162,13 @@ def get_strict_thread(msgs, msgid):
 
     return strict
 
+def reorder_headers(msg):
+    """ Reorder "From", "Date" and "Subject" headers in the order `git imap-send` expects"""
+    for header in ['From', 'Date', 'Subject']:
+        header_content = msg[header]
+        del msg[header]
+        msg[header] = header_content
+    return msg
 
 def mailsplit_bytes(bmbox: bytes, outdir: str) -> list:
     logger.debug('Mailsplitting the mbox into %s', outdir)
@@ -2174,7 +2181,7 @@ def mailsplit_bytes(bmbox: bytes, outdir: str) -> list:
     # Read in the files
     for msg in os.listdir(outdir):
         with open(os.path.join(outdir, msg), 'rb') as fh:
-            msgs.append(email.message_from_binary_file(fh))
+            msgs.append(reorder_headers(email.message_from_binary_file(fh)))
     return msgs
 
 

base-commit: 45ef591d2fb245a06600514d8df57ab64d519fd8
-- 
2.29.2


             reply	other threads:[~2021-08-11  2:41 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-11  2:41 Philippe Blain [this message]
2021-08-26 19:40 ` [PATCH b4] Reorder headers for `git imap-send` compatibility Konstantin Ryabitsev
2021-08-26 22:41   ` Philippe Blain

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210811024132.8496-1-levraiphilippeblain@gmail.com \
    --to=levraiphilippeblain@gmail.com \
    --cc=tools@linux.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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 NNTP newsgroup(s).