fatal: неоднозначный аргумент '>' ошибка при непосредственном выполнении git diff из C#

Я пытаюсь сделать следующее в C#:

  1. Получите разницу между двумя ветвями.
  2. Перенаправить вывод в файл патча.
  3. Оформить заказ новой пустой ветке.
  4. Примените файл патча к этой новой ветке.
  5. Добавить файлы и зафиксировать эту ветку в удаленном репо.

Текущие команды git, которые я выполняю:

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

Хотя это работает нормально с git bash, это не так при выполнении его из C#, и я получаю следующее сообщение для команды diff:

git diff branch1 > delta.patch

введите описание изображения здесь

РЕДАКТИРОВАТЬ:

Метод C#, который я использую для запуска каждой из вышеупомянутых команд, следующий:

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();
        }

И это называется так:

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);
 }

Примечание: я использую LibGit2Sharp в других частях моего проекта, но в этом конкретном случае я не могу использовать его, так как LibGit2Sharp не реализует git-apply.

1 ответ

Решение

Вы не можете просто перенаправить файл в Process.Start Информация. Это операция оболочки, а не то, что вы можете просто вызвать. Вместо этого вам нужно прочитать git стандартный вывод приложения самостоятельно. Например:

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...
}
Другие вопросы по тегам