通过C#进程在批处理文件中运行git fetch挂断 [英] Running git fetch in batch file via c# process hangs up
问题描述
我正在尝试通过c#运行多个git命令,并继续使用结果输出.
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;
}
可以肯定的是,有些设置是用于调试的(例如不必要的 Console.WriteLine()
),但是它可以与大多数git命令一起使用,例如 status
或 rev-解析HEAD
.
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
有两个参数,因为 rev-parse HEAD
是两个参数.我在执行 status
时没有任何问题,所以这个双重参数不应该成为问题.
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.
一切正常,但我执行 fetch
时,程序挂断了.
Everything works exactly like intended but when I execute fetch
the program hangs up.
我在cmd中使用具有相同输入的批处理文件,它确实花费了时间(大约1-2秒),但是它退出时没有任何问题(那里没有输出).
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).
有什么想法吗?
推荐答案
问题是无法正常工作的RSA身份验证.由于 git fetch
不与服务器联系,而 git status
不与服务器联系,因此只能通过在cmd中手动键入使用的命令(而不是git bash)才能看到此错误.
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).
程序正在等待用户输入.正如@ardila所建议的那样,为了在C#中可靠地使用git,应使用 lib2git 的用法
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#.
这篇关于通过C#进程在批处理文件中运行git fetch挂断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!