Skip to content

Instantly share code, notes, and snippets.

@skaji
Last active October 8, 2020 05:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skaji/5d0cdc9f591e5771ca667ff033478689 to your computer and use it in GitHub Desktop.
Save skaji/5d0cdc9f591e5771ca667ff033478689 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use strict;
use warnings;
use 5.10.1;
use Time::Piece ();
use Time::Seconds ();
sub is_stale {
my ($date, $name) = @_;
state $now = Time::Piece->new;
state $two_month = 60 * Time::Seconds::ONE_DAY;
$date < $now - $two_month && $name =~ m{^refs/heads/};
}
my @branch = `git branch --color=never --all --sort=-authordate --format='%(HEAD)%00%(authordate:short)%00%(refname)%00%(authoremail)'`;
$? == 0 or exit 1;
chomp @branch;
my @stale;
for my $branch (@branch) {
my ($head, $date, $name, $email) = split /\0/, $branch, 4;
$date = Time::Piece->new->localtime->strptime($date, "%Y-%m-%d");
if ($head ne "*" && is_stale $date, $name) {
$name =~ s{^refs/heads/}{};
push @stale, { date => $date->strftime("%Y-%m-%d"), name => $name };
}
}
for my $stale (@stale) {
my @cmd = ("git", "branch", "-D", $stale->{name});
if ($ENV{FORCE}) {
!system @cmd or die;
} else {
print "$stale->{date}: @cmd", "\n";
}
}
if (@stale && !$ENV{FORCE}) {
warn "try again with FORCE=1\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment