用于避免提交到特定分支的 SVN 预提交钩子 [英] SVN Pre-commit hook for avoiding commits to specific branches

查看:32
本文介绍了用于避免提交到特定分支的 SVN 预提交钩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

What is a possible way to block svn commits to specified branches using c#? How could I get the path of the branch from the arguments in the pre-commit hook? (or any other suggestions for getting the path to block)

有没有办法使用 svnlook 来查看它修改了哪些文件?

Is there a way to do this using svnlook to see what files it modified, maybe?

非常感谢任何建议!

推荐答案

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

When I had to do this I followed this guide: http://www.troyhunt.com/2010/02/creating-subversion-pre-commit-hooks-in.html

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

I wrote a C# application that called svnlook and was fired by the precommit hook to check if the path was allowed or not.

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

Following is my code, it should be easily adaptable to your situation:

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;
    }
}

这篇关于用于避免提交到特定分支的 SVN 预提交钩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆