Mercurial - 记录执行的命令

时间:2011-07-20 21:06:26

标签: version-control mercurial

我们一直在使用mercurial一段时间,一切正常。

我们遇到的唯一问题是有人遇到“坏命令”。

一个例子是,在稳定主干中合并一个不稳定的分支,或者在完全不相关的东西上拉一个类似名称的分支覆盖一堆东西...

你有hg日志,但是你总是让那些不相信输出的人说“我没有那样做”......现在是为了公众羞辱:)并给予你“合适的特权”打破了建造的帽子“,我想知道,有没有办法让Mercurial将每个命令记录到一个给我们类似的文本文件:

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

1 个答案:

答案 0 :(得分:0)

好的,我手上有几分钟,所以我写了一个exe并命名为hg.exe并将Mercurial的原始exe重命名为real_hg ......

一个丑陋的黑客不介意代码质量,请它工作!

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);
}