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
|
#!/usr/bin/perl -w
package SVN::Notify::Mirror;
use base qw/SVN::Notify/;
use strict;
use vars qw ($VERSION);
$VERSION = 0.038;
__PACKAGE__->register_attributes(
'ssh_host' => 'ssh-host=s',
'ssh_user' => 'ssh-user:s',
'ssh_tunnel' => 'ssh-tunnel:s',
'ssh_identity' => 'ssh-identity:s',
'svn_binary' => 'svn-binary:s',
'tag_regex' => 'tag-regex:s',
'minimal' => 'minimal',
);
sub prepare {
my $self = shift;
$self->prepare_recipients;
$self->prepare_files;
}
sub execute {
my ($self) = @_;
return unless defined $self->to;
$self->svn_binary( $ENV{SVN} || SVN::Notify->find_exe('svn') )
unless $self->svn_binary;
foreach my $to ( $self->to ) {
my $command = 'update';
my @args = (
-r => $self->revision,
);
# need to swap function calls for backwards compatibility for now
if ( defined $self->ssh_host
and not $self->isa('SVN::Notify::Mirror::SSH') )
{
no warnings 'redefine';
warn "Deprecated - please use SVN::Notify::Mirror::SSH directly";
require SVN::Notify::Mirror::SSH;
*_cd_run = \&SVN::Notify::Mirror::SSH::_cd_run;
}
# deal with the possible switch case
if ( defined $self->tag_regex ) {
$DB::single = 1;
$command = 'switch';
my $regex = $self->tag_regex;
my ($tag) = grep /$regex/, @{$self->{'files'}->{'A'}};
$tag =~ s/^.+\/tags\/(.+)/$1/;
return unless $tag;
my @message = $self->_cd_run(
$to,
$self->svn_binary,
'info',
);
my $URL = (split ": ", $message[1], 2)[1];
if ( $URL =~ m/^(.+\/tags\/).+$/m ) {
my $url = $1;
$tag = $url.$tag;
}
push @args, $tag;
}
if ( $self->minimal ) {
# perform minimal update only
my @paths;
my $prefix = $self->{'handle_path'}; # simple case
unless ( $prefix ) {
# hard case
$DB::single = 1;
my @message = $self->_cd_run($to, $self->svn_binary, 'info');
my $URL = (split ": ", $message[1], 2)[1];
my $ROOT = (split ": ", $message[2], 2)[1];
$ROOT .= '/' unless $ROOT =~ m:/$:;
($prefix = $URL) =~ s/$ROOT//;
}
foreach my $files ( values %{ $self->files } ) {
push @paths, map { s/$prefix// && $_ } @{ $files };
}
$to .= '/'. _shortest_path(@paths);
}
print join("\n",
$self->_cd_run(
$to,
$self->svn_binary,
$command,
@args,
)
);
}
}
sub _cd_run {
my ($self, $path, $binary, $command, @args) = @_;
my @message;
my $cmd ="$binary $command " . join(" ",@args);
chdir ($path) or die "Couldn't CD to $path: $!";
open my $RUN, '-|', $cmd
or die "Running [$cmd] failed with $?: $!";
while (<$RUN>) {
chomp;
push @message, $_;
}
close $RUN;
return (@message);
}
sub _shortest_path {
my @dirs = @_;
my @shortest;
DIR: foreach my $thisdir (@dirs) {
my @this = split "/", $thisdir;
pop @this; # either remove the filename or the last directory entry
unless (@shortest) {
# if we don't have anything yet
@shortest = @this;
next DIR;
}
if ( $#shortest > $#this ) {
# swap the shorter path around
my @temp = @shortest;
@shortest = @this;
@this = @temp;
}
while ( $shortest[$#shortest] ne $this[$#shortest] ) {
# keep removing the last term until we match
pop @shortest;
}
}
return join "/", @shortest;
}
1;
__END__
########################################### main pod documentation begin ##
=head1 NAME
SVN::Notify::Mirror - Keep a mirrored working copy of a repository path
=head1 SYNOPSIS
Use F<svnnotify> in F<post-commit>:
svnnotify --repos-path "$1" --revision "$2" \
--handler Mirror --to "/path/to/www/htdocs" \
[--svn-binary /full/path/to/svn] \
[--tag-regex "regex"]
[--minimal]
or better yet, use L<SVN::Notify::Config> for a more
sophisticated setup:
#!/usr/bin/perl -MSVN::Notify::Config=$0
--- #YAML:1.0
'':
PATH: "/usr/bin:/usr/local/bin"
'path/in/repository':
handler: Mirror
minimal: 1
to: "/path/to/www/htdocs"
'some/other/path/in/repository':
handler: Mirror
to: "/path/to/remote/www/htdocs"
'some/project/tags':
handler: Mirror
to: "/path/to/another/dir"
tag-regex: "TRUNK-"
=head1 DESCRIPTION
Keep a directory in sync with a portion of a Subversion repository.
Typically used to keep a development web server in sync with the changes
made to the repository. This directory can either be on the same box as
the repository itself, or it can be remote (via SSH connection).
=head1 USAGE
Depending on whether the target is a L<Local Mirror> or a L<Remote
Mirror>, there are different options available. All options are
available either as a commandline option to svnnotify or as a hash
key in L<SVN::Notify::Config> (see their respective documentation for
more details).
=head2 Common Options
In addition to all of the options available to svnnotify, there is a
single global option that affects both L<Local Mirror> and L<Remote
Mirror> targets:
=over 4
=item minimal
If the C<--minimal> flag is passed on the commandline (or a hash entry
created in a config file), then the update will occur at the shortest common
path of all files/dirs that are changed for that revision. For example,
using the example config file above, the following changes occured in
rev 23:
Changed paths:
M path/in/repository/subdir/subdir2/file.1
A path/in/repository/subdir/file.2
M path/in/repository/subdir/subdir3/file.3
B<and> the checked-out mirror was in C</path/to/www/htdocs>, then the
update would be executed in the C</path/to/www/htdocs/subdir> directory.
This is the shorted common path of the updated files.
NOTE: when using the SVN::Notify::Config mode, the matching path (in the
above example C<path/in/repository>) is stripped off of the shortest
common path prior to appending that to the destination path (the C<to:>
key). It is assumed that a matching key is the "root" of the working
copy.
=back
=head2 Working Copy on Mirror
Because 'svn export' is not able to be consistently updated, the
sync'd directory must be a full working copy, and if you are running
Apache, you should add lines like the following to your Apache
configuration file:
# Disallow browsing of Subversion working copy
# administrative directories.
<DirectoryMatch "^/.*/\.svn/">
Order deny,allow
Deny from all
</DirectoryMatch>
The files in the working copy must be writeable (preferrably owned)
by the user identity executing the hook script (this is the user
identity that is running Apache or svnserve respectively).
=head2 Local Mirror
Used for directories local to the repository itself (NFS or other
network mounted drives count). The only required options are:
=over 4
=item * handler = Mirror
Specifies that this module is called to process the Notify event.
=item * to = /path/to/working/copy
Specified which directory should be updated.
=back
=head2 Remote Mirror
Used for mirrors on some other box, e.g. a web server in a DMZ
network. See L<SVN::Notify::Mirror::SSH> or L<SVN::Notify::Mirror::Rsync>
for more details. All C<ssh-*> options are now deprecated in the
base class and support for them will be removed in the next major release.
Please update your configurations (see L<SVN::Notify::Mirror::SSH> for
details).
=over 4
=head2 Methods of Mirroring
There are two schemes to keep a directory synced to a path in the
repository:
=over 4
=item 1. Update a checked out working copy
This is the normal mode of operation and is commonly used to keep a
test web server in sync with the repository on every commit.
=item 2. Switch a working copy to a new tag
This is the preferred method when you want to keep a production web
server up to date with only specific revisions, through the use of
smart tagging.
=back
For the latter case, L<SVN::Notify::Mirror> can be configured to
monitor a path in the repository and only trigger an update when
the path matches a specific regular expression, and do so by switching
the mirrored path to the new tag.
=over 4
=item * tag-regex
This optional parameter works with any L<Local Mirror> or L<Remote Mirror>,
using any of the applicable transfer methods (currently local, SSH, or
Rsync). The C<--tag-regex> parameter takes a string which will be
interpreted as a conventional Perl regex, and only those repository
paths which match the regex will be updated. The regex also determines
what the mirrored directory will be switched to.
For example, using a SVN::Notify::Config file (which is the most useful
way to employ this option):
#!/usr/bin/perl -MSVN::Notify::Config=$0
--- #YAML:1.0
'':
PATH: "/usr/bin:/usr/local/bin"
'project1/trunk':
handler: Mirror
to: "/path/to/test/htdocs"
'project1/tags':
handler: Mirror
to: "/path/to/production/htdocs"
tag-regex: "TRUNK-"
This would have the effect of keeping the path C</path/to/test/htdocs>
in sync on every commit, but C</path/to/production/htdocs> would be
switched only when a tag was created in C<project1/tags> that matched
the string C<TRUNK->. B<NOTE:> this is not a sophisticated regex; in
particular you should not employ anchors, since the URL is not split
into repos-root and path segments before testing.
To initialize the switch directory, you must perform an initial
checkout like this:
$ svn checkout file://$REPOS/project1/tags/TRUNK-0_0_1
where C<TRUNK-0_0_1> is the name of any path in the C<.../tags/>
folder.
=back
=head1 PREREQUISITES (Optional and otherwise)
The only mandatory prerequisite is SVN::Notify (obviously), but the
presence of several other modules will enable other features:
=over 4
=item SVN::Notify::Config
Permits a YAML config file to be used as the entire postcommit script, like
the second example in the L<SYNOPSIS> above.
=item Net::SSH
Required to use SSH to update a remote working copy. See
L<SVN::Notify::Mirror::SSH> for usage of that transport method.
=item File::Rsync
Required to use rsync to update a remote working copy See
L<SVN::Notify::Mirror::RSync> for usage of that transport method..
=back
If you install any of these modules after installing SVN::Notify::Mirror,
those features will be available immediately (though you can rerun the
install in order to see the tests).
=head1 AUTHOR
John Peacock <jpeacock@cpan.org>
=head1 COPYRIGHT
Copyright (c) 2005-2008 John Peacock
This program is free software; you can redistribute
it and/or modify it under the same terms as Perl itself.
The full text of the license can be found in the
LICENSE file included with this module.
=head1 SEE ALSO
L<SVN::Notify>, L<SVN::Notify::Config>
=cut
############################################# main pod documentation end ##
|