Skip to contentSkip to conversation

ez: add subject prefix flag

PATCH Superseded Andreas Hindborg 1 participant 1 message Dec 20, 2024

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ez: add subject prefix flag

Add a flag -p PREFIX/--add-subject-prefix=PREFIX to am and shazam commands. This flag will add PREFIX to the subject line of each patch before applying the patch.

One use case for this is when composing trees that consist of patches from many different sources. Adding a prefix to the subject of each series makes it easy to see the origin when scanning the commit log.

Another side effect is that git will not remove the PREFIX: [PATCH vX x/y] prefix from the patch when applying it. This might similarly be desirable in some situations.

---
 src/b4/__init__.py | 14 ++++++++------
 src/b4/command.py  |  2 ++
 src/b4/mbox.py     |  2 +-
 3 files changed, 11 insertions(+), 7 deletions(-)
src/b4/__init__.py +8 -6
index bb93656d8ad7d50cbcda1f7d0d820be50725962f..da2c3734184a44a4c547f1c3daacfcdfbf8f4e0c 100644
@@ -616,7 +616,7 @@ class LoreSeries:
 
     def get_am_ready(self, noaddtrailers: bool = False, addmysob: bool = False,
                      addlink: bool = False, cherrypick: Optional[List[int]] = None, copyccs: bool = False,
-                     allowbadchars: bool = False, showchecks: bool = False) -> List[email.message.EmailMessage]:
+                     allowbadchars: bool = False, showchecks: bool = False, addprefix: Optional[str] = None) -> List[email.message.EmailMessage]:
 
         usercfg = get_user_config()
         config = get_main_config()
@@ -750,7 +750,7 @@ class LoreSeries:
                 if noaddtrailers:
                     add_trailers = False
                 msg = lmsg.get_am_message(add_trailers=add_trailers, extras=extras, copyccs=copyccs,
-                                          addmysob=addmysob, allowbadchars=allowbadchars)
+                                          addmysob=addmysob, allowbadchars=allowbadchars, addprefix=addprefix)
                 if local_check_cmds:
                     lmsg.load_local_ci_status(local_check_cmds)
                 if lmsg.local_ci_status or lmsg.pw_ci_status in {'success', 'fail', 'warning'}:
@@ -2326,7 +2326,7 @@ class LoreMessage:
 
         self.body = LoreMessage.rebuild_message(bheaders, message, fixtrailers, basement, signature)
 
-    def get_am_subject(self, indicate_reroll: bool = True, use_subject: Optional[str] = None) -> str:
+    def get_am_subject(self, indicate_reroll: bool = True, use_subject: Optional[str] = None, prefix: Optional[str] = None) -> str:
         # Return a clean patch subject
         parts = ['PATCH']
         if self.lsubject.rfc:
@@ -2347,11 +2347,13 @@ class LoreMessage:
         if not use_subject:
             use_subject = self.lsubject.subject
 
-        return '[%s] %s' % (' '.join(parts), use_subject)
+        prefix = "" if prefix is None else prefix
+
+        return '%s: [%s] %s' % (prefix, ' '.join(parts), use_subject)
 
     def get_am_message(self, add_trailers: bool = True, addmysob: bool = False,
                        extras: Optional[List['LoreTrailer']] = None, copyccs: bool = False,
-                       allowbadchars: bool = False) -> email.message.EmailMessage:
+                       allowbadchars: bool = False, addprefix: Optional[str] = None) -> email.message.EmailMessage:
         # Look through the body to make sure there aren't any suspicious unicode control flow chars
         # First, encode into ascii and compare for a quick utf8 presence test
         if not allowbadchars and self.body.encode('ascii', errors='replace') != self.body.encode():
@@ -2396,7 +2398,7 @@ class LoreMessage:
 
         am_msg = email.message.EmailMessage()
         hfrom = format_addrs([(i.get('Author', ''), i.get('Email'))])
-        am_msg.add_header('Subject', self.get_am_subject(indicate_reroll=False, use_subject=i.get('Subject')))
+        am_msg.add_header('Subject', self.get_am_subject(indicate_reroll=False, use_subject=i.get('Subject'), prefix=addprefix))
         am_msg.add_header('From', hfrom)
         am_msg.add_header('Date', i.get('Date'))
         am_msg.add_header('Message-Id', f'<{self.msgid}>')
src/b4/command.py +2 -0
index 5d90b4f52b4f3bd84f8660c26fc005af98309c26..ad02765c7ef715f44eddc859b1577dae74c85292 100644
@@ -61,6 +61,8 @@ def cmd_am_common_opts(sp):
                     help='Break thread at the msgid specified and ignore any parent messages')
     sp.add_argument('--allow-unicode-control-chars', dest='allowbadchars', action='store_true', default=False,
                     help='Allow unicode control characters (very rarely legitimate)')
+    sp.add_argument('-p', '--add-subject-prefix', dest='addprefix', default=None,
+                      help='Add a PREFIX: to every patch subject line')
     sa_g = sp.add_mutually_exclusive_group()
     sa_g.add_argument('-l', '--add-link', dest='addlink', action='store_true', default=False,
                       help='Add a Link: trailer with message-id lookup URL to every patch')
src/b4/mbox.py +1 -1
index 7061d1493be3e0d116ec504c9a1638be34b65a9a..195f24d0e4d968e2e6e4997265cc7e318b2248ed 100644
@@ -159,7 +159,7 @@ def make_am(msgs: List[email.message.EmailMessage], cmdargs: argparse.Namespace,
 
     am_msgs = lser.get_am_ready(noaddtrailers=cmdargs.noaddtrailers, addmysob=cmdargs.addmysob, addlink=cmdargs.addlink,
                                 cherrypick=cherrypick, copyccs=cmdargs.copyccs, allowbadchars=cmdargs.allowbadchars,
-                                showchecks=cmdargs.check)
+                                showchecks=cmdargs.check, addprefix=cmdargs.addprefix)
     logger.info('---')
 
     if cherrypick is None:

---
base-commit: 1b0d65dfcb498531da131511cab7f14e5845d969
change-id: 20241220-prefix-87d15717e4a4

Best regards,

Signature · 1 line
Andreas Hindborg <[email protected]>