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