我正在为SVN存储库写一个预提交钩子(不要问!!!)

我想针对正则表达式验证提交消息,以确保它的格式正确 .

所以,我写了以下代码:

static void Main(string[] args)
    {
        var repos = args[0];
        var txn = args[1];
        string branch = GetSvnLookOutput(repos, txn, "dirs-changed").ToString().Trim();
        bool isWorkingCode = IsWorkingCode(branch);
        bool isMergedCode = IsMergedCode(branch);
        bool isTaggedCode = IsTaggedCode(branch);
        var log = GetSvnLookOutput(repos, txn, "log");
        string author = GetSvnLookOutput(repos, txn, "author").ToString().Trim();
        var logValidation = GetCommitMessageErrors(log, isWorkingCode, isMergedCode, isTaggedCode);
        if (logValidation != null)
        {
            Console.Error.WriteLine(logValidation);
            Environment.Exit(1);
        }
        Environment.Exit(0);
    }

其中GetCommitMessageErrors根据正则表达式检查消息,如果不匹配则返回错误消息,如果是,则返回null:

private static string GetCommitMessageErrors(string log, bool isWorkingCode, bool isMergedCode, bool isTaggedCode)
        {
            if (isWorkingCode)
            { 
                    if (!Regex.IsMatch(log, workingCodeCommitMessageRegex, RegexOptions.Multiline))
                    {
                        StreamReader streamReader = new StreamReader(ConfigurationManager.AppSettings["Coding Commit Message Sample"]);
                        string commitMessageSample = streamReader.ReadToEnd();
                        return "Commit message needs to be in the following format:"
                        + Environment.NewLine
                        + Environment.NewLine
                        + commitMessageSample;
                    }
            }
            return null;
        }

和GetSvnLookOutput使用svnlook.exe检查要提交的事务的属性:

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

当我使用类似的东西将它作为预提交钩子附加到存储库中时

C:\Repositories\SvnPreCommitHooks.exe %1 %2

如果我输入的日志消息不符合正则表达式,那么我会收到一条错误消息(那部分我很开心!) .

但是,如果我输入一个有效的提交消息(即一个符合正则表达式的消息),那么我不会收到错误消息但是应用程序挂起(我可以在任务管理器中看到它) .

取消评论线:

if (logValidation != null)
        {
            Console.Error.WriteLine(logValidation);
            Environment.Exit(1);
        }

将确保挂钩执行没有错误,但那时我想要捕获与标准消息格式的任何偏差 .

是否有人知道或建议可能导致应用程序在此实例中挂起的内容?我无法在安装它的地方(即在 生产环境 环境中)调试它,因为我无法附加调试器,因此我可以看到它在执行中的位置 .

谢谢,

肖恩