Debian Codegraph @

script/kgb-client

raw
1 #!/usr/bin/perl
2 use utf8;
3 require v5.10.0;
4 use feature 'switch';
5 # vim: ts=4:sw=4:et:ai:sts=4
6 #
7 # KGB - an IRC bot helping collaboration
8 # Copyright © 2008 Martina Ferrari
9 # Copyright © 2009, 2011, 2012 Damyan Ivanov
10 #
11 # This program is free software; you can redistribute it and/or modify it under
12 # the terms of the GNU General Public License as published by the Free Software
13 # Foundation; either version 2 of the License, or (at your option) any later
14 # version.
15 #
16 # This program is distributed in the hope that it will be useful, but WITHOUT
17 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
19 # details.
20 #
21 # You should have received a copy of the GNU General Public License along with
22 # this program; if not, write to the Free Software Foundation, Inc., 51
23 # Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24
25 use strict;
26 use warnings;
27
28 =head1 NAME
29
30 kgb-client - relay commits to KGB servers
31
32 =head1 SYNOPSIS
33
34 =over
35
36 =item B<kgb-client> --conf I</path/to/config> [I<other-option> ...]
37
38 =item B<kgb-client> --uri I<http://some.server:port/service>
39 --password I<password>
40 --repo-id I<repository>
41 --repository I<svn|git|cvs>
42 --timeout I<timeout-in-seconds>
43 --single-line-commits I<off|forced|auto>
44 --use-irc-notices
45 --web-link I<template>
46 --short-url-service I<service>
47 --status-dir I<directory>
48 --[no]-use-color
49 --message-template I<template>
50 --batch-messages
51 --dry-run
52
53 =item B<kgb-client> I<option>... I</svn/repo> I<revision>
54
55 =item B<kgb-client> I<option>... I<old-rev> I<new-rev> I<ref-name>
56
57 =item B<kgb-client> I<option>... $CVSROOT "%p"
58
59 =item B<kgb-client> I<option>... --fake
60
61 =item B<kgb-client> I<option>... --relay-msg I<message>...
62
63 =item B<kgb-client> --help
64
65 =item B<kgb-client> --man
66
67 =back
68
69 =head1 DESCRIPTION
70
71 B<kgb-client> is the client counterpart of L<kgb-bot(1)>. It is intended to be
72 used as a hook in your version control system, executed after the repository
73 gets updated. It analyzes the commit(s) and then relays the information to the
74 KGB server, which will show it on IRC.
75
76 =head1 CONFIGURATION
77
78 =over
79
80 =item B<--conf> I<configuration file>
81
82 Specifies the path to kgb-client configuration file.
83
84 =back
85
86 Configuration options (except B<--conf>, B<--fake> and B<--relay-msg>) may be
87 specified both in the configuration file and on the command line. Usually you
88 want to have all the
89 options in a configuration file, because having passwords on the command line
90 is insecure. The configuration file also gives more control, for example it
91 supports multiple servers and multiple ways of detection of branch and module
92 names.
93
94 See L<kgb-client.conf(5)> for available configuration options. Unless noted
95 otherwise, all the options in the configuration file can also be used on
96 command line, prefixing them with two dashes. E.g. C<repo-id> becomes
97 C<--repo-id>.
98
99 Options given on the command line take precedence over configuration file.
100
101 =head1 FAKE COMMIT MODE
102
103 If invoked with the B<--fake> option, B<kgb-client> will send a fake commit to
104 the servers. This is useful for testing client-server communication
105 independently from VCS setup.
106
107 =head1 MESSAGE RELAY MODE
108
109 When the B<--relay-msg> option is given, there is no repository to be
110 inspected. Instead, the non-option command line arguments are passed verbatim
111 to the bot to display on IRC. This can be used for real-time notification about
112 other events like bug submissions etc.
113
114 =head1 SUPPORTED VERSION CONTROL SYSTEMS
115
116 =head2 Subversion
117
118 Installation requires calling B<kgb-client> with two command line arguments,
119 like this:
120
121 /path/to/kgb-client "$REPOS" "$REV"
122
123 =over
124
125 =item I<path to the subversion repository>
126
127 This is the physical path to the Subversion repository. Something like I</srv/svn/my-repo>
128
129 =item I<revision>
130
131 This is the revision number of the commit, that has triggered the hook.
132
133 =back
134
135 Both these arguments are supplied to the standard Subversion post-commit hooks.
136
137 =head2 Git
138
139 B<kgb-client> shall be installed as a B<post-receive> hook. Something along the
140 following shall do:
141
142 #!/bin/sh
143 exec /path/to/kgb-client --conf /path/to.conf ...
144
145 By default, B<kgb-client> will read the reflog information from standard input
146 as any standard Git post-receive hook. See B<--git-reflog> option if something
147 else is wanted.
148
149 There are other ways to give kgb-client information about Git reflog, mostly
150 useful when debugging on in unusual situations. See L<App::KGB::Client::Git>.
151
152 The following settings can be configured via L<git-config(1)>:
153
154 =over
155
156 =item conf
157
158 =item web-link
159
160 =back
161
162 =head2 CVS
163
164 B<kgb-client> shall be installed in the F<loginfo> file in the C<CVSROOT>
165 module of the CVS repository. It shall be given two arguments -- the repository
166 root, and the directory in which the changes are being made.
167
168 For example:
169
170 ALL /path/to/kgb-client --conf /path/to.conf ... "$CVSROOT" "%p"
171
172 =head1 SEE ALSO
173
174 =over
175
176 =item L<kgb-client.conf(5)>
177
178 =item L<App::KGB::Client>
179
180 =item L<App::KGB::Client::Subversion>
181
182 =item L<App::KGB::Client::Git>
183
184 =item L<App::KGB::Client::CVS>
185
186 =back
187
188 =cut
189
190 use App::KGB::Client::ServerRef;
191 use Carp;
192 use Getopt::Long;
193 use Pod::Usage;
194 use YAML ();
195
196 # option settings may come from three sources:
197 # 1. defaults
198 # 2. configuration file
199 # 3. command line
200 # Sources are evaluated in this order. Every source overrides previous ones
201
202 my $debug;
203
204 my ($conf_file, $uri, $proxy,
205 $repo_id, $password, $timeout,
206 $verbose, $repo_type, @br_mod_re, @mod_br_re,
207 $br_mod_re_swap, $module, $ignore_branch,
208 @servers, $git_reflog, $single_line_commits,
209 $status_dir, $web_link, $short_url_service,
210 $fake, $protocol, $relay_msg,
211 $help, $man, $use_irc_notices,
212 $use_color, %colors, $msg_template,
213 $batch_messages, $dry_run,
214
215 # Git
216 $squash_threshold, $squash_msg_template,
217 $branch_ff_msg_template,
218 $enable_branch_ff_notification,
219 $tag_squash_threshold, $tag_squash_msg_template,
220 );
221
222 # defaults
223 $single_line_commits = 'auto';
224 $use_color = 1;
225 $msg_template = '${{module}} ${{branch} }${{commit} }${{author-name} }${{changes}}${ {log-first-line}}${ * {web-link}}';
226 # multi-line notifications
227 # $msg_template = '${{author-name} }${{branch} }${{commit} }${{module}}${ {changes}}${ * {web-link}}
228 # ${{log}}';
229
230 Getopt::Long::Configure('pass_through');
231
232 GetOptions(
233 'conf=s' => \$conf_file,
234 'help|?' => \$help,
235 'man' => \$man,
236 'debug!' => \$debug,
237 ) or pod2usage(2);
238 pod2usage(0) if $help;
239 pod2usage(-exitval => 0, -verbose => 2) if $man;
240
241 # configuration file
242 my $conf;
243
244 sub from_conf {
245 my ( $setting, $ref, $descr ) = @_;
246 return unless exists $conf->{$setting};
247 ref($ref) or die "Internal error -- expecting reference ($setting)";
248
249 my $descr_name;
250 if ($descr) {
251 if ( ref($descr) eq 'ARRAY' ) {
252 $descr_name = 'lists';
253 }
254 elsif ( ref($descr) eq 'HASH' ) {
255 $descr_name = 'maps';
256 }
257 else { confess "Should not happen ($_)"; }
258 }
259
260 my $value = $conf->{$setting};
261 if ( ref($ref) eq 'SCALAR' ) {
262 die "'$setting' configuration option must be scalar"
263 if ref($value);
264 $$ref = $value;
265 }
266 elsif ( ref($ref) eq 'ARRAY' ) {
267 die "'$setting' configuration option must be array"
268 unless ref($value)
269 and ref($value) eq 'ARRAY';
270
271 for (@$value) {
272 my $ref_name;
273 if ( ref($_) eq 'ARRAY' ) {
274 $ref_name = 'list';
275 }
276 elsif ( ref($_) eq 'HASH' ) {
277 $ref_name = 'map';
278 }
279 else {
280 $ref_name = 'plain value';
281 }
282
283 if ( $descr and ( not ref($_) or ref($_) ne ref($descr) ) ) {
284 warn "'$setting' must be a list of $descr_name. "
285 . "Found a $ref_name. Entry ignored.\n";
286 next;
287 }
288
289 if ( not $descr and ref($_) ) {
290 warn "'$setting' must be a list of plain values. "
291 . "Found a $ref_name. Entry ignored.\n";
292 next;
293 }
294
295 push @$ref, $_;
296 }
297 }
298 elsif ( ref($ref) eq 'HASH' ) {
299 die "'$setting' configuration option must be map"
300 unless ref($value)
301 and ref($value) eq 'HASH';
302
303 while ( my ( $k, $v ) = each %$value ) {
304 my $ref_name;
305 if ( ref($v) eq 'ARRAY' ) {
306 $ref_name = 'list';
307 }
308 elsif ( ref($v) eq 'HASH' ) {
309 $ref_name = 'map';
310 }
311 else {
312 $ref_name = 'plain value';
313 }
314
315 if ( $descr and ( not ref($v) or ref($v) ne ref($descr) ) ) {
316 warn "'$setting' must be a map having $descr_name as values. "
317 . "Found a $ref_name in the '$k' element. Entry ignored.\n";
318 next;
319 }
320
321 if ( not $descr and ref($v) ) {
322 warn "'$setting' must be a map having plain values. "
323 . "Found a $ref_name in the '$k' element. Entry Ignored.\n";
324 next;
325 }
326
327 $ref->{$k} = $v;
328 }
329 }
330 else {
331 die "Internal error -- unknown reference ($setting)";
332 }
333 }
334
335 # detect repository type
336 if ( exists $ENV{GIT_DIR} ) {
337 die "Unable to determine repository type\n"
338 . "both GIT_DIR and CVSROOT present in the environment.\n"
339 . "Please use --repository to force repository type.\n"
340 if exists $ENV{CVSROOT};
341
342 $repo_type = 'git';
343
344 warn "Repository type detected to be 'Git' from the environment\n"
345 if $debug;
346
347 # default configuration file
348 unless ($conf_file) {
349 $conf_file = `git config kgb.conf`;
350 chomp($conf_file);
351 warn "Config file from git-config: $conf_file\n"
352 if $debug;
353 }
354 }
355 elsif ( exists $ENV{CVSROOT} ) {
356 $repo_type = 'cvs';
357 }
358 else {
359 $repo_type = 'svn';
360 }
361
362
363 if( $conf_file ) {
364 $conf = YAML::LoadFile($conf_file)
365 or die "Error loading config from $conf_file\n";
366
367 if ( ref($conf) ne "HASH" ) {
368 die "Invalid configuration: root element must be a map.\n";
369 }
370 from_conf( uri => \$uri );
371 from_conf( proxy => \$proxy );
372 from_conf( 'repo-id' => \$repo_id );
373 from_conf( password => \$password );
374 from_conf( timeout => \$timeout );
375 from_conf( 'branch-and-module-re' => \@br_mod_re );
376 from_conf( 'module-and-branch-re' => \@mod_br_re );
377 from_conf( 'branch-and-module-re-swap' => \$br_mod_re_swap );
378 from_conf( 'ignore-branch' => \$ignore_branch );
379 from_conf( module => \$module );
380 from_conf( repository => \$repo_type );
381 from_conf( 'single-line-commits' => \$single_line_commits );
382 from_conf( 'use-irc-notices' => \$use_irc_notices );
383 from_conf( 'status-dir' => \$status_dir );
384 from_conf( 'web-link' => \$web_link );
385 from_conf( 'short-url-service' => \$short_url_service );
386 from_conf( protocol => \$protocol );
387 from_conf( 'relay_msg' => \$relay_msg );
388 from_conf( 'use-color' => \$use_color );
389 from_conf( 'servers' => \@servers, {} );
390 from_conf( 'color-scheme' => \%colors );
391 from_conf( 'message-template' => \$msg_template );
392 from_conf( 'batch-messages' => \$batch_messages );
393
394 # Git-specific stuff
395 from_conf( 'squash-threshold' => \$squash_threshold );
396 from_conf( 'squash-msg-template' => \$squash_msg_template );
397 from_conf( 'branch-ff-msg-template' => \$branch_ff_msg_template );
398 from_conf(
399 'enable-branch-ff-notification' => \$enable_branch_ff_notification );
400 from_conf( 'tag-squash-threshold' => \$tag_squash_threshold );
401 from_conf( 'tag-squash-msg-template' => \$tag_squash_msg_template );
402 }
403
404
405 Getopt::Long::Configure('no_pass_through');
406
407 GetOptions(
408 'uri=s' => \$uri,
409 'proxy=s' => \$proxy,
410 'repo-id=s' => \$repo_id,
411 'pass|password=s' => \$password,
412 'timeout=s' => \$timeout,
413 'branch-and-module-re=s' => \@br_mod_re,
414 'br-mod-re=s' => \@br_mod_re,
415 'module-and-branch-re=s' => \@mod_br_re,
416 'mod-br-re=s' => \@mod_br_re,
417 'branch-and-module-re-swap!' => \$br_mod_re_swap,
418 'module=s' => \$module,
419 'ignore-branch=s' => \$ignore_branch,
420 'repository=s' => \$repo_type,
421 'verbose!' => \$verbose,
422 'git-reflog=s' => \$git_reflog,
423 'single-line-commits=s' => \$single_line_commits,
424 'use-irc-notices!' => \$use_irc_notices,
425 'status-dir=s' => \$status_dir,
426 'web-link=s' => \$web_link,
427 'short-url-service=s' => \$short_url_service,
428 'fake!' => \$fake,
429 'protocol=s' => \$protocol,
430 'relay-msg' => \$relay_msg,
431 'use-color!' => \$use_color,
432 'message-template=s' => \$msg_template,
433 'batch-messages!' => \$batch_messages,
434 'dry-run!' => \$dry_run,
435 'squash-threshold=s' => \$squash_threshold,
436 'squash-msg-template=s' => \$squash_msg_template,
437 'branch-ff-msg-template=s' => \$branch_ff_msg_template,
438 'enable-branch-ff-notification!' => \$enable_branch_ff_notification,
439 'tag-squash-threshold=s' => \$tag_squash_threshold,
440 'tag-squash-msg-template' => \$tag_squash_msg_template,
441 ) or pod2usage(2);
442
443 if ( $br_mod_re_swap ) {
444 push @mod_br_re, @br_mod_re;
445 @br_mod_re = ();
446 }
447
448 undef($use_irc_notices)
449 if $use_irc_notices and $use_irc_notices =~ /^(?:no|false)$/i;
450
451 push @servers, { uri => $uri } if $uri;
452
453 @servers = map {
454 App::KGB::Client::ServerRef->new(
455 { password => $password,
456 timeout => $timeout,
457 verbose => $verbose,
458 dry_run => $dry_run,
459 %$_
460 }
461 )
462 } @servers;
463
464 die "no servers defined. use 'uri' or 'servers' configuration options\n"
465 unless @servers;
466
467 die "repo-id not given\n" unless $repo_id;
468
469 $single_line_commits =~ m/^(?:off|forced|auto)$/
470 or die "Unknown value for the --single-line-commits option "
471 . "('$single_line_commits')\n";
472
473 if ( $relay_msg ) {
474 die "--relay-msg cannot be used together with --fake\n" if $fake;
475
476 $repo_type = 'IGNORED';
477 $relay_msg = join( ' ', @ARGV );
478
479 my $ok = eval {
480 $relay_msg = Encode::decode_utf8( $relay_msg, $Encode::FB_CROAK );
481 1;
482 };
483
484 die "--relay-msg supports only UTF-8 arguments\n" unless $ok;
485 }
486
487 use Cwd();
488 use File::Spec;
489
490 my @client_args = (
491 repo_id => $repo_id,
492 servers => \@servers,
493 br_mod_re => \@br_mod_re,
494 mod_br_re => \@mod_br_re,
495 module => $module,
496 ignore_branch => $ignore_branch,
497 verbose => $verbose,
498 single_line_commits => $single_line_commits,
499 use_irc_notices => $use_irc_notices,
500 use_color => $use_color,
501 status_dir => $status_dir,
502 web_link => $web_link,
503 short_url_service => $short_url_service,
504 protocol => $protocol,
505 colors => \%colors,
506 msg_template => $msg_template,
507 batch_messages => $batch_messages,
508 );
509
510 push @client_args, ( relay_message => $relay_msg ) if $relay_msg;
511
512 if ($fake) {
513 warn "Sending fake commit notification\n";
514
515 require App::KGB::Client::Fake;
516
517 my $client = App::KGB::Client::Fake->new( {@client_args} );
518 $client->process();
519
520 exit 0;
521 }
522
523 if ( $relay_msg ) {
524 eval { require App::KGB::Client::RelayMsg; 1 }
525 or die "Message relay support unavailable\n"
526 . "Error loading App::KGB::Client::RelayMsg:\n"
527 . $@;
528
529 my $client = App::KGB::Client::RelayMsg->new( { @client_args } );
530 $client->process;
531 }
532 elsif ( $repo_type eq 'svn' ) {
533 my($path, $rev) = @ARGV;
534 die "Repository path and revision must be given as arguments\n"
535 unless $path and $rev;
536
537 print "Processing r$rev of SVN repository $path\n"
538 if $verbose;
539
540 eval { require App::KGB::Client::Subversion; 1; }
541 or die "Subversion support unavailable\n"
542 . "Error loading App::KGB::Client::Subversion:\n"
543 . $@;
544
545 my $client = App::KGB::Client::Subversion->new(
546 { @client_args,
547 repo_path => $path,
548 revision => $rev,
549 }
550 );
551
552 $client->process();
553 }
554 elsif ( $repo_type eq 'git' ) {
555 print "Processing Git receive pack\n"
556 if $verbose;
557
558 eval { require App::KGB::Client::Git; 1; }
559 or die "Git support unavailable\n"
560 . "Error loading App::KGB::Client::Git:\n"
561 . $@;
562
563 my $client = App::KGB::Client::Git->new(
564 { @client_args,
565 reflog => $git_reflog,
566 squash_threshold => $squash_threshold,
567 squash_msg_template => $squash_msg_template,
568 branch_ff_msg_template => $branch_ff_msg_template,
569 enable_branch_ff_notification => $enable_branch_ff_notification,
570 tag_squash_threshold => $tag_squash_threshold,
571 tag_squash_msg_template => $tag_squash_msg_template,
572 }
573 );
574
575 $client->process();
576
577 }
578 elsif ( $repo_type eq 'cvs' ) {
579 print "Processing CVS commit\n"
580 if $verbose;
581
582 eval { require App::KGB::Client::CVS; 1; }
583 or die "CVS support unavailable\n"
584 . "Error loading App::KGB::Client::CVS:\n"
585 . $@;
586
587 my $cvs_root = shift(@ARGV);
588 my $dir = shift(@ARGV);
589 my $client = App::KGB::Client::CVS->new(
590 { @client_args,
591 cvs_root => $cvs_root,
592 author => $ENV{USER},
593 directory => $dir,
594 files => [@ARGV],
595 }
596 );
597
598 $client->process();
599 }
600 else {
601 die "Repository type '$repo_type' not supported\n";
602 }
603