Ir para o conteúdo

SVN update

Uma das coisas bastante chatas que pode acontecer a quem usa SVN é começar a editar um ficheiro e, ao fazer commit, verificar que entretanto outros utilizadores tinham modificado precisamente o mesmo ficheiro, originando conflitos que obrigam à junção manual dos ficheiros.

Além disso, muita gente gosta de manter uma cópia local dos seus projectos favoritos actualizada.

Este script serve precisamente para isso. Faz regularmente svn update de uma lista de projectos. O script só faz update se não houver modificações locais para evitar problemas (verifica com svn status).

Para usar este script basta modificar o valor da variavel $svn_wcopy_owner_username.

Um ficheiro deve ser passado como argumento. Esse ficheiro deve ter os caminhos das cópias locais (uma por linha) dos repositórios que pretende actualizar regularmente.

Script

svnupdate.pl:

#! /usr/bin/perl -W

#  GPL
#
#  codestats - Written by Diogo Sousa aka orium
#  Copyright (C) 2008 Diogo Sousa
# 
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.


use strict;

sub update_svn_path
{
    my $svnpath=shift;
    my $username=shift;
    my @status;
    my $whoami;

    unless(chdir($svnpath)) # Check if $svnpath exists
    {
        print(STDERR "$svnpath: $!n");
        return;
    }

    @status=`svn --non-interactive status`;

    if (!($#status+1)) # Only update when there is nothing new
    {
        $whoami=`whoami`;
        chomp($whoami);

        if ($whoami eq "root")
        {
            system("su $username -c 'svn --non-interactive update'");
        }
        else
        {
            system("svn --non-interactive update");
        }
    }
}

my $svn_wcopy_owner_username='orium'; # Por aqui o vosso username
my $svnpath;
my $wd=`pwd`;

while ($svnpath=<>)
{
    chomp($svnpath);
    update_svn_path($svnpath,$svn_wcopy_owner_username);
}

chdir($wd);

A função update_svn_path é chamada para cada repositório especificado. Esta função começa por verificar se a pasta referente ao repositório existe. Em caso afirmativo, testa então se não houve modificações na cópia local, e então procede à sua actualização. Também verifica qual o utilizador que está a executar a script, e se a mesma estiver a ser executada pelo utilizador root, o comando svn update será executado pelo utilizador definido na variável $svn_wcopy_owner_username.

Exemplo de uso via crontab

Pode usar o crontab para fazer com que a script seja executada regularmente.

30 3 * * * root /root/scripts/svnupdate.pl /root/scripts/config/svn_online_repos # Todas os dias às 3:30