Subversion Hooks with C#
One thing I noticed yesterday when setting up Subversion is that it has
"Hook Scripts". "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.