/usr/share/perl5/SVN/Hooks/UpdateConfFile.pm is in libsvn-hooks-perl 1.34-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 | package SVN::Hooks::UpdateConfFile;
# ABSTRACT: Maintain the repository configuration versioned.
$SVN::Hooks::UpdateConfFile::VERSION = '1.34';
use strict;
use warnings;
use Carp;
use Data::Util qw(:check);
use File::Spec::Functions;
use File::Temp qw/tempdir/;
use Cwd qw/abs_path/;
use SVN::Hooks;
use Exporter qw/import/;
my $HOOK = 'UPDATE_CONF_FILE';
our @EXPORT = ($HOOK);
my @Config;
sub UPDATE_CONF_FILE {
my ($from, $to, @args) = @_;
is_string($from) || is_rx($from) or croak "$HOOK: invalid first argument.\n";
is_string($to) or croak "$HOOK: invalid second argument.\n";
(@args % 2) == 0 or croak "$HOOK: odd number of arguments.\n";
file_name_is_absolute($to) and croak "$HOOK: second argument cannot be an absolute pathname ($to).\n";
my %confs = (from => $from, to => $to);
my %args = @args;
for my $function (qw/validator generator actuator/) {
if (my $what = delete $args{$function}) {
if (is_code_ref($what)) {
$confs{$function} = $what;
} elsif (is_array_ref($what)) {
# This should point to list of command arguments
@$what > 0 or croak "$HOOK: $function argument must have at least one element.\n";
-x $what->[0] or croak "$HOOK: $function argument is not a valid command ($what->[0]).\n";
$confs{$function} = _functor($what);
} else {
croak "$HOOK: $function argument must be a CODE-ref or an ARRAY-ref.\n";
}
PRE_COMMIT(\&pre_commit);
}
}
if (my $rotate = delete $args{rotate}) {
$rotate =~ /^\d+$/ or croak "$HOOK: rotate argument must be numeric, not '$rotate'";
$rotate < 10 or croak "$HOOK: rotate argument must be less than 10, not '$rotate'";
$confs{rotate} = $rotate;
}
if (my $remove = delete $args{remove}) {
$confs{remove} = $remove;
}
keys %args == 0
or croak "$HOOK: invalid option names: ", join(', ', sort keys %args), ".\n";
push @Config, \%confs;
POST_COMMIT(\&post_commit);
return 1;
}
sub pre_commit {
my ($svnlook) = @_;
CONF:
foreach my $conf (@Config) {
if (my $validator = $conf->{validator}) {
my $from = $conf->{from};
for my $file ($svnlook->added(), $svnlook->updated()) {
if (is_string($from)) {
next if $file ne $from;
} else {
next if $file !~ $from;
}
my $text = $svnlook->cat($file);
if (my $generator = $conf->{generator}) {
$text = eval { $generator->($text, $file, $svnlook) };
defined $text
or croak "$HOOK: Generator aborted for: $file\n", $@, "\n";
}
my $validation = eval { $validator->($text, $file, $svnlook) };
defined $validation
or croak "$HOOK: Validator aborted for: $file\n", $@, "\n";
next CONF;
}
}
}
return;
}
sub post_commit {
my ($svnlook) = @_;
my $absbase = abs_path(catdir($SVN::Hooks::Repo, 'conf'));
foreach my $conf (@Config) {
my $from = $conf->{from};
for my $file ($svnlook->added(), $svnlook->updated()) {
my $to = _post_where_to($absbase, $file, $from, $conf->{to});
next unless defined $to;
my $text = $svnlook->cat($file);
if (my $generator = $conf->{generator}) {
$text = eval { $generator->($text, $file, $svnlook) };
defined $text or croak <<"EOS";
$HOOK: generator in post-commit aborted for: $file
This means that $file was committed but the associated
configuration file wasn't generated in the server at:
$to
Please, investigate the problem and re-commit the file.
Any error message produced by the generator appears below:
$@
EOS
}
# Create the directory where $to is to be created, if it doesn't
# already exist.
my $todir = (File::Spec->splitpath($to))[1];
unless (-d $todir) {
require File::Path;
File::Path::make_path($todir);
}
open my $fd, '>', "$to.new"
or croak "$HOOK: Can't open file \"$to.new\" for writing: $!\n";
print $fd $text;
close $fd;
_rotate($to, $conf->{rotate}) if $conf->{rotate};
rename "$to.new", $to;
if (my $actuator = $conf->{actuator}) {
my $rc = eval { $actuator->($text, $file, $svnlook) };
defined $rc or croak <<"EOS";
$HOOK: actuator in post-commit aborted for: $file
This means that $file was committed and the associated
configuration file was generated in the server at:
$to
But the actuator command that was called after the file generation
didn't work right.
Please, investigate the problem.
Any error message produced by the actuator appears below:
$@
EOS
}
}
if ($conf->{remove}) {
for my $file ($svnlook->deleted()) {
my $to = _post_where_to($absbase, $file, $from, $conf->{to});
next unless defined $to && -f $to;
if (my $rotate = $conf->{rotate}) {
_rotate($to, $rotate);
} else {
unlink $to or carp "$HOOK: can't unlink '$to'.\n";
}
}
}
}
return;
}
sub _functor {
my ($cmdlist) = @_;
my $cmd = join(' ', @$cmdlist);
return sub {
my ($text, $path, $svnlook) = @_;
my $temp = tempdir('UpdateConfFile.XXXXXX', TMPDIR => 1, CLEANUP => 1);
# FIXME: this is Unix specific!
open my $th, '>', "$temp/file"
or croak "Can't create $temp/file: $!";
print $th $text;
close $th;
local $ENV{SVNREPOPATH} = $svnlook->repo();
if (system("$cmd $temp/file $path $ENV{SVNREPOPATH} 1>$temp/output 2>$temp/error") == 0) {
return `cat $temp/output`;
} else {
croak `cat $temp/error`;
}
};
}
# Return the server-side absolute path mapping for the configuration file, or
# undef if $file doesn't match $from. $absbase is the absolute path to the
# repo's conf directory. $file is the path of a file added, modified, or
# deleted in the commit. $from and $to are the configured mapping.
sub _post_where_to {
my ($absbase, $file, $from, $to) = @_;
if (is_string($from)) {
return if $file ne $from;
} else {
return if $file !~ $from;
# interpolate backreferences
$to = eval qq{"$to"}; ## no critic (BuiltinFunctions::ProhibitStringyEval)
}
$to !~ m@(?:^|/)\.\.(?:/|$)@
or croak <<"EOS";
$HOOK: post-commit aborted for: $file
This means that $file was committed but the associated
configuration file wasn't generated because its specified
location ($to)
contains a '..' path component which is not accepted by this hook.
Please, correct the ${HOOK}'s second argument.
EOS
my $is_directory = ($to =~ s:/$::);
$to =~ s:^/+::;
my $abs_to = catfile($absbase, $to);
if ($is_directory || -d $abs_to) {
$abs_to = catfile($abs_to, (File::Spec->splitpath($file))[2]);
}
return $abs_to;
}
# Rotates file $to $rotate times.
sub _rotate {
my ($to, $rotate) = @_;
for (my $i=$rotate-1; $i >= 0; --$i) {
rename "$to.$i", sprintf("$to.%d", $i+1)
if -e "$to.$i";
}
rename $to, "$to.0"
if -e $to;
}
1; # End of SVN::Hooks::UpdateConfFile
__END__
=pod
=encoding UTF-8
=head1 NAME
SVN::Hooks::UpdateConfFile - Maintain the repository configuration versioned.
=head1 VERSION
version 1.34
=head1 SYNOPSIS
This SVN::Hooks plugin allows you to maintain the repository
configuration files under version control.
The repository configuration is usually kept in the directory C<conf>
under the directory where the repository was created. In a brand new
repository you see there the files C<authz>, C<passwd>, and
C<svnserve.conf>. It's too bad that these important files are usually
kept out of any version control system. This plugin tries to solve
this problem allowing you to keep these files versioned under the same
repository where they are used.
It's active in the C<pre-commit> and the C<post-commit> hooks.
It's configured by the following directive.
=head2 UPDATE_CONF_FILE(FROM, TO, @ARGS)
This directive makes that after a successful commit in which the file
FROM, under version control, have been added or modified, its newest
version is copied to TO.
FROM can be a string or a qr/Regexp/ specifying the file path relative
to the repository's root (e.g. "trunk/src/version.c" or
"qr:^conf/(\w+).conf$:").
TO must be a relative path indicating where the original file must be copied
to below the C</repo/conf> directory in the server. It can be an explicit
file name or a directory, in which case the basename of FROM is used as the
name of the destination file. Non-existing directory components of TO are
automatically created.
Note that if the path doesn't exist the hook assumes that it should be a
file. To make sure it's understood as a directory you may end it with a
forward slash (/).
If FROM is a qr/Regexp/, TO is evaluated as a string in order to allow
for the interpolation of capture buffers from the regular
expression. This is useful to map the copy operation to a different
directory structure. For example, this configuration
"qr:^conf/(\w+).conf$: => '$1.conf'" updates any .conf file in the
repository conf directory.
The optional @ARGS must be a sequence of pairs like these:
=over
=item validator => ARRAY or CODE
A validator is a function or a command (specified by an array of
strings that will be passed to the shell) that will check the contents
of FROM in the pre-commit hook to see if it's valid. If there is no
validator, the contents are considered valid.
The function receives three arguments:
=over
=item A string with the contents of FROM
=item A string with the relative path to FROM in the repository
=item An SVN::Look object representing the commit transaction
=back
The command is called with three arguments:
=over
=item The path to a temporary copy of FROM
=item The relative path to FROM in the repository
=item The path to the root of the repository in the server
=back
=item generator => ARRAY or CODE
A generator is a function or a command (specified by an array of
strings that will be passed to the shell) that will transform the
contents of FROM in the post-commit hook before copying it to TO. If
there is no generator, the contents are copied as is.
The function receives the same three arguments as the validator's
function above.
The command is called with the same three arguments as the validator's
command above.
=item actuator => ARRAY or CODE
An actuator is a function or a command (specified by an array of
strings that will be passed to the shell) that will be invoked after a
successful commit of FROM in the post-commit hook.
The function receives the same three arguments as the validator's
function above.
The command is called with the same three arguments as the validator's
command above.
=item rotate => NUMBER
By default, after each successful commit the TO file is overwritten by
the new contents of FROM. With this option, the last NUMBER versions
of TO are kept on disk with numeric suffixes ranging from C<.0> to
C<.NUMBER-1>. This can be useful, for instance, in case you manage to
commit a wrong authz file that denies any subsequent commit.
=item remove => BOOL
By default, if FROM is B<deleted> in the commit, nothing happens to
TO. If you want to have the file TO removed from the repository when
FROM is deleted, set this option to a true value such as '1'.
=back
UPDATE_CONF_FILE(
'conf/authz' => 'authz',
validator => ['/usr/local/bin/svnauthcheck'],
generator => ['/usr/local/bin/authz-expand-includes'],
actuator => ['/usr/local/bin/notify-auth-change'],
rotate => 2,
);
UPDATE_CONF_FILE(
'conf/svn-hooks.conf' => 'svn-hooks.conf',
validator => [qw(/usr/bin/perl -c)],
actuator => sub {
my ($contents, $file) = @_;
die "Can't use Gustavo here." if $contents =~ /gustavo/;
},
rotate => 2,
);
UPDATE_CONF_FILE(
qr:/file(\n+)$:' => 'subdir/$1/file',
rotate => 2,
remove => 1,
);
=for Pod::Coverage post_commit pre_commit
=head1 AUTHOR
Gustavo L. de M. Chaves <gnustavo@cpan.org>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2016 by CPqD <www.cpqd.com.br>.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
|