Subversion Hooks with C#
"A hook is a program triggered by some repository event, such as the creation of a new revision or the modification of an unversioned property. Each hook is handed enough information to tell what that event is, what target(s) it's operating on, and the username of the person who triggered the event. Depending on the hook's output or return status, the hook program may continue the action, stop it, or suspend it in some way."
In the doc it mentions...
"Windows, however, uses file extensions to determine whether or not a program is executable, so you would need to supply a program whose basename is the name of the hook, and whose extension is one of the special extensions recognized by Windows for executable programs, such as .exe or .com for programs, and .bat for batch files."
This has to be too easy. So I whipped up a quick C# Console Executable:
"post-commit.cs" Source Listing:
----------------------------------
using System;
namespace post_commit {
class Class1 {
[STAThread]
static void Main(string[] args) {
System.IO.StreamWriter sw = new System.IO.StreamWriter(@"f:\PostCommit.txt", true, System.Text.Encoding.Default);
sw.Write(System.DateTime.Now.ToString() + ":" + string.Join("|", args));
sw.Close();
}
}
}
----------------------------------
Now build it, "csc post-commit.cs"
Built it, and placed the .exe into the Project\Hooks\ Folder. Now commit any file into the repository; you should see a file f:\PostCommit.txt created. Inspect the contents.
I wonder what cool things are being done with Hooks and subversion. Obvious examples (in the Doc's) include triggering backups, and sending emails.
4 Comments
Comments have been disabled for this content.
Chad Myers said
We've implemented a generic hook script that will load up assemblies and execute them as "plug-ins". Current we have a plug-in for sending out a nice HTML-formatted email with details about the recent commit (who did it, what they changed, the diffs, a URL to our Trac instance, etc) and another plug-in that integrates with our bug-tracking system.
We can put special XML in our SVN comments and it will do various things like mark a bug as "fixed" and associate the "fix" of the bug with the SVN revision so we can track the relationship between fixes and changes
Ricky Dhatt said
FogBugz (bugtracker) uses Subversion hooks to link checkins to bugs.
Josh Blair said
Rob, have you done any cool things with C# hook console apps? Most of the hook scripts that are contributed seem to be in Python. I speak C# not Python. Thanks,
seek said
your title is about HOOKs, but i didn't see what hooks in your content?