diff mbox series

[b4,2/2] am, shazam: allow cherry-picking an out-of-series patch

Message ID 20230415-am-cherry-pick-suggestion-v1-2-8951a2274256@gmail.com (mailing list archive)
State Accepted
Headers show
Series am, shazam: allow cherry-picking an out-of-series patch | expand

Commit Message

Philippe Blain April 15, 2023, 7:31 p.m. UTC
Reviewers sometimes include a patch as a reply when reviewing a
contributor's series, for example, to suggest tweaks or additional
changes. The contributor might want to add that patch to their series
for the next revision.

Currently, using 'b4 shazam -P_ $msgid', where msgid is the message-id
or full URL of the reviewer's message, does not work:

    $ b4 am -o- -P_ ZDnCMegeiw0kT5oj@nand.local
    Analyzing 2 messages in the thread
    No patches found.

mbox.py::make_am returns early because the LoreSeries 'lser' returned by
lmbx.get_series is empty ('None'). It is empty because when using '-P_',
b4::retrieve_messages only retrieves the specific message-id given (and
its replies), so the LoreSeries 'lmbx' created in make_am does not have
any series.

Also check if the user asked to cherry-pick a specific message-id before
returning early in make_am. This allows us to reach the 'if
cmdargs.cherrypick == '_'' condition, but then we iterate on
lser.patches to find the patch to cherry-pick, and so get a runtime
error:

    $ b4 am -o-  -P_ ZDnCMegeiw0kT5oj@nand.local
    Analyzing 2 messages in the thread
    Traceback (most recent call last):
      File "/Users/Philippe/Code/b4/b4/command.py", line 381, in <module>
        cmd()
      File "/Users/Philippe/Code/b4/b4/command.py", line 364, in cmd
        cmdargs.func(cmdargs)
      File "/Users/Philippe/Code/b4/b4/command.py", line 91, in cmd_am
        b4.mbox.main(cmdargs)
      File "/Users/Philippe/Code/b4/b4/mbox.py", line 719, in main
        make_am(msgs, cmdargs, msgid)
      File "/Users/Philippe/Code/b4/b4/mbox.py", line 82, in make_am
        for lmsg in lser.patches[1:]:
    AttributeError: 'NoneType' object has no attribute 'patches'

Fix this by creating a fake LoreSeries and adding to it any followup
messages in 'lmbx' with a diff in the body.

Signed-off-by: Philippe Blain <levraiphilippeblain@gmail.com>
---
 b4/mbox.py | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/b4/mbox.py b/b4/mbox.py
index 05c40e9..70b866f 100644
--- a/b4/mbox.py
+++ b/b4/mbox.py
@@ -57,7 +57,7 @@  def make_am(msgs: List[email.message.Message], cmdargs: argparse.Namespace, msgi
         reroll = False
 
     lser = lmbx.get_series(revision=wantver, sloppytrailers=cmdargs.sloppytrailers, reroll=reroll)
-    if lser is None:
+    if lser is None and cmdargs.cherrypick != '_':
         if wantver is None:
             logger.critical('No patches found.')
         else:
@@ -70,6 +70,13 @@  def make_am(msgs: List[email.message.Message], cmdargs: argparse.Namespace, msgi
     if cmdargs.cherrypick:
         cherrypick = list()
         if cmdargs.cherrypick == '_':
+            # We might want to pick a patch sent as a followup, so create a fake series
+            # and add followups with diffs
+            if lser is None:
+                lser = b4.LoreSeries(revision=1, expected=1)
+            for followup in lmbx.followups:
+                if followup.has_diff:
+                    lser.add_patch(followup)
             # Only grab the exact msgid provided
             at = 0
             for lmsg in lser.patches[1:]: