SVN Pre-commit hook for avoiding commits to specific branches

微笑、不失礼 提交于 2019-12-24 00:33:55

问题


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)

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

Any suggestions are highly appreciated!


回答1:


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

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


来源:https://stackoverflow.com/questions/13691400/svn-pre-commit-hook-for-avoiding-commits-to-specific-branches

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!