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=%aN%x00%ad%x00%s', @{$options->{cmdline}};
67 die "failed to get revs" unless $fd;
70 my ($author, $date, $sub) = split '\0', $_, 3;
72 push @commits, { date => str2time($date), author => $author };
77 @commits = sort { $a->{date} cmp $b->{date} } @commits;
79 print "...done, " . @commits . " commits.\n";
81 die "No commits!" if (@commits < 1);
83 my $gap = $commits[$#commits]->{date} - $commits[0]->{date};
86 if (defined $options->{step}) {
87 $step = $options->{step};
89 $step = $gap > $steps{yearly} ? 'monthly' :
90 $gap > $steps{monthly} ? 'weekly' :
91 $gap > $steps{weekly}/2 ? 'daily' :
94 $options->{step} = $step;
97 # truncate each commit (date) to the wanted step
98 # e.g. if the step is 'monthly', then commit YYYY/MM/DD
101 for my $commit (@commits) {
102 my $date = $commit->{date};
104 if (exists $trunc{$step}) {
105 # LOL -- no, seriously, is there a smarter way to do this?
106 $key = str2time(strftime($trunc{$step}, localtime($date)));
107 } elsif ($step eq 'weekly') {
108 # horrible special case: do it daily, and then subtract
109 # as many days as necessary to go back to the previous sunday
110 # TODO config week start
111 $key = str2time(strftime($trunc{daily}, localtime($date)));
112 my $wd = strftime("%w", localtime($date));
113 $key -= $wd*$steps{daily};
115 $key = $date - ($date % $step);
117 if (exists $dataset{$key}) {
124 # fill missing steps and find max
125 my @keys = sort keys %dataset;
127 # ensure numeric step, taking into account
128 # that our numerical steps are approximate
130 if (exists $steps{$step}) {
131 $step = $steps{$step};
132 $tolerance = $step/2;
136 my $key = shift @keys;
138 $max = $dataset{$key} if $max < $dataset{$key};
139 my $next_step = $key + $step;
140 my $next = shift @keys;
142 while (abs($next - $next_step) > $tolerance) {
143 # next step is too far away
144 $dataset{$next_step} = 0;
150 $options->{max} = $max;
154 # functions to plot the datasets.
155 # each function can be called with either one or two parameters.
156 # when called with two parameters, the first is assumed to be the dataset, and the second the options
157 # (array and hash ref respectively).
158 # when called with a single parameter, it is assumed to be an options hash ref, and the dataset is
159 # created by calling gather_data with the passed options.
162 # TODO needs a lot of customization
163 sub google_chart($;$) {
166 if (! defined $options) {
168 $dataset = gather_data($options);
171 my $height=$options->{chart_height};
172 my $max = $options->{max};
174 my @keys = sort keys %$dataset;
176 my $to = $keys[@keys-1];
179 while (my $key = shift @keys) {
180 push @data, $dataset->{$key};
183 my $width=ceil(4*$height/3);
185 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";
187 my $launch = sprintf $url, join(",",@data);
189 # `git web--browse "$launch"`
193 sub gnuplot_chart($;$) {
196 if (! defined $options) {
198 $dataset = gather_data($options);
201 my @keys = sort keys %$dataset;
202 my $step=$options->{step};
204 my $to = $keys[$#keys];
207 while (my $key = shift @keys) {
208 $data .= "$key $dataset->{$key}\n";
210 my $max = $options->{max} + 1;
212 # add a fake datapoint lest to prevent the last
213 # datum from becoming invibile with style data steps
214 if (exists $steps{$step}) {
215 $to += $steps{$step};
221 # TODO allow customization
222 # in particular, detect (lack of) display and set term to dumb accordingly
223 my $termcmd = $options->{gnuplot_term};
225 my $plotsetup = $options->{gnuplot_setup};
226 $plotsetup .= "\nset yrange [0:$max]\n";
227 $plotsetup .= "set xrange ['$from':'$to']\n";
228 my ($formatx, $ticks);
229 if ($to - $from > $steps{yearly}) {
231 $ticks = $steps{yearly};
232 } elsif ($to - $from > $steps{monthly}) {
233 $formatx = "%b\\n%Y";
234 $ticks = $steps{monthly};
235 } elsif ($to - $from > $steps{weekly}/2) {
236 $formatx = "%d\\n%b";
237 $ticks = $steps{daily};
239 $formatx = "%H\\n%d";
240 $ticks = $steps{hourly};
242 $plotsetup .= "set format x \"$formatx\"\n";
243 $plotsetup .= "set xtics $ticks\n";
244 my $plotstyle = $options->{gnuplot_style};
245 my $plotoptions = $options->{gnuplot_plotwith};
247 open my $gp, "|gnuplot -persist";
249 my $gp_script = <<GPCMD
255 plot "-" using 1:2 $plotoptions
260 print STDOUT $gp_script;
261 print $gp $gp_script;
268 # charting/plotting options
269 plotter => \&gnuplot_chart,
272 gnuplot_setup => "set nokey",
273 gnuplot_style => 'set style data steps',
274 gnuplot_plotwith => '',
280 if (exists $steps{$key}) {
281 $options{step} = $key;
284 die "this can't happen ($key)" unless $key eq 'step';
286 if ($step =~/^\d+$/) {
287 $options{step} = 0 + $step;
290 if (exists $steps{$step}) {
291 $options{step} = $step;
294 die "unknown step $step";
299 # read our options first
300 Getopt::Long::Configure('pass_through');
302 'hourly' => \&parse_step,
303 'daily' => \&parse_step,
304 'weekly' => \&parse_step,
305 'monthly' => \&parse_step,
306 'yearly' => \&parse_step,
307 'step=s' => sub { parse_step(@_) },
308 'chart-height=i' => sub { $options{chart_height} = $_[1]},
309 google => sub { $options{plotter} = \&google_chart },
310 gnuplot => sub { $options{plotter} = \&gnuplot_chart },
319 # if anything was left, check for log options
321 $options{cmdline} = \@ARGV;
324 $options{plotter}->(\%options);