| 1 | package SVN::Hooks::CheckProperty; | ||
| 2 | # ABSTRACT: Check properties in added files. | ||
| 3 | $SVN::Hooks::CheckProperty::VERSION = '1.35'; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | use Carp; | ||
| 8 | use Data::Util qw(:check); | ||
| 9 | use SVN::Hooks; | ||
| 10 | |||
| 11 | use Exporter qw/import/; | ||
| 12 | my $HOOK = 'CHECK_PROPERTY'; | ||
| 13 | our @EXPORT = ($HOOK); | ||
| 14 | |||
| 15 | |||
| 16 | my @Checks; | ||
| 17 | |||
| 18 | sub CHECK_PROPERTY { | ||
| 19 | my ($where, $prop, $what) = @_; | ||
| 20 | |||
| 21 | is_string($where) || is_rx($where) | ||
| 22 | or croak "$HOOK: first argument must be a STRING or a qr/Regexp/\n"; | ||
| 23 | is_string($prop) | ||
| 24 | or croak "$HOOK: second argument must be a STRING\n"; | ||
| 25 | ! defined $what || is_string($what) || is_rx($what) | ||
| 26 | or croak "$HOOK: third argument must be undefined, or a NUMBER, or a STRING, or a qr/Regexp/\n"; | ||
| 27 | |||
| 28 | push @Checks, [$where, $prop => $what]; | ||
| 29 | |||
| 30 | PRE_COMMIT(\&pre_commit); | ||
| 31 | |||
| 32 | return 1; | ||
| 33 | } | ||
| 34 | |||
| 35 | sub pre_commit { | ||
| 36 | my ($svnlook) = @_; | ||
| 37 | |||
| 38 | my @errors; | ||
| 39 | |||
| 40 | foreach my $added ($svnlook->added()) { | ||
| 41 | foreach my $check (@Checks) { | ||
| 42 | my ($where, $prop, $what) = @$check; | ||
| 43 | if (is_rx($where) && $added =~ $where | ||
| 44 | || is_string($where) && $where eq substr($added, 0, length $where)) { | ||
| 45 | my $props = $svnlook->proplist($added); | ||
| 46 | my $is_set = exists $props->{$prop}; | ||
| 47 | if (! defined $what) { | ||
| 48 | $is_set or push @errors, "property $prop must be set for: $added"; | ||
| 49 | } elsif (is_value($what)) { | ||
| 50 | if (is_integer($what)) { | ||
| 51 | if ($what) { | ||
| 52 | $is_set or push @errors, "property $prop must be set for: $added"; | ||
| 53 | } else { | ||
| 54 | $is_set and push @errors, "property $prop must not be set for: $added"; | ||
| 55 | } | ||
| 56 | } elsif (! $is_set) { | ||
| 57 | push @errors, "property $prop must be set to \"$what\" for: $added"; | ||
| 58 | } elsif ($props->{$prop} ne $what) { | ||
| 59 | push @errors, "property $prop must be set to \"$what\" and not to \"$props->{$prop}\" for: $added"; | ||
| 60 | } | ||
| 61 | } elsif (! $is_set) { | ||
| 62 | push @errors, "property $prop must be set and match \"$what\" for: $added"; | ||
| 63 | } elsif ($props->{$prop} !~ $what) { | ||
| 64 | push @errors, "property $prop must match \"$what\" but is \"$props->{$prop}\" for: $added"; | ||
| 65 | } | ||
| 66 | } | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | croak join("\n", "$HOOK:", @errors), "\n" | ||
| 71 | if @errors; | ||
| 72 | |||
| 73 | return; | ||
| 74 | } | ||
| 75 | |||
| 76 | 1; # End of SVN::Hooks::CheckProperty | ||
| 77 | |||
| 78 | __END__ |