【问题标题】:Git diff gets stuck when run as .NET Process作为 .NET 进程运行时,Git diff 卡住了
【发布时间】:2015-04-30 16:14:07
【问题描述】:

运行 Git diff 会卡住,直到以 System.Diagnostics.Process 运行时被杀死。

代码:

class Program
    {
        static void Main(string[] args)
        {
            ProcessStartInfo pInfo = new ProcessStartInfo();
            pInfo.FileName = "git.exe";
            pInfo.Arguments = "diff --name-only --exit-code V2.4-Beta-01 HEAD";
            pInfo.WorkingDirectory = @"C:\Git";
            pInfo.UseShellExecute = false;
            pInfo.CreateNoWindow = true;
            pInfo.RedirectStandardError = true;
            pInfo.RedirectStandardOutput = true;

            Process p = new Process();
            p.StartInfo = pInfo;

            p.Start();

            p.WaitForExit(10000);

            if (!p.HasExited)
            {
                p.Kill();
                Console.WriteLine("Killed!!!");
            }

            Console.WriteLine(p.StandardOutput.ReadToEnd());
            Console.WriteLine(p.StandardError.ReadToEnd());
            Console.ReadLine();
        }
    }

如何避免这种情况,让程序正常存在而不超时?

【问题讨论】:

  • 刚刚在我的机器上用一个 git 项目测试了你的代码,它运行良好。你有更多信息吗?如果在 Git Bash 中运行命令会发生什么。但顺便说一句:安装 git 时,我也选择了能够在命令行中使用 git。
  • 我在 Windows 中运行它(所以 .NET,没有 Mono),我添加了“--exit-code”以避免在最后输入“:”以键入“q” (退出)在命令行中运行命令时。但是当从 .NET 运行时,它仍然会卡住。我不知道这是否有帮助。这是任何 Git 配置问题吗?我在网上冲浪和 diff 命令帮助没有成功
  • 即使没有 --exit-code,我也不必输入 q 即可退出。该命令在命令行中自动退出。你安装了什么git?来自 git-scm.com 版本?我有 1.9.5 版
  • 和你一样的版本。运行命令时,你真的有什么不同吗?否则,该过程可以正常完成。
  • 如果我使用命令,我会得到一个文件列表并且命令完成。

标签: c# .net git


【解决方案1】:

问题是有人必须消耗标准输出缓冲区,否则它将被填满并且进程被卡住(参见解释here)。我尝试的 diff 检索了 983 行,这导致了缓冲区溢出。

以下是我的问题的解决方案:

class Program
    {
        static void Main(string[] args)
        {
            ProcessStartInfo pInfo = new ProcessStartInfo();
            pInfo.FileName = "git.exe";
            pInfo.Arguments = "diff --name-only --exit-code V2.4-Beta-01 HEAD";
            pInfo.WorkingDirectory = @"C:\Git";
            pInfo.UseShellExecute = false;
            pInfo.CreateNoWindow = true;
            pInfo.RedirectStandardError = true;
            pInfo.RedirectStandardOutput = true;

            string output = string.Empty;

            Process p = new Process();
            p.StartInfo = pInfo;

            p.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
            {
                if (!String.IsNullOrEmpty(e.Data))
                {
                    output += e.Data + Environment.NewLine;
                }
            });

            p.Start();

            p.BeginOutputReadLine();

            p.WaitForExit();
            p.Close();

            Console.WriteLine(output);
            Console.ReadLine();
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-22
    • 1970-01-01
    • 2017-05-22
    • 2012-10-14
    • 2021-11-28
    • 1970-01-01
    相关资源
    最近更新 更多