From: Charles McGarvey Date: Sun, 22 Mar 2020 07:26:13 +0000 (-0600) Subject: add update_owner method to File::Codeowners X-Git-Tag: v0.49~1 X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fgit-codeowners;a=commitdiff_plain;h=39c158d76aefae4be3ff0c92e3977402c691323d add update_owner method to File::Codeowners --- diff --git a/lib/File/Codeowners.pm b/lib/File/Codeowners.pm index f4caf47..9238a29 100644 --- a/lib/File/Codeowners.pm +++ b/lib/File/Codeowners.pm @@ -449,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);