Last active
September 27, 2018 08:16
-
-
Save jhthorsen/9364fc3f97fc04cf859181b7112c75ec to your computer and use it in GitHub Desktop.
Check when you worked in the git repos
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
my $dir = ($ARGV[0] and -d $ARGV[0]) ? shift @ARGV : '.'; | |
my @ymd = (shift @ARGV); | |
my @today = (localtime)[5, 4]; # (year, mon) | |
$today[0] += 1900 if $today[0] <= 1900; | |
$today[1]++; | |
@ymd = map { s!^0+!!; $_ } split /-/, $ymd[0] || sprintf '%04s-%02s', @today; | |
unshift @ymd, $today[0] if @ymd == 1; | |
opendir my $DH, $dir or die "No such directory $dir: $!"; | |
scan_repos([grep { -d "$_->[1]/.git" } map { [$_, "$dir/$_"] } sort { $a cmp $b } readdir $DH]); | |
sub dump_report { | |
my $report = shift; | |
my $now = time; | |
for my $date (keys %$report) { | |
my ($earliest, $latest) = ($now, 0); | |
for my $e (@{$report->{$date}}) { | |
$earliest = $e->{author_time} if $e->{author_time} < $earliest; | |
$earliest = $e->{commit_time} if $e->{commit_time} < $earliest; | |
$latest = $e->{commit_time} if $latest < $e->{commit_time}; | |
$latest = $e->{author_time} if $latest < $e->{author_time}; | |
} | |
print sprintf "# %s - %02s:%02s to %02s:%02s\n", $date, (localtime $earliest)[2, 1], (localtime $latest)[2, 1]; | |
for my $e (@{$report->{$date}}) { | |
print "- $e->{project}: $e->{subject}\n"; | |
} | |
print "\n"; | |
} | |
} | |
sub scan_repos { | |
my $repos = shift; | |
my $report = {}; | |
my $n = 0; | |
local $ENV{GIT_PAGER} = 'cat'; | |
for my $offset (1 .. 31) { | |
my $ymd = sprintf '%04s-%02s-%02s', $ymd[0], $ymd[1], $offset; | |
for my $repo (@$repos) { | |
local $ENV{GIT_DIR} = "$ENV{PWD}/$repo->[1]/.git"; | |
local $ENV{GIT_WORK_DIR} = "$ENV{PWD}/$repo->[1]"; | |
my @git = (qw(git log), "--format=%at|%ct|%an|%s", "--after=$ymd 00:00", "--before=$ymd 23:59"); | |
push @git, sprintf '--author=%s', $ENV{GIT_AUTHOR} if $ENV{GIT_AUTHOR}; | |
open my $GIT, '-|', @git or next; | |
while (<$GIT>) { | |
chomp; | |
my ($author_time, $commit_time, $author_name, $subject) = split m!\|!, $_, 4; | |
push @{$report->{$ymd}}, { | |
author_name => $author_name, | |
author_time => $author_time, | |
commit_time => $commit_time, | |
project => $repo->[0], | |
subject => $subject, | |
}; | |
} | |
} | |
dump_report({$ymd => $report->{$ymd}}) if $report->{$ymd}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment