Akron | e581315 | 2021-02-23 10:40:44 +0100 | [diff] [blame] | 1 | #!/usr/bin/env perl |
| 2 | use strict; |
| 3 | use warnings; |
| 4 | use Mojo::File qw'path'; |
| 5 | use feature 'say'; |
| 6 | use DateTime; |
| 7 | use Getopt::Long; |
| 8 | |
| 9 | GetOptions ( |
| 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 |
| 21 | my $dt = DateTime->now; |
| 22 | if (!$current && !$quarter) { |
| 23 | $dt->subtract(months => 1); |
| 24 | } |
| 25 | elsif ($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 | |
| 44 | my $year = $dt->year; |
| 45 | my $from = $dt->month_abbr; |
| 46 | |
| 47 | if ($quarter) { |
| 48 | $dt->add(months => 2); |
| 49 | }; |
| 50 | |
| 51 | my $to = $dt->month_abbr; |
| 52 | |
| 53 | my $cmd = "git shortlog -sne " . |
| 54 | "--since='01 $from $year' " . |
| 55 | "--before='31 $to $year' " . |
| 56 | "--max-parents=1"; |
| 57 | |
| 58 | my $sum = 0; |
| 59 | my @components = (); |
| 60 | |
| 61 | my $file = path(path(__FILE__)->dirname, 'count_commits.conf'); |
| 62 | my @lines = split "\n", $file->slurp; |
| 63 | |
| 64 | if ($lines[0] =~ /^\[([^\]]+?)\]$/) { |
| 65 | $user ||= $1; |
| 66 | shift @lines; |
| 67 | }; |
| 68 | |
| 69 | unless ($user) { |
| 70 | die 'No user defined'; |
| 71 | }; |
| 72 | |
| 73 | say "\nCommits from 01 $from $year - 31 $to $year by $user"; |
| 74 | say "------"; |
| 75 | |
| 76 | foreach (@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 | |
| 93 | say '------'; |
| 94 | say "Commits: $sum"; |
| 95 | say "In: " . join(', ', map { $_->[0] } sort { $b->[1] <=> $a->[1] } @components),"\n"; |
| 96 | |
| 97 | exit(0); |
| 98 | |
| 99 | =pod |
| 100 | |
| 101 | This script will count and sum up all commits |
| 102 | in all repositories listed in the configured repositories |
| 103 | for a certain user in the current branches. |
| 104 | |
| 105 | The configuration file is expected in the same folder as the script |
| 106 | and named C<count_commits.conf>. |
| 107 | |
| 108 | It has the following structure: |
| 109 | |
| 110 | [Username] |
| 111 | /path/to/my/repo1 |
| 112 | /path/to/my/repo2 |
| 113 | ... |
| 114 | |
| 115 | Two different time ranges are supported: |
| 116 | |
| 117 | Per default the last finished month |
| 118 | is listed. With the parameter C<--quarter>, the last quarter are listed. |
| 119 | |
| 120 | With the parameter C<--current> the current month or quarter is taken into account. |
| 121 | |
| 122 | The username can be overwritten with the C<--user> parameter. |
| 123 | |
| 124 | =cut |