[svn.haxx.se] · SVN Dev · SVN Users · SVN Org · TSVN Dev · TSVN Users · Subclipse Dev · Subclipse Users · this month's index

Re: svn commit: rev 2177 - trunk/tools/hook-scripts

From: Greg Stein <gstein_at_lyra.org>
Date: 2002-06-13 02:57:22 CEST

On Wed, Jun 12, 2002 at 07:27:49PM -0500, blair@tigris.org wrote:
>...
> +my @command = ($svnlook, $repos, 'rev', $rev, 'info');
> +my ($status, @svnlooklines) = &safe_read_from_pipe(@command);
> +if ($status) {
> + die join("\n", "$0: @command failed with this output:", @svnlooklines),
> + "\n";
> +}

This six-line pattern is used over and over and over. Can you make it a
single function like so:

  @svnlooklines = &run_command($svnlook, $repos, ...);

The function itself can 'die' and print the error and whatnot. This will
also simplify and streamline the interesting part of the script.

Cheers,
-g

> my $author = shift @svnlooklines;
> my $date = shift @svnlooklines;
> shift @svnlooklines;
> -my @log = @svnlooklines;
> -chomp $author;
> -chomp $date;
> +my @log = map { "$_\n" } @svnlooklines;
>
> # figure out what directories have changed (using svnlook)
> -open (INPUT, "$svnlook $repos rev $rev dirs-changed |")
> - or die ("Error running svnlook (dirs-changed)");
> -my @dirschanged = <INPUT>;
> +my @dirschanged;
> +@command = ($svnlook, $repos, 'rev', $rev, 'dirs-changed');
> +($status, @dirschanged) = &safe_read_from_pipe(@command);
> +if ($status) {
> + die join("\n", "$0: @command failed with this output:", @dirschanged),
> + "\n";
> +}
> my $rootchanged = 0;
> -close (INPUT);
> -chomp @dirschanged;
> grep
> {
> # lose the trailing slash if one exists (except in the case of '/')
> @@ -114,16 +105,18 @@
> @dirschanged;
>
> # figure out what's changed (using svnlook)
> -open (INPUT, "$svnlook $repos rev $rev changed |")
> - or die ("Error running svnlook (changed)");
> -@svnlooklines = <INPUT>;
> -close (INPUT);
> +@command = ($svnlook, $repos, 'rev', $rev, 'changed');
> +($status, @svnlooklines) = &safe_read_from_pipe(@command);
> +if ($status) {
> + die join("\n", "$0: @command failed with this output:", @svnlooklines),
> + "\n";
> +}
>
> # parse the changed nodes
> my @adds = ();
> my @dels = ();
> my @mods = ();
> -foreach $line (@svnlooklines)
> +foreach my $line (@svnlooklines)
> {
> my $path;
> my $code;
> @@ -147,10 +140,13 @@
> }
>
> # get the diff from svnlook
> -open (INPUT, "$svnlook $repos rev $rev diff |")
> - or die ("Error running svnlook (diff)");
> -my @difflines = <INPUT>;
> -close (INPUT);
> +my @difflines;
> +@command = ($svnlook, $repos, 'rev', $rev, 'diff');
> +($status, @difflines) = &safe_read_from_pipe(@command);
> +if ($status) {
> + die join("\n", "$0: @command failed with this output:", @difflines),
> + "\n";
> +}
>
> ######################################################################
> # Mail headers
> @@ -162,7 +158,7 @@
> {
> my $firstline = shift (@dirschanged);
> push (@commonpieces, split ('/', $firstline));
> - foreach $line (@dirschanged)
> + foreach my $line (@dirschanged)
> {
> my @pieces = ();
> my $i = 0;
> @@ -210,39 +206,41 @@
> {
> $mail_from = "$mail_from\@$hostname";
> }
> -push (@output, ("To: $userlist\n"));
> -push (@output, ("From: $mail_from\n"));
> -push (@output, ("Subject: $subject\n"));
> -push (@output, ("Reply-to: dev\@subversion.tigris.org\n"));
> -push (@output, ("\n"));
> +
> +my @output;
> +push (@output, "To: $userlist\n");
> +push (@output, "From: $mail_from\n");
> +push (@output, "Subject: $subject\n");
> +push (@output, "Reply-to: $reply_to\n") if $reply_to;
> +push (@output, "\n");
>
> # mail body
> -push (@output, ("Author: $author\n"));
> -push (@output, ("Date: $date\n"));
> -push (@output, ("New Revision: $rev\n"));
> -push (@output, ("\n"));
> +push (@output, "Author: $author\n");
> +push (@output, "Date: $date\n");
> +push (@output, "New Revision: $rev\n");
> +push (@output, "\n");
> if (scalar @adds)
> {
> @adds = sort @adds;
> - push (@output, ("Added:\n"));
> - push (@output, (@adds));
> + push (@output, "Added:\n");
> + push (@output, @adds);
> }
> if (scalar @dels)
> {
> @dels = sort @dels;
> - push (@output, ("Removed:\n"));
> - push (@output, (@dels));
> + push (@output, "Removed:\n");
> + push (@output, @dels);
> }
> if (scalar @mods)
> {
> @mods = sort @mods;
> - push (@output, ("Modified:\n"));
> - push (@output, (@mods));
> + push (@output, "Modified:\n");
> + push (@output, @mods);
> }
> -push (@output, ("Log:\n"));
> -push (@output, (@log));
> -push (@output, ("\n"));
> -push (@output, (@difflines));
> +push (@output, "Log:\n");
> +push (@output, @log);
> +push (@output, "\n");
> +push (@output, map { "$_\n" } @difflines);
>
>
> # dump output to logfile (if its name is not empty)
> @@ -261,4 +259,51 @@
> or die ("Error opening a pipe to sendmail");
> print SENDMAIL @output;
> close SENDMAIL;
> +}
> +
> +exit 0;
> +
> +sub usage {
> + warn "@_\n" if @_;
> + die "usage: $0 [options] REPOS REVNUM email_address1 [email_address2 ... ]]\n",
> + "options are\n",
> + " -h hostname Hostname to append to author for 'From:'\n",
> + " -l logfile File to which mail contents should be appended\n",
> + " -r email_address Set email Reply-To header to this email address\n",
> + " -s subject_prefix Subject line prefix\n";
> +}
> +
> +sub safe_read_from_pipe {
> + unless (@_) {
> + croak "$0: safe_read_from_pipe passed no arguments.\n";
> + }
> + print "Running @_\n";
> + my $pid = open(SAFE_READ, '-|');
> + unless (defined $pid) {
> + die "$0: cannot fork: $!\n";
> + }
> + unless ($pid) {
> + open(STDERR, ">&STDOUT") or
> + die "$0: cannot dup STDOUT: $!\n";
> + exec(@_) or
> + die "$0: cannot exec `@_': $!\n";
> + }
> + my @output;
> + while (<SAFE_READ>) {
> + chomp;
> + push(@output, $_);
> + }
> + close(SAFE_READ);
> + my $result = $?;
> + my $exit = $result >> 8;
> + my $signal = $result & 127;
> + my $cd = $result & 128 ? "with core dump" : "";
> + if ($signal or $cd) {
> + warn "$0: pipe from `@_' failed $cd: exit=$exit signal=$signal\n";
> + }
> + if (wantarray) {
> + return ($result, @output);
> + } else {
> + return $result;
> + }
> }
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: svn-unsubscribe@subversion.tigris.org
> For additional commands, e-mail: svn-help@subversion.tigris.org

-- 
Greg Stein, http://www.lyra.org/
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org
Received on Thu Jun 13 02:56:01 2002

This is an archived mail posted to the Subversion Dev mailing list.

This site is subject to the Apache Privacy Policy and the Apache Public Forum Archive Policy.