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' " . |
Akron | 9dc12c4 | 2021-07-01 09:57:23 +0200 | [diff] [blame^] | 56 | "--no-merges " . |
Akron | e581315 | 2021-02-23 10:40:44 +0100 | [diff] [blame] | 57 | "--max-parents=1"; |
| 58 | |
| 59 | my $sum = 0; |
| 60 | my @components = (); |
| 61 | |
| 62 | my $file = path(path(__FILE__)->dirname, 'count_commits.conf'); |
| 63 | my @lines = split "\n", $file->slurp; |
| 64 | |
| 65 | if ($lines[0] =~ /^\[([^\]]+?)\]$/) { |
| 66 | $user ||= $1; |
| 67 | shift @lines; |
| 68 | }; |
| 69 | |
| 70 | unless ($user) { |
| 71 | die 'No user defined'; |
| 72 | }; |
| 73 | |
| 74 | say "\nCommits from 01 $from $year - 31 $to $year by $user"; |
| 75 | say "------"; |
| 76 | |
| 77 | foreach (@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 | |
| 94 | say '------'; |
| 95 | say "Commits: $sum"; |
| 96 | say "In: " . join(', ', map { $_->[0] } sort { $b->[1] <=> $a->[1] } @components),"\n"; |
| 97 | |
| 98 | exit(0); |
| 99 | |
| 100 | =pod |
| 101 | |
| 102 | This script will count and sum up all commits |
| 103 | in all repositories listed in the configured repositories |
| 104 | for a certain user in the current branches. |
| 105 | |
| 106 | The configuration file is expected in the same folder as the script |
| 107 | and named C<count_commits.conf>. |
| 108 | |
| 109 | It has the following structure: |
| 110 | |
| 111 | [Username] |
| 112 | /path/to/my/repo1 |
| 113 | /path/to/my/repo2 |
| 114 | ... |
| 115 | |
| 116 | Two different time ranges are supported: |
| 117 | |
| 118 | Per default the last finished month |
| 119 | is listed. With the parameter C<--quarter>, the last quarter are listed. |
| 120 | |
| 121 | With the parameter C<--current> the current month or quarter is taken into account. |
| 122 | |
| 123 | The username can be overwritten with the C<--user> parameter. |
| 124 | |
| 125 | =cut |