/*************************************************************
* Subversion Local Working Directory Sync post-commit Hook *
*************************************************************
* CopyLeft (c) Alexandre Gauthier 2005-2006 *
* supernaut@underwares.org *
* *
* main.cs *
* Main program file *
* --------------------------------------------------------- *
* This program can be used as subversion hook to keep *
* a local Working Copy of the trunk of a repository updated *
* at every commit. *
* *
* It's basically a cheap hack that could have been *
* implemented with anything but C#, but since the *
* subversion server where I work runs on Windows, it was my *
* only choice, apart from a lousy batch file. *
* *
* This program compiles and run fine under Mono. *
* Check out the Makefile in the source directory. *
* *
* This software is free software, released under the GPL *
* THERE IS NO WARRANTY; NOT EVEN FOR MERCHANTABILITY OR *
* FITNESS FOR A PARTICULAR PURPOSE. *
*************************************************************/
using System;
using System.Text;
using System.IO;
using System.Reflection;
using System.Configuration;
namespace org.underwares.SVNSyncTree
{
class main
{
// Read settings from App.config, depreciated style. For mono compatibility.
static string svnpath = System.Configuration.ConfigurationSettings.AppSettings["svnpath"];
static string wcpath = System.Configuration.ConfigurationSettings.AppSettings["wcpath"];
static string log = System.Configuration.ConfigurationSettings.AppSettings["write_on_error"];
static string logpath = System.Configuration.ConfigurationSettings.AppSettings["logpath"];
// See how we are called
static string[] self = Environment.CommandLine.Split(new char[] { ' ' });
static void Main(string[] args)
{
// fetch version from assembly for display in the header
string soft_ver = String.Format("{0}", Assembly.GetExecutingAssembly().GetName().Version.ToString());
Console.WriteLine("SVN staging server project updater Version " + soft_ver);
Console.WriteLine("----------------------------------------------------------");
Console.WriteLine("Copyleft (c) Alexandre Gauthier (supernaut@underwares.org)");
Console.WriteLine("");
Console.WriteLine("Path to subversion: " + svnpath);
DoUpdate(wcpath);
}
///
/// Run an "svn update" on a working copy.
///
/// Path to Working Copy
///
private static void DoUpdate(string wc)
{
string stderr;
System.Diagnostics.Process svn = new System.Diagnostics.Process();
svn.StartInfo.FileName = svnpath;
svn.StartInfo.Arguments = "update" + " " + wc;
svn.StartInfo.UseShellExecute = false;
svn.StartInfo.RedirectStandardOutput = false;
svn.StartInfo.RedirectStandardError = true;
try {
svn.Start();
} catch (System.ComponentModel.Win32Exception e) {
Console.WriteLine("Error: Unable to execute SVN Binary!");
Console.WriteLine();
Console.WriteLine("Exception: " + e);
Environment.Exit(1);
}
// Buffer standard err stream so we can dump it to log
// later in the event that something goes wrong
stderr = svn.StandardError.ReadToEnd();
svn.WaitForExit();
// This is only temporary, until I figure how to work with the
// proper return code and perhaps do something meaningful.
if(svn.ExitCode != 0){
// Store postmortem information
int svnexitcode = svn.ExitCode;
// dump output streams to log
WriteLineLog(stderr);
Console.WriteLine("ERROR: svn.exe returned error status: "+ svnexitcode);
} else {
Console.WriteLine("Operation Completed Successfully.");
}
}
///
/// Write out to console and also to log, if "write_on_error" is set in App.config
///
/// line to write out
private static void WriteLineLog(string line)
{
if (log.ToLower() == "true"){
StreamWriter w = File.AppendText(logpath);
Log(line,w);
// Close the writer and underlying file.
w.Close();
}
Console.WriteLine(line);
}
public static void Log (String logMessage, TextWriter w)
{
w.Write("\r\nSVNSyncTree Log: ");
w.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(),
DateTime.Now.ToLongDateString());
w.WriteLine("|");
w.WriteLine("|{0}", logMessage);
w.WriteLine ("-------------------------------");
// Update the underlying file.
w.Flush();
}
private static void PrintHelp()
{
Console.WriteLine("Usage: " + self[0] + " [repository] [revision]");
Console.WriteLine(" Where 'repository' is the path to the repository");
Console.WriteLine(" and 'revision' is the revision to update the working directory to.");
Console.WriteLine("");
Console.WriteLine("See " + self[0] + ".config for further options.");
}
}
}