Hooks

A hook script is a program triggered by some repository event, such as the creation of a new revision or the modification of an unversioned property. Each hook is handed enough information to tell what that event is, what target(s) it's operating on, and the username of the person who triggered the event. Depending on the hook's output or return status, the hook program may continue the action, stop it, or suspend it in some way.

To actually install a working hook, you need only place some executable program or script into the repos/hooks directory, which can be executed as the name (such as start-commit or post-commit) of the hook.

Veamos el directorio hooks/. El fichero pre-commit tiene permisos de ejecución:

pp2@nereida:~$ ls -l svnrep/hooks/
total 40
-rw-r--r-- 1 pp2 pp2 2000 2010-04-12 10:33 post-commit.tmpl
-rw-r--r-- 1 pp2 pp2 1690 2010-04-12 10:33 post-lock.tmpl
-rw-r--r-- 1 pp2 pp2 2307 2010-04-12 10:33 post-revprop-change.tmpl
-rw-r--r-- 1 pp2 pp2 1606 2010-04-12 10:33 post-unlock.tmpl
-rwxr-xr-x 1 pp2 pp2  110 2010-04-19 08:30 pre-commit
-rw-r--r-- 1 pp2 pp2 2982 2010-04-19 07:45 pre-commit.tmpl
-rw-r--r-- 1 pp2 pp2 2038 2010-04-12 10:33 pre-lock.tmpl
-rw-r--r-- 1 pp2 pp2 2764 2010-04-12 10:33 pre-revprop-change.tmpl
-rw-r--r-- 1 pp2 pp2 1980 2010-04-12 10:33 pre-unlock.tmpl
-rw-r--r-- 1 pp2 pp2 2758 2010-04-12 10:33 start-commit.tmpl

Estos son los contenidos de svnrep/hooks/pre-commit:

pp2@nereida:~$ cat -n svnrep/hooks/pre-commit
     1  #!/bin/sh
     2  
     3  REPOS="$1"
     4  TXN="$2"
     5  
     6  /home/pp2/src/perl/subversion/pre-commit.pl "$REPOS" "$TXN" || exit 1
     7  
     8  exit 0

El programa Perl simplemente comprueba que el mensaje de log es suficientemente largo:

pp2@nereida:~$ cat -n /home/pp2/src/perl/subversion/pre-commit.pl
     1  #!/usr/bin/perl -w
     2  use strict;
     3  # creating scalar variables that holds some values
     4  
     5  open my $file, '> /tmp/mylog';
     6  print $file "executing hook\n";
     7  close($file);
     8  
     9  my $min = 8;
    10  my $svnlook = '/usr/bin/svnlook';
    11  #--------------------------------------------
    12  my $repos = shift;
    13  my $txn   = shift;
    14  
    15  unless (defined($repos) and defined($txn)) {
    16    warn "Error: Expected repos and txn args\n";
    17    exit(3);
    18  }
    19  
    20  my $msg;
    21  eval {
    22    $msg = `$svnlook log -t "$txn" "$repos" 2>&1`;
    23  };
    24  
    25  if ($@ or $?) {
    26    warn qq{Error executing '$svnlook log -t "$txn" "$repos"'\nmessage:\n$msg\n};
    27    exit(2);
    28  }
    29  warn "repos=$repos txn=$txn msg=$msg\n";
    30  chomp($msg);
    31  
    32  if (length($msg) < $min) { 
    33    warn "Message should be at least $min characters in length\n"; 
    34    exit(1); 
    35  } 
    36  
    37  exit(0);

Ahora modificamos un fichero en un proyecto y hacemos un commit con un mensaje corto:

pp2@nereida:~/src/perl/subversion/project$ svn commit -mm
Enviando       trunk/Makefile.PL
Transmitiendo contenido de archivos .svn: Falló el commit (detalles a continuación):
svn: Commit bloqueado por hook pre-commit (código de salida 1) con salida:
repos=/home/pp2/svnrep txn=16-j msg=m

Message should be at least 8 characters in length

El commit es aceptado si el mensaje es suficientemente largo:

pp2@nereida:~/src/perl/subversion/project$ svn commit -m 'longer message'
Enviando       trunk/Makefile.PL
Transmitiendo contenido de archivos .
Commit de la revisión 17.

Casiano Rodríguez León
Licencia de Creative Commons
Principios de Programación Imperativa, Funcional y Orientada a Objetos Una Introducción en Perl/Una Introducción a Perl
por Casiano Rodríguez León is licensed under a Creative Commons Reconocimiento 3.0 Unported License.

Permissions beyond the scope of this license may be available at http://campusvirtual.ull.es/ocw/course/view.php?id=43.
2012-06-19