// This code is in the public domain -- Charles Nicholson (charles@powerof2games.com) using System; using System.Runtime.InteropServices; using System.Diagnostics; using System.Text.RegularExpressions; namespace svnc { public static class Program { [DllImport("kernel32.dll")] static extern bool SetConsoleTextAttribute(IntPtr hConsoleOutput, ushort wAttributes); [DllImport("kernel32.dll")] static extern IntPtr GetStdHandle(int nStdHandle); const ushort Gray = 7; const ushort Red = 4; const ushort Yellow = 14; const ushort Green = 2 | 8; const ushort Blue = 1 | 8; const ushort White = 15; static Regex s_conflictRegex = new Regex("^C( ){4}.+$"); static Regex s_mergedRegex = new Regex("^G( ){4}.+$"); static Regex s_modifyRegex = new Regex("^M( ){4}.+$"); static Regex s_addRegex = new Regex("^A( ){4}.+$"); static Regex s_deleteRegex = new Regex("^D( ){4}.+$"); const int StdOutHandleId = -11; static IntPtr s_stdOutHandle = GetStdHandle(StdOutHandleId); static void OutputLine(string text) { ushort color = Gray; if (s_conflictRegex.Match(text).Success) color = Red; else if (s_mergedRegex.Match(text).Success) color = Yellow; else if (s_modifyRegex.Match(text).Success) color = White; else if (s_addRegex.Match(text).Success) color = Green; else if (s_deleteRegex.Match(text).Success) color = Blue; SetConsoleTextAttribute(s_stdOutHandle, color); Console.WriteLine(text); SetConsoleTextAttribute(s_stdOutHandle, Gray); } static int Main(string[] args) { Regex whitespace = new Regex(@"[\s\n\r]{1,}"); for (int i = 0; i < args.Length; ++i) { if (whitespace.Match(args[i]).Success) args[i] = '"' + args[i] + '"'; } Process svnProcess = new Process(); svnProcess.StartInfo.FileName = "svn.exe"; svnProcess.StartInfo.Arguments = string.Join(" ", args); svnProcess.StartInfo.UseShellExecute = false; svnProcess.StartInfo.RedirectStandardOutput = true; svnProcess.StartInfo.RedirectStandardError = true; bool started = svnProcess.Start(); if (!started) { Console.WriteLine("Unable to start svn.exe process, aborting."); return -1; } string output = null; while (svnProcess.HasExited == false) { output = svnProcess.StandardOutput.ReadLine(); if (output != null) OutputLine(output); } while (output != null) { output = svnProcess.StandardOutput.ReadLine(); if (output != null) OutputLine(output); } string error = svnProcess.StandardError.ReadToEnd(); if (string.IsNullOrEmpty(error) == false) Console.Write(error); return svnProcess.ExitCode; } } }