in reply to Re: SVN API log_msg help
in thread Reaped: SVN API log_msg help

Thanks for reply I am new to perl please help me out in this. I am Trying to create a SVN API. In which when i commit a file it will call back the log_msg. i am trying to write the log_msg function which need to take massage from user and kept in a tmp file and commit using that message. please find the code below when i run this code its giving me an error " unable to find the object svn_client_commit_item3"

use SVN::Client; use IO:File; use vars qw($fh $filename); my $template = 'filexxxx'; my $target = '/home/XXX/bla/bla'; my $ctx-> new SVN::Client(); $ctx->log_msg(\&log_msg); print " enter path to commit"; my $path =<>; chomp($path); $ctx->commit($path,0); sub log_msg{ my $log_msg=shift; my $messgpath=shift; my $citem = shift; my $pool = shift; $logmsg =&message(); $citem ={ $citem->path($path), $citem->revision('HEAD') } sub message { ($fh,$filename)=temfile($template, DIR => $target or die "error creating $filename :$!"; $fh =new IO::File "+>> $filename " or die " could not open a file :$!" +; if (defined $fh){ my $line =<STDIN>; print $fh $line; } $logmsg = ` cat $filename`; close($fh) return $logmsg; $messgpath ="$pathh/$filename"; } }

Replies are listed 'Best First'.
Re^3: SVN API log_msg help
by pemungkah (Priest) on Dec 05, 2011 at 22:23 UTC
    First, I think that 'use strict' would help you immensely. You have two variables, $logmsg and $log_msg in the log_msg routine, and I'm quite certain that is not what you want!

    Second, you are not setting the log message properly. Since, as the documentation says, the first parameter is a reference to a scalar that should get the log message, you should be doing

    ${ $log_msg } = message();
    (Don't put the leading ampersand on a subroutine call; this makes Perl do funny things like ignore prototypes, none of which you care about, but which could bite you at a later date. Just get out of the habit of using & on subroutines unless you're creating references to them.)

    Your code as it stands seems to want to read standard input, write it to a file, and then read that back in again to set the log message. This could be loads simpler - just read it once, write it out, and then take the copy you already have to set the message:

    open my $fh, '>>', $filename or die "Can't open $filename for appe +nd: $!"; my @lines = <STDIN>; print $fh @lines; close $fh; ${ $log_msg } = join("", @lines); return;
    If there was some reason that you might get an error, then you'd return
    return SVN::Error::create($apr_err, $child, $message);
    $apr_err is documented in there somewhere, not sure where though. The default error handler should be sufficient unless you want to do something like trap errors writing to the log via eval and pass back a specific "hey, I can't log the message" error.

    Leave $citem alone; you can do everything you want with just the $log_msg reference. I found all this poking at SVN::Core and SVN::Client - this much should get you considerably further than you are now.

      Thanks very much. I slightly modified and now i am able to commit.

      ${ $log_msg } = join("", @lines); return; # i changed this to my $data =join("",@lines); return $data;

      Now the question comes is when i enter a comment at

       my @lines =<STDIN>;

      It is still waiting to take data until i used "ctrl +D" how can i automate it to close and continue executing and the second thing is the temporary file which it is creating to save message it is dont deleting from temp after program exits

        It depends on what you want to do. If you want it to read only one line and then proceed, use
        ${ $log_msg } = <STDIN>;
        That will read just one line. If you instead want to do something like "read until I enter a null line", use a while loop, read one line at a time, and look for an empty one (only contains "\n").

        For the second option, try File::Temp.

        ($fh, $filename) = tempfile($template, UNLINK => 1);
        should work for you. That will automatically unlink the file when the program ends. If you don't want the filehandle (though I think you do), just use undef as the first item in the list being assigned to. Since you're going to write to the file, I'd suggest just using the handy already-opened-for-write filehandle that the call to tmpfile returns.