#!/usr/bin/perl # # git-branches - Display branches in activity order akin to gitweb # # -a to display both local and remote branches # -r to display remote branches only # # An optional argument can be specified to show only branches matching # the given string. # # Copyright (C) 2009 Martin K. Petersen # Released under the terms of the GNU General Public License v2. use Getopt::Std; use List::Util qw(max); die "Usage: git-branches [-r | -a] []\n" if getopts('ar') == false; $opts = "-r" if $opt_r; $opts = "-a" if $opt_a; $patt = shift; open GIT, "git branch $opts|" || die "git branch $opts failed\n"; while () { next unless /$patt/; ($b, $rest) = split /\s/, substr($_, 2); $brlist{$b}{current} = substr($_, 0, 2); $branchlen = max($branchlen, length $b); } close GIT; foreach (keys %brlist) { $b = $_; open COMMIT, "git log -1 --pretty=format:'%ct:%h:%cr' $b |" || die "git log failed\n"; while () { ($brlist{$b}{ts}, $brlist{$b}{hash}, $brlist{$b}{when}) = split /:/; } close COMMIT; $hashlen = max($hashlen, length $brlist{$b}{hash}); } exit unless keys %brlist; sub branchsort { $brlist{$b}{ts} <=> $brlist{$a}{ts}; } $fmt = sprintf "%%s%%-%ds %%-%ds %%s\n", $branchlen, $hashlen; printf $fmt, " ", "BRANCH", "COMMIT", "LAST CHANGED"; foreach $b (sort branchsort (keys(%brlist))) { printf $fmt, $brlist{$b}{current}, $b, $brlist{$b}{hash}, $brlist{$b}{when}; }