File Coverage

blib/lib/App/GitGot/Command/checkout.pm
Criterion Covered Total %
statement 43 57 75.4
branch 13 28 46.4
condition 2 6 33.3
subroutine 12 12 100.0
pod 0 1 0.0
total 70 104 67.3


line stmt bran cond sub pod time code
1             package App::GitGot::Command::checkout;
2             our $AUTHORITY = 'cpan:GENEHACK';
3             $App::GitGot::Command::checkout::VERSION = '1.339';
4             # ABSTRACT: checkout specific branch for managed repositories
5 15     15   8791 use 5.014;
  15         72  
6              
7 15     15   143 use Try::Tiny;
  15         47  
  15         1614  
8              
9 15     15   149 use App::GitGot -command;
  15         43  
  15         158  
10              
11 15     15   5389 use Moo;
  15         61  
  15         146  
12             extends 'App::GitGot::Command';
13 15     15   4974 use namespace::autoclean;
  15         45  
  15         118  
14              
15             sub options {
16 1     1 0 157 my( $class , $app ) = @_;
17             return (
18 1         19 [ 'branch=s' => 'branch to checkout in the different repos' => { required => 1 } ] ,
19             [ 'create|b' => 'create branch like checkout -b in each of the repos' ],
20             );
21             }
22              
23             sub _execute {
24 1     1   5 my( $self, $opt, $args ) = @_;
25              
26 1         12 $self->_checkout( $self->opt->branch, $self->active_repos );
27             }
28              
29             sub _checkout {
30 1     1   255 my( $self , $branch, @repos ) = @_;
31              
32 1         13 my $max_len = $self->max_length_of_an_active_repo_label;
33              
34 1         4 REPO: for my $repo ( @repos ) {
35 4 100       119 next REPO unless $repo->repo;
36              
37 2         10 my $name = $repo->name;
38              
39 2         35 my $msg = sprintf "%3d) %-${max_len}s : ", $repo->number, $repo->label;
40              
41 2         6 my ( $status, $fxn );
42              
43 2         9 my $repo_type = $repo->type;
44              
45 2 50       7 if ( $repo_type eq 'git' ) { $fxn = '_git_checkout' }
  2         5  
46             ### FIXME elsif( $repo_type eq 'svn' ) { $fxn = 'svn_update' }
47 0         0 else { $status = $self->error("ERROR: repo type '$_' not supported") }
48              
49 2 50       11 $status = $self->$fxn($repo, $branch) if ($fxn);
50              
51 2 50 33     43 next REPO if $self->quiet and !$status;
52              
53 2         135 say "$msg$status";
54             }
55             }
56              
57             sub _git_checkout {
58 2 50   2   11 my ( $self, $entry, $branch ) = @_
59             or die "Need entry";
60              
61             # no callback because we need to run checkout even if just cloned
62 2     1   25 $self->_git_clone_or_callback( $entry , sub { '' } );
  1         4  
63              
64 2         7 my $msg = '';
65              
66 2 50   2   29 my @o = try { $entry->checkout($self->opt->create ? '-b' : (), $branch); } catch { $_->error };
  2         100  
  0         0  
67              
68 2     2   48 my @err = try { @{ $entry->_wrapper->ERR } } catch { $_ };
  2         83  
  2         48  
  0         0  
69              
70             # Typically STDOUT will contain something similar to
71             # Your branch is up-to-date with 'origin/main'.
72             # or
73             # Your branch is ahead of 'origin/main' by 2 commits.
74              
75             # Typically STDERR will contain something similar to
76             # Switched to branch 'beta'
77             # or
78             # Already on 'beta'
79 2 50 33     51 if ( grep { /(ahead|behind).*?by (\d+) commits./ } @o ) {
  0 50       0  
    50          
    50          
    50          
80             # branch checked out but not yet in sync
81 0         0 $msg .= $self->major_change("\u$1\e by $2");
82             }
83 0         0 elsif ( grep { /^Switched to/ } @err ) {
84             # branch checked out and in sync
85 0         0 $msg .= $self->major_change('Checked out');
86             #$msg .= "\n" . join("\n",@err) unless $self->quiet;
87             }
88 0         0 elsif ( grep { /^Already on/ } @err ) {
89             # already on requested branch
90 0 0       0 $msg .= $self->minor_change('OK') unless $self->quiet;
91             }
92 0         0 elsif ( grep { /did not match/ } @o ) {
93             # branch doesn't exist and was not created
94 0         0 $msg .= $self->error('Unknown branch');
95             }
96             elsif ( scalar @o == 0 && scalar @err == 0 ) {
97             # No messages to STDERR means repo was already updated (or this is a test)
98 2 50       62 $msg .= $self->minor_change('OK') unless $self->quiet;
99             }
100             else {
101             # Something else occured (possibly a warning)
102             # Print STDOUT/STDERR and move on
103 0         0 $msg .= $self->warning('Problem during checkout');
104 0 0       0 $msg .= "\n" . join("\n", @o, @err) unless $self->quiet;
105 0         0 return $msg;
106             }
107              
108 2         9 return $msg;
109             }
110              
111             1;
112              
113             ### FIXME docs
114              
115             __END__