English 中文(简体)
Using Perl to build a SVN post-commit hook: IRC Bot to print commit Message
原标题:

I m trying to build an IRC Bot which tells me in a private channel every commit-message which I want to know about. But I have trouble to get per

#!/bin/bash 
REPOS="$1" 
REV="$2" 
# call bot with arguments reposname, revison and commit message in one string 
/usr/bin/perl /home/user/repo/svn_irc_bot.pl "$REPOS" "$REV" 
# all checks passed, so allow the commit 
exit 0

Then, the invoked Perl-Skript:

#!/usr/bin/perl -w
# see http://www.javalinux.it/wordpress/2009/10/15/writing-an-irc-bot-for-svn-commit-notification/
# see http://oreilly.com/pub/h/1964
use strict;
# We will use a raw socket to connect to the IRC server.
use IO::Socket;
my $repos = $ARGV[0];
my $rev = $ARGV[1];
my $commit = `/usr/bin/svnlook log $repos`;
my $user = `whoami`;
# The server to connect to and our details.
my $server = "irc.server.com";
my $nick = "bot2";
my $login = "bot2";
# The channel which the bot will join.
# my $channel = "#channel";
# Connect to the IRC server.
my $sock = new IO::Socket::INET(PeerAddr => $server,
                                PeerPort => 6667,
                                Proto =>  tcp ) or
                                die "Can t connect
";
# Log on to the server.
print $sock "NICK $nick
";
print $sock "USER $login 8 * :Perl IRC Hacks Robot
";
# Read lines from the server until it tells us we have connected.
while (my $input = <$sock>) {
    # Check the numerical responses from the server.
    if ($input =~ /004/) {
        # We are now logged in.
        print $sock "PRIVMSG mynick : $user: $repos r$rev -- $commit
";
        last;
    }
    elsif ($input =~ /433/) {
        die "Nickname is already in use.";
    }
}
sleep(5);
print $sock "QUIT bye... 
";
sleep(5);
close($sock);

So, my Bot does connect, and can talk to me...

If I start the shell Script manually, only one word (the string inside $user, not even the following colon) is sent.

If the script is invoked by SVN trough a commit, is seems like the $user and $commit strings are empty, $user and $repos are transmitted...

I guess that something is wrong with my usage of whoami and svnlook... But I can t figure it out. Maybe someone can give me a hint?

最佳回答

The reason you only get "$user" with no preceding colon is because you are capturing output from whoami, and that output includes a newline. That newline is interpreted as the end of your string to sent. Try chomp $user to get rid of the newline before using $user.

If the script is invoked by SVN trough a commit, is seems like the $user and $commit strings are empty, $user and $repos are transmitted...

I m going to assume that you mean through SVN $user and $commit are empty, but $rev and $repos are transmitted, since that would make sense...

You ll have the same problem with $commit from svnlook, but because the commit comes at the end of your message, you ll only have a problem if your message has newlines in it. For example, if the first line of your message is a newline, you won t see anything. For that, I d recommend removing all of the newlines from the message, probably with y/ //.

As to $user being blank from within the hook, that depends on how you are using svn. It s entirely possible that whoami isn t able to find the user id, for example, if the process running the hook is not associated with any login. You will probably need another method of determining the user in that case, such as the first line of output from svnlook info.

问题回答

You re using just whoami instead of the full path to the command, but there s no assurance that when the script is invoked by SVN, the $PATH env variable will contain the same directories as the one your shell has.

Another thing you should check is that the uid under which SVN runs the script has permissions to use svnlook and access the repository.

Not sure if your problem comes from this, but it sure is a good place to start looking at.

I m not sure about it but try two things.

First check the permissions of the files you re running. If they don t have permissions to run whoami and svnlookup then you re pretty much screwed. Second just give qx(cmd) a shot instead of cmd.





相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签