our
$AUTHORITY
=
'cpan:GENEHACK'
;
$App::GitGot::Command::checkout::VERSION
=
'1.339'
;
use
5.014;
sub
options {
my
(
$class
,
$app
) =
@_
;
return
(
[
'branch=s'
=>
'branch to checkout in the different repos'
=> {
required
=> 1 } ] ,
[
'create|b'
=>
'create branch like checkout -b in each of the repos'
],
);
}
sub
_execute {
my
(
$self
,
$opt
,
$args
) =
@_
;
$self
->_checkout(
$self
->opt->branch,
$self
->active_repos );
}
sub
_checkout {
my
(
$self
,
$branch
,
@repos
) =
@_
;
my
$max_len
=
$self
->max_length_of_an_active_repo_label;
REPO:
for
my
$repo
(
@repos
) {
next
REPO
unless
$repo
->repo;
my
$name
=
$repo
->name;
my
$msg
=
sprintf
"%3d) %-${max_len}s : "
,
$repo
->number,
$repo
->label;
my
(
$status
,
$fxn
);
my
$repo_type
=
$repo
->type;
if
(
$repo_type
eq
'git'
) {
$fxn
=
'_git_checkout'
}
else
{
$status
=
$self
->error(
"ERROR: repo type '$_' not supported"
) }
$status
=
$self
->
$fxn
(
$repo
,
$branch
)
if
(
$fxn
);
next
REPO
if
$self
->quiet and !
$status
;
say
"$msg$status"
;
}
}
sub
_git_checkout {
my
(
$self
,
$entry
,
$branch
) =
@_
or
die
"Need entry"
;
$self
->_git_clone_or_callback(
$entry
,
sub
{
''
} );
my
$msg
=
''
;
my
@o
=
try
{
$entry
->checkout(
$self
->opt->create ?
'-b'
: (),
$branch
); }
catch
{
$_
->error };
my
@err
=
try
{ @{
$entry
->_wrapper->ERR } }
catch
{
$_
};
if
(
grep
{ /(ahead|behind).*?by (\d+) commits./ }
@o
) {
$msg
.=
$self
->major_change(
"\u$1\e by $2"
);
}
elsif
(
grep
{ /^Switched to/ }
@err
) {
$msg
.=
$self
->major_change(
'Checked out'
);
}
elsif
(
grep
{ /^Already on/ }
@err
) {
$msg
.=
$self
->minor_change(
'OK'
)
unless
$self
->quiet;
}
elsif
(
grep
{ /did not match/ }
@o
) {
$msg
.=
$self
->error(
'Unknown branch'
);
}
elsif
(
scalar
@o
== 0 &&
scalar
@err
== 0 ) {
$msg
.=
$self
->minor_change(
'OK'
)
unless
$self
->quiet;
}
else
{
$msg
.=
$self
->warning(
'Problem during checkout'
);
$msg
.=
"\n"
.
join
(
"\n"
,
@o
,
@err
)
unless
$self
->quiet;
return
$msg
;
}
return
$msg
;
}
1;