Skip to content

Instantly share code, notes, and snippets.

@Ovid
Last active November 21, 2016 08:48
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 Ovid/0893b1f6acbb5dcc7c0805a17e7424fb to your computer and use it in GitHub Desktop.
Save Ovid/0893b1f6acbb5dcc7c0805a17e7424fb to your computer and use it in GitHub Desktop.
A little Perl script to make it easier to track down git branches
#!/usr/bin/env perl
use 5.24.0;
use warnings;
use Term::ANSIColor;
chomp( my @branches
= qx/git show -s --pretty='%cn|%ci %D' \$(git rev-parse --branches --remotes)/
);
my %branches_for;
for my $branch (@branches) {
my ( $author, $branch_info ) = split /\|/ => $branch, 2;
push @{ $branches_for{$author} //= [] } => $branch_info;
}
my @authors = sort keys %branches_for;
my $author;
do {
my $current = 1;
foreach (@authors) {
my $num_branches = @{ $branches_for{$_} };
if ( 1 == $num_branches ) {
say "[$current] $_$num_branches branch";
}
else {
say "[$current] $_$num_branches branches";
}
$current++;
}
$current--;
print "Choose an author ('q' to quit): ";
chomp( my $response = <STDIN> );
if ( $response =~ /^([0-9]+)$/ ) {
if ( $response < 1 || $response > $current ) {
say colored(['bright_red on_black'], "Response '$response' is out of range");
}
else {
$author = $authors[ $response - 1 ];
}
}
elsif ( $response =~ /^\s*[qQ]/ ) {
exit;
}
else {
say colored(['bright_red on_black'], "Please enter an integer from 1 to $current");
}
} while not $author;
my $branches = $branches_for{$author};
say "\nShowing branches for: $author\n";
# due to iso date format, we sort on dates for free
foreach my $branch (sort @$branches) {
say $branch;
}
__END__
=head1 NAME
git-show-authors.pl
=head1 SYNOPSIS
perl bin/git-show-authors
=head1 DESCRIPTION
Prints a list of branch authors and prompts you to choose one. Once chosen,
displays a list of all branches believed to be pushed by them, along with the
date. This should allow you to easily track down your remote branches and
remove them after they've been merged.
The syntax for deleting a remote branch is:
git push origin :branchname
Note that there is a space after C<origin>. This does not delete your local copy.
See also: http://gitready.com/beginner/2009/02/02/push-and-delete-branches.html
=head1 BRANCH DETECTIONS
Currently we guess that the name on the last commit is the person responsible
for a given branch. It's up to them to verify that a branch may be safely
deleted.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment