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);
18 my $SECS_PER_DAY = 24*3600;
22 daily => $SECS_PER_DAY,
23 weekly => 7*$SECS_PER_DAY,
24 monthly => 30*$SECS_PER_DAY,
25 yearly => 12*30*$SECS_PER_DAY,
29 hourly => "%Y-%m-%d %H:00",
30 daily => "%Y-%m-%d 00:00",
31 monthly => "%Y-%m-01 00:00",
32 yearly => "%Y-01-01 00:00",
38 Usage: git chart [options...] [log specs...]
40 Creates a chart plotting the distribution over time of the commits
41 specified as <log specs>. By default, commits are grouped by hour, day,
42 week, month or year depending on the time spanned, but this can be
43 controlled by the user.
46 --hourly, --daily, --weekly, --monthly, --yearly:
47 force a specific commit grouping
49 force commits to be grouped each <integer> seconds
51 produce a chart with gnuplot (default)
53 produce a chart with Google Charts
54 --chart-height=<integer>:
55 set the Google Charts height; the width is set
56 to a 4:3 ratio (default: 100)
64 print "Gathering data ...\n";
68 open my $fd, '-|', qw(git log --date=local), '--pretty=%aN%x00%ad%x00%s', @{$options->{cmdline}};
69 die "failed to get revs" unless $fd;
72 my ($author, $date, $sub) = split '\0', $_, 3;
74 push @commits, { date => str2time($date), author => $author };
79 @commits = sort { $a->{date} cmp $b->{date} } @commits;
81 print "...done, " . @commits . " commits.\n";
83 die "No commits!" if (@commits < 1);
85 my $gap = $commits[$#commits]->{date} - $commits[0]->{date};
88 if (defined $options->{step}) {
89 $step = $options->{step};
91 $step = $gap > $steps{yearly} ? 'monthly' :
92 $gap > $steps{monthly} ? 'weekly' :
93 $gap > $steps{weekly}/2 ? 'daily' :
96 $options->{step} = $step;
99 # truncate each commit (date) to the wanted step
100 # e.g. if the step is 'monthly', then commit YYYY/MM/DD
103 for my $commit (@commits) {
104 my $date = $commit->{date};
105 my $author = $commit->{author};
107 if (exists $trunc{$step}) {
108 # LOL -- no, seriously, is there a smarter way to do this?
109 $key = str2time(strftime($trunc{$step}, localtime($date)));
110 } elsif ($step eq 'weekly') {
111 # horrible special case: do it daily, and then subtract
112 # as many days as necessary to go back to the previous sunday
113 # TODO config week start
114 $key = str2time(strftime($trunc{daily}, localtime($date)));
115 my $wd = strftime("%w", localtime($date));
116 $key -= $wd*$steps{daily};
118 $key = $date - ($date % $step);
121 # make sure the value is a hash
122 $dataset{$key} = {_commits => 0} unless exists $dataset{$key};
123 $dataset{_authors} = { $author => 0} unless exists $dataset{_authors};
125 $dataset{$key}{_commits} += 1;
126 $dataset{$key}{$author} += 1;
127 $dataset{_authors}{$author} += 1;
130 # fill missing steps and find max
131 my @keys = sort keys %dataset;
132 pop @keys; # get rid of _authors
134 # ensure numeric step, taking into account
135 # that our numerical steps are approximate
137 if (exists $steps{$step}) {
138 $step = $steps{$step};
139 $tolerance = $step/2;
143 my $key = shift @keys;
145 $max = $dataset{$key}{_commits} if $max < $dataset{$key}{_commits};
146 my $next_step = $key + $step;
147 my $next = shift @keys;
149 while (abs($next - $next_step) > $tolerance) {
150 # next step is too far away
151 $dataset{$next_step} = {_commits => 0};
157 $options->{max} = $max;
161 # functions to plot the datasets.
162 # each function can be called with either one or two parameters.
163 # when called with two parameters, the first is assumed to be the dataset, and the second the options
164 # (array and hash ref respectively).
165 # when called with a single parameter, it is assumed to be an options hash ref, and the dataset is
166 # created by calling gather_data with the passed options.
169 # TODO needs a lot of customization
170 sub google_chart($;$) {
173 if (! defined $options) {
175 $dataset = gather_data($options);
178 my $height=$options->{chart_height};
179 my $max = $options->{max};
181 my @keys = sort keys %$dataset;
182 pop @keys; # get rid of _authors
185 my $to = $keys[@keys-1];
188 while (my $key = shift @keys) {
189 push @data, $dataset->{$key}{_commits};
192 my $width=ceil(4*$height/3);
194 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";
196 my $launch = sprintf $url, join(",",@data);
198 # `git web--browse "$launch"`
202 sub gnuplot_chart($;$) {
205 if (! defined $options) {
207 $dataset = gather_data($options);
210 my $authors = delete $dataset->{_authors};
211 my @authors = sort { $authors->{$b} <=> $authors->{$a} } keys %$authors;
212 $authors = join "\t", map { "\"$_\"" } @authors;
214 my @keys = sort keys %$dataset;
216 my $step=$options->{step};
218 my $to = $keys[$#keys];
220 my $data = "date\tcommits\t$authors\n";
221 while (my $key = shift @keys) {
222 $data .= "$key\t$dataset->{$key}{_commits}";
223 foreach my $author (@authors) {
224 if (exists $dataset->{$key}{$author}) {
225 $data .= "\t$dataset->{$key}{$author}";
232 my $max = $options->{max} + 1;
234 # add a fake datapoint lest to prevent the last
235 # datum from becoming invibile with style data steps
236 if (exists $steps{$step}) {
237 $to += $steps{$step};
242 foreach my $author (@authors) {
247 # TODO allow customization
248 # in particular, detect (lack of) display and set term to dumb accordingly
249 my $termcmd = $options->{gnuplot_term};
251 my $plotsetup = $options->{gnuplot_setup} ||
252 @authors == 1 ? 'set nokey' :
253 'set key outside autotitle columnheader' ;
254 $plotsetup .= "\nset yrange [0:$max]\n";
255 $plotsetup .= "set xrange ['$from':'$to']\n";
256 my ($formatx, $ticks);
257 if ($to - $from > $steps{yearly}) {
259 $ticks = $steps{yearly};
260 } elsif ($to - $from > $steps{monthly}) {
261 $formatx = "%b\\n%Y";
262 $ticks = $steps{monthly};
263 } elsif ($to - $from > $steps{weekly}/2) {
264 $formatx = "%d\\n%b";
265 $ticks = $steps{daily};
267 $formatx = "%H\\n%d";
268 $ticks = $steps{hourly};
270 $plotsetup .= "set format x \"$formatx\"\n";
271 $plotsetup .= "set xtics $ticks\n";
272 my $plotstyle = $options->{gnuplot_style};
273 my $plotoptions = $options->{gnuplot_plotwith};
275 my $datafile = File::Temp->new(
276 TEMPLATE => 'git-chart-XXXXXX',
277 DIR => File::Spec->tmpdir()
280 print $datafile $data;
282 open my $gp, "|gnuplot -persist";
284 my $plotcmd = "plot '$datafile' using 1:2";
286 my $lastcolumn = $#authors + 3;
287 $plotcmd.= " notitle";
288 $plotcmd.= ", for [a=3:$lastcolumn] '' using 1:a";
291 my $gp_script = <<GPCMD
301 print STDOUT $gp_script;
302 print $gp $gp_script;
309 # charting/plotting options
310 plotter => \&gnuplot_chart,
314 gnuplot_style => 'set style data steps',
315 gnuplot_plotwith => '',
321 if (exists $steps{$key}) {
322 $options{step} = $key;
325 die "this can't happen ($key)" unless $key eq 'step';
327 if ($step =~/^\d+$/) {
328 $options{step} = 0 + $step;
331 if (exists $steps{$step}) {
332 $options{step} = $step;
335 die "unknown step $step";
340 # read our options first
341 Getopt::Long::Configure('pass_through');
343 'hourly' => \&parse_step,
344 'daily' => \&parse_step,
345 'weekly' => \&parse_step,
346 'monthly' => \&parse_step,
347 'yearly' => \&parse_step,
348 'step=s' => sub { parse_step(@_) },
349 'chart-height=i' => sub { $options{chart_height} = $_[1]},
350 google => sub { $options{plotter} = \&google_chart },
351 gnuplot => sub { $options{plotter} = \&gnuplot_chart },
360 # if anything was left, check for log options
362 $options{cmdline} = \@ARGV;
365 $options{plotter}->(\%options);