4 # * default to something else than "the whole history", probably
5 # * if no commits are specified and a commit grouping step is specified,
6 # limit commits proportionally (e.g. --daily => one week worth of commits)
7 # * allow the steps to be counted from the most recent rather than from the
12 use POSIX qw(ceil strftime);
16 my $SECS_PER_DAY = 24*3600;
20 daily => $SECS_PER_DAY,
21 weekly => 7*$SECS_PER_DAY,
22 monthly => 30*$SECS_PER_DAY,
23 yearly => 12*30*$SECS_PER_DAY,
27 hourly => "%Y-%m-%d %H:00",
28 daily => "%Y-%m-%d 00:00",
29 monthly => "%Y-%m-01 00:00",
30 yearly => "%Y-01-01 00:00",
36 Usage: git chart [options...] [log specs...]
38 Creates a chart plotting the distribution over time of the commits
39 specified as <log specs>. By default, commits are grouped by hour, day,
40 week, month or year depending on the time spanned, but this can be
41 controlled by the user.
44 --hourly, --daily, --weekly, --monthly, --yearly:
45 force a specific commit grouping
47 force commits to be grouped each <integer> seconds
49 produce a chart with gnuplot (default)
51 produce a chart with Google Charts
52 --chart-height=<integer>:
53 set the Google Charts height; the width is set
54 to a 4:3 ratio (default: 100)
62 print "Gathering data ...\n";
66 open my $fd, '-|', qw(git log --date=local), '--pretty=%ad,%s', @{$options->{cmdline}};
67 die "failed to get revs" unless $fd;
70 my ($date, $sub) = split ',', $_, 2;
72 push @commits, str2time($date);
77 @commits = sort @commits;
79 print "...done, " . @commits . " commits.\n";
81 my $gap = $commits[$#commits] - $commits[0];
84 if (defined $options->{step}) {
85 $step = $options->{step};
87 $step = $gap > $steps{yearly} ? 'monthly' :
88 $gap > $steps{monthly} ? 'weekly' :
89 $gap > $steps{weekly}/2 ? 'daily' :
92 $options->{step} = $step;
95 # truncate each commit (date) to the wanted step
96 # e.g. if the step is 'monthly', then commit YYYY/MM/DD
99 for my $commit (@commits) {
101 if (exists $trunc{$step}) {
102 # LOL -- no, seriously, is there a smarter way to do this?
103 $key = str2time(strftime($trunc{$step}, localtime($commit)));
105 $key = $commit - ($commit % $step);
107 if (exists $dataset{$key}) {
114 # fill missing steps and find max
115 my @keys = sort keys %dataset;
117 # ensure numeric step, taking into account
118 # that our numerical steps are approximate
120 if (exists $steps{$step}) {
121 $step = $steps{$step};
122 $tolerance = $step/2;
126 my $key = shift @keys;
128 $max = $dataset{$key} if $max < $dataset{$key};
129 my $next_step = $key + $step;
130 my $next = shift @keys;
132 while (abs($next - $next_step) > $tolerance) {
133 # next step is too far away
134 $dataset{$next_step} = 0;
140 $options->{max} = $max;
144 # functions to plot the datasets.
145 # each function can be called with either one or two parameters.
146 # when called with two parameters, the first is assumed to be the dataset, and the second the options
147 # (array and hash ref respectively).
148 # when called with a single parameter, it is assumed to be an options hash ref, and the dataset is
149 # created by calling gather_data with the passed options.
152 # TODO needs a lot of customization
153 sub google_chart($;$) {
156 if (! defined $options) {
158 $dataset = gather_data($options);
161 my $height=$options->{chart_height};
162 my $max = $options->{max};
164 my @keys = sort keys %$dataset;
166 my $to = $keys[@keys-1];
169 while (my $key = shift @keys) {
170 push @data, $dataset->{$key};
173 my $width=ceil(4*$height/3);
175 my $url="https://chart.googleapis.com/chart?chs=${width}x${height}&cht=bvg&chd=t:%s&chds=0,$max&chbh=a&chxt=y&chxr=0,0,$max";
177 my $launch = sprintf $url, join(",",@data);
179 # `git web--browse "$launch"`
183 sub gnuplot_chart($;$) {
186 if (! defined $options) {
188 $dataset = gather_data($options);
191 my @keys = sort keys %$dataset;
192 my $step=$options->{step};
194 my $to = $keys[$#keys];
197 while (my $key = shift @keys) {
198 $data .= "$key $dataset->{$key}\n";
200 my $max = $options->{max};
202 # add a fake datapoint lest to prevent the last
203 # datum from becoming invibile with style data steps
204 if (exists $steps{$step}) {
205 $to += $steps{$step};
211 # TODO allow customization
212 # in particular, detect (lack of) display and set term to dumb accordingly
213 my $termcmd = $options->{gnuplot_term};
215 my $plotsetup = $options->{gnuplot_setup};
216 $plotsetup .= "\nset yrange [0:$max]\n";
217 $plotsetup .= "set xrange ['$from':'$to']\n";
218 my ($formatx, $ticks);
219 if ($to - $from > $steps{yearly}) {
221 $ticks = $steps{yearly};
222 } elsif ($to - $from > $steps{monthly}) {
223 $formatx = "%b\\n%Y";
224 $ticks = $steps{monthly};
225 } elsif ($to - $from > 2*$steps{daily}) {
226 $formatx = "%d\\n%b";
227 $ticks = $steps{daily};
229 $formatx = "%H\\n%d";
230 $ticks = $steps{hourly};
232 $plotsetup .= "set format x \"$formatx\"\n";
233 $plotsetup .= "set xtics $ticks\n";
234 my $plotstyle = $options->{gnuplot_style};
235 my $plotoptions = $options->{gnuplot_plotwith};
237 open my $gp, "|gnuplot -persist";
239 my $gp_script = <<GPCMD
245 plot "-" using 1:2 $plotoptions
250 print STDOUT $gp_script;
251 print $gp $gp_script;
258 # charting/plotting options
259 plotter => \&gnuplot_chart,
262 gnuplot_setup => "set nokey",
263 gnuplot_style => 'set style data steps',
264 gnuplot_plotwith => '',
270 if (exists $steps{$key}) {
271 $options{step} = $key;
274 die "this can't happen ($key)" unless $key eq 'step';
276 if ($step =~/^\d+$/) {
277 $options{step} = 0 + $step;
280 if (exists $steps{$step}) {
281 $options{step} = $step;
284 die "unknown step $step";
289 # read our options first
290 Getopt::Long::Configure('pass_through');
292 'hourly' => \&parse_step,
293 'daily' => \&parse_step,
294 'weekly' => \&parse_step,
295 'monthly' => \&parse_step,
296 'yearly' => \&parse_step,
297 'step=s' => sub { parse_step(@_) },
298 'chart-height=i' => sub { $options{chart_height} = $_[1]},
299 google => sub { $options{plotter} = \&google_chart },
300 gnuplot => sub { $options{plotter} = \&gnuplot_chart },
309 # if anything was left, check for log options
311 $options{cmdline} = \@ARGV;
314 $options{plotter}->(\%options);