I am executing a pull request via the command line in a c# program. 9 times out of 10, the pull command never seems to execute on the first call, but always executes within seconds on the next call. What gives?
Here's the code.
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;
}
The console output will reach "Pulling from staging...", and, on the first execution, never finish, and never reach any of the other console writes. However, on the second execution, it finishes very quickly and properly hits all console writes.
The pathToGitInstall and pathToRepo are passed in as the file paths to the local installation of Git and the path to the repository being used.