X-Git-Url: https://git.dogcows.com/gitweb?a=blobdiff_plain;f=lib%2FFile%2FCodeowners.pm;fp=lib%2FFile%2FCodeowners.pm;h=183208bfa93209ac59c8cce90a7ec77173c2c21e;hb=2ce4094a9656c27b2b83442e11b86e44306525bf;hp=ee50d99f14356e5bfd94cea9297ce200cdeaa412;hpb=7cd7abef6e13815b2deb4abb9c2e1edc1eee380e;p=chaz%2Fgit-codeowners diff --git a/lib/File/Codeowners.pm b/lib/File/Codeowners.pm index ee50d99..183208b 100644 --- a/lib/File/Codeowners.pm +++ b/lib/File/Codeowners.pm @@ -10,7 +10,7 @@ use Path::Tiny 0.089; use Scalar::Util qw(openhandle); use Text::Gitignore qw(build_gitignore_matcher); -our $VERSION = '0.48'; # VERSION +our $VERSION = '0.49'; # VERSION sub _croak { require Carp; Carp::croak(@_); } sub _usage { _croak("Usage: @_\n") } @@ -137,31 +137,33 @@ sub write_to_filepath { my $self = shift; my $path = shift or _usage(q{$codeowners->write_to_filepath($filepath)}); - path($path)->spew_utf8([map { "$_\n" } @{$self->write_to_array('')}]); + path($path)->spew_utf8([map { "$_\n" } @{$self->write_to_array}]); } sub write_to_fh { - my $self = shift; - my $fh = shift or _usage(q{$codeowners->write_to_fh($fh)}); + my $self = shift; + my $fh = shift or _usage(q{$codeowners->write_to_fh($fh)}); + my $charset = shift; - for my $line (@{$self->write_to_array}) { + for my $line (@{$self->write_to_array($charset)}) { print $fh "$line\n"; } } sub write_to_string { - my $self = shift; + my $self = shift; + my $charset = shift; - my $str = join("\n", @{$self->write_to_array}) . "\n"; + my $str = join("\n", @{$self->write_to_array($charset)}) . "\n"; return \$str; } sub write_to_array { my $self = shift; - my $charset = shift // 'UTF-8'; + my $charset = shift; my @format; @@ -187,7 +189,7 @@ sub write_to_array { } } - if ($charset) { + if (defined $charset) { $_ = encode($charset, $_) for @format; } return \@format; @@ -316,6 +318,29 @@ sub update_owners_by_project { } +sub rename_owner { + my $self = shift; + my $old_owner = shift; + my $new_owner = shift; + $old_owner && $new_owner or _usage(q{$codeowners->rename_owner($owner => $new_owner)}); + + $self->_clear; + + my $count = 0; + + for my $line (@{$self->_lines}) { + next if !exists $line->{owners}; + for (my $i = 0; $i < @{$line->{owners}}; ++$i) { + next if $line->{owners}[$i] ne $old_owner; + $line->{owners}[$i] = $new_owner; + ++$count; + } + } + + return $count; +} + + sub rename_project { my $self = shift; my $old_project = shift; @@ -405,7 +430,7 @@ File::Codeowners - Read and write CODEOWNERS files =head1 VERSION -version 0.48 +version 0.49 =head1 METHODS @@ -525,6 +550,14 @@ Set a new set of owners for all patterns under the given project. Nothing happens if the file does not have a project with the given name. +=head2 rename_owner + + $codeowners->rename_owner($old_name => $new_name); + +Rename an owner. + +Nothing happens if the file does not have an owner with the old name. + =head2 rename_project $codeowners->rename_project($old_name => $new_name);