0

I am writing a pre-commit hook for an SVN Repository (don't ask!!!)

I want to validate the commit message against a regular expression to ensure that it is formatted correctly.

So, I have written the following code:

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

Where GetCommitMessageErrors checks the message against a regular expression and returns an error message if it does not match, null if it does:

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

and GetSvnLookOutput examines the properties of the transaction to be committed using 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;
        }

When I attach this as a pre-commit hook in the repository using something like

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

I get an error message returned if I enter a log message that does not conform to the regular expression (that part I'm happy enough with!).

However, if I enter a valid commit message (i.e. one that does conform to the regular expression) then I do not get an error message but then the application hangs (I can see it in the task manager).

Un-commenting the lines:

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

Will ensure that the hook executes without errors but then that's where I want to trap any deviations from the standard message format.

Does anyone know or care to suggest what might be causing the application to hang in this instance? I am having trouble debugging it where it is installed (i.e. on a production environment) as I am unable to attach a debugger so I can see where it has gotten to in its execution.

Thanks,

Sean

Seán
  • 523
  • 2
  • 10
  • 17
  • I was doing something like this in python. There was two ways to debug it. One was print to standard out and then crash the application. It would then print to the console doing the commit. The other was to print to a file. I suggest printing to a file when debugging to see whats happening. – camccar May 30 '17 at 17:13
  • It seems that 6 threads are being created by the application but only 3 are being exited when I look in the debugger so I still have a thread that is 'hanging around' when the application finishes. – Seán May 31 '17 at 09:34

0 Answers0