X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fgit-codeowners;a=blobdiff_plain;f=lib%2FFile%2FCodeowners.pm;h=9238a29a2939084390c3567eeadbda2480687839;hp=7212c238ae25ca8976444b0892b26056d0d1cce1;hb=39c158d76aefae4be3ff0c92e3977402c691323d;hpb=6568d5ded4f54ec5ff663b0196f10488910145b4 diff --git a/lib/File/Codeowners.pm b/lib/File/Codeowners.pm index 7212c23..9238a29 100644 --- a/lib/File/Codeowners.pm +++ b/lib/File/Codeowners.pm @@ -192,7 +192,7 @@ 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}]); } =method write_to_fh @@ -204,10 +204,11 @@ Format the file contents and write to a filehandle. =cut 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"; } } @@ -221,9 +222,10 @@ Format the file contents and return a reference to a formatted string. =cut 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; } @@ -237,7 +239,7 @@ Format the file contents as an arrayref of lines. sub write_to_array { my $self = shift; - my $charset = shift // 'UTF-8'; + my $charset = shift; my @format; @@ -263,7 +265,7 @@ sub write_to_array { } } - if ($charset) { + if (defined $charset) { $_ = encode($charset, $_) for @format; } return \@format; @@ -447,6 +449,38 @@ sub update_owners_by_project { return $count; } +=method 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. + +=cut + +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; +} + =method rename_project $codeowners->rename_project($old_name => $new_name);