SVN预提交挂钩,以确保.csproj中引用的所有文件都是版本化的?

时间:2011-10-17 18:29:46

标签: visual-studio svn

有时我们会向SVN提交一个C#项目文件,该文件引用我们忘记添加到SVN的文件。是否有任何预提交钩子脚本解析.csproj文件并在引用未版本控制的文件时拒绝提交?

2 个答案:

答案 0 :(得分:1)

#!/usr/bin/perl
# Checks for source files which have been added to a csproj in a commit
# but haven't themselves been committed.
use Modern::Perl;
use warnings FATAL => 'syntax';
use File::Basename qw(basename);
use XML::Simple;

die "usage: $0 repo transaction\n" if @ARGV != 2;
my $opt = "-t"; # -r for testing
my ($repos, $txn) = @ARGV;

# If you really, really want to add a file to the proj and
# not commit it, start your commit message with a !
my @info = `svnlook info $opt $txn "$repos"`;
exit 0 if ($info[3] =~ /\A!/);

my @lines = `svnlook changed $opt $txn "$repos"`;

my @projects = grep { /\AU/ }
               grep { /[.]csproj\z/ }
               map { chomp; $_ } @lines;

my @filelist = `svnlook tree $opt $txn "$repos" --full-paths`;
my %present;

foreach (@filelist) {
    chomp;
    $present{$_} = 1;
}

foreach (@projects) {
    m"\AU.\s\s([\w/.]+/)([\w]+\.csproj)\z" or die "bad line $_";
    my ($path, $proj) = ($1, $2);

    my $projfile = `svnlook cat $opt $txn "$repos" $path/$proj`;
    my $xml = XMLin($projfile);

    # Tested with VS 2012 project files
    my @includes = @{$xml->{ItemGroup}->[1]->{Compile}};
    # All the source files in the csproj
    my @filenames = map {$_->{Include}} @includes;

    foreach (@filenames) {
        # ignore "../etc", not below the project file in the tree
        next if /\A[.][.]/;
        # if you have files that are in the proj but shouldn't be committed
        # eg some generated files, add checks for them here
        # next if /MyGeneratedFile.cs\z/;

        my $file = $path . $_;
        # The csproj file speaks windows paths, but svn will output unix ones
        $file =~ tr|\\|/|;

        if (!defined $present{$file}) {
            die "The file $file is included in the project $path\\$proj, but is not present in the tree, did you forget to commit it?";
        }
    }
}

答案 1 :(得分:0)

如果您使用的是Windows服务器,可以查看Subversion Notify for Windows - http://sourceforge.net/projects/svn-notify/

我用它来对提交消息进行简单检查,以确保用户使用我们的内部规则进行确认。我没有进入任何其他预先提交的用途,但它可能值得一试!

我引用手册:

预先提交检查

通过实施提交消息标准来确保存储库的完整性

限制可以提交的文件类型(消除临时或开发人员特定配置文件的意外提交)

实施文件类型检查 - 可以提交的文件,但需要特殊的提交标记以确保按照您的规则提交

检查您的任务跟踪/错误跟踪系统,确保它是一个有效的跟踪项目并强制执行您的标准(例如,没有针对已关闭的错误的提交)