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

Я пытаюсь сделать следующее в 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

enter image description here

EDIT:

Метод 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 Ответ

0 голосов
/ 02 мая 2018

Вы не можете просто перенаправить файл в информацию Process.Start. Это операция shell , а не то, что вы можете просто вызвать. Вместо этого вам нужно прочитать стандартный вывод 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...
}
...