public
Last active

'git spark -days 30 Batman' gives you a sparkline graph of Batman's commit history over the last 30 days

  • Download Gist
git-spark
Perl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
#!/usr/bin/env perl
# Inspired by @trisweb:
# http://github.com/holman/spark/wiki/Wicked-Cool-Usage
 
use strict;
use warnings;
 
use Getopt::Long::Descriptive;
use Encode qw/encode decode/;
use DateTime;
use List::AllUtils qw/max sum/;
use Math::Round qw/round/;
 
my ($option, $usage) = describe_options(
'usage: git spark %o [AUTHOR]',
['hours|o=i' => 'Commits from the last x hours'],
['days|d=i' => 'Commits from the last x days'],
['weeks|w=i' => 'Commits from the last x weeks'],
['months|m=i' => 'Commits from the last x months'],
['years|y=i' => 'Commits from the last x years'],
['scale|s=i' => 'Set the max value of the graph. Use this option to compare this graph with other graphs.'],
['help|h' => 'Show this message'],
);
 
my $author = $ARGV[0] || $ENV{USER};
my $scale = $option->{scale} || 0;
 
print($usage), exit 0 if $option->{help};
print($usage), exit 0
if (!$option->{hours} &&
!$option->{days} &&
!$option->{weeks} &&
!$option->{months} &&
!$option->{years});
 
foreach my $key (qw/hours days weeks months years/) {
spark($option->{$key}, $key, $author, $scale) if $option->{$key};
}
 
sub spark {
my ($value, $units, $author, $scale) = @_;
 
my @data = countCommits(@_);
my $total = sum @data;
my $avg = round($total / $#data);
my $max = max @data;
 
my $sparkArgs = $scale
? join(" ", $scale, 0, @data)
: join(" ", @data);
 
print "Commits by $author over the last $value $units\n";
print "total: $total avg: $avg max: $max\n";
print join(" ", @data) . "\n";
my $out = `spark $sparkArgs`;
my $utf8out = decode('utf8', $out);
print encode('utf8', substr($utf8out, 2));
}
 
sub countCommits {
my ($value, $units, $author) = @_;
 
my @commits;
foreach my $i (0..($value - 1)) {
my $cmd = "git log " .
"--author=${author} " .
"--before='${i} ${units}' " .
"--after='" . ($i + 1) . " ${units}' " .
"--format=oneline | wc -l";
my $count = `$cmd`;
chomp($count);
push @commits, $count;
}
 
return reverse @commits;
}

I decided to make put this in a real repo. If you have any suggestions or pull requests please do it over here: https://github.com/kablamo/git-spark

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.