blob: 251fe0ac7ebc32589e262d6d47bf00a546e65de9 [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) {
Marc Kupietz62e31322026-04-14 11:42:28 +020085 my $git_dir = path($dir_str, '.git')->to_string;
86 unless (-d $git_dir || -f $git_dir) {
87 warn "$dir is not a git repository\n";
88 next;
89 };
Akrone5813152021-02-23 10:40:44 +010090 chdir $dir_str;
91 my $out = `$cmd`;
92 if ($out =~ /\s+(\d+)\s+$user/) {
93 $sum += $1;
94 say $dir->basename, ": $1";
95 push @components, [ $dir->basename => $1 ];
96 };
97 } else {
98 warn "$dir does not exist\n";
99 }
100};
101
102say '------';
103say "Commits: $sum";
104say "In: " . join(', ', map { $_->[0] } sort { $b->[1] <=> $a->[1] } @components),"\n";
105
106exit(0);
107
Akronebace122021-07-12 17:46:29 +0200108__END__
109
Akrone5813152021-02-23 10:40:44 +0100110=pod
111
Akronebace122021-07-12 17:46:29 +0200112=head1 SYNOPSIS
113
114 Usage: count_commits --quarter --current --user Akron
115
116=head1 DESCRIPTION
117
Akrone5813152021-02-23 10:40:44 +0100118This script will count and sum up all commits
119in all repositories listed in the configured repositories
120for a certain user in the current branches.
121
122The configuration file is expected in the same folder as the script
123and named C<count_commits.conf>.
124
125It has the following structure:
126
127 [Username]
128 /path/to/my/repo1
129 /path/to/my/repo2
130 ...
131
Akronebace122021-07-12 17:46:29 +0200132The username has to be in the first line and written in square brackets.
133
Akrone5813152021-02-23 10:40:44 +0100134Two different time ranges are supported:
135
136Per default the last finished month
137is listed. With the parameter C<--quarter>, the last quarter are listed.
138
139With the parameter C<--current> the current month or quarter is taken into account.
140
141The username can be overwritten with the C<--user> parameter.
142
143=cut