Running git fetch in batch file via c# process hangs up

て烟熏妆下的殇ゞ 提交于 2021-01-28 13:36:44

问题


I am trying to run several git commands via c# and continuing to work with the resulting outputs.

I wrote this little Method to work with:

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

sure thing, some settings are for debugging (like the unneccessary Console.WriteLine()) but it works with most git commands like status or rev-parse HEAD.

This is my batch-file I use:

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

There are two arguments since rev-parse HEAD are two arguments. I don't have any Problems executing status so this double arguments shouldn't be the problem.

Everything works exactly like intended but when I execute fetch the program hangs up.

I used the batch file in cmd with same inputs and it did take its time (about 1-2 seconds) but it exited without any problems (no output was given there).

any thoughts?


回答1:


The issue was a not working RSA authentication. Since git fetch contacts the server and git status not, this error could only be seen by typing the used commands by hand in cmd (not git bash).

The program was waiting for user input. As suggested by @ardila the usage of lib2git should be used for a robust use of git in C#.



来源:https://stackoverflow.com/questions/54476940/running-git-fetch-in-batch-file-via-c-sharp-process-hangs-up

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!