| 1 | package App::GitGot::Command::checkout; | ||||||||||||||||||||||||||||
| 2 | 16 | our $AUTHORITY = 'cpan:GENEHACK'; | |||||||||||||||||||||||||||
| 3 | 16 | $App::GitGot::Command::checkout::VERSION = '1.339'; | |||||||||||||||||||||||||||
| 4 | # ABSTRACT: checkout specific branch for managed repositories | ||||||||||||||||||||||||||||
| 5 | 16 | ▶ | use 5.014; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 16 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 16 | ▶ | use App::GitGot -command; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 16 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 16 | extends 'App::GitGot::Command'; | |||||||||||||||||||||||||||
| 13 | 16 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 1 | ▶ | sub options { | ||||||||||||||||||||||||||
|
Subroutine
options: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 16 | 1 | my( $class , $app ) = @_; | |||||||||||||||||||||||||||
| 17 | return ( | ||||||||||||||||||||||||||||
| 18 | 1 | [ '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 | 1 | ▶ | sub _execute { | ||||||||||||||||||||||||||
|
Subroutine
_execute: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 1 | my( $self, $opt, $args ) = @_; | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 1 | $self->_checkout( $self->opt->branch, $self->active_repos ); | |||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 1 | ▶ | sub _checkout { | ||||||||||||||||||||||||||
|
Subroutine
_checkout: called CC=7 SCAR=20.4
| |||||||||||||||||||||||||||||
| 30 | 1 | my( $self , $branch, @repos ) = @_; | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 1 | my $max_len = $self->max_length_of_an_active_repo_label; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 1 | REPO: for my $repo ( @repos ) { | |||||||||||||||||||||||||||
| 35 | 4 | ▶ | next REPO unless $repo->repo; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 2 | my $name = $repo->name; | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 2 | my $msg = sprintf "%3d) %-${max_len}s : ", $repo->number, $repo->label; | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 2 | my ( $status, $fxn ); | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 2 | my $repo_type = $repo->type; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 2 | ▶ | if ( $repo_type eq 'git' ) { $fxn = '_git_checkout' } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | ### FIXME elsif( $repo_type eq 'svn' ) { $fxn = 'svn_update' } | ||||||||||||||||||||||||||||
| 47 | 0 | else { $status = $self->error("ERROR: repo type '$_' not supported") } | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 2 | ▶ | $status = $self->$fxn($repo, $branch) if ($fxn); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 2 | ▶ | next REPO if $self->quiet and !$status; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->quiet and not $status
MC/DC0%0/2
$self->quiet and not $status
$self->quiet
not $status
Truth table33%1/3
A and not(B)
A= $self->quietB= not $status
| |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 2 | say "$msg$status"; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 2 | ▶ | sub _git_checkout { | ||||||||||||||||||||||||||
|
Subroutine
_git_checkout: called CC=11 SCAR=34.8
| |||||||||||||||||||||||||||||
| 58 | 2 | ▶ | my ( $self, $entry, $branch ) = @_ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | or die "Need entry"; | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | # no callback because we need to run checkout even if just cloned | ||||||||||||||||||||||||||||
| 62 | 2 | ▶ | $self->_git_clone_or_callback( $entry , sub { '' } ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 2 | my $msg = ''; | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 0 | ▶ | my @o = try { $entry->checkout($self->opt->create ? '-b' : (), $branch); } catch { $_->error }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=8.1
Subroutine
__ANON__: not called CC=1 SCAR=0.6
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 0 | ▶ | my @err = try { @{ $entry->_wrapper->ERR } } catch { $_ }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.2
Subroutine
__ANON__: not called CC=1 SCAR=0.2
| |||||||||||||||||||||||||||||
| 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 | 0 | ▶ | if ( grep { /(ahead|behind).*?by (\d+) commits./ } @o ) { | ||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||
| 80 | # branch checked out but not yet in sync | ||||||||||||||||||||||||||||
| 81 | 0 | $msg .= $self->major_change("\u$1\e by $2"); | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | 0 | elsif ( grep { /^Switched to/ } @err ) { | |||||||||||||||||||||||||||
| 84 | # branch checked out and in sync | ||||||||||||||||||||||||||||
| 85 | 0 | $msg .= $self->major_change('Checked out'); | |||||||||||||||||||||||||||
| 86 | #$msg .= "\n" . join("\n",@err) unless $self->quiet; | ||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | 0 | elsif ( grep { /^Already on/ } @err ) { | |||||||||||||||||||||||||||
| 89 | # already on requested branch | ||||||||||||||||||||||||||||
| 90 | 0 | ▶ | $msg .= $self->minor_change('OK') unless $self->quiet; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | 0 | elsif ( grep { /did not match/ } @o ) { | |||||||||||||||||||||||||||
| 93 | # branch doesn't exist and was not created | ||||||||||||||||||||||||||||
| 94 | 0 | $msg .= $self->error('Unknown branch'); | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | ▶ | elsif ( scalar @o == 0 && scalar @err == 0 ) { | |||||||||||||||||||||||||||
|
Condition33%1/3
scalar @o == 0 and scalar @err == 0
MC/DC0%0/2
scalar @o == 0 and scalar @err == 0
scalar @o == 0
scalar @err == 0
Truth table33%1/3
A and B
A= scalar @o == 0B= scalar @err == 0
| |||||||||||||||||||||||||||||
| 97 | # No messages to STDERR means repo was already updated (or this is a test) | ||||||||||||||||||||||||||||
| 98 | 2 | ▶ | $msg .= $self->minor_change('OK') unless $self->quiet; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | else { | ||||||||||||||||||||||||||||
| 101 | # Something else occured (possibly a warning) | ||||||||||||||||||||||||||||
| 102 | # Print STDOUT/STDERR and move on | ||||||||||||||||||||||||||||
| 103 | 0 | $msg .= $self->warning('Problem during checkout'); | |||||||||||||||||||||||||||
| 104 | 0 | ▶ | $msg .= "\n" . join("\n", @o, @err) unless $self->quiet; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 0 | return $msg; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 2 | return $msg; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 16 | 1; | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | ### FIXME docs | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | __END__ | ||||||||||||||||||||||||||||