Reputation: 609
I am trying to run several git commands via c# and continuing to work with the resulting outputs.
I wrote this little Method to work with:
private string runGitCommand(string gitCommand, string path)
{
ProcessStartInfo processInfo = new ProcessStartInfo();
processInfo.FileName = System.Windows.Forms.Application.StartupPath+"\\gitcommand.bat";
processInfo.WindowStyle = ProcessWindowStyle.Hidden;
processInfo.Arguments = gitCommand;
processInfo.CreateNoWindow = true;
processInfo.UseShellExecute = false;
processInfo.WorkingDirectory = path;
processInfo.RedirectStandardError = true;
processInfo.RedirectStandardOutput = true;
Process process = new Process();
process.StartInfo = processInfo;
string output="";
process.OutputDataReceived += (a, b) => {
Console.WriteLine(b.Data);
output += b.Data;
}
;
process.ErrorDataReceived += (a, b) => Console.WriteLine(b.Data);
process.Start();
process.BeginErrorReadLine();
process.BeginOutputReadLine();
process.WaitForExit();
return output;
}
sure thing, some settings are for debugging (like the unneccessary Console.WriteLine()
) but it works with most git commands like status
or rev-parse HEAD
.
This is my batch-file I use:
@"C:/Program Files (x86)/Git/bin/git.exe" %1 %2
There are two arguments since rev-parse HEAD
are two arguments.
I don't have any Problems executing status
so this double arguments shouldn't be the problem.
Everything works exactly like intended but when I execute fetch
the program hangs up.
I used the batch file in cmd with same inputs and it did take its time (about 1-2 seconds) but it exited without any problems (no output was given there).
any thoughts?
Upvotes: 1
Views: 605
Reputation: 609
The issue was a not working RSA authentication.
Since git fetch
contacts the server and git status
not, this error could only be seen by typing the used commands by hand in cmd (not git bash).
The program was waiting for user input. As suggested by @ardila the usage of lib2git should be used for a robust use of git in C#.
Upvotes: 1