]> Dogcows Code - chaz/git-codeowners/blobdiff - lib/App/Codeowners/Util.pm
Version 0.50
[chaz/git-codeowners] / lib / App / Codeowners / Util.pm
index cb0b795d676088abcf62af9f46d09031499b3bff..fd8b793d4fde354717b0b4b1d534e06ad091d920 100644 (file)
@@ -5,8 +5,8 @@ package App::Codeowners::Util;
 use warnings;
 use strict;
 
-use Encode qw(decode);
 use Exporter qw(import);
+use File::Codeowners::Util;
 use Path::Tiny;
 
 our @EXPORT_OK = qw(
@@ -15,87 +15,34 @@ our @EXPORT_OK = qw(
     find_nearest_codeowners
     git_ls_files
     git_toplevel
+    run_command
     run_git
     stringf
+    stringify
     unbackslash
+    zip
 );
 
-our $VERSION = '0.41'; # VERSION
+our $VERSION = '0.50'; # VERSION
 
 
-sub find_nearest_codeowners {
-    my $path = path(shift || '.')->absolute;
+sub find_nearest_codeowners { goto &File::Codeowners::Util::find_nearest_codeowners }
 
-    while (!$path->is_rootdir) {
-        my $filepath = find_codeowners_in_directory($path);
-        return $filepath if $filepath;
-        $path = $path->parent;
-    }
-}
-
-
-sub find_codeowners_in_directory {
-    my $path = path(shift) or die;
-
-    my @tries = (
-        [qw(CODEOWNERS)],
-        [qw(docs CODEOWNERS)],
-        [qw(.bitbucket CODEOWNERS)],
-        [qw(.github CODEOWNERS)],
-        [qw(.gitlab CODEOWNERS)],
-    );
 
-    for my $parts (@tries) {
-        my $try = $path->child(@$parts);
-        return $try if $try->is_file;
-    }
-}
+sub find_codeowners_in_directory { goto &File::Codeowners::Util::find_codeowners_in_directory }
 
-sub run_git {
-    my @cmd = ('git', @_);
 
-    require IPC::Open2;
+sub run_command { goto &File::Codeowners::Util::run_command }
 
-    my ($child_in, $child_out);
-    my $pid = IPC::Open2::open2($child_out, $child_in, @cmd);
-    close($child_in);
 
-    binmode($child_out, ':encoding(UTF-8)');
-    chomp(my @lines = <$child_out>);
+sub run_git { goto &File::Codeowners::Util::run_git }
 
-    waitpid($pid, 0);
-    return if $? != 0;
 
-    return @lines;
-}
+sub git_ls_files { goto &File::Codeowners::Util::git_ls_files }
 
-sub git_ls_files {
-    my $dir = shift || '.';
 
-    my @files = run_git('-C', $dir, qw{ls-files}, @_);
+sub git_toplevel { goto &File::Codeowners::Util::git_toplevel }
 
-    return undef if !@files;    ## no critic (Subroutines::ProhibitExplicitReturn)
-
-    # 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);
-    }
-
-    return \@files;
-}
-
-sub git_toplevel {
-    my $dir = shift || '.';
-
-    my ($path) = run_git('-C', $dir, qw{rev-parse --show-toplevel});
-
-    return if !$path;
-    return path($path);
-}
 
 sub colorstrip {
     my $str = shift || '';
@@ -103,6 +50,13 @@ sub colorstrip {
     return $str;
 }
 
+
+sub stringify {
+    my $item = shift;
+    return ref($item) eq 'ARRAY' ? join(',', @$item) : $item;
+}
+
+
 # The stringf code is from String::Format (thanks SREZIC), with changes:
 # - Use Unicode::GCString for better Unicode character padding,
 # - Strip ANSI color sequences,
@@ -178,6 +132,7 @@ sub stringf {
     return $format;
 }
 
+
 # The unbacklash code is from String::Escape (thanks EVO), with changes:
 # - Handle \a, \b, \f and \v (thanks Berk Akinci)
 my %unbackslash;
@@ -195,6 +150,18 @@ sub unbackslash {
     return $str;
 }
 
+
+# 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;
 
 __END__
@@ -209,7 +176,7 @@ App::Codeowners::Util - Grab bag of utility subs for Codeowners modules
 
 =head1 VERSION
 
-version 0.41
+version 0.50
 
 =head1 DESCRIPTION
 
@@ -219,44 +186,66 @@ B<DO NOT USE> except in L<App::Codeowners> and related modules.
 
 =head2 find_nearest_codeowners
 
-    $filepath = find_nearest_codeowners($dirpath);
+Deprecated.
 
-Find the F<CODEOWNERS> file in the current working directory, or search in the
-parent directory recursively until a F<CODEOWNERS> file is found.
-
-Returns C<undef> if no F<CODEOWNERS> is found.
+Use L<File::Codeowners::Util/find_nearest_codeowners> instead.
 
 =head2 find_codeowners_in_directory
 
-    $filepath = find_codeowners_in_directory($dirpath);
+Deprecated.
+
+Use L<File::Codeowners::Util/find_codeowners_in_directory> instead.
+
+=head2 run_command
+
+Deprecated.
+
+Use L<File::Codeowners::Util/run_command> instead.
+
+=head2 run_git
+
+Deprecated.
+
+Use L<File::Codeowners::Util/run_git> instead.
+
+=head2 git_ls_files
+
+Deprecated.
+
+Use L<File::Codeowners::Util/git_ls_files> instead.
+
+=head2 git_toplevel
+
+Deprecated.
 
-Find the F<CODEOWNERS> file in a given directory. No recursive searching is done.
+Use L<File::Codeowners::Util/git_toplevel> instead.
 
-Returns the first of (or undef if none found):
+=head2 colorstrip
 
-=over 4
+    $str = colorstrip($str);
 
-=item *
+Strip ANSI color control commands.
 
-F<CODEOWNERS>
+=head2 stringify
 
-=item *
+    $str = stringify($scalar);
+    $str = stringify(\@array);
 
-F<docs/CODEOWNERS>
+Get a useful string representation of a scallar or arrayref.
 
-=item *
+=head2 stringf
 
-F<.bitbucket/CODEOWNERS>
+TODO
 
-=item *
+=head2 unbackslash
 
-F<.github/CODEOWNERS>
+Deprecated.
 
-=item *
+Use L<File::Codeowners::Util/unbackslash> instead.
 
-F<.gitlab/CODEOWNERS>
+=head2 zip
 
-=back
+Same as L<List::SomeUtils/zip-ARRAY1-ARRAY2-[-ARRAY3-...-]>.
 
 =head1 BUGS
 
@@ -273,7 +262,7 @@ Charles McGarvey <chazmcgarvey@brokenzipper.com>
 
 =head1 COPYRIGHT AND LICENSE
 
-This software is copyright (c) 2019 by Charles McGarvey.
+This software is copyright (c) 2021 by Charles McGarvey.
 
 This is free software; you can redistribute it and/or modify it under
 the same terms as the Perl 5 programming language system itself.
This page took 0.026846 seconds and 4 git commands to generate.