App-Multigit

 view release on metacpan or  search on metacpan

script/mg-init  view on Meta::CPAN

#!perl
use strict;
use warnings;
use 5.014;

use Getopt::Long qw(:config gnu_getopt);
use App::Multigit qw(mgconfig mg_parent all_repositories mg_each);
use App::Multigit::Script ();  # Avoid trying to get --workdir and chdir to it.
use Future;
use curry;

use Cwd qw(getcwd);
use File::Copy;
use HTTP::Tiny;
use Path::Class;
use Try::Tiny;

my %options = App::Multigit::Script::get_default_options;
GetOptions(\%options,
    'update-only|u',
    'clean|c',
    'remove-repos|rm',
);

die "update-only is not compatible with clean"
    if $options{'update-only'} and $options{clean};
die "update-only is not compatible with remove-missing"
    if $options{'update-only'} and $options{'remove-missing'};

$options{workdir} //= getcwd;

chdir $options{workdir};

$options{workdir} = dir($options{workdir});

my $config = shift;

if ($config) {
    if($config eq '-') {
        # read updated config from stdout
        copy(\*STDIN, mgconfig);
    }
    elsif ($config =~ m{://}) {
        HTTP::Tiny->new->mirror($config, mgconfig);
    }
    else {
        copy($config, mgconfig);
    }
}

my $existing_mg = try {
    mg_parent
}
catch {
    die "Failed to initialise from $config.\n" if $config;
    return;
};

if ($existing_mg) {
    my $f = mg_each(sub {
        my $repo = shift;
        return Future->done if -e $repo->config->{dir};

        return Future->done
            if $options{'update-only'}
            or $options{clean};

        my $cmd = [
            qw(git clone),
            (
                $repo->config->{branch} 
                ? ('-b', $repo->config->{branch} )
                : ()
            ),
            $repo->config->{url}
        ];

        $repo->run($cmd, ia_config => { no_cd => 1 })
            ->finally($repo->curry::report)
    });

    say for $f->get;
}

# Create the config if we don't have one.
# Don't update it if we're going to delete dirs.
if (not $existing_mg
     or not $options{'remove-missing'})
{
    App::Multigit::mkconfig($options{workdir});
}

if ($options{'remove-missing'}) {
    my %dirs = map { $_->{dir} => 1 } values %{ all_repositories($options{workdir}) };

    for my $dir ($options{workdir}->children) {
        next unless $dir->is_dir;
        next if $dirs{ $dir->relative($options{workdir}) };

        $dir->rmtree and say "Removed " . $dir->relative($options{workdir});



( run in 0.859 second using v1.01-cache-2.11-cpan-39bf76dae61 )