0

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.

  • add breakpoints ... ? its a long way from your 'Pulling from staging...' to when you invoke the process. see if everything works well and if not, where does it fail – Stavm Aug 28 '17 at 16:32
  • Not directly related, but might end up helping anyway: never use human-oriented commands like `git pull` from other programs, if you can avoid it. Break these down into more-machine-oriented Git commands, like `git fetch` and `git merge`. – torek Aug 28 '17 at 16:42
  • Why is *WaitForExit()* commented out? My first suggestion would be to add it, so it is interesting to know why (if) it doesn't help. Also, note that reading both StandardOutput and StandardError can be a pitfall, see https://stackoverflow.com/questions/139593/processstartinfo-hanging-on-waitforexit-why . – Paul-Jan Aug 28 '17 at 17:58

0 Answers0