如何以编程方式检测tar存档中丢失的文件并从svn repo中删除它们

时间:2009-08-12 09:20:37

标签: svn repository

我有一个Perl脚本将一些配置文件从远程服务器复制到SVN repo并执行提交。远程服务器上的配置文件由不同的用户管理。有时文件会被用户删除。我想让我的脚本足够智能,以检测该文件已被删除,并能够在SVN服务器中发出后续删除和报告。

如何以编程方式检测已删除的文件?

我正在使用的流程是

1) On remote server, the files are gzipping every midnight and placed at backup path.
2) on local server, script is using Net::SSH::Perl to copy the gzipped file and untar at svn repo path and execute a commit.

让我们说昨天有人删除了一个文件(例如aa.txt),并且在gzip备份文件中也没有。我希望以编程方式在本地服务器上解压缩之前或之后检测该文件(aa.txt),并在提交之前从svn中删除。

希望我现在很清楚。

7 个答案:

答案 0 :(得分:2)

您是否使用rsync将配置文件从远程计算机镜像到本地工作副本?

如果是这样,您可以使用--delete标志使其在远程服务器上删除本地文件时删除它们。

然后,您可以使用svn status来确定添加或删除了哪些文件,然后在提交更改之前发出相应的svn命令来添加和删除它们。

答案 1 :(得分:1)

所以在远程服务器上,这些文件不受svn控制,你每晚都将它们拉进你的svn项目?

在工作副本中删除文件后,“svn status”将显示带有“!”的文件名在前。有关详细信息,请参阅此doc for svn status

$ rm README.txt
...
$ svn status
!      README.txt

您可以在Perl中解析“svn status”的输出以查找缺少的文件,并为每个文件发出“svn delete”命令,然后单个“svn commit”。

答案 2 :(得分:0)

是否要从服务器中删除从工作副本中删除的文件?

如果是这样,提交步骤将执行此操作。诀窍是复制包含文件的目录,而不仅仅是单个文件并提交。从该目录中删除的所有文件都将从仓库中删除。

答案 3 :(得分:0)

  

如何以编程方式检测   删除文件?

如果只想通过Net::SSH::Perl在远程主机上执行shell命令来检测已删除的文件,则答案取决于远程shell。以下是远程shell为bash的示例:

my $ssh = Net::SSH::Perl->new("host1");
$ssh->login("user", "password");
$ssh->cmd("if [ ! -f /var/log/messages ]; then echo \"file missing!\"");

您可以用其他方式替换文件路径和echo命令。

我知道这可能无法回答你的问题。要获得更好的答案,您应该点击“编辑”按钮并添加一些已发送到远程主机的命令示例。目前尚不清楚你是如何“完全复制文件”的。

答案 4 :(得分:0)

我可以看到我的问题现在已经改变,现在有不同的含义。我想回答我自己的原始问题。希望这能使事情变得清晰。

1) copy the tar archvie at some /tmp path as you are doing now.
2) make an array with the files in the archvie. I suggest below code.
3) Read the files from svn repo and create another array. Remember to remove the .svn directories.
4) Compare both array for the missing files in tar archvie and use svn delete command to delete missing files from svn repo. Dont forgot to commit after that.

这是我代码的一部分。

#! /usr/bin/perl
use strict;
use warnings;

my @tararray = tarData("tar_archive");
my @configarray = configFiles();
foreach my $i (@configarray) {
    $i =~ s/^\.\///;
          my @diffs = "$i" if ! grep {$i eq $_} @tararray;
          foreach my $diff (@diffs) {
        system("svn -q delete $diff") || print $!;
                   }
system("svn commit -m 'message'");
}


sub tarData {
        my $tarfile = shift;
        my @lists = Archive::Tar->list_archive($tarfile);
        return @lists;
}


sub configFiles {
        use Cwd;
        my $path = getcwd;
        my @config;
        find(\&d, "$path");

        foreach my $file (@files) {
                chomp($file);
                next if $file =~ s/.svn//g;
                push @config, $file;
        }
        return @config;
}

sub d {
        -f and -r and push @files, $File::Find::name;
}

答案 5 :(得分:0)

使用svn rm $( svn status | sed -e '/^!/!d' -e 's/^!//' )

有关更多命令行颠覆的信息,请参阅http://geryit.com/blog/2011/03/command-line-subversion-practices/

答案 6 :(得分:-1)

冷却。

你的问题是什么?

也许你正在寻找提交后挂钩?