diff mbox series

[2/2] Allow negative number in parse_int_range()

Message ID 20240502191205.3964106-2-lucas.demarchi@intel.com (mailing list archive)
State Accepted
Headers show
Series [1/2] Make upper non-optional in parse_int_range() | expand

Commit Message

Lucas De Marchi May 2, 2024, 7:12 p.m. UTC
This is useful when applying patches and sending a thank you note
following that, which can now be done with `b4 ty -t -1`.

Signed-off-by: Lucas De Marchi <lucas.demarchi@intel.com>
---
 docs/maintainer/am-shazam.rst |  4 ++++
 docs/maintainer/ty.rst        |  6 ++++--
 src/b4/__init__.py            | 12 +++++++++---
 3 files changed, 17 insertions(+), 5 deletions(-)
diff mbox series

Patch

diff --git a/docs/maintainer/am-shazam.rst b/docs/maintainer/am-shazam.rst
index 2d672c3..416a941 100644
--- a/docs/maintainer/am-shazam.rst
+++ b/docs/maintainer/am-shazam.rst
@@ -128,6 +128,10 @@  The following flags are common to both commands:
 
       b4 am -P _ <msgid>
 
+  This picks just the last patch from a series::
+
+      b4 am -P -1 <msgid>
+
   This picks all patches where the subject matches "iscsi"::
 
       b4 am -P *iscsi*
diff --git a/docs/maintainer/ty.rst b/docs/maintainer/ty.rst
index 3b205c2..3945b08 100644
--- a/docs/maintainer/ty.rst
+++ b/docs/maintainer/ty.rst
@@ -92,8 +92,10 @@  Optional flags
 ``-t THANKFOR, --thank-for THANKFOR``
   From the listing generated by ``--list``, specify which thank-you
   notes should be sent. This command accepts comma-separated values and
-  ranges, including open-ended ranges, e.g.: ``-t 1,3,5-7,9-``. It also
-  accepts ``all``.
+  ranges, including open-ended ranges, e.g.: ``-t 1,3,5-7,9-``. When using
+  a sing number, it's possible to use negative values to refer to the
+  latest patch series, e.g.: ``-t -1`` refers to the last patch series.
+  It also accepts ``all``.
 
 ``-d DISCARD, --discard DISCARD``
   From the listing generated by ``--list``, specify which thank-you
diff --git a/src/b4/__init__.py b/src/b4/__init__.py
index b3f1c90..fc0a759 100644
--- a/src/b4/__init__.py
+++ b/src/b4/__init__.py
@@ -3545,9 +3545,15 @@  def parse_int_range(intrange: str, upper: int) -> Iterator[int]:
     # Remove all whitespace
     intrange = re.sub(r'\s', '', intrange)
     for n in intrange.split(','):
-        if n.isdigit():
-            yield int(n)
-        elif n.find('<') == 0 and len(n) > 1 and n[1:].isdigit():
+        # Allow single numbers to be negative
+        try:
+            i = int(n)
+            if i < 0 and abs(i) <= upper:
+                yield upper + i + 1
+        except ValueError:
+            pass
+
+        if n.find('<') == 0 and len(n) > 1 and n[1:].isdigit():
             yield from range(1, int(n[1:]))
         elif n.find('-') > 0:
             nr = n.split('-')