#!perl

use strict;
use warnings;
use 5.014;

# PODNAME: mg-st

use Getopt::Long qw(:config gnu_getopt pass_through);
use App::Multigit qw(mg_each);
use App::Multigit::Script;
use Future;
use curry;

GetOptions(
    \%options,
    'debug',
);

$App::Multigit::BEHAVIOUR{report_on_no_output} = 0;

my $future = mg_each(sub {
    my ($repo) = @_;
    $repo->run(
            [qw/ git status /, @ARGV]
        )
        ->then(\&prune)
        ->finally($repo->curry::report)
});

my %report = $future->get;

for my $repo (sort keys %report) {
    say for $repo, $report{$repo};
}

sub prune {
    my %data = @_;

    if ($data{stdout} =~ /nothing to commit/i
            and $data{stdout} =~ /Your branch is up-to-date/i
            and not $options{debug}) {
        Future->done
    }
    else {
        Future->done(@_);
    }
}

=head1 SYNOPSIS

    mg st [--debug] [-uno ...]

Runs C<git status>, not displaying repos that have no changes.

Command line parameters other than --debug are passed through to git status.

To see all the statuses, regardless of changes use the --debug flag.

=head1 OPTIONS

Unrecognised options are passed through to C<git status>.

=over

=item --debug

Outputs information about all repositories, regardless of whether they had
changes or not.

=back
