blob: e700424ab9b7da4e978538bd16015f330ca33259 [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' " .
Akron9dc12c42021-07-01 09:57:23 +020056 "--no-merges " .
Akrone5813152021-02-23 10:40:44 +010057 "--max-parents=1";
58
59my $sum = 0;
60my @components = ();
61
62my $file = path(path(__FILE__)->dirname, 'count_commits.conf');
63my @lines = split "\n", $file->slurp;
64
65if ($lines[0] =~ /^\[([^\]]+?)\]$/) {
66 $user ||= $1;
67 shift @lines;
68};
69
70unless ($user) {
71 die 'No user defined';
72};
73
74say "\nCommits from 01 $from $year - 31 $to $year by $user";
75say "------";
76
77foreach (@lines) {
78 chomp $_;
79 my $dir = path($_);
80 my $dir_str = $dir->to_string;
81 if (-d $dir_str) {
82 chdir $dir_str;
83 my $out = `$cmd`;
84 if ($out =~ /\s+(\d+)\s+$user/) {
85 $sum += $1;
86 say $dir->basename, ": $1";
87 push @components, [ $dir->basename => $1 ];
88 };
89 } else {
90 warn "$dir does not exist\n";
91 }
92};
93
94say '------';
95say "Commits: $sum";
96say "In: " . join(', ', map { $_->[0] } sort { $b->[1] <=> $a->[1] } @components),"\n";
97
98exit(0);
99
100=pod
101
102This script will count and sum up all commits
103in all repositories listed in the configured repositories
104for a certain user in the current branches.
105
106The configuration file is expected in the same folder as the script
107and named C<count_commits.conf>.
108
109It has the following structure:
110
111 [Username]
112 /path/to/my/repo1
113 /path/to/my/repo2
114 ...
115
116Two different time ranges are supported:
117
118Per default the last finished month
119is listed. With the parameter C<--quarter>, the last quarter are listed.
120
121With the parameter C<--current> the current month or quarter is taken into account.
122
123The username can be overwritten with the C<--user> parameter.
124
125=cut