| 1 | package SVN::Hooks::UpdateConfFile; | ||
| 2 | # ABSTRACT: Maintain the repository configuration versioned. | ||
| 3 | $SVN::Hooks::UpdateConfFile::VERSION = '1.35'; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | use Carp; | ||
| 8 | use Data::Util qw(:check); | ||
| 9 | use File::Spec::Functions; | ||
| 10 | use File::Temp qw/tempdir/; | ||
| 11 | use Cwd qw/abs_path/; | ||
| 12 | use SVN::Hooks; | ||
| 13 | |||
| 14 | use Exporter qw/import/; | ||
| 15 | my $HOOK = 'UPDATE_CONF_FILE'; | ||
| 16 | our @EXPORT = ($HOOK); | ||
| 17 | |||
| 18 | |||
| 19 | my @Config; | ||
| 20 | |||
| 21 | sub UPDATE_CONF_FILE { | ||
| 22 | my ($from, $to, @args) = @_; | ||
| 23 | |||
| 24 | is_string($from) || is_rx($from) or croak "$HOOK: invalid first argument.\n"; | ||
| 25 | is_string($to) or croak "$HOOK: invalid second argument.\n"; | ||
| 26 | (@args % 2) == 0 or croak "$HOOK: odd number of arguments.\n"; | ||
| 27 | file_name_is_absolute($to) and croak "$HOOK: second argument cannot be an absolute pathname ($to).\n"; | ||
| 28 | |||
| 29 | my %confs = (from => $from, to => $to); | ||
| 30 | |||
| 31 | my %args = @args; | ||
| 32 | |||
| 33 | for my $function (qw/validator generator actuator/) { | ||
| 34 | if (my $what = delete $args{$function}) { | ||
| 35 | if (is_code_ref($what)) { | ||
| 36 | $confs{$function} = $what; | ||
| 37 | } elsif (is_array_ref($what)) { | ||
| 38 | # This should point to list of command arguments | ||
| 39 | @$what > 0 or croak "$HOOK: $function argument must have at least one element.\n"; | ||
| 40 | $confs{$function} = _functor($what); | ||
| 41 | } else { | ||
| 42 | croak "$HOOK: $function argument must be a CODE-ref or an ARRAY-ref.\n"; | ||
| 43 | } | ||
| 44 | |||
| 45 | PRE_COMMIT(\&pre_commit); | ||
| 46 | } | ||
| 47 | } | ||
| 48 | |||
| 49 | if (my $rotate = delete $args{rotate}) { | ||
| 50 | $rotate =~ /^\d+$/ or croak "$HOOK: rotate argument must be numeric, not '$rotate'"; | ||
| 51 | $rotate < 10 or croak "$HOOK: rotate argument must be less than 10, not '$rotate'"; | ||
| 52 | $confs{rotate} = $rotate; | ||
| 53 | } | ||
| 54 | |||
| 55 | if (my $remove = delete $args{remove}) { | ||
| 56 | $confs{remove} = $remove; | ||
| 57 | } | ||
| 58 | |||
| 59 | keys %args == 0 | ||
| 60 | or croak "$HOOK: invalid option names: ", join(', ', sort keys %args), ".\n"; | ||
| 61 | |||
| 62 | push @Config, \%confs; | ||
| 63 | |||
| 64 | POST_COMMIT(\&post_commit); | ||
| 65 | |||
| 66 | return 1; | ||
| 67 | } | ||
| 68 | |||
| 69 | sub pre_commit { | ||
| 70 | my ($svnlook) = @_; | ||
| 71 | |||
| 72 | CONF: | ||
| 73 | foreach my $conf (@Config) { | ||
| 74 | if (my $validator = $conf->{validator}) { | ||
| 75 | my $from = $conf->{from}; | ||
| 76 | for my $file ($svnlook->added(), $svnlook->updated()) { | ||
| 77 | if (is_string($from)) { | ||
| 78 | next if $file ne $from; | ||
| 79 | } else { | ||
| 80 | next if $file !~ $from; | ||
| 81 | } | ||
| 82 | |||
| 83 | my $text = $svnlook->cat($file); | ||
| 84 | |||
| 85 | if (my $generator = $conf->{generator}) { | ||
| 86 | $text = eval { $generator->($text, $file, $svnlook) }; | ||
| 87 | defined $text | ||
| 88 | or croak "$HOOK: Generator aborted for: $file\n", $@, "\n"; | ||
| 89 | } | ||
| 90 | |||
| 91 | my $validation = eval { $validator->($text, $file, $svnlook) }; | ||
| 92 | defined $validation | ||
| 93 | or croak "$HOOK: Validator aborted for: $file\n", $@, "\n"; | ||
| 94 | |||
| 95 | next CONF; | ||
| 96 | } | ||
| 97 | } | ||
| 98 | } | ||
| 99 | return; | ||
| 100 | } | ||
| 101 | |||
| 102 | sub post_commit { | ||
| 103 | my ($svnlook) = @_; | ||
| 104 | |||
| 105 | my $absbase = abs_path(catdir($SVN::Hooks::Repo, 'conf')); | ||
| 106 | |||
| 107 | foreach my $conf (@Config) { | ||
| 108 | my $from = $conf->{from}; | ||
| 109 | for my $file ($svnlook->added(), $svnlook->updated()) { | ||
| 110 | my $to = _post_where_to($absbase, $file, $from, $conf->{to}); | ||
| 111 | next unless defined $to; | ||
| 112 | |||
| 113 | my $text = $svnlook->cat($file); | ||
| 114 | |||
| 115 | if (my $generator = $conf->{generator}) { | ||
| 116 | $text = eval { $generator->($text, $file, $svnlook) }; | ||
| 117 | defined $text or croak <<"EOS"; | ||
| 118 | $HOOK: generator in post-commit aborted for: $file | ||
| 119 | |||
| 120 | This means that $file was committed but the associated | ||
| 121 | configuration file wasn't generated in the server at: | ||
| 122 | |||
| 123 | $to | ||
| 124 | |||
| 125 | Please, investigate the problem and re-commit the file. | ||
| 126 | |||
| 127 | Any error message produced by the generator appears below: | ||
| 128 | |||
| 129 | $@ | ||
| 130 | EOS | ||
| 131 | } | ||
| 132 | |||
| 133 | # Create the directory where $to is to be created, if it doesn't | ||
| 134 | # already exist. | ||
| 135 | my $todir = (File::Spec->splitpath($to))[1]; | ||
| 136 | unless (-d $todir) { | ||
| 137 | require File::Path; | ||
| 138 | File::Path::make_path($todir); | ||
| 139 | } | ||
| 140 | |||
| 141 | open my $fd, '>', "$to.new" | ||
| 142 | or croak "$HOOK: Can't open file \"$to.new\" for writing: $!\n"; | ||
| 143 | print $fd $text; | ||
| 144 | close $fd; | ||
| 145 | |||
| 146 | _rotate($to, $conf->{rotate}) if $conf->{rotate}; | ||
| 147 | |||
| 148 | rename "$to.new", $to; | ||
| 149 | |||
| 150 | if (my $actuator = $conf->{actuator}) { | ||
| 151 | my $rc = eval { $actuator->($text, $file, $svnlook) }; | ||
| 152 | defined $rc or croak <<"EOS"; | ||
| 153 | $HOOK: actuator in post-commit aborted for: $file | ||
| 154 | |||
| 155 | This means that $file was committed and the associated | ||
| 156 | configuration file was generated in the server at: | ||
| 157 | |||
| 158 | $to | ||
| 159 | |||
| 160 | But the actuator command that was called after the file generation | ||
| 161 | didn't work right. | ||
| 162 | |||
| 163 | Please, investigate the problem. | ||
| 164 | |||
| 165 | Any error message produced by the actuator appears below: | ||
| 166 | |||
| 167 | $@ | ||
| 168 | EOS | ||
| 169 | } | ||
| 170 | } | ||
| 171 | if ($conf->{remove}) { | ||
| 172 | for my $file ($svnlook->deleted()) { | ||
| 173 | my $to = _post_where_to($absbase, $file, $from, $conf->{to}); | ||
| 174 | next unless defined $to && -f $to; | ||
| 175 | if (my $rotate = $conf->{rotate}) { | ||
| 176 | _rotate($to, $rotate); | ||
| 177 | } else { | ||
| 178 | unlink $to or carp "$HOOK: can't unlink '$to'.\n"; | ||
| 179 | } | ||
| 180 | } | ||
| 181 | } | ||
| 182 | } | ||
| 183 | return; | ||
| 184 | } | ||
| 185 | |||
| 186 | sub _functor { | ||
| 187 | my ($cmdlist) = @_; | ||
| 188 | my $cmd = join(' ', @$cmdlist); | ||
| 189 | |||
| 190 | return sub { | ||
| 191 | my ($text, $path, $svnlook) = @_; | ||
| 192 | |||
| 193 | my $temp = tempdir('UpdateConfFile.XXXXXX', TMPDIR => 1, CLEANUP => 1); | ||
| 194 | |||
| 195 | # FIXME: this is Unix specific! | ||
| 196 | open my $th, '>', "$temp/file" | ||
| 197 | or croak "Can't create $temp/file: $!"; | ||
| 198 | print $th $text; | ||
| 199 | close $th; | ||
| 200 | |||
| 201 | local $ENV{SVNREPOPATH} = $svnlook->repo(); | ||
| 202 | if (system("$cmd $temp/file $path $ENV{SVNREPOPATH} 1>$temp/output 2>$temp/error") == 0) { | ||
| 203 | return `cat $temp/output`; | ||
| 204 | } else { | ||
| 205 | croak `cat $temp/error`; | ||
| 206 | } | ||
| 207 | }; | ||
| 208 | } | ||
| 209 | |||
| 210 | # Return the server-side absolute path mapping for the configuration file, or | ||
| 211 | # undef if $file doesn't match $from. $absbase is the absolute path to the | ||
| 212 | # repo's conf directory. $file is the path of a file added, modified, or | ||
| 213 | # deleted in the commit. $from and $to are the configured mapping. | ||
| 214 | |||
| 215 | sub _post_where_to { | ||
| 216 | my ($absbase, $file, $from, $to) = @_; | ||
| 217 | |||
| 218 | if (is_string($from)) { | ||
| 219 | return if $file ne $from; | ||
| 220 | } else { | ||
| 221 | return if $file !~ $from; | ||
| 222 | # interpolate backreferences | ||
| 223 | $to = eval qq{"$to"}; ## no critic (BuiltinFunctions::ProhibitStringyEval) | ||
| 224 | } | ||
| 225 | |||
| 226 | $to !~ m@(?:^|/)\.\.(?:/|$)@ | ||
| 227 | or croak <<"EOS"; | ||
| 228 | $HOOK: post-commit aborted for: $file | ||
| 229 | |||
| 230 | This means that $file was committed but the associated | ||
| 231 | configuration file wasn't generated because its specified | ||
| 232 | location ($to) | ||
| 233 | contains a '..' path component which is not accepted by this hook. | ||
| 234 | |||
| 235 | Please, correct the ${HOOK}'s second argument. | ||
| 236 | EOS | ||
| 237 | |||
| 238 | my $is_directory = ($to =~ s:/$::); | ||
| 239 | |||
| 240 | $to =~ s:^/+::; | ||
| 241 | |||
| 242 | my $abs_to = catfile($absbase, $to); | ||
| 243 | if ($is_directory || -d $abs_to) { | ||
| 244 | $abs_to = catfile($abs_to, (File::Spec->splitpath($file))[2]); | ||
| 245 | } | ||
| 246 | |||
| 247 | return $abs_to; | ||
| 248 | } | ||
| 249 | |||
| 250 | # Rotates file $to $rotate times. | ||
| 251 | |||
| 252 | sub _rotate { | ||
| 253 | my ($to, $rotate) = @_; | ||
| 254 | for (my $i=$rotate-1; $i >= 0; --$i) { | ||
| 255 | rename "$to.$i", sprintf("$to.%d", $i+1) | ||
| 256 | if -e "$to.$i"; | ||
| 257 | } | ||
| 258 | rename $to, "$to.0" | ||
| 259 | if -e $to; | ||
| 260 | } | ||
| 261 | |||
| 262 | 1; # End of SVN::Hooks::UpdateConfFile | ||
| 263 | |||
| 264 | __END__ |