/
Hooks.pm
451 lines (312 loc) · 12.4 KB
/
Hooks.pm
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
package SVN::Hooks;
use warnings;
use strict;
use File::Basename;
use Memoize;
use SVN::Look;
use Exporter qw/import/;
our @EXPORT = qw/run_hook/;
=head1 NAME
SVN::Hooks - A framework for implementing Subversion hooks.
=head1 VERSION
Version 0.28
=cut
our $VERSION = '0.28';
=head1 SYNOPSIS
A single, simple script like the following can be used as any kind of
Subversion hook.
#!/usr/bin/perl
use SVN::Hooks;
use SVN::Hooks::DenyFilenames;
use SVN::Hooks::DenyChanges;
use SVN::Hooks::CheckProperty;
...
run_hook($0, @ARGV);
=head1 CONFIGURATION
Of course, you'll have to configure the plugins (the sub-modules of
SVN::Hooks), but this is just another (almost as) simple Perl script
like this.
# Accept only letters, digits, underlines, periods, and hifens
DENY_FILENAMES(qr/[^-\/\.\w]/i);
# Disallow modifications in the tags directory
DENY_UPDATE(qr:^tags:);
# OpenOffice.org documents need locks
CHECK_PROPERTY(qr/\.(?:od[bcfgimpst]|ot[ghpst])$/i => 'svn:needs-lock');
=head1 USER TUTORIAL
In order to really understand what this is all about you'll need to
understand L<Subversion|http://subversion.tigris.org/> and its
L<hooks|http://svnbook.red-bean.com/nightly/en/svn.reposadmin.create.html#svn.reposadmin.create.hooks>.
Subversion is a version control system, and as such it is used to
maintain current and historical versions of files and
directories. Each revision maintains information about all the changes
introduced since the previous one: date, author, log message, files
changed, files renamed, etc.
Subversion uses a client/server model. The server maintains the
B<repository>, which is the database containing all the historical
information we talked about above. The users use a Subversion client
tool to query and change the repository but also to maintain one or
More B<working areas>. A working area is a directory in the user
machine containing a copy of a particular revision of the
repository. The user can use the client tool to make all sorts of
changes in his working area and to "commit" them all in an atomic
operation that bumps the repository to a new revision.
A hook is a specifically named program that is called by the
Subversion server during the execution of some operations. There are
exactly nine hooks which must reside under the C<conf> directory in
the repository. When you create a new repository, you get nine
template files in this directory, all of them having the C<.tmpl>
suffix and helpful instructions inside explaining how to convert them
into working hooks.
When Subversion is performing a commit operation on behalf of a
client, for example, it calls first the C<start-commit> hook, then the
C<pre-commit> hook, and then the C<post-commit> hoook. The first two
can gather all sorts of information about the specific commit
transaction being performed and decide to reject it in case it doesn't
comply to a set of policies. The C<post-commit> can be used to log or
alert interested parties about the commit just done.
There are several useful L<hook scripts
available|http://subversion.tigris.org/tools_contrib.html#hook_scripts>,
mainly for those three associated with the commit operation. However,
when you try to combine the functionality of two or more of those
scripts in a single hook you normally end up facing two problems.
=over
=item B<Complexity>
In order to integrate the funcionality of more than one script you
have to write a driver script that's called by Subversion and calls
all the other scripts in order, passing to them the arguments they
need. Moreover, some of those scripts may have configuration files to
read and you may have to maintain several of them.
=item B<Inefficiency>
This arrangement is inefficient in two ways. First because each script
runs is a separate process, which usually have a high startup cost
because they are, well, scripts and not binaries. And second, because
as each script is called in turn they have no memory of the scripts
called before and have to gather the information about the transaction
again and again, normally by calling the C<svnlook> command, which
spawns yet another process.
=back
SVN::Hooks is a framework for implementing Subversion hooks that tries
to solve these problems.
Instead of having separate scripts implementing different
functionality you have a single script using a single simple
configuration file. Different plugins, implemented by Perl modules in
the SVN::Hooks:: namespace, implement the different
functionality. Moreover, a single script can be used to implement all
the nine standard hooks, because each plugin knows when to perform
based on the context in which they were called.
=head2 Plugins
Each plugin is implemented as a Perl module. The main ones are
described succinctly below. Please, see their own documentation for
more details.
=over
=item SVN::Hooks::AllowPropChange
Allow changes in revision properties.
=item SVN::Hooks::CheckCapability
Check if the Subversion client implements the required capabilities.
=item SVN::Hooks::CheckJira
Integrate Subversion with the
L<JIRA|http://www.atlassian.com/software/jira/> ticketing system.
=item SVN::Hooks::CheckLog
Check if the log message in a commit conforms to a Regexp.
=item SVN::Hooks::CheckMimeTypes
Check if the files added to the repository have the C<svn:mime-type>
property set. Moreover, for text files, check if the properties
C<svn:eol-style> and C<svn:keywords> are also set.
=item SVN::Hooks::CheckProperty
Check for specific properties for specific kinds of files.
=item SVN::Hooks::CheckStructure
Check if the files and directories being added to the repository
conform to a specific structure.
=item SVN::Hooks::DenyChanges
Deny the addition, modification, or deletion of specific files and
directories in the repository. Usually used to deny modifications in
the C<tags> directory.
=item SVN::Hooks::DenyFilenames
Deny the addition of files which file names doesn't comply with a
Regexp. Usually used to disallow some characteres in the filenames.
=item SVN::Hooks::Notify
Sends notification emails after successful commits.
=item SVN::Hooks::UpdateConfFile
Allows you to maintain Subversion configuration files versioned in the
same repository where they are used. Usually used to maintain the
configuration file for the hooks and the repository access control
file.
=back
=head2 Example usage
In the Subversion server, go to the C<hooks> directory under the
directory where the repository was created. You should see there the
nine hook templates. Create a script there using all the plugins in
which you are interested.
$ cd /path/to/repo/hooks
$ cat svn-hooks.pl
#!/usr/bin/perl
use strict;
use warnings;
use SVN::Hooks;
use SVN::Hooks::AllowPropChange;
use SVN::Hooks::CheckCapability;
use SVN::Hooks::CheckJira;
use SVN::Hooks::CheckLog;
use SVN::Hooks::CheckMimeTypes;
use SVN::Hooks::CheckProperty;
use SVN::Hooks::CheckStructure;
use SVN::Hooks::DenyChanges;
use SVN::Hooks::DenyFilenames;
use SVN::Hooks::Notify;
use SVN::Hooks::UpdateRepoFile;
run_hook($0, @ARGV);
$ chmod +x svn-hooks.pl
This script will serve for any hook. Create symbolic links pointing to
it for each hook you are interested in.
$ ln -s svn-hooks.pl start-commit
$ ln -s svn-hooks.pl pre-commit
$ ln -s svn-hooks.pl post-commit
$ ln -s svn-hooks.pl pre-revprop-change
The default configuration file for the hook is called
C<svn-hooks.conf> in the C<conf> directory under the directory where
the repository was created. It's just another Perl script calling
special functions acting as configuration directives that were defined
by the plugins.
$ cd ../conf
$ cat svn-hooks.conf
DENY_FILENAMES(qr:[^-/\.\w]:i);
CHECK_CAPABILITY('mergeinfo');
CHECK_MIMETYPES();
# Binary+editable files must have the svn:needs-lock property set
CHECK_PROPERTY(qr/\.(?:do[ct]x?|xl[bst]x?|pp[st]x?|rtf|od[bcfgimpst]|ot[ghpst]|sd[acdpsw]|s[tx][cdiw]|mpp|vsd)$/i
=> 'svn:needs-lock');
DENY_UPDATE(qr:^tags:);
1;
Being a Perl script, it's possible to get fancy with the configuration
file, using variables, functions, and whatever. But for most purposes
it consists just in a series of configuration directives.
Don't forget to end it with the C<1;> statement, though, because it's
evaluated with a C<do> statement and needs to end with a true
expression.
Please, see the plugins documentation to know about the directives.
=head1 PLUGIN DEVELOPER TUTORIAL
Yet to do.
=head1 EXPORT
=head2 run_hook
SVN::Hooks exports a single function, B<run_hook>, which is
responsible to invoke the right plugins depending on the context in
which it was called.
Its first argument must be the name of the hook that was
called. Usually you just pass C<$0> to it, since it knows to extract
the basename of the parameter.
Its second argument must be the path to the directory where the
repository was created.
The remaining arguments depend on the hook for which it's being
called, like this:
=over
=item * start-commit repo-path user capabilities
=item * pre-commit repo-path txn
=item * post-commit repo-path rev
=item * pre-lock repo-path path user
=item * post-lock repo-path user
=item * pre-unlock repo-path path user
=item * post-unlock repo-path user
=item * pre-revprop-change repo-path rev user propname action
=item * post-revprop-change repo-path rev user propname action
=back
But as these are exactly the arguments Subversion passes when it calls
the hooks, you usually call C<run_hook> like this:
run_hook($0, @ARGV);
=cut
our @Conf_Files = ('conf/svn-hooks.conf');
sub run_hook {
my ($hook_name, $repo_path, @args) = @_;
$hook_name = basename $hook_name;
my $repo = repo($repo_path);
_load_configs($repo);
# Substitute a SVN::Look object for the first argument
# in the hooks where this makes sense.
if ($hook_name eq 'pre-commit') {
# The next arg is a transaction number
$args[0] = SVN::Look->new($repo_path, '-t' => $args[0]);
}
elsif ($hook_name =~ /^(?:post-commit|(?:pre|post)-revprop-change)$/) {
# The next arg is a revision number
$args[0] = SVN::Look->new($repo_path, '-r' => $args[0]);
}
foreach my $conf (values %{$repo->{confs}}) {
if (my $hook = $conf->{$hook_name}) {
$hook->($conf, @args);
}
}
return;
}
memoize('repo');
sub repo {
my ($repo_path) = @_;
-d $repo_path or die "not a directory: $_\n";
my @conf_files;
foreach my $file (@Conf_Files) {
my $conf = ($file =~ m:^/:) ? $file : "$repo_path/$file";
-r $conf or die "can't read: $conf\n";
push @conf_files, {file => $conf, mtime => 0};
}
return {
repo_path => $repo_path,
conf_files => \@conf_files,
confs => {},
};
}
our (%Inits, $Repo, $Confs);
sub _load_configs {
($Repo) = @_;
my $touched = 0;
foreach my $conf (@{$Repo->{conf_files}}) {
my $mtime = (stat $conf->{file})[9];
if ($conf->{mtime} != $mtime) {
# Update the mtime of every configuration file
$conf->{mtime} = $mtime;
$touched = 1;
}
}
return unless $touched;
# Reset all configuration
$Confs = $Repo->{confs};
while (my ($hook_name, $hook_init) = each %Inits) {
$Confs->{$hook_name} = $hook_init->();
}
# Reload all configuration files
foreach my $conf (@{$Repo->{conf_files}}) {
package main;
unless (my $return = do $conf->{file}) {
die "couldn't parse $conf->{file}: $@\n" if $@;
die "couldn't do $conf->{file}: $!\n" unless defined $return;
die "couldn't run $conf->{file}\n" unless $return;
}
package SVN::Hooks;
}
return;
}
=head1 AUTHOR
Gustavo Chaves, C<< <gnustavo@cpan.org> >>
=head1 BUGS
Please report any bugs or feature requests to C<bug-svn-hooks at
rt.cpan.org>, or through the web interface at
L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=SVN-Hooks>. I will
be notified, and then you'll automatically be notified of progress on
your bug as I make changes.
=head1 SUPPORT
You can find documentation for this module with the perldoc command.
perldoc SVN::Hooks
You can also look for information at:
=over 4
=item * RT: CPAN's request tracker
L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=SVN-Hooks>
=item * AnnoCPAN: Annotated CPAN documentation
L<http://annocpan.org/dist/SVN-Hooks>
=item * CPAN Ratings
L<http://cpanratings.perl.org/d/SVN-Hooks>
=item * Search CPAN
L<http://search.cpan.org/dist/SVN-Hooks>
=back
=head1 COPYRIGHT & LICENSE
Copyright 2008-2009 CPqD, all rights reserved.
This program is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut
1; # End of SVN::Hooks