http://qs1969.pair.com?node_id=562373
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;