]> Dogcows Code - chaz/git-codeowners/blobdiff - lib/App/Codeowners/Util.pm
split off File::Codeowners
[chaz/git-codeowners] / lib / App / Codeowners / Util.pm
index afad95df9ae3a092d45038f97c466cc51b82551e..6509cd3f3046202b2933299c64f1ff5054df9d7c 100644 (file)
@@ -10,8 +10,8 @@ B<DO NOT USE> except in L<App::Codeowners> 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<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.
 
 =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<File::Codeowners::Util/find_codeowners_in_directory> instead.
 
-Find the F<CODEOWNERS> 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<CODEOWNERS>
-* F<docs/CODEOWNERS>
-* F<.bitbucket/CODEOWNERS>
-* F<.github/CODEOWNERS>
-* F<.gitlab/CODEOWNERS>
+=func run_command
+
+Deprecated.
+
+Use L<File::Codeowners::Util/run_command> 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<File::Codeowners::Util/run_git> 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<File::Codeowners::Util/git_ls_files> 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<File::Codeowners::Util/git_toplevel> 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<File::Codeowners::Util/unbackslash> 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<List::SomeUtils/zip-ARRAY1-ARRAY2-[-ARRAY3-...-]>.
+
+=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;
This page took 0.027369 seconds and 4 git commands to generate.