1 package App
::Codeowners
::Util
;
2 # ABSTRACT: Grab bag of utility subs for Codeowners modules
9 use Exporter
qw(import);
14 find_codeowners_in_directory
15 find_nearest_codeowners
23 our $VERSION = '0.41'; # VERSION
26 sub find_nearest_codeowners
{
27 my $path = path
(shift || '.')->absolute;
29 while (!$path->is_rootdir) {
30 my $filepath = find_codeowners_in_directory
($path);
31 return $filepath if $filepath;
32 $path = $path->parent;
37 sub find_codeowners_in_directory
{
38 my $path = path
(shift) or die;
42 [qw(docs CODEOWNERS)],
43 [qw(.bitbucket CODEOWNERS)],
44 [qw(.github CODEOWNERS)],
45 [qw(.gitlab CODEOWNERS)],
48 for my $parts (@tries) {
49 my $try = $path->child(@$parts);
50 return $try if $try->is_file;
55 my @cmd = ('git', @_);
59 my ($child_in, $child_out);
60 my $pid = IPC
::Open2
::open2
($child_out, $child_in, @cmd);
63 binmode($child_out, ':encoding(UTF-8)');
64 chomp(my @lines = <$child_out>);
73 my $dir = shift || '.';
75 my @files = run_git
('-C', $dir, qw{ls-files}, @_);
77 return undef if !@files; ## no critic (Subroutines::ProhibitExplicitReturn)
79 # Depending on git's "core.quotepath" config, non-ASCII chars may be
80 # escaped (identified by surrounding dquotes), so try to unescape.
81 for my $file (@files) {
82 next if $file !~ /^"(.+)"$/;
84 $file = unbackslash
($file);
85 $file = decode
('UTF-8', $file);
92 my $dir = shift || '.';
94 my ($path) = run_git
('-C', $dir, qw{rev-parse --show-toplevel});
101 my $str = shift || '';
102 $str =~ s/\e\[[\d;]*m//g;
106 # The stringf code is from String::Format (thanks SREZIC), with changes:
107 # - Use Unicode::GCString for better Unicode character padding,
108 # - Strip ANSI color sequences,
109 # - Prevent 'Negative repeat count does nothing' warnings
111 my ($args, $orig, $alignment, $min_width,
112 $max_width, $passme, $formchar) = @_;
114 # For unknown escapes, return the orignial
115 return $orig unless defined $args->{$formchar};
117 $alignment = '+' unless defined $alignment;
119 my $replacement = $args->{$formchar};
120 if (ref $replacement eq 'CODE') {
121 # $passme gets passed to subrefs.
124 $replacement = $replacement->($passme);
128 if (eval { require Unicode
::GCString
}) {
129 my $gcstring = Unicode
::GCString-
>new(colorstrip
($replacement));
130 $replength = $gcstring->columns;
133 $replength = length colorstrip
($replacement);
136 $min_width ||= $replength;
137 $max_width ||= $replength;
139 # length of replacement is between min and max
140 if (($replength > $min_width) && ($replength < $max_width)) {
144 # length of replacement is longer than max; truncate
145 if ($replength > $max_width) {
146 return substr($replacement, 0, $max_width);
149 my $padding = $min_width - $replength;
150 $padding = 0 if $padding < 0;
152 # length of replacement is less than min: pad
153 if ($alignment eq '-') {
154 # left align; pad in front
155 return $replacement . ' ' x
$padding;
158 # right align, pad at end
159 return ' ' x
$padding . $replacement;
163 (-)? # left-align, rather than right
164 (\d
*)? # (optional) minimum field width
165 (?:\
.(\d
*))? # (optional) maximum field width
166 (\
{.*?\
})? # (optional) stuff inside
167 (\S
) # actual format character
170 my $format = shift || return;
171 my $args = UNIVERSAL
::isa
($_[0], 'HASH') ? shift : { @_ };
172 $args->{'n'} = "\n" unless exists $args->{'n'};
173 $args->{'t'} = "\t" unless exists $args->{'t'};
174 $args->{'%'} = "%" unless exists $args->{'%'};
176 $format =~ s/$regex/_replace($args, $1, $2, $3, $4, $5, $6)/ge;
181 # The unbacklash code is from String::Escape (thanks EVO), with changes:
182 # - Handle \a, \b, \f and \v (thanks Berk Akinci)
186 # Earlier definitions are preferred to later ones, thus we output \n not \x0d
188 ( map { $_ => $_ } ( '\\', '"', '$', '@' ) ),
189 ( 'r' => "\r", 'n' => "\n", 't' => "\t" ),
190 ( map { 'x' . unpack('H2', chr($_)) => chr($_) } (0..255) ),
191 ( map { sprintf('%03o', $_) => chr($_) } (0..255) ),
192 ( 'a' => "\x07", 'b' => "\x08", 'f' => "\x0c", 'v' => "\x0b" ),
194 $str =~ s/ (\A|\G|[^\\]) \\ ( [0-7]{3} | x[\da-fA-F]{2} | . ) / $1 . $unbackslash{lc($2)} /gsxe;
208 App::Codeowners::Util - Grab bag of utility subs for Codeowners modules
216 B<DO NOT USE> except in L<App::Codeowners> and related modules.
220 =head2 find_nearest_codeowners
222 $filepath = find_nearest_codeowners($dirpath);
224 Find the F<CODEOWNERS> file in the current working directory, or search in the
225 parent directory recursively until a F<CODEOWNERS> file is found.
227 Returns C<undef> if no F<CODEOWNERS> is found.
229 =head2 find_codeowners_in_directory
231 $filepath = find_codeowners_in_directory($dirpath);
233 Find the F<CODEOWNERS> file in a given directory. No recursive searching is done.
235 Returns the first of (or undef if none found):
249 F<.bitbucket/CODEOWNERS>
253 F<.github/CODEOWNERS>
257 F<.gitlab/CODEOWNERS>
263 Please report any bugs or feature requests on the bugtracker website
264 L<https://github.com/chazmcgarvey/git-codeowners/issues>
266 When submitting a bug or request, please include a test-file or a
267 patch to an existing test-file that illustrates the bug or desired
272 Charles McGarvey <chazmcgarvey@brokenzipper.com>
274 =head1 COPYRIGHT AND LICENSE
276 This software is copyright (c) 2019 by Charles McGarvey.
278 This is free software; you can redistribute it and/or modify it under
279 the same terms as the Perl 5 programming language system itself.