开发者

Mercurial - log the command executed

We've been using mercurial for a while now and everything is working fine.

The only problem we encounter is when someone runs a "bad command".

An example would be, merging an unstable branch in the stable trunk or pulli开发者_如何学Pythonng a similarly named branch over something completely unrelated overwriting a bunch of stuff...

You've got hg log but you always get people that won't believe the output saying "I didn't do that"...now in the interest of public shaming :) and giving rightful priviledges to the "You broke the built hat", I'm wondering, is there a way to have Mercurial log every command its given to a text file that would give us something like:

hg pull -b something
hg merge TotallyWrongBranch
hg ci -m "I didn't do it!" -u bsimpson


Ok, I had a few minutes on my hand so I wrote an exe and named it hg.exe and renamed Mercurial's original exe to real_hg...

An ugly hack don't mind the code quality please IT WORKS!

public static StreamWriter sw;

static void Main(string[] args)
{
    sw = new StreamWriter("hgCommandLog.txt", true);
    StringBuilder sbArguments = new StringBuilder();
    if (args.Length > 0)
    {
        for (int i = 0; i < args.Length; i++)
        {
            sbArguments.Append(args[i]);
            sbArguments.Append(" ");
        }
    }
    //Console.WriteLine("arg:" + sbArguments.ToString());
    //Console.WriteLine("Session ID = " + System.Diagnostics.Process.GetCurrentProcess().SessionId.ToString());
    //Console.WriteLine("hello ->"+Environment.GetEnvironmentVariable("CLIENTNAME"));

    string sessionID = System.Diagnostics.Process.GetCurrentProcess().SessionId.ToString();
    string clientName = Environment.GetEnvironmentVariable("CLIENTNAME");

    //log the command to sw
    sw.WriteLine(DateTime.Now.ToString() + "\t" + clientName + "("+sessionID+")\t" + "hg " + sbArguments.ToString()); 
    sw.Flush();

    // Start the child process.
    Process p = new Process();
    // Redirect the output stream of the child process.
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.FileName = "real_hg";
    p.StartInfo.Arguments = sbArguments.ToString();

    p.StartInfo.CreateNoWindow = true;
    p.ErrorDataReceived += outputReceived;
    p.OutputDataReceived += outputReceived;
    p.EnableRaisingEvents = true;

    p.Start();
    // Do not wait for the child process to exit before
    // reading to the end of its redirected stream.
    p.BeginOutputReadLine();
    //p.BeginErrorReadLine();
    p.WaitForExit();

    sw.Close();
}

static void outputReceived(object sender, DataReceivedEventArgs e)
{
  sw.WriteLine("\t"+e.Data);
  Console.WriteLine(e.Data);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜