Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Subversion(SVN) pre-commit hook

by runrig (Abbot)
on Jul 19, 2006 at 18:12 UTC ( #562373=sourcecode: print w/replies, xml ) Need Help??
Category: Miscellaneous
Author/Contact Info /msg runrig
Description:

I needed a Subversion pre-commit hook script to commit based on the name and contents of the file being committed. I found a python example, so I thought I'd try something similar in perl.

Warning: this seems to work, but I do not know if I've got the subclassing of the SVN::Delta::Editor methods correct (can't find good docs or examples). Advice is appreciated.

#!/usr/bin/perl

require SVN::Core;
require SVN::Repos;
require SVN::Fs;
require SVN::Delta;

package ChangeReceiver;

my $file_re = qr/\.txt/;
my $bad_re = qr/foo/;

our @ISA = qw(SVN::Delta::Editor);

sub add_file {
  return [0, $_[1]];
}

sub open_file {
  return [0, $_[1]];
}

sub apply_textdelta {
  $_[1]->[0] = 1;
  return;
}

sub close_file {
  my ($self, $file_baton) = @_;
  my ($changed, $path) = @$file_baton;
  return unless $changed;
  return unless $path =~ /$file_re/;

  my $pool = SVN::Pool->new_default_sub;

  my $stream = SVN::Fs::file_contents( $self->{txn_root}, $path, $pool
+ );
  my $bytes;
  my $byte_cnt = 0;
  my $exit_code = 0;
  my $buffer = '';
  while ( $bytes = $stream->read( $buffer, $SVN::Core::STREAM_CHUNK_SI
+ZE, 0 ) ) {
    if ( $buffer =~ /$bad_re/ ) {
      $exit_code=1;
      last;
    }
    $byte_cnt += $bytes;
    $buffer = '';
  }
  $stream->close;
  if ($exit_code) {
    warn "Bad file: $path\n";
    exit $exit_code;
  }
  return;
}

package main;

my ($rep, $txn) = @ARGV;

my $rep = SVN::Repos::open($rep);
my $fs = $rep->fs;
my $txn_ptr = $fs->open_txn($txn);
my $txn_root = SVN::Fs::txn_root($txn_ptr);
my $base_root = $fs->revision_root($fs_ptr, SVN::Fs::txn_base_revision
+($txn_ptr));
my $editor = ChangeReceiver->new;
$editor->{txn_root} = $txn_root; # Bad OO I know
SVN::Repos::dir_delta($base_root, '', '', $txn_root, '', $editor, sub{
+1}, 1, 1, 0, 0);

exit 0;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://562373]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (2)
As of 2022-01-25 07:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (65 votes). Check out past polls.

    Notices?