File Coverage

blib/lib/SVN/Hook/Redispatch.pm
Criterion Covered Total %
statement 12 47 25.5
branch 1 20 5.0
condition 0 5 0.0
subroutine 4 5 80.0
pod 0 1 0.0
total 17 78 21.7


line stmt bran cond sub pod time code
1             package SVN::Hook::Redispatch;
2 1     1   792 use strict;
  1         2  
  1         27  
3 1     1   5 use Path::Class;
  1         2  
  1         50  
4 1     1   5 use SVN::Hook;
  1         1  
  1         5  
5              
6             sub import {
7 1     1   9 my $class = shift;
8 1         3 my $spec = shift;
9 1 50       12 return unless $spec;
10              
11 0           my $hook_base = Path::Class::File->new($0);
12              
13 0           my $type;
14             my $svnlook_arg;
15              
16             # $0 can be either hooks/_pre-commit/random_name or
17             # hooks/pre-commit itself
18              
19 0 0         if ($hook_base->parent =~ m'hooks$') { # the hook file itself
20 0           $type = $hook_base->basename;
21 0           $hook_base = $hook_base->parent->subdir("_".$type);
22             }
23             else {
24 0           $hook_base = $hook_base->parent;
25 0           $type = $hook_base;
26 0           $type =~ s{^.*/_}{};
27             }
28              
29             # if we are able to pull out the toplevel path
30 0 0         if ($type eq 'pre-commit') {
    0          
31 0           $svnlook_arg = "-t $_[1]";
32             }
33             elsif ($type eq 'post-commit') {
34 0           $svnlook_arg = "-r $_[1]";
35             }
36             else {
37             }
38              
39 0 0         my $ignore_error = $type =~ m/^post-/? 1 : 0;
40              
41 0 0         if (defined (my $dir = delete $spec->{''})) { # global ones
42 0           my @scripts = SVN::Hook::Script->load_from_dir
43             ( $hook_base.'/'.$dir );
44 0           SVN::Hook->run_scripts( \@scripts, $ignore_error, @_ );
45             }
46              
47 0 0         return unless $svnlook_arg;
48              
49 0           my $toplevel = $class->find_toplevel_change($_[0], $svnlook_arg);
50              
51 0           for (map { Path::Class::Dir->new_foreign('Unix', $_) } sort keys %$spec) {
  0            
52 0 0 0       next unless $_ eq $toplevel || $_->subsumes($toplevel);
53 0           my @scripts = SVN::Hook::Script->load_from_dir
54             ( $hook_base.'/'.$spec->{$_} );
55 0           SVN::Hook->run_scripts( \@scripts, $ignore_error, @_ );
56             }
57              
58             };
59              
60             sub find_toplevel_change {
61 0     0 0   my $class = shift;
62 0           my $repos = shift;
63 0           my $arg = shift;
64              
65 0   0       my $svnlook = $ENV{SVNLOOK} || 'svnlook';
66 0 0         open my $fh, '-|', "$svnlook dirs-changed $arg $repos"
67             or die "Unable to run svnlook: $!";
68 0           my $toplevel;
69 0           while (<$fh>) {
70 0           chomp;
71 0 0         if (!$toplevel) {
72 0           $toplevel = Path::Class::Dir->new_foreign('Unix', $_);
73             }
74             else {
75 0           while (!$toplevel->subsumes($_)) {
76 0           $toplevel = $toplevel->parent;
77             }
78              
79             }
80             }
81 0           return $toplevel;
82             }
83              
84             1;