| 1 | package File::Codeowners::Util; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Grab bag of utility subs for Codeowners modules | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use Encode qw(decode); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use Exporter qw(import); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use Path::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 3 | our @EXPORT_OK = qw( | |||||||||||||||||||||||||||
| 13 | find_codeowners_in_directory | ||||||||||||||||||||||||||||
| 14 | find_nearest_codeowners | ||||||||||||||||||||||||||||
| 15 | git_ls_files | ||||||||||||||||||||||||||||
| 16 | git_toplevel | ||||||||||||||||||||||||||||
| 17 | run_command | ||||||||||||||||||||||||||||
| 18 | run_git | ||||||||||||||||||||||||||||
| 19 | ); | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 3 | our $VERSION = '0.53'; # VERSION | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | sub find_nearest_codeowners { | ||||||||||||||||||||||||||||
| 25 | 1 | ▶ | my $path = path(shift || '.')->absolute; | ||||||||||||||||||||||||||
|
Subroutine
find_nearest_codeowners: called CC=4 SCAR=14.1
Pod
documented
Condition50%1/2
shift() || '.'
MC/DC0%0/1
shift() || '.'
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 1 | ▶ | while (!$path->is_rootdir) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | 4 | my $filepath = find_codeowners_in_directory($path); | |||||||||||||||||||||||||||
| 29 | 4 | ▶ | return $filepath if $filepath; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | 3 | $path = $path->parent; | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | sub find_codeowners_in_directory { | ||||||||||||||||||||||||||||
| 36 | 6 | ▶ | my $path = path(shift) or die; | ||||||||||||||||||||||||||
|
Subroutine
find_codeowners_in_directory: called CC=4 SCAR=13.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 6 | my @tries = ( | |||||||||||||||||||||||||||
| 39 | [qw(CODEOWNERS)], | ||||||||||||||||||||||||||||
| 40 | [qw(docs CODEOWNERS)], | ||||||||||||||||||||||||||||
| 41 | [qw(.bitbucket CODEOWNERS)], | ||||||||||||||||||||||||||||
| 42 | [qw(.github CODEOWNERS)], | ||||||||||||||||||||||||||||
| 43 | [qw(.gitlab CODEOWNERS)], | ||||||||||||||||||||||||||||
| 44 | ); | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 6 | for my $parts (@tries) { | |||||||||||||||||||||||||||
| 47 | 20 | my $try = $path->child(@$parts); | |||||||||||||||||||||||||||
| 48 | 20 | ▶ | return $try if $try->is_file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | sub run_command { | ||||||||||||||||||||||||||||
| 54 | 19 | ▶ | my $filter; | ||||||||||||||||||||||||||
|
Subroutine
run_command: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 55 | 19 | ▶ | $filter = pop if ref($_[-1]) eq 'CODE'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 19 | ▶ | print STDERR "# @_\n" if $ENV{FILE_CODEOWNERS_DEBUG}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 19 | my ($child_in, $child_out); | |||||||||||||||||||||||||||
| 60 | 19 | require IPC::Open2; | |||||||||||||||||||||||||||
| 61 | 19 | my $pid = IPC::Open2::open2($child_out, $child_in, @_); | |||||||||||||||||||||||||||
| 62 | 19 | close($child_in); | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 19 | ▶ | binmode($child_out, ':encoding(UTF-8)'); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 19 | my $proc = File::Codeowners::Util::Process->new( | |||||||||||||||||||||||||||
| 67 | pid => $pid, | ||||||||||||||||||||||||||||
| 68 | fh => $child_out, | ||||||||||||||||||||||||||||
| 69 | filter => $filter, | ||||||||||||||||||||||||||||
| 70 | ); | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 19 | ▶ | return wantarray ? ($proc, @{$proc->all}) : $proc; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | sub run_git { | ||||||||||||||||||||||||||||
| 77 | 19 | ▶ | return run_command('git', @_); | ||||||||||||||||||||||||||
|
Subroutine
run_git: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | sub git_toplevel { | ||||||||||||||||||||||||||||
| 82 | 2 | ▶ | my $dir = shift || '.'; | ||||||||||||||||||||||||||
|
Subroutine
git_toplevel: called CC=4 SCAR=15.6
Pod
documented
Condition50%1/2
shift() || '.'
MC/DC0%0/1
shift() || '.'
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 2 | my ($proc, $path) = run_git('-C', $dir, qw{rev-parse --show-toplevel}); | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 2 | ▶ | return if $proc->wait != 0 || !$path; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$proc->wait != 0 or not $path
MC/DC0%0/2
$proc->wait != 0 or not $path
$proc->wait != 0
not $path
Truth table33%1/3
A or not(B)
A= $proc->wait != 0B= not $path
| |||||||||||||||||||||||||||||
| 87 | 2 | return path($path); | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | sub git_ls_files { | ||||||||||||||||||||||||||||
| 92 | 2 | ▶ | my $dir = shift || '.'; | ||||||||||||||||||||||||||
|
Subroutine
git_ls_files: called CC=2 SCAR=7.2
Pod
documented
Condition50%1/2
shift() || '.'
MC/DC0%0/1
shift() || '.'
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 93 | 2 | return run_git('-C', $dir, 'ls-files', @_, \&_unescape_git_filepath); | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | # Depending on git's "core.quotepath" config, non-ASCII chars may be | ||||||||||||||||||||||||||||
| 97 | # escaped (identified by surrounding dquotes), so try to unescape. | ||||||||||||||||||||||||||||
| 98 | sub _unescape_git_filepath { | ||||||||||||||||||||||||||||
| 99 | 2 | ▶ | return $_ if $_ !~ /^"(.+)"$/; | ||||||||||||||||||||||||||
|
Subroutine
_unescape_git_filepath: called CC=2 SCAR=9.2
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 0 | return decode('UTF-8', _unbackslash($1)); | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | # The unbacklash code is from String::Escape (thanks EVO), with changes: | ||||||||||||||||||||||||||||
| 104 | # - Handle \a, \b, \f and \v (thanks Berk Akinci) | ||||||||||||||||||||||||||||
| 105 | 3 | my %unbackslash; | |||||||||||||||||||||||||||
| 106 | sub _unbackslash { | ||||||||||||||||||||||||||||
| 107 | 0 | ▶ | my $str = shift; | ||||||||||||||||||||||||||
|
Subroutine
_unbackslash: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 108 | # Earlier definitions are preferred to later ones, thus we output \n not \x0d | ||||||||||||||||||||||||||||
| 109 | %unbackslash = ( | ||||||||||||||||||||||||||||
| 110 | 0 | ( map { $_ => $_ } ( '\\', '"', '$', '@' ) ), | |||||||||||||||||||||||||||
| 111 | ( 'r' => "\r", 'n' => "\n", 't' => "\t" ), | ||||||||||||||||||||||||||||
| 112 | 0 | ( map { 'x' . unpack('H2', chr($_)) => chr($_) } (0..255) ), | |||||||||||||||||||||||||||
| 113 | 0 | ▶ | ( map { sprintf('%03o', $_) => chr($_) } (0..255) ), | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | ( 'a' => "\x07", 'b' => "\x08", 'f' => "\x0c", 'v' => "\x0b" ), | ||||||||||||||||||||||||||||
| 115 | ) if !%unbackslash; | ||||||||||||||||||||||||||||
| 116 | 0 | $str =~ s/ (\A|\G|[^\\]) \\ ( [0-7]{3} | x[\da-fA-F]{2} | . ) / $1 . $unbackslash{lc($2)} /gsxe; | |||||||||||||||||||||||||||
| 117 | 0 | return $str; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | { | ||||||||||||||||||||||||||||
| 121 | package File::Codeowners::Util::Process; | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | sub new { | ||||||||||||||||||||||||||||
| 124 | 19 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 125 | 19 | return bless {@_}, $class; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | sub next { | ||||||||||||||||||||||||||||
| 129 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
next: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 130 | 0 | my $line = readline($self->{fh}); | |||||||||||||||||||||||||||
| 131 | 0 | ▶ | if (defined $line) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 0 | chomp $line; | |||||||||||||||||||||||||||
| 133 | 0 | ▶ | if (my $filter = $self->{filter}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 0 | local $_ = $line; | |||||||||||||||||||||||||||
| 135 | 0 | $line = $filter->($line); | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | 0 | $line; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | sub all { | ||||||||||||||||||||||||||||
| 142 | 5 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
all: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 143 | 5 | chomp(my @lines = readline($self->{fh})); | |||||||||||||||||||||||||||
| 144 | 5 | ▶ | if (my $filter = $self->{filter}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | 2 | $_ = $filter->($_) for @lines; | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | 5 | \@lines; | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | sub wait { | ||||||||||||||||||||||||||||
| 151 | 35 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
wait: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 152 | 35 | ▶ | my $pid = $self->{pid} or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | 19 | ▶ | if (my $fh = $self->{fh}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | 19 | close($fh); | |||||||||||||||||||||||||||
| 155 | 19 | delete $self->{fh}; | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | 19 | waitpid($pid, 0); | |||||||||||||||||||||||||||
| 158 | 19 | my $status = $?; | |||||||||||||||||||||||||||
| 159 | 19 | ▶ | print STDERR "# -> status $status\n" if $ENV{FILE_CODEOWNERS_DEBUG}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | 19 | delete $self->{pid}; | |||||||||||||||||||||||||||
| 161 | 19 | return $status; | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | sub DESTROY { | ||||||||||||||||||||||||||||
| 165 | 19 | ▶ | my ($self, $global_destruction) = @_; | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 166 | 19 | ▶ | return if $global_destruction; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | 19 | $self->wait; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 3 | 1; | |||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | __END__ | ||||||||||||||||||||||||||||