0

I am using Microsoft.PowerShell.SDK (Version 7.3.3) in order to clone a git repository from C# like this:

(Note: I know there is libgit2sharp, but it doesn't support all of my use cases).

    using (var ps = PowerShell.Create())
    {
        ps.AddScript($"git clone {url} {localPath}");
        ps.Invoke();
        if (ps.HadErrors)
        {
            Console.WriteLine("Errors occurred: ");
            foreach (var error in ps.Streams.Error)
            {
                Console.WriteLine(error.ToString());
            }
        }
    }

When successfully pulling a repository with that, I get the following output on the console:

Errors occurred:

Cloning into '<local path>' ...

Why does the informational output end up in the error stream?

Tim Meyer
  • 12,210
  • 8
  • 64
  • 97
  • This has nothing to do with PowerShell. Git uses stdout and stderr in non-standard ways. Read the Git documentation to understand what it writes to each stream. – madreflection Mar 16 '23 at 16:11
  • `stderr` is "for writing diagnostic output", any metadata about the progress of the operation. It's been this way since forever, that language is straight from [the actual standard](https://pubs.opengroup.org/onlinepubs/9699919799/functions/stderr.html), it's been that way for as long as I can remember. – jthill Mar 17 '23 at 02:13
  • Intermixing progress reports with the requested data in the same stream is the best possible way to corrupt the requested data. – jthill Mar 17 '23 at 02:22

1 Answers1

0

Thanks to @madreflection 's comment, I found out this is a specialty of git

Adding the --quiet flag will cause git to only report actual errors to the error stream and be silent in any "good" case (Compare documentation here):

ps.AddScript($"git clone {url} {localPath} --quiet");
Tim Meyer
  • 12,210
  • 8
  • 64
  • 97