【问题标题】:Running git fetch in batch file via c# process hangs up通过 c# 进程在批处理文件中运行 git fetch 挂起
【发布时间】:2019-02-01 09:53:21
【问题描述】:

我正在尝试通过 c# 运行几个 git 命令并继续使用结果输出。

我写了这个小方法来使用:

private string runGitCommand(string gitCommand, string path)
    {
        ProcessStartInfo processInfo = new ProcessStartInfo();
        processInfo.FileName = System.Windows.Forms.Application.StartupPath+"\\gitcommand.bat";
        processInfo.WindowStyle = ProcessWindowStyle.Hidden;
        processInfo.Arguments = gitCommand;
        processInfo.CreateNoWindow = true;
        processInfo.UseShellExecute = false;
        processInfo.WorkingDirectory = path;
        processInfo.RedirectStandardError = true;
        processInfo.RedirectStandardOutput = true;
        Process process = new Process();
        process.StartInfo = processInfo;
        string output="";
        process.OutputDataReceived += (a, b) => {
            Console.WriteLine(b.Data);
            output += b.Data;
        }
        ;
        process.ErrorDataReceived += (a, b) => Console.WriteLine(b.Data);
        process.Start();
        process.BeginErrorReadLine();
        process.BeginOutputReadLine();
        process.WaitForExit();

        return output;
    }

当然,有些设置是用于调试的(例如不必要的 Console.WriteLine()),但它适用于大多数 git 命令,例如 statusrev-parse HEAD

这是我使用的批处理文件:

@"C:/Program Files (x86)/Git/bin/git.exe" %1 %2

有两个参数,因为rev-parse HEAD 是两个参数。 我在执行 status 时没有任何问题,所以这个双参数应该不是问题。

一切都按预期工作,但是当我执行 fetch 时,程序挂断了。

我在 cmd 中使用了具有相同输入的批处理文件,它确实花费了时间(大约 1-2 秒),但它没有任何问题地退出(那里没有给出输出)。

有什么想法吗?

【问题讨论】:

  • 完全取决于 repo 及其配置,但它可能正在等待用户输入。当您在终端上执行相同的命令(逐字)时会发生什么?您是否收到身份验证提示?
  • 您应该发表您的评论作为答案 ^^ 这正是问题所在,显然,只有与服务器联系的 git 命令确实存在 RSA 身份验证问题(因为我复制了 repo 进行测试目的)。所以是的,等待用户输入。知道如何检查程序是否正在等待用户输入吗?
  • 知道如何检查程序是否在等待用户输入。 如果您将此代码编写为除了快速原型之外的任何其他内容,我会选择一些东西更健壮(如libgit2),这是一种非常迂回的表达“我不知道”的方式;)
  • 不要将解决方案添加到问题中,而是将其作为答案发布,因为这就是他们的目的!回答自己的问题绝对没问题,您甚至可以稍后接受自己的回答...

标签: c# git batch-file


【解决方案1】:

问题是 RSA 身份验证不工作。 由于git fetch 联系服务器而git status 没有联系,因此只能通过在 cmd(不是 git bash)中手动输入使用的命令来查看此错误。

程序正在等待用户输入。正如@ardila 所建议的那样,lib2git 的用法应该用于在 C# 中稳健地使用 git。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-27
    • 1970-01-01
    相关资源
    最近更新 更多