X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fgit-codeowners;a=blobdiff_plain;f=lib%2FApp%2FCodeowners%2FUtil.pm;h=6509cd3f3046202b2933299c64f1ff5054df9d7c;hp=ac613f1dcb24e1db226d7ed98002aa7de4237c3c;hb=fd8bad44a70558edb1e643ec6f86cad1650700fa;hpb=b94d5acee38ce17d5d4e5fa233cae415d5c9f52c diff --git a/lib/App/Codeowners/Util.pm b/lib/App/Codeowners/Util.pm index ac613f1..6509cd3 100644 --- a/lib/App/Codeowners/Util.pm +++ b/lib/App/Codeowners/Util.pm @@ -10,8 +10,8 @@ B except in L and related modules. use warnings; use strict; -use Encode qw(decode); use Exporter qw(import); +use File::Codeowners::Util; use Path::Tiny; our @EXPORT_OK = qw( @@ -32,105 +32,71 @@ our $VERSION = '9999.999'; # VERSION =func find_nearest_codeowners - $filepath = find_nearest_codeowners($dirpath); +Deprecated. -Find the F file in the current working directory, or search in the -parent directory recursively until a F file is found. +Use L instead. -Returns C if no F is found. +=cut + +sub find_nearest_codeowners { goto &File::Codeowners::Util::find_nearest_codeowners } + +=func find_codeowners_in_directory + +Deprecated. + +Use L instead. =cut -sub find_nearest_codeowners { - my $path = path(shift || '.')->absolute; +sub find_codeowners_in_directory { goto &File::Codeowners::Util::find_codeowners_in_directory } - while (!$path->is_rootdir) { - my $filepath = find_codeowners_in_directory($path); - return $filepath if $filepath; - $path = $path->parent; - } -} +=func run_command -=func find_codeowners_in_directory +Deprecated. + +Use L instead. + +=cut - $filepath = find_codeowners_in_directory($dirpath); +sub run_command { goto &File::Codeowners::Util::run_command } -Find the F file in a given directory. No recursive searching is done. +=func run_git -Returns the first of (or undef if none found): +Deprecated. -=for :list -* F -* F -* F<.bitbucket/CODEOWNERS> -* F<.github/CODEOWNERS> -* F<.gitlab/CODEOWNERS> +Use L instead. =cut -sub find_codeowners_in_directory { - my $path = path(shift) or die; +sub run_git { goto &File::Codeowners::Util::run_git } - my @tries = ( - [qw(CODEOWNERS)], - [qw(docs CODEOWNERS)], - [qw(.bitbucket CODEOWNERS)], - [qw(.github CODEOWNERS)], - [qw(.gitlab CODEOWNERS)], - ); +=func git_ls_files - for my $parts (@tries) { - my $try = $path->child(@$parts); - return $try if $try->is_file; - } -} +Deprecated. -sub run_command { - my $filter; - $filter = pop if ref($_[-1]) eq 'CODE'; +Use L instead. - print STDERR "# @_\n" if $ENV{GIT_CODEOWNERS_DEBUG}; +=cut - my ($child_in, $child_out); - require IPC::Open2; - my $pid = IPC::Open2::open2($child_out, $child_in, @_); - close($child_in); +sub git_ls_files { goto &File::Codeowners::Util::git_ls_files } - binmode($child_out, ':encoding(UTF-8)'); +=func git_toplevel - my $proc = App::Codeowners::Util::Process->new( - pid => $pid, - fh => $child_out, - filter => $filter, - ); +Deprecated. - return wantarray ? ($proc, @{$proc->all}) : $proc; -} +Use L instead. -sub run_git { - return run_command('git', @_); -} +=cut -sub git_ls_files { - my $dir = shift || '.'; - return run_git('-C', $dir, 'ls-files', @_, \&_unescape_git_filepath); -} +sub git_toplevel { goto &File::Codeowners::Util::git_toplevel } -# Depending on git's "core.quotepath" config, non-ASCII chars may be -# escaped (identified by surrounding dquotes), so try to unescape. -sub _unescape_git_filepath { - return $_ if $_ !~ /^"(.+)"$/; - return decode('UTF-8', unbackslash($1)); -} +=func colorstrip -sub git_toplevel { - my $dir = shift || '.'; + $str = colorstrip($str); - my ($proc, $path) = run_git('-C', $dir, qw{rev-parse --show-toplevel}); +Strip ANSI color control commands. - return if $proc->wait != 0 || !$path; - return path($path); -} +=cut sub colorstrip { my $str = shift || ''; @@ -138,21 +104,25 @@ sub colorstrip { return $str; } +=func stringify + + $str = stringify($scalar); + $str = stringify(\@array); + +Get a useful string representation of a scallar or arrayref. + +=cut + sub stringify { my $item = shift; return ref($item) eq 'ARRAY' ? join(',', @$item) : $item; } -# The zip code is from List::SomeUtils (thanks DROLSKY), copied just so as not -# to bring in the extra dependency. -sub zip (\@\@) { ## no critic (Subroutines::ProhibitSubroutinePrototypes) - my $max = -1; - $max < $#$_ && ( $max = $#$_ ) foreach @_; - map { - my $ix = $_; - map $_->[$ix], @_; - } 0 .. $max; -} +=func stringf + +TODO + +=cut # The stringf code is from String::Format (thanks SREZIC), with changes: # - Use Unicode::GCString for better Unicode character padding, @@ -229,6 +199,14 @@ sub stringf { return $format; } +=func unbackslash + +Deprecated. + +Use L instead. + +=cut + # The unbacklash code is from String::Escape (thanks EVO), with changes: # - Handle \a, \b, \f and \v (thanks Berk Akinci) my %unbackslash; @@ -246,55 +224,21 @@ sub unbackslash { return $str; } -{ - package App::Codeowners::Util::Process; +=func zip - sub new { - my $class = shift; - return bless {@_}, $class; - } +Same as L. - sub next { - my $self = shift; - my $line = readline($self->{fh}); - if (defined $line) { - chomp $line; - if (my $filter = $self->{filter}) { - local $_ = $line; - $line = $filter->($line); - } - } - $line; - } - - sub all { - my $self = shift; - chomp(my @lines = readline($self->{fh})); - if (my $filter = $self->{filter}) { - $_ = $filter->($_) for @lines; - } - \@lines; - } - - sub wait { - my $self = shift; - my $pid = $self->{pid} or return; - if (my $fh = $self->{fh}) { - close($fh); - delete $self->{fh}; - } - waitpid($pid, 0); - my $status = $?; - print STDERR "# -> status $status\n" if $ENV{GIT_CODEOWNERS_DEBUG}; - delete $self->{pid}; - return $status; - } +=cut - sub DESTROY { - my ($self, $global_destruction) = @_; - return if $global_destruction; - $self->wait; - } +# The zip code is from List::SomeUtils (thanks DROLSKY), copied just so as not +# to bring in the extra dependency. +sub zip (\@\@) { ## no critic (Subroutines::ProhibitSubroutinePrototypes) + my $max = -1; + $max < $#$_ && ( $max = $#$_ ) foreach @_; + map { + my $ix = $_; + map $_->[$ix], @_; + } 0 .. $max; } 1;