X-Git-Url: https://git.dogcows.com/gitweb?a=blobdiff_plain;f=lib%2FApp%2FCodeowners%2FUtil.pm;h=6509cd3f3046202b2933299c64f1ff5054df9d7c;hb=fd8bad44a70558edb1e643ec6f86cad1650700fa;hp=afad95df9ae3a092d45038f97c466cc51b82551e;hpb=67e86f990f2556f12465052911eb7b96a03b8dcc;p=chaz%2Fgit-codeowners diff --git a/lib/App/Codeowners/Util.pm b/lib/App/Codeowners/Util.pm index afad95d..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( @@ -20,113 +20,83 @@ our @EXPORT_OK = qw( find_nearest_codeowners git_ls_files git_toplevel + run_command run_git stringf + stringify unbackslash + zip ); 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. - -Returns C if no F is found. +Use L instead. =cut -sub find_nearest_codeowners { - my $path = path(shift || '.')->absolute; - - while (!$path->is_rootdir) { - my $filepath = find_codeowners_in_directory($path); - return $filepath if $filepath; - $path = $path->parent; - } -} +sub find_nearest_codeowners { goto &File::Codeowners::Util::find_nearest_codeowners } =func find_codeowners_in_directory - $filepath = find_codeowners_in_directory($dirpath); +Deprecated. + +Use L instead. -Find the F file in a given directory. No recursive searching is done. +=cut -Returns the first of (or undef if none found): +sub find_codeowners_in_directory { goto &File::Codeowners::Util::find_codeowners_in_directory } -=for :list -* F -* F -* F<.bitbucket/CODEOWNERS> -* F<.github/CODEOWNERS> -* F<.gitlab/CODEOWNERS> +=func run_command + +Deprecated. + +Use L instead. =cut -sub find_codeowners_in_directory { - my $path = path(shift) or die; +sub run_command { goto &File::Codeowners::Util::run_command } - my @tries = ( - [qw(CODEOWNERS)], - [qw(docs CODEOWNERS)], - [qw(.bitbucket CODEOWNERS)], - [qw(.github CODEOWNERS)], - [qw(.gitlab CODEOWNERS)], - ); +=func run_git - for my $parts (@tries) { - my $try = $path->child(@$parts); - return $try if $try->is_file; - } -} +Deprecated. + +Use L instead. -sub run_git { - my @cmd = ('git', @_); +=cut - require IPC::Open2; +sub run_git { goto &File::Codeowners::Util::run_git } - my ($child_in, $child_out); - my $pid = IPC::Open2::open2($child_out, $child_in, @cmd); - close($child_in); +=func git_ls_files - binmode($child_out, ':encoding(UTF-8)'); - chomp(my @lines = <$child_out>); +Deprecated. - waitpid($pid, 0); - return if $? != 0; +Use L instead. - return @lines; -} +=cut -sub git_ls_files { - my $dir = shift || '.'; +sub git_ls_files { goto &File::Codeowners::Util::git_ls_files } - my @files = run_git('-C', $dir, qw{ls-files}, @_); +=func git_toplevel - return undef if !@files; ## no critic (Subroutines::ProhibitExplicitReturn) +Deprecated. - # Depending on git's "core.quotepath" config, non-ASCII chars may be - # escaped (identified by surrounding dquotes), so try to unescape. - for my $file (@files) { - next if $file !~ /^"(.+)"$/; - $file = $1; - $file = unbackslash($file); - $file = decode('UTF-8', $file); - } +Use L instead. - return \@files; -} +=cut -sub git_toplevel { - my $dir = shift || '.'; +sub git_toplevel { goto &File::Codeowners::Util::git_toplevel } - my ($path) = run_git('-C', $dir, qw{rev-parse --show-toplevel}); +=func colorstrip - return if !$path; - return path($path); -} + $str = colorstrip($str); + +Strip ANSI color control commands. + +=cut sub colorstrip { my $str = shift || ''; @@ -134,6 +104,26 @@ 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; +} + +=func stringf + +TODO + +=cut + # The stringf code is from String::Format (thanks SREZIC), with changes: # - Use Unicode::GCString for better Unicode character padding, # - Strip ANSI color sequences, @@ -209,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; @@ -226,4 +224,21 @@ sub unbackslash { return $str; } +=func zip + +Same as L. + +=cut + +# 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;