Skip to contentSkip to conversation

ez: allow cleaning multiple branches at once

PATCH Superseded Antonin Godard 2 participants 3 messages Feb 25 – Feb 26

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 ez: allow cleaning multiple branches at once

Allow "b4 prep --cleanup" to cleanup multiple branches in one go. If an error occurs (branch not known/empty branch/currently checked out), just return to continue cleaning up branches. Give the user multiple options:
"y" to cleanup, "n" to skip, "q" to abort the cleanup, "s" to show the branch log with diffs, and "?" to print help about these options.

Passing nothing to --cleanup still shows the available branches to cleanup.

---
Changes in v3:
- Address feedback from Konstantin
- Link to v2: https://patch.msgid.link/[email protected]

Changes in v2:
- return instead of failing if branch is not prep-managed
- show branch log instead of general information on the branch
- Link to v1: https://patch.msgid.link/[email protected]

---
 docs/contributor/prep.rst | 10 ++---
 src/b4/command.py         |  4 +-
 src/b4/ez.py              | 99 +++++++++++++++++++++++++++++++++--------------
 3 files changed, 77 insertions(+), 36 deletions(-)
docs/contributor/prep.rst +5 -5
index e6b45c7..9e9da65 100644
@@ -342,15 +342,15 @@ up the prep-managed branch, together with all of its sent tags::
 
     b4 prep --cleanup
 
-This command lists all prep-managed branches in your repository. Pick a
-branch to clean up, make sure it's not currently checked out, and run
-the command again::
+This command lists all prep-managed branches in your repository. Pick one or
+more branches to clean up, make sure it's not currently checked out, and run the
+command again::
 
-    b4 prep --cleanup b4/my-topical-branch
+    b4 prep --cleanup b4/my-topical-branch ...
 
 After you confirm your action, this should create a tarball with all the
 patches, cover letters, and tracking information from your series.
-Afterwards, b4 deletes the branch and all related tags from your local
+Afterwards, b4 deletes the branch(es) and all related tags from your local
 repository.
 
 .. _prep_flags:
src/b4/command.py +2 -2
index 0e685cd..54c32a5 100644
@@ -344,8 +344,8 @@ def setup_parser() -> argparse.ArgumentParser:
                        help='Mark current revision as sent and reroll (requires cover letter msgid)')
     spp_g.add_argument('--show-info', metavar='PARAM', nargs='?', const=':_all',
                        help='Show series info in a format that can be passed to other commands.')
-    spp_g.add_argument('--cleanup', metavar='BRANCHNAME', nargs='?', const='_show',
-                       help='Archive and remove a prep-tracked branch and all its sent/ tags')
+    spp_g.add_argument('--cleanup', metavar='BRANCHNAME', nargs='*',
+                       help='Archive and remove prep-tracked branches and all associated sent/ tags')
 
     ag_prepn = sp_prep.add_argument_group('Create new branch', 'Create a new branch for working on patch series')
     ag_prepn.add_argument('-n', '--new', dest='new_series_name',
src/b4/ez.py +70 -29

150 lines not on this page · raw message

---
base-commit: 858baa2b2eeb93ea1b0e1b57adfb77e4a126043e
change-id: 20250407-multiple-prep-cleanup-b60966ba8bf5

#1 lore raw
Quoted 8 lines · Antonin Godard

Allow "b4 prep --cleanup" to cleanup multiple branches in one go. If an error occurs (branch not known/empty branch/currently checked out), just return to continue cleaning up branches. Give the user multiple options:
"y" to cleanup, "n" to skip, "q" to abort the cleanup, "s" to show the branch log with diffs, and "?" to print help about these options.

Passing nothing to --cleanup still shows the available branches to cleanup.

I have a few tiny notes, but I hope we can get them addressed. :)

src/b4/ez.py def get_prep_managed_branches(gitdir: Optional[str] = None) -> List[str]: · line 2530

Quoted 8 lines
src/b4/ez.py +2 -6
-    mybranch = param
-    if not b4.git_branch_exists(None, mybranch):
-        logger.critical('Not a known branch: %s', mybranch)
-        sys.exit(1)
-    is_prep_branch(mustbe=True, usebranch=mybranch)
-    base_commit, start_commit, end_commit = get_series_range(usebranch=mybranch)
+    if not is_prep_branch(usebranch=branch):
+        logger.error(f'ERROR: {branch} is not a prep-managed branch')

This is a nitpick, but in general it's preferred to use %-formatting for logger calls, because this skips unnecessary interpolation when not needed. So, in this case it should be:

logger.error('ERROR: %s is not a prep-managed branch', branch)

Quoted 17 lines
src/b4/ez.py +12 -1
@@ -2575,7 +2569,37 @@ def cleanup(param: str) -> None:
             tags.append((tagname, base_commit, tag_commit, revision, cover))
     logger.info('---')
     try:
-        input('Press Enter to confirm or Ctrl-C to abort')
+        resp = None
+        while resp is None:
+            resp = input('Proceed? [y/s/q/N/?] ')
+            if resp == "?":
+                logger.info(textwrap.dedent(
+                    """
+                    Possible answers:
+                    y: cleanup the branch
+                    s: show branch log
+                    q or Ctrl-C: abort cleanup
+                    n (default): do not cleanup this branch
+                    ?: show this help message

src/b4/ez.py def cleanup(param: str) -> None: · line 2593

Quoted 10 lines
src/b4/ez.py +10 -0
+                    """))
+                resp = None
+            elif resp in ("show", "s"):
+                ecode, out = b4.git_run_command(None, ["log",
+                                                       "--patch",
+                                                       "--color=always",
+                                                       f"{start_commit}~..{end_commit}"])
+                if ecode > 0:
+                    logger.critical('ERROR: unable to show git log between'
+                                    f'{start_commit} and {end_commit}')

There are two small notes here. First, there's no space between two strings, so on concatenation this will produce "...between<sha>" with no space before the SHA. Second, this should also be using %-interpolation, so the line should just really be:

logger.critical('ERROR: unable to show git log between %s and %s',
start_commit, end_commit)

Thanks!

Signature · 1 line
KR
#2 lore raw

Hi,

Quoted 21 lines · Konstantin Ryabitsev
Quoted 8 lines · Antonin Godard

Allow "b4 prep --cleanup" to cleanup multiple branches in one go. If an error occurs (branch not known/empty branch/currently checked out), just return to continue cleaning up branches. Give the user multiple options:
"y" to cleanup, "n" to skip, "q" to abort the cleanup, "s" to show the branch log with diffs, and "?" to print help about these options.

Passing nothing to --cleanup still shows the available branches to cleanup.

I have a few tiny notes, but I hope we can get them addressed. :)

src/b4/ez.py def get_prep_managed_branches(gitdir: Optional[str] = None) -> List[str]: · line 2530

Quoted 8 lines
src/b4/ez.py +2 -6
-    mybranch = param
-    if not b4.git_branch_exists(None, mybranch):
-        logger.critical('Not a known branch: %s', mybranch)
-        sys.exit(1)
-    is_prep_branch(mustbe=True, usebranch=mybranch)
-    base_commit, start_commit, end_commit = get_series_range(usebranch=mybranch)
+    if not is_prep_branch(usebranch=branch):
+        logger.error(f'ERROR: {branch} is not a prep-managed branch')
Quoted 5 lines

This is a nitpick, but in general it's preferred to use %-formatting for logger calls, because this skips unnecessary interpolation when not needed. So, in this case it should be:

logger.error('ERROR: %s is not a prep-managed branch', branch)

TIL! I'll send a v4. Thanks.

Antonin