]> Dogcows Code - chaz/git-codeowners/blobdiff - lib/File/Codeowners.pm
add rename_project function to File::Codeowners
[chaz/git-codeowners] / lib / File / Codeowners.pm
index fb3ec552c889bd6431be1ac7dd9d3c0c000f3fa3..7212c238ae25ca8976444b0892b26056d0d1cce1 100644 (file)
@@ -6,7 +6,7 @@ use warnings;
 use strict;
 
 use Encode qw(encode);
-use Path::Tiny;
+use Path::Tiny 0.089;
 use Scalar::Util qw(openhandle);
 use Text::Gitignore qw(build_gitignore_matcher);
 
@@ -98,11 +98,13 @@ sub parse_from_fh {
         }
         elsif ($line =~ /^\h*#(.*)/) {
             my $comment = $1;
+            my $project;
             if ($comment =~ /^\h*Project:\h*(.+?)\h*$/i) {
-                $current_project = $1 || undef;
+                $project = $current_project = $1 || undef;
             }
             $lines[$lineno] = {
                 comment => $comment,
+                $project ? (project => $project) : (),
             };
         }
         elsif ($line =~ /^\h*$/) {
@@ -355,6 +357,31 @@ sub patterns {
     return $patterns;
 }
 
+=method projects
+
+    $projects = $codeowners->projects;
+
+Get an arrayref of all projects defined.
+
+=cut
+
+sub projects {
+    my $self  = shift;
+
+    return $self->{projects} if $self->{projects};
+
+    my %projects;
+    for my $line (@{$self->_lines}) {
+        my $project = $line->{project};
+        $projects{$project}++ if $project;
+    }
+
+    my $projects = [sort keys %projects];
+    $self->{projects} = $projects;
+
+    return $projects;
+}
+
 =method update_owners
 
     $codeowners->update_owners($pattern => \@new_owners);
@@ -376,11 +403,78 @@ sub update_owners {
 
     $self->_clear;
 
+    my $count = 0;
+
     for my $line (@{$self->_lines}) {
         next if !$line->{pattern};
         next if $pattern ne $line->{pattern};
         $line->{owners} = [@$owners];
+        ++$count;
     }
+
+    return $count;
+}
+
+=method update_owners_by_project
+
+    $codeowners->update_owners_by_project($project => \@new_owners);
+
+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.
+
+=cut
+
+sub update_owners_by_project {
+    my $self    = shift;
+    my $project = shift;
+    my $owners  = shift;
+    $project && $owners or _usage(q{$codeowners->update_owners_by_project($project => \@owners)});
+
+    $owners = [$owners] if ref($owners) ne 'ARRAY';
+
+    $self->_clear;
+
+    my $count = 0;
+
+    for my $line (@{$self->_lines}) {
+        next if !$line->{project} || !$line->{owners};
+        next if $project ne $line->{project};
+        $line->{owners} = [@$owners];
+        ++$count;
+    }
+
+    return $count;
+}
+
+=method rename_project
+
+    $codeowners->rename_project($old_name => $new_name);
+
+Rename a project.
+
+Nothing happens if the file does not have a project with the old name.
+
+=cut
+
+sub rename_project {
+    my $self        = shift;
+    my $old_project = shift;
+    my $new_project = shift;
+    $old_project && $new_project or _usage(q{$codeowners->rename_project($project => $new_project)});
+
+    $self->_clear;
+
+    my $count = 0;
+
+    for my $line (@{$self->_lines}) {
+        next if !exists $line->{project} || $old_project ne $line->{project};
+        $line->{project} = $new_project;
+        $line->{comment} = " Project: $new_project" if exists $line->{comment};
+        ++$count;
+    }
+
+    return $count;
 }
 
 =method append
@@ -497,6 +591,7 @@ sub _clear {
     delete $self->{match_lines};
     delete $self->{owners};
     delete $self->{patterns};
+    delete $self->{projects};
 }
 
 1;
This page took 0.025429 seconds and 4 git commands to generate.