Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get git command line return value using c#

Tags:

git

c#

I want to run git commands from c#. below is the coded I had written and it does execute the git command but I am not able to capture the return value. When I manually run it from command line this is the output I get.

enter image description here

When I run from the program the only thing I get is

Cloning into 'testrep'...

Rest of the info is not capture, but the command is executed successfully.

class Program
{
    static void Main(string[] args)
    {
        ProcessStartInfo startInfo = new ProcessStartInfo("git.exe");

        startInfo.UseShellExecute = false;
        startInfo.WorkingDirectory = @"D:\testrep";
        startInfo.RedirectStandardInput = true;
        startInfo.RedirectStandardOutput = true;
        startInfo.Arguments = "clone http://tk1:tk1@localhost/testrep.git";

        Process process = new Process();
        process.StartInfo = startInfo;
        process.Start();

        List<string> output = new List<string>();
        string lineVal = process.StandardOutput.ReadLine();

        while (lineVal != null)
        {

            output.Add(lineVal);
            lineVal = process.StandardOutput.ReadLine();

        }

        int val = output.Count();
        process.WaitForExit();

    }
}
like image 897
kumar Avatar asked Oct 17 '12 10:10

kumar


3 Answers

Have you tried libgit2sharp? The documentation is not complete, but it is pretty easy to use and there's a nuget package for it. You can always look at the test code to see about usage as well. A simple clone would be like this:

string URL = "http://tk1:tk1@localhost/testrep.git";
string PATH = @"D:\testrep";
Repository.Clone(URL, PATH);

Fetching changes is easy as well:

using (Repository r = new Repository(PATH))
{
    Remote remote = r.Network.Remotes["origin"];
    r.Network.Fetch(remote, new FetchOptions());
}
like image 106
Jason Goemaat Avatar answered Sep 27 '22 00:09

Jason Goemaat


From the manual page for git clone:

--progress Progress status is reported on the standard error stream by default when it is attached to a terminal, unless -q is specified. This flag forces progress status even if the standard error stream is not directed to a terminal.

The last three lines in the output when running git clone interactively are sent to standard error, not standard output. They won't show up there when you run the command from your program, however, since it's not an interactive terminal. You could force them to appear, but the output isn't going to be anything usable for a program to parse (lots of \rs to update the progress values).

You are better off not parsing the string output at all, but looking at the integer return value of git clone. If it's nonzero, you had an error (and there will probably be something in standard error that you can show to your user).

like image 42
jbowes Avatar answered Sep 25 '22 00:09

jbowes


Once you call process.WaitForExit() and the process has terminated, you can simply use process.ExitCode which will get you the value that you want.

like image 43
nickolayratchev Avatar answered Sep 24 '22 00:09

nickolayratchev