#! /usr/bin/env perl
# PODNAME: cpangit-create
our $VERSION = '0.000_002'; # TRIAL VERSION
# ABSTRACT: create git branches for custom CPAN trees

use v5.36;
use FindBin;
BEGIN { push @INC, "$FindBin::RealBin/../lib" if -f "$FindBin::RealBin/../dist.ini" }
use Cwd;
use CPAN::InGit;
use Git::Raw::Branch;
use Getopt::Long;
use Pod::Usage;
use Log::Any::Adapter 'Stdout', log_level => 'debug';


GetOptions(
   'repo=s'     => \my $repo_path,
   'from=s'     => \my @sources,
   'upstream=s' => \my $upstream_url,
   'help'       => sub { pod2usage(1) },
) && @ARGV == 1 or pod2usage(2);

pod2usage(-message => "Specify only --from or --upstream, not both", -exit => 2)
   if defined $upstream_url && @sources;

my $branch_name= shift;

{ # scope to help run destructors in the right order
   my $git_repo= Git::Raw::Repository->discover($repo_path // getcwd);
   my $cpan_repo= CPAN::InGit->new(git_repo => $git_repo);
   $cpan_repo->create_archive_tree($branch_name,
      upstream_url => $upstream_url,
      default_import_sources => \@sources,
   );
}
exit 0;

__END__

=pod

=encoding UTF-8

=head1 NAME

cpangit-create - create git branches for custom CPAN trees

=head1 USAGE

  cpangit-create --upstream=URL BRANCH_NAME
  cpangit-create --from=BRANCH [--from=BRANCH2 ...] BRANCH_NAME

Create a new Archive branch.  If you specify C<--upstream>, it automatically pulls from the
remote URL as needed and acts like a local cache of the remote archive.  If you specify
C<--from> it behaves like a curated archive and looks to C<--branch> to import/update modules.
C<--from> can be specified multiple times.

You can later edit the C<cpan_ingit.json> file to reconfigure a branch.

=head1 OPTIONS

=over

=item --repo=PATH

Path to the Git repository of L<CPAN::InGit>.  Uses the C<.git> dir at or above the
current directory by default.

=item --upstream=URL

The URL of the upstream CPAN mirror

=item --from=BRANCH

Set a default source branch to import new moduels from.

=back

=head1 VERSION

version 0.000_002

=head1 AUTHOR

Michael Conrad <mike@nrdvana.net>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2025 by Michael Conrad, and IntelliTree Solutions.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut
