I am trying to do the following in C#:
- Get the difference between two branches.
- Redirect the output in a patch file.
- Checkout a new empty branch.
- Apply the patch file to this new branch.
- 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
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.