Search code examples
c#gitgit-diffgit-patchgit-apply

fatal: ambiguous argument '>' error when executing git diff from C# directly


I am trying to do the following in C#:

  1. Get the difference between two branches.
  2. Redirect the output in a patch file.
  3. Checkout a new empty branch.
  4. Apply the patch file to this new branch.
  5. Add files & commit this branch to the remote repo.

The current git commands I am running:

git checkout branch2
git diff branch1 > delta.patch
git checkout --orphan delta_branch 
git rm -rf . 
git apply delta.patch
git add -A  
git commit -m "Adding a temporary branch.." 
git push -u origin delta_branch

While this works fine from the git bash, it does not when executing it from C# and I get the following message for the diff command:

git diff branch1 > delta.patch

enter image description here

EDIT:

The C# method I am using to run each of the above mentioned commands is the following:

public void ExecuteGitCommand(string sourceDirectory, string gitExePath, string command)
        {
            ProcessStartInfo gitInfo = new ProcessStartInfo();
            gitInfo.CreateNoWindow = true;
            gitInfo.RedirectStandardError = true;
            gitInfo.RedirectStandardOutput = true;
            gitInfo.FileName = gitExePath;
            gitInfo.UseShellExecute = false;

            Process gitProcess = new Process();

            gitInfo.Arguments = command;
            gitInfo.WorkingDirectory = sourceDirectory;

            gitProcess.StartInfo = gitInfo;
            gitProcess.Start();

            string output;
            string error;

            using (StreamReader streamReader = gitProcess.StandardOutput)
            {
                output = streamReader.ReadToEnd();
            }

            using (StreamReader streamReader = gitProcess.StandardError)
            {
                error = streamReader.ReadToEnd();
            }

            Console.WriteLine("Output:");
            Console.WriteLine(output);

            if (!string.IsNullOrEmpty(error))
            {
                Console.WriteLine("Error:");
                Console.WriteLine(error);
            }

            gitProcess.WaitForExit();
            gitProcess.Close();
        }

And it is called like this:

string[] commands = new string[] { gitCheckout, gitDiff, gitCheckoutDelta, gitRmDeltaFiles, gitApplyPatch, gitAdd, gitCommit, gitPush };

foreach(string command in commands)
 {
     Console.WriteLine(command); //debug only
     ExecuteGitCommand(sourceDirectory, gitExePath, command);
 }

Note: I am using LibGit2Sharp in other parts of my project but in this specific case I cannot make use of it, since LibGit2Sharp does not implement git-apply.


Solution

  • You cannot simply redirect to a file in the Process.Start information. That's a shell operation, not something that you can simply invoke. Instead, you'll need to read the git application's standard output yourself. For example:

    ProcessStartInfo startInfo = new ProcessStartInfo();
    startInfo.UseShellExecute = false;
    
    startInfo.RedirectStandardInput = true;
    startInfo.RedirectStandardOutput = true;
    startInfo.RedirectStandardError = true;
    
    startInfo.FileName = "git.exe";
    startInfo.Arguments = "diff branch1";
    
    Process process = new Process();
    process.StartInfo = startInfo;
    process.Start();
    
    while ((line = process.StandardOutput.ReadLine()) != null)
    {
         // This is the output you're reading...
    }