SVN预提交钩子,用于避免对特定分支的提交

时间:2012-12-03 20:46:37

标签: c# svn

使用c#阻止svn提交到指定分支的可能方法是什么?我怎样才能从预提交钩子中的参数中获取分支的路径? (或获取阻止路径的任何其他建议)

有没有办法用svnlook来查看它修改过的文件呢?

任何建议都非常感谢!

1 个答案:

答案 0 :(得分:4)

当我必须这样做时,我遵循了本指南: http://www.troyhunt.com/2010/02/creating-subversion-pre-commit-hooks-in.html

我写了一个名为svnlook的C#应用​​程序,并被precommit钩子触发,以检查路径是否被允许。

以下是我的代码,它应该很容易适应您的情况:

class Program
{
    static void Main(string[] args)
    {
        var repos = args[0];
        var txn = args[1];

        var log = GetSvnLookOutput(repos, txn, "log");
        var changedPaths = GetSvnLookOutput(repos, txn, "changed");

        var logValidation = GetLogMessageErrors(log.Replace("\r", "").Replace("\n", ""));
        if (logValidation != null)
        {
            Console.Error.WriteLine(logValidation);
            Environment.Exit(1);
        }

        if (log.Contains("Autoversioning commit"))
        {
            // this is an autoversion webdav client, enforce path rules
            var changedPathsValidation = GetFileNameErrors(changedPaths);
            if (changedPathsValidation != null)
            {
                Console.Error.WriteLine(changedPathsValidation);
                Environment.Exit(1);
            }
        }

        Environment.Exit(0);
    }

    private static string GetLogMessageErrors(string log)
    {
        if (string.IsNullOrEmpty(log))
        {
            return "Log message is required.";
        }

        return null;
    }

    private static string GetFileNameErrors(string changedPaths)
    {
        var changeRows = Regex.Split(changedPaths.TrimEnd(), Environment.NewLine);
        foreach (var changeRow in changeRows)
        {
            var filePath = changeRow.Substring(4, changeRow.Length - 4);

            if (filePath.ToLower().Contains("/code/"))
            {
                return "Autoversioning commits are not allowed inside /CODE/ folders. Use a SVN client for this.";
            }
        }
        return null;
    }

    private static string GetSvnLookOutput(string repos, string txn, string subcommand)
    {
        var processStartInfo = new ProcessStartInfo
        {
            FileName = @"svnlook.exe",
            UseShellExecute = false,
            CreateNoWindow = true,
            RedirectStandardOutput = true,
            RedirectStandardError = true,
            Arguments = String.Format("{0} -t \"{1}\" \"{2}\"", subcommand, txn, repos)
        };

        var process = Process.Start(processStartInfo);
        var output = process.StandardOutput.ReadToEnd();
        process.WaitForExit();
        return output;
    }
}