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.336';
4             # ABSTRACT: checkout specific branch for managed repositories
5 15     15   6746 use 5.014;
  15         51  
6              
7 15     15   98 use Try::Tiny;
  15         32  
  15         1159  
8              
9 15     15   116 use App::GitGot -command;
  15         35  
  15         90  
10              
11 15     15   4015 use Moo;
  15         30  
  15         73  
12             extends 'App::GitGot::Command';
13 15     15   3896 use namespace::autoclean;
  15         43  
  15         101  
14              
15             sub options {
16 1     1 0 126 my( $class , $app ) = @_;
17             return (
18 1         14 [ '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   3 my( $self, $opt, $args ) = @_;
25              
26 1         9 $self->_checkout( $self->opt->branch, $self->active_repos );
27             }
28              
29             sub _checkout {
30 1     1   181 my( $self , $branch, @repos ) = @_;
31              
32 1         8 my $max_len = $self->max_length_of_an_active_repo_label;
33              
34 1         3 REPO: for my $repo ( @repos ) {
35 4 100       123 next REPO unless $repo->repo;
36              
37 2         8 my $name = $repo->name;
38              
39 2         30 my $msg = sprintf "%3d) %-${max_len}s : ", $repo->number, $repo->label;
40              
41 2         6 my ( $status, $fxn );
42              
43 2         7 my $repo_type = $repo->type;
44              
45 2 50       7 if ( $repo_type eq 'git' ) { $fxn = '_git_checkout' }
  2         4  
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     62 next REPO if $self->quiet and !$status;
52              
53 2         67 say "$msg$status";
54             }
55             }
56              
57             sub _git_checkout {
58 2 50   2   9 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   18 $self->_git_clone_or_callback( $entry , sub { '' } );
  1         3  
63              
64 2         5 my $msg = '';
65              
66 2 50   2   21 my @o = try { $entry->checkout($self->opt->create ? '-b' : (), $branch); } catch { $_->error };
  2         88  
  0         0  
67              
68 2     2   35 my @err = try { @{ $entry->_wrapper->ERR } } catch { $_ };
  2         65  
  2         37  
  0         0  
69              
70             # Typically STDOUT will contain something similar to
71             # Your branch is up-to-date with 'origin/master'.
72             # or
73             # Your branch is ahead of 'origin/master' 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     47 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       41 $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         8 return $msg;
109             }
110              
111             1;
112              
113             ### FIXME docs
114              
115             __END__