All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH b4] prep: add --cleanup-older-than option to clean up stale branches
@ 2026-03-23 11:58 Breno Leitao
  2026-08-03 19:07 ` Konstantin Ryabitsev
  0 siblings, 1 reply; 3+ messages in thread
From: Breno Leitao @ 2026-03-23 11:58 UTC (permalink / raw)
  To: Kernel.org Tools; +Cc: Konstantin Ryabitsev, kernel-team, Breno Leitao

B4 is so useful that I have too many branches now, create a way to
easily remove old branches.

Add a new --cleanup-older-than DAYS option that archives and removes
prep-tracked branches whose latest commit is older than the specified
number of days. Each matching branch goes through the normal per-branch
confirmation prompt.

Signed-off-by: Breno Leitao <leitao@debian.org>
---
 src/b4/command.py |  2 ++
 src/b4/ez.py      | 44 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 46 insertions(+)

diff --git a/src/b4/command.py b/src/b4/command.py
index ca7f238..2d263de 100644
--- a/src/b4/command.py
+++ b/src/b4/command.py
@@ -401,6 +401,8 @@ def setup_parser() -> argparse.ArgumentParser:
                        help='Show series info in a format that can be passed to other commands.')
     spp_g.add_argument('--cleanup', metavar='BRANCHNAME', nargs='*',
                        help='Archive and remove prep-tracked branches and all associated sent/ tags')
+    sp_prep.add_argument('--cleanup-older-than', metavar='DAYS', type=int, default=None,
+                         help='Archive prep-tracked branches older than DAYS days')
 
     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',
diff --git a/src/b4/ez.py b/src/b4/ez.py
index b7afab5..70b9f97 100644
--- a/src/b4/ez.py
+++ b/src/b4/ez.py
@@ -2681,6 +2681,47 @@ def _cleanup_branch(branch: str) -> None:
     logger.info('Wrote: %s', tarpath)
 
 
+def _get_branch_latest_commit_age_days(branch: str) -> Optional[int]:
+    """Return the age in days of the latest commit on *branch*, or None on error."""
+    lines = b4.git_get_command_lines(None, ['log', '-1', '--format=%ct', branch])
+    if not lines:
+        return None
+    try:
+        commit_ts = int(lines[0])
+    except ValueError:
+        return None
+    age_days = (time.time() - commit_ts) / 86400
+    return int(age_days)
+
+
+def cleanup_older_than(older_than_days: int) -> None:
+    """Find prep-tracked branches whose latest commit exceeds *older_than_days* and clean them up.
+
+    The currently checked-out branch is always skipped.  Each matching
+    branch is passed to ``_cleanup_branch``, which prompts the user for
+    confirmation before archiving and deleting it.
+    """
+    mybranches = get_prep_managed_branches(None)
+    if not mybranches:
+        logger.info('No b4-tracked branches found')
+        sys.exit(0)
+
+    curbranch = b4.git_get_current_branch()
+    old_branches = []
+    for branch in mybranches:
+        if branch == curbranch:
+            logger.debug('Skipping currently checked out branch: %s', branch)
+            continue
+        age = _get_branch_latest_commit_age_days(branch)
+        if age is not None and age > older_than_days:
+            old_branches.append(branch)
+    if not old_branches:
+        logger.info('No branches older than %d days found', older_than_days)
+        return
+    for branch in old_branches:
+        _cleanup_branch(branch)
+
+
 def cleanup(branches: List[str]) -> None:
     if not branches:
         # Show all b4-tracked branches
@@ -3089,6 +3130,9 @@ def cmd_prep(cmdargs: argparse.Namespace) -> None:
     if cmdargs.show_info:
         return show_info(cmdargs.show_info)
 
+    if cmdargs.cleanup_older_than is not None:
+        return cleanup_older_than(cmdargs.cleanup_older_than)
+
     if cmdargs.cleanup is not None:
         return cleanup(cmdargs.cleanup)
 

---
base-commit: bc4c73c5333d7d88428e4ce970facd48d6b8b107
change-id: 20260323-cleanup-4dea45140971

Best regards,
--  
Breno Leitao <leitao@debian.org>


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH b4] prep: add --cleanup-older-than option to clean up stale branches
  2026-03-23 11:58 [PATCH b4] prep: add --cleanup-older-than option to clean up stale branches Breno Leitao
@ 2026-08-03 19:07 ` Konstantin Ryabitsev
  2026-08-04  9:26   ` Breno Leitao
  0 siblings, 1 reply; 3+ messages in thread
From: Konstantin Ryabitsev @ 2026-08-03 19:07 UTC (permalink / raw)
  To: Breno Leitao; +Cc: Kernel.org Tools, Konstantin Ryabitsev, kernel-team

> B4 is so useful that I have too many branches now, create a way to
> easily remove old branches.
> 
> Add a new --cleanup-older-than DAYS option that archives and removes
> prep-tracked branches whose latest commit is older than the specified
> number of days. Each matching branch goes through the normal per-branch
> confirmation prompt.

Sorry this sat so long -- I missed it initially and just caught up to it
now going over old series submissions. It needs rebasing, but I don't
see why not to take it.

> diff --git a/src/b4/command.py b/src/b4/command.py
> index ca7f238..2d263de 100644
> --- a/src/b4/command.py
> +++ b/src/b4/command.py
> @@ -401,6 +401,8 @@ def setup_parser() -> argparse.ArgumentParser:
>                         help='Show series info in a format that can be passed to other commands.')
>      spp_g.add_argument('--cleanup', metavar='BRANCHNAME', nargs='*',
>                         help='Archive and remove prep-tracked branches and all associated sent/ tags')
> +    sp_prep.add_argument('--cleanup-older-than', metavar='DAYS', type=int, default=None,
> +                         help='Archive prep-tracked branches older than DAYS days')
>  
>      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',

Suggestion: add it to `spp_g` instead of straight to sp_prep
(`spp_g.add_argument('--cleanup-older-than', ...)`), right next to
`--cleanup`.

Minor nit: the help text says "Archive prep-tracked branches older than
DAYS days", but the operation also removes the branch (like
`--cleanup`'s help text, which says "Archive and remove..."). Worth
aligning the wording so it's not surprising that the branch is deleted,
not just archived.

-- 
Konstantin Ryabitsev <konstantin@linuxfoundation.org>

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH b4] prep: add --cleanup-older-than option to clean up stale branches
  2026-08-03 19:07 ` Konstantin Ryabitsev
@ 2026-08-04  9:26   ` Breno Leitao
  0 siblings, 0 replies; 3+ messages in thread
From: Breno Leitao @ 2026-08-04  9:26 UTC (permalink / raw)
  To: Konstantin Ryabitsev; +Cc: Kernel.org Tools, kernel-team

On Mon, Aug 03, 2026 at 07:07:24PM +0000, Konstantin Ryabitsev wrote:
> > B4 is so useful that I have too many branches now, create a way to
> > easily remove old branches.
> > 
> > Add a new --cleanup-older-than DAYS option that archives and removes
> > prep-tracked branches whose latest commit is older than the specified
> > number of days. Each matching branch goes through the normal per-branch
> > confirmation prompt.
> 
> Sorry this sat so long -- I missed it initially and just caught up to it
> now going over old series submissions. It needs rebasing, but I don't
> see why not to take it.

No problem, thanks for getting to it. I am using this on my b4, and this
has proved valuable to cleanup old branches. 

I will rebase and resend.

> > diff --git a/src/b4/command.py b/src/b4/command.py
> > index ca7f238..2d263de 100644
> > --- a/src/b4/command.py
> > +++ b/src/b4/command.py
> > @@ -401,6 +401,8 @@ def setup_parser() -> argparse.ArgumentParser:
> >                         help='Show series info in a format that can be passed to other commands.')
> >      spp_g.add_argument('--cleanup', metavar='BRANCHNAME', nargs='*',
> >                         help='Archive and remove prep-tracked branches and all associated sent/ tags')
> > +    sp_prep.add_argument('--cleanup-older-than', metavar='DAYS', type=int, default=None,
> > +                         help='Archive prep-tracked branches older than DAYS days')
> >  
> >      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',
> 
> Suggestion: add it to `spp_g` instead of straight to sp_prep
> (`spp_g.add_argument('--cleanup-older-than', ...)`), right next to
> `--cleanup`.


Will do. It turns out not to be purely cosmetic: because the option sits
outside the mutually exclusive group and cmd_prep dispatches it before
--cleanup, argparse accepts

    b4 prep --cleanup b4/foo --cleanup-older-than 30
    b4 prep -n newtopic --cleanup-older-than 30

and silently ignores --cleanup / -n in both cases. Moving it into spp_g
makes argparse reject them outright.

> Minor nit: the help text says "Archive prep-tracked branches older than
> DAYS days", but the operation also removes the branch (like
> `--cleanup`'s help text, which says "Archive and remove..."). Worth
> aligning the wording so it's not surprising that the branch is deleted,
> not just archived.

Agreed, I'll align the wording with --cleanup's "Archive and remove...".


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-08-04  9:27 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-23 11:58 [PATCH b4] prep: add --cleanup-older-than option to clean up stale branches Breno Leitao
2026-08-03 19:07 ` Konstantin Ryabitsev
2026-08-04  9:26   ` Breno Leitao

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.