blob: d765c713a95663b7cf3fcdf824a6efc34e9fecdf [file] [log] [blame]
Akrone5813152021-02-23 10:40:44 +01001#!/usr/bin/env perl
2use strict;
3use warnings;
4use Mojo::File qw'path';
Akronebace122021-07-12 17:46:29 +02005use Mojo::Util qw!extract_usage!;
Akrone5813152021-02-23 10:40:44 +01006use feature 'say';
7use DateTime;
8use Getopt::Long;
9
10GetOptions (
11 "help|h" => sub {
Akronebace122021-07-12 17:46:29 +020012 say "--quarter (show statistics for a quarter instead of a month)";
13 say "--current (show statistics for the current instead of the last unit)";
14 say "--user []";
15 say extract_usage;
Akrone5813152021-02-23 10:40:44 +010016 exit(0);
17 },
18 "quarter|q" => \(my $quarter),
19 "current|c" => \(my $current),
20 "user|u=s" => \(my $user)
21);
22
23# Get the relevant timeslice
24my $dt = DateTime->now;
25if (!$current && !$quarter) {
26 $dt->subtract(months => 1);
27}
28elsif ($quarter) {
29 if ($dt->month <= 3) {
30 $dt->set_month(1);
31 }
32 elsif ($dt->month <= 6) {
33 $dt->set_month(4);
34 }
35 elsif ($dt->month <= 9) {
36 $dt->set_month(7);
37 }
38 else {
39 $dt->set_month(10);
40 };
41
42 if (!$current) {
43 $dt->subtract(months => 3);
44 };
45};
46
47my $year = $dt->year;
48my $from = $dt->month_abbr;
49
50if ($quarter) {
51 $dt->add(months => 2);
52};
53
54my $to = $dt->month_abbr;
55
56my $cmd = "git shortlog -sne " .
57 "--since='01 $from $year' " .
58 "--before='31 $to $year' " .
Akron9dc12c42021-07-01 09:57:23 +020059 "--no-merges " .
Akrone5813152021-02-23 10:40:44 +010060 "--max-parents=1";
61
62my $sum = 0;
63my @components = ();
64
65my $file = path(path(__FILE__)->dirname, 'count_commits.conf');
66my @lines = split "\n", $file->slurp;
67
68if ($lines[0] =~ /^\[([^\]]+?)\]$/) {
69 $user ||= $1;
70 shift @lines;
71};
72
73unless ($user) {
74 die 'No user defined';
75};
76
77say "\nCommits from 01 $from $year - 31 $to $year by $user";
78say "------";
79
80foreach (@lines) {
81 chomp $_;
82 my $dir = path($_);
83 my $dir_str = $dir->to_string;
84 if (-d $dir_str) {
85 chdir $dir_str;
86 my $out = `$cmd`;
87 if ($out =~ /\s+(\d+)\s+$user/) {
88 $sum += $1;
89 say $dir->basename, ": $1";
90 push @components, [ $dir->basename => $1 ];
91 };
92 } else {
93 warn "$dir does not exist\n";
94 }
95};
96
97say '------';
98say "Commits: $sum";
99say "In: " . join(', ', map { $_->[0] } sort { $b->[1] <=> $a->[1] } @components),"\n";
100
101exit(0);
102
Akronebace122021-07-12 17:46:29 +0200103__END__
104
Akrone5813152021-02-23 10:40:44 +0100105=pod
106
Akronebace122021-07-12 17:46:29 +0200107=head1 SYNOPSIS
108
109 Usage: count_commits --quarter --current --user Akron
110
111=head1 DESCRIPTION
112
Akrone5813152021-02-23 10:40:44 +0100113This script will count and sum up all commits
114in all repositories listed in the configured repositories
115for a certain user in the current branches.
116
117The configuration file is expected in the same folder as the script
118and named C<count_commits.conf>.
119
120It has the following structure:
121
122 [Username]
123 /path/to/my/repo1
124 /path/to/my/repo2
125 ...
126
Akronebace122021-07-12 17:46:29 +0200127The username has to be in the first line and written in square brackets.
128
Akrone5813152021-02-23 10:40:44 +0100129Two different time ranges are supported:
130
131Per default the last finished month
132is listed. With the parameter C<--quarter>, the last quarter are listed.
133
134With the parameter C<--current> the current month or quarter is taken into account.
135
136The username can be overwritten with the C<--user> parameter.
137
138=cut