我正在c#程序中通过命令行执行pull请求。 10次中有9次,pull命令似乎永远不会在第一次调用时执行,但总是在下一次调用后的几秒钟内执行。是什么给了什么?
这是代码。
private static bool PullFromMaster(string pathToGitInstall, string pathToRepo)
{
Console.WriteLine("Pulling from staging...");
ProcessStartInfo gitInfo = new ProcessStartInfo();
gitInfo.CreateNoWindow = true;
gitInfo.RedirectStandardError = true;
gitInfo.RedirectStandardOutput = true;
gitInfo.FileName = pathToGitInstall + @"\bin\git.exe";
gitInfo.UseShellExecute = false;
Process gitProcess = new Process();
gitInfo.Arguments = "pull origin master";
gitInfo.WorkingDirectory = pathToRepo;
try
{
gitProcess.StartInfo = gitInfo;
gitProcess.Start();
}
catch (Exception)
{
Console.WriteLine("Failed to start git. Check your config file to ensure the path is correct.");
}
string stderr_str = gitProcess.StandardError.ReadToEnd(); // pick up STDERR
string stdout_str = gitProcess.StandardOutput.ReadToEnd(); // pick up STDOUT
if (stderr_str.Contains("fatal"))
{
Console.WriteLine("A fatal error has occurred while pulling from master.");
return false;
}
if (stderr_str.Contains("conflict"))
{
Console.WriteLine("Merge conflicts are present. Please fix these conflicts, then run this application again.");
return false;
}
Console.WriteLine("Here's the output for git pull origin master:");
Console.WriteLine(stderr_str);
//gitProcess.WaitForExit();
gitProcess.Close();
Console.WriteLine("Pull successful.");
return true;
}
控制台输出将达到“从暂存中拉出来......”,并且在第一次执行时,永远不会完成,并且永远不会到达任何其他控制台写入。但是,在第二次执行时,它会非常快速地完成并正确地命中所有控制台写入。
将pathToGitInstall和pathToRepo作为文件路径传递给Git的本地安装以及正在使用的存储库的路径。