5

I'm trying to include opening Git Bash, pushing and pulling in my c# code. Whilst opening Git Bash with Process.Start() is not the problem, I cannot manage to write commands into Git Bash.

I've tried including commands in ProcessStartInfo.Arguments, as well as redirecting the standard Output. Both has not worked at all. Down below you can see the different code snippets I tried.

private void Output()
{
    //Try 1
    processStartInfo psi = new ProcessStartInfo();
    psi.FileName = "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Git\Git Bash.lnk";
    psi.UseShellExecute = false;
    psi.RedirectStandardOutput = true;
    psi.Argument = "git add *";
    Process p = Process.Start(psi);
    string strOutput = p.StandardOutput.ReadToEnd();
    Console.WriteLine(strOutput);

    //Try 2
    ProcessStartInfo psi = new ProcessStartInfo(@"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Git\Git Bash.lnk");
    Process.Start(psi);
    psi.Arguments = "git add *";
    Process.Start(psi);

    //Try 3
    var escapedArgs = cmd.Replace("\"", "\\\"");
    var process = new Process()
    {
        StartInfo = new ProcessStartInfo
        {
            FileName = @"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Git\Git Bash.lnk",
            Arguments = "cd C:\\Users\\strit\\autocommittest2\\autocommittest2\n",
             RedirectStandardOutput = true,
             UseShellExecute = false,
             CreateNoWindow = true,
         }
    };
    process.Start();
    string result = process.StandardOutput.ReadToEnd();
    process.WaitForExit();
}

Git Bash opens but nothing is written in the command line.

3 Answers 3

3

I know it is old question, still adding answer as few days ago I was also facing same issue.

I think what you are missing is -c parameter. I used below code and it solved this issue. -c tells git-bash to execute whatever follows, it is similar to -cmd parameter in command line.

In below mentioned function -

fileName = path of git-bash.exe.

command = git command which you want to execute.

workingDir = Local path of git repository.

public static void ExecuteGitBashCommand(string fileName, string command, string workingDir)
{

    ProcessStartInfo processStartInfo = new ProcessStartInfo(fileName, "-c \" " + command + " \"")
    {
        WorkingDirectory = workingDir,
        RedirectStandardOutput = true,
        RedirectStandardError = true,
        RedirectStandardInput = true,
        UseShellExecute = false,
        CreateNoWindow = true
    };

    var process = Process.Start(processStartInfo);       
    process.WaitForExit();

    string output = process.StandardOutput.ReadToEnd();
    string error = process.StandardError.ReadToEnd();
    var exitCode = process.ExitCode;

    process.Close();
}

I hope it solves the issue.

Sign up to request clarification or add additional context in comments.

Comments

0

I think you are on the right way. I would try to use the git in the path, but it should be possible to also use the git-bash.exe directly, at my machine it is located here: C:\Program Files\Git\git-bash.exe.

Process gitProcess = new Process();
gitInfo.Arguments = YOUR_GIT_COMMAND; // such as "fetch origin"
gitInfo.WorkingDirectory = YOUR_GIT_REPOSITORY_PATH;
gitInfo.UseShellExecute = false; 

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

string stderr_str = gitProcess.StandardError.ReadToEnd();  // pick up STDERR
string stdout_str = gitProcess.StandardOutput.ReadToEnd(); // pick up STDOUT

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

1 Comment

thanks, I changed my code. Problem: the command added as argument isn't inserted as a regular Git Bash command. If the command ist simply "test" I get bash: /usr/bin/test: cannot execute binary file Exit 126. If the command is e.g. "cd c:" git bash just closes right after opening. Any clue why that's happening? Code: ProcessStartInfo startInfo = new ProcessStartInfo { FileName = @"C:\Program Files\git-bash.exe", Arguments = command, RedirectStandardOuput = true, UseShellExecute = false, CreateNoWIndow = true, } Process pro = Process.Start(startInfo); pro.WaitForExit(); pro.Close();
0

Like @S.Spieker already told you in it's good answer, no need to use git bash (it makes it harder to achieve and less performant), just call directly the git executable.

You could have a look to the GitExtensions code that is doing that: https://github.com/gitextensions/gitextensions/blob/027eabec3be497f8d780cc68ece268c64a43a7d5/GitExtensionsVSIX/Git/GitCommands.cs#L112

You could also achieve what you want using libgit2sharp (https://github.com/libgit2/libgit2sharp). That could be easier if you want to interpret the results of the command run.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.