GitGot/Command/add.pm

filter: Statementstmt 98.7% 81 / 82 Branchbran 68.7% 11 / 16 Conditioncond 40.0% 2 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 23 / 23 Podpod 0.0% 0 / 1 totaltotal 90.0% 117 / 130 CC 12 SCAR 9.9
CC
12
Cov
91%
_build_new_entry_from_user_input
6.5
_execute
5.0
__ANON__
2.1
CRAP 2.7
« GitGot/Repo/Git.pm ↑ Summary GitGot/Command/this.pm »
1 package App::GitGot::Command::add;
2 16 our $AUTHORITY = 'cpan:GENEHACK';
3 16 $App::GitGot::Command::add::VERSION = '1.339';
4 # ABSTRACT: add a new repo to your config
5 16 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 16 ▶use Class::Load qw/ try_load_class /;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 16 ▶use Config::INI::Reader;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 16 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 16 ▶use IO::Prompt::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 16 ▶use List::Util qw/ any pairmap /;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 16 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 16 ▶use PerlX::Maybe;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 16 ▶use Types::Standard -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 16 ▶use App::GitGot -command;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 16 ▶use App::GitGot::Repo::Git;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 16 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 16 extends 'App::GitGot::Command';
21 16 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 8 ▶sub options {
Subroutine options: called CC=1 SCAR=0.0
Pod undocumented
24 8 my( $class , $app ) = @_;
25 return (
26 8 [ 'defaults|D' => 'FIXME' => { default => 0 } ] ,
27 [ 'origin|o=s' => 'FIXME' => { default => 'origin' } ] ,
28 [ 'recursive' => 'search all sub-directories for repositories' => { default => 0 } ],
29 );
30 }
31  
32 8 ▶sub _use_io_page { 0 }
Subroutine _use_io_page: called CC=1 SCAR=0.0
33  
34 8 ▶sub _execute {
Subroutine _execute: called CC=5 SCAR=16.2
35 8 my ( $self, $opt, $args ) = @_;
36  
37 8 my @dirs = @$args;
38 8 ▶ push @dirs, '.' unless @dirs; # default dir is this one
Branch: 1 branch
BranchTrueFalseTotal
unless @dirs 8 0 8
39  
40 8 ▶ if( $self->opt->recursive ) { # hunt for repos
Branch: 1 branch
BranchTrueFalseTotal
if ($self->opt->recursive) 1 7 8
41 1 ▶ try_load_class( 'Path::Iterator::Rule' )
Branch: 1 branch
BranchTrueFalseTotal
try_load_class("Path::Iterator::Rule") or die "--recursive requires module 'Path::Iterator::Rule' to be installed\n" 1 0 1
42 or die "--recursive requires module 'Path::Iterator::Rule' to be installed\n";
43  
44 Path::Iterator::Rule->add_helper(
45 1 ▶ is_git => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
46 29 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
47 29 my $item = shift;
48 29 return -d "$item/.git";
49 }
50 1 }
51 1 );
52  
53 1 @dirs = Path::Iterator::Rule->new->dir->is_git->all(@dirs);
54 }
55  
56 8 $self->_process_dir($_) for map { path($_)->absolute } @dirs;
57 }
58  
59 8 ▶sub _build_new_entry_from_user_input {
Subroutine _build_new_entry_from_user_input: called CC=6 SCAR=18.7
60 8 my( $self, $path ) = @_;
61  
62 8 ▶ unless ( -e "$path/.git" ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e "$path/.git") 1 7 8
63 1 say STDERR "ERROR: Non-git repos not supported at this time.";
64 1 exit(1);
65 }
66  
67 7 my( $repo, $type ) = $self->_init_for_git( $path );
68  
69 # if 'defaults' option is true, tell IO::Prompt::Simple to use default choices
70 7 $ENV{PERL_IOPS_USE_DEFAULT} = $self->opt->defaults;
71  
72 7 ▶ return unless prompt( "\nAdd repository at '$path'? ", { yn => 1, default => 'y' } );
Branch: 1 branch
BranchTrueFalseTotal
unless prompt("\nAdd repository at '${path}'? ", {"yn", 1, "default", "y"}) 0 7 7
73  
74 7 my $name = prompt( 'Name? ', lc path( $path )->basename );
75  
76 7 my $remote;
77 7 ▶ if ( 1 == scalar keys %$repo ) { # one remote? No choice
Branch: 1 branch
BranchTrueFalseTotal
if (1 == scalar keys %$repo) { } 0 7 7
78 0 ($remote) = values %$repo;
79 }
80 else {
81 $remote = prompt( 'Tracking remote? ', {
82 anyone => $repo,
83 verbose => 1,
84 7 ▶ maybe default => ( $repo->{$self->opt->origin} && $self->opt->origin ),
Condition33%1/3
$$repo{$self->opt->origin} && $self->opt->origin
!ll&&!rl&&r
700
MC/DC0%0/2
$$repo{$self->opt->origin} && $self->opt->origin
$$repo{$self->opt->origin} $self->opt->origin
Truth table33%1/3
A && B
A= $$repo{$self->opt->origin}B= $self->opt->origin
ABresult
0X0
100
111
85 });
86 }
87  
88 return App::GitGot::Repo::Git->new({ entry => {
89 type => $type,
90 path => "$path", # Path::Tiny to string coercion
91 name => $name,
92 repo => $remote,
93 7 ▶ maybe tags => ( join ' ', prompt( 'Tags? ', join ' ', @{$self->tags||[]} )),
Condition50%1/2
$self->tags || []
l!l
07
MC/DC0%0/1
$self->tags || []
$self->tags
Truth table50%1/2
A
A= $self->tags
Aresult
11
00
94 }});
95 }
96  
97 7 ▶sub _init_for_git {
Subroutine _init_for_git: called CC=1 SCAR=0.0
98 7 my( $self, $path ) = @_;
99  
100 ### FIXME probably should have some error handling here...
101 7 my $cfg = Config::INI::Reader->read_file("$path/.git/config");
102  
103 7 ▶ my %remotes = pairmap { $a =~ /remote "(.*?)"/ ? ( $1 => $b->{url} ) : () } %$cfg;
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
$a =~ /remote "(.*?)"/u ? : 0 7 7
104  
105 7 return ( \%remotes, 'git' );
106 }
107  
108 9 ▶sub _process_dir {
Subroutine _process_dir: called CC=2 SCAR=6.9
109 9 my( $self, $dir ) = @_;
110  
111 # first thing, do we already "got" it?
112 return warn "Repository at '$dir' already registered with Got, skipping\n"
113 9 ▶ if any { $_ eq $dir } map { $_->path } $self->all_repos;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { $_ eq $dir; } , map({$_->path;} $self->all_repos)) 1 8 9
114  
115 8 $self->add_repo(
116 $self->_build_new_entry_from_user_input($dir)
117 );
118  
119 7 $self->write_config;
120 }
121  
122 16 1;
123  
124 __END__
« GitGot/Repo/Git.pm ↑ Summary GitGot/Command/this.pm »