<p>What is the best approach to write <strong>hooks</strong> for <strong>Subversion</strong> in <strong>Windows</strong>? As far as I know, only executable files can be used. So what is the best choice? </p>

<ul>
<li>Plain batch files (very limited but perhaps OK for very simple solutions)</li>
<li>Dedicated compiled executable applications (sledgehammer to crack a nutshell?)</li>
<li>Some other hybrid choice (like a batch file running a Powershell script)</li>
</ul>

[{'answer_id': 5144, 'author': 'Tanerax', 'author_id': 567, 'author_profile': 'https://Stackoverflow.com/users/567', 'pm_score': 1, 'selected': False, 'text': "<p>Depending on the complexity, each situation is different, If I am just simply moving files around, I'll write a quick batch file. If I want to do something more complex Ill normally just skip the scripting part and write a quick c# program that can handle it. </p>\n\n<p>The question then is do you put that c# program in svn and have it versioned :)</p>\n\n<p><strong>edit:</strong> The benefits of a dedicated c# application is that I can reuse code fragments to create new hooks later, including a simple log output I created to handle hook logging.</p>\n"}, {'answer_id': 6362, 'author': 'Sean Carpenter', 'author_id': 729, 'author_profile': 'https://Stackoverflow.com/users/729', 'pm_score': 1, 'selected': False, 'text': '<p>I\'ve written hooks in <a href="http://python.org" rel="nofollow noreferrer">Python</a> on Windows since there are a lot of examples on the net (usually for Linux but the differences are small).  We also use <a href="http://trac.edgewall.org" rel="nofollow noreferrer">Trac</a> integrated with SVN and there is a Trac API accessible via Python which lets us automatically create/modify Trac tickets from SVN hook scripts.</p>\n'}, {'answer_id': 13720, 'author': 'Sören Kuklau', 'author_id': 1600, 'author_profile': 'https://Stackoverflow.com/users/1600', 'pm_score': 2, 'selected': False, 'text': '<p>Check <a href="https://sourceforge.net/projects/captainhook/" rel="nofollow noreferrer">CaptainHook</a>, "a simple plugin framework for writing Subversion hooks using .NET".</p>\n'}, {'answer_id': 16624, 'author': 'Matt', 'author_id': 1985, 'author_profile': 'https://Stackoverflow.com/users/1985', 'pm_score': 3, 'selected': False, 'text': '<p>We\'ve got complex requirements like:</p>\n\n<ol>\n<li>Only certain users can create folders in parts of the SVN tree, but everyone can edit files there</li>\n<li>Certain file extensions cannot contain certain text in the file</li>\n<li>Certain file extensions can only be stored in a subset of directories</li>\n<li>As well as several simpler ones like, Must have a commit comment</li>\n<li>Regression testable by running new hook against all previous SVN commits</li>\n</ol>\n\n<p>#5 is huge for us, there\'s no better way to know you\'re not gonna break commits moving forward than to be able to push all previous commits through your new hook.  Making the hook understand that 1234 was a revision and 1234-1 was a transaction and making the appropriate argument changes when calling svnlook, etc. was the best decision we made during the process.</p>\n\n<p>For us the nut got big enough that a fully unit testable, regression testable, C# console exe made the most sense.  We have config files that feed the directory restrictions, parse the existing httpd_authz file to get "privileged" users, etc.  Had we not been running on Windows with a .NET development work force, I would have probably written it all in Python, but since others might need to support it in the future I went .NET over .BAT, .VBS, Powershell silliness.  </p>\n\n<p>Personally I think Powershell is different enough from .NET to be mostly useless as a "scripting" language.  It\'s good if the only cmd line support for a product comes via PS (Exchange, Windows 2k8), etc. but if all you want to do is parse some text or access regular .NET objects PS just adds a crazy syntax and stupid Security Iron Curtain to what could be a quick and easy little .NET app.</p>\n'}, {'answer_id': 1198468, 'author': 'Troy Hunt', 'author_id': 73948, 'author_profile': 'https://Stackoverflow.com/users/73948', 'pm_score': 4, 'selected': True, 'text': '<p>I’ve just spent several days procrastinating about exactly this question. There are third party products available and plenty of PERL and Python scripts but I wanted something simple and a language I was familiar with so ended up just writing hooks in a C# console app. It’s very straight forward:</p>\n\n<pre><code>public void Main(string[] args)\n{\n  string repositories = args[0];\n  string transaction = args[1];\n\n  var processStartInfo = new ProcessStartInfo\n                           {\n                             FileName = "svnlook.exe",\n                             UseShellExecute = false,\n                             CreateNoWindow = true,\n                             RedirectStandardOutput = true,\n                             RedirectStandardError = true,\n                             Arguments = String.Format("log -t \\"{0}\\" \\"{1}\\"", transaction, repositories)\n                           };\n\n  var p = Process.Start(processStartInfo);\n  var s = p.StandardOutput.ReadToEnd();\n  p.WaitForExit();\n\n  if (s == string.Empty)\n  {\n    Console.Error.WriteLine("Message must be provided");\n    Environment.Exit(1);\n  }\n\n  Environment.Exit(0);\n}\n</code></pre>\n\n<p>You can then invoke this on pre commit by adding a pre-commit.cmd file to the hooks folder of the repo with the following line:</p>\n\n<pre><code>[path]\\PreCommit.exe %1 %2\n</code></pre>\n\n<p>You may consider this overkill but ultimately it’s only a few minutes of coding. What’s more, you get the advantage of the .NET language suite which IMHO is far preferable to the alternatives. I’ll expand my hooks out significantly and write appropriate tests against them as well – bit hard to do this with a DOS batch file!</p>\n\n<p>BTW, the code has been adapted from <a href="http://irwinj.blogspot.com/2008/04/simple-subversion-trigger.html" rel="noreferrer">this post</a>.</p>\n'}, {'answer_id': 2067561, 'author': 'Aleksandr', 'author_id': 251070, 'author_profile': 'https://Stackoverflow.com/users/251070', 'pm_score': 1, 'selected': False, 'text': '<p>If you have a php executable with a help of simple php class you may write hook script in php like it is shown here <a href="http://www.devhands.com/2010/01/subversion-hook-php-framework-in/" rel="nofollow noreferrer">http://www.devhands.com/2010/01/subversion-hook-php-framework-in/</a></p>\n'}]
['https://Stackoverflow.com/questions/5134', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/563/']