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'; |
Akron | ebace12 | 2021-07-12 17:46:29 +0200 | [diff] [blame] | 5 | use Mojo::Util qw!extract_usage!; |
Akron | e581315 | 2021-02-23 10:40:44 +0100 | [diff] [blame] | 6 | use feature 'say'; |
| 7 | use DateTime; |
| 8 | use Getopt::Long; |
| 9 | |
| 10 | GetOptions ( |
| 11 | "help|h" => sub { |
Akron | ebace12 | 2021-07-12 17:46:29 +0200 | [diff] [blame] | 12 | 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; |
Akron | e581315 | 2021-02-23 10:40:44 +0100 | [diff] [blame] | 16 | 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 |
| 24 | my $dt = DateTime->now; |
| 25 | if (!$current && !$quarter) { |
| 26 | $dt->subtract(months => 1); |
| 27 | } |
| 28 | elsif ($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 | |
| 47 | my $year = $dt->year; |
| 48 | my $from = $dt->month_abbr; |
| 49 | |
| 50 | if ($quarter) { |
| 51 | $dt->add(months => 2); |
| 52 | }; |
| 53 | |
| 54 | my $to = $dt->month_abbr; |
| 55 | |
| 56 | my $cmd = "git shortlog -sne " . |
| 57 | "--since='01 $from $year' " . |
| 58 | "--before='31 $to $year' " . |
Akron | 9dc12c4 | 2021-07-01 09:57:23 +0200 | [diff] [blame] | 59 | "--no-merges " . |
Akron | e581315 | 2021-02-23 10:40:44 +0100 | [diff] [blame] | 60 | "--max-parents=1"; |
| 61 | |
| 62 | my $sum = 0; |
| 63 | my @components = (); |
| 64 | |
| 65 | my $file = path(path(__FILE__)->dirname, 'count_commits.conf'); |
| 66 | my @lines = split "\n", $file->slurp; |
| 67 | |
| 68 | if ($lines[0] =~ /^\[([^\]]+?)\]$/) { |
| 69 | $user ||= $1; |
| 70 | shift @lines; |
| 71 | }; |
| 72 | |
| 73 | unless ($user) { |
| 74 | die 'No user defined'; |
| 75 | }; |
| 76 | |
| 77 | say "\nCommits from 01 $from $year - 31 $to $year by $user"; |
| 78 | say "------"; |
| 79 | |
| 80 | foreach (@lines) { |
| 81 | chomp $_; |
| 82 | my $dir = path($_); |
| 83 | my $dir_str = $dir->to_string; |
| 84 | if (-d $dir_str) { |
| 85 | chdir $dir_str; |
| 86 | my $out = `$cmd`; |
| 87 | if ($out =~ /\s+(\d+)\s+$user/) { |
| 88 | $sum += $1; |
| 89 | say $dir->basename, ": $1"; |
| 90 | push @components, [ $dir->basename => $1 ]; |
| 91 | }; |
| 92 | } else { |
| 93 | warn "$dir does not exist\n"; |
| 94 | } |
| 95 | }; |
| 96 | |
| 97 | say '------'; |
| 98 | say "Commits: $sum"; |
| 99 | say "In: " . join(', ', map { $_->[0] } sort { $b->[1] <=> $a->[1] } @components),"\n"; |
| 100 | |
| 101 | exit(0); |
| 102 | |
Akron | ebace12 | 2021-07-12 17:46:29 +0200 | [diff] [blame] | 103 | __END__ |
| 104 | |
Akron | e581315 | 2021-02-23 10:40:44 +0100 | [diff] [blame] | 105 | =pod |
| 106 | |
Akron | ebace12 | 2021-07-12 17:46:29 +0200 | [diff] [blame] | 107 | =head1 SYNOPSIS |
| 108 | |
| 109 | Usage: count_commits --quarter --current --user Akron |
| 110 | |
| 111 | =head1 DESCRIPTION |
| 112 | |
Akron | e581315 | 2021-02-23 10:40:44 +0100 | [diff] [blame] | 113 | This script will count and sum up all commits |
| 114 | in all repositories listed in the configured repositories |
| 115 | for a certain user in the current branches. |
| 116 | |
| 117 | The configuration file is expected in the same folder as the script |
| 118 | and named C<count_commits.conf>. |
| 119 | |
| 120 | It has the following structure: |
| 121 | |
| 122 | [Username] |
| 123 | /path/to/my/repo1 |
| 124 | /path/to/my/repo2 |
| 125 | ... |
| 126 | |
Akron | ebace12 | 2021-07-12 17:46:29 +0200 | [diff] [blame] | 127 | The username has to be in the first line and written in square brackets. |
| 128 | |
Akron | e581315 | 2021-02-23 10:40:44 +0100 | [diff] [blame] | 129 | Two different time ranges are supported: |
| 130 | |
| 131 | Per default the last finished month |
| 132 | is listed. With the parameter C<--quarter>, the last quarter are listed. |
| 133 | |
| 134 | With the parameter C<--current> the current month or quarter is taken into account. |
| 135 | |
| 136 | The username can be overwritten with the C<--user> parameter. |
| 137 | |
| 138 | =cut |