简体   繁体   English

从git捕获STDOUT / STDERR

[英]Catching STDOUT/STDERR from git

I'm trying to execute a git command from perl via IPC::Run which catches STDOUT/STDERR-Output from git in perl variables. 我正在尝试通过IPC :: Run从perl执行git命令,该命令从perl变量中的git捕获STDOUT / STDERR-输出。

Here is what i did: 这是我所做的:

use strict;
use warnings;
use IPC::Run;
my $stderr, $stdout, @cmd;
push @cmd, "git";
push @cmd, "clone";
push @cmd, "http://my.gitserver.com/scm/tst2/abc.git";

my $success = IPC::Run::run \@cmd, '>', \$stdout, '2>', \$stderr;
1;

Running this results in: 运行此结果将导致:

  • $stdout: Cloning into 'abc'... $ stdout: 克隆到'abc'...
  • $stderr: EMPTY $ stderr:空

Running git command from commandline directly results in following output: 从命令行运行git命令直接导致以下输出:

$ git clone http://my.gitserver.com/scm/tst2/abc.git
Cloning into 'abc'...
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.

As you can see, git produces some more output on commandline than I actually capture within my perl implementation. 如您所见,git在命令行上产生的输出比我在perl实现中捕获的输出还要多。

What's going wrong? 怎么了

The good news is your code is correct. 好消息是您的代码正确。 The confusing part is git changes its output depending on whether the standard error is connected to a terminal. 令人困惑的部分是git会根据标准错误是否连接到端子来更改其输出。

To reproduce your Perl program's output from a shell, run the following command from a bash prompt. 要从shell复制Perl程序的输出,请在bash提示符下运行以下命令。

$ git clone http://my.gitserver.com/scm/tst2/abc.git |& cat
Cloning into 'abc'...

The |& redirection connect the git's standard error (where the expected progress output goes) and standard output to a pipe rather than the terminal. |&重定向将git的标准错误(预期进度输出到达的地方)和标准输出连接到管道而不是终端。

To see the usual output, you will have to connect git's standard error to a pseudo terminal , also known as a pty . 要查看常规输出,您必须将git的标准错误连接到伪终端 ,也称为pty IPC::Run has support for pseudo terminals, described in the Pseudo Terminals section of its documentation. IPC::Run支持伪终端,如其文档的“ 伪终端”部分所述。 Instead of redirecting standard error with 2> , use 2>pty> instead. 不用2>重定向标准错误,而是使用2>pty> Note that this adds a dependency to IO::Pty . 请注意,这为IO::Pty添加了依赖项。

Tweaking the run redirect and adding debug output as in the code below 调整run重定向并添加调试输出,如以下代码所示

#! /usr/bin/env perl

use strict;
use warnings;

use IPC::Run;

my $cloneurl = 'http://my.gitserver.com/scm/tst2/abc.git';

my($stderr,$stdout);
my @cmd = (qw/ git clone /, $cloneurl);

my $success = IPC::Run::run \@cmd, '>', \$stdout, '2>pty>', \$stderr;
print "success=[$success]\n";
print "stdout=[$stdout]\n";
print "stderr=[$stderr]\n";

produces output that resembles 产生类似于

success=[1]
stdout=[]
stderr=[Cloning into 'abc'...
remote: Counting objects: 2818, done.
remote: Compressing objects: 100% (1965/1965), done.
remote: Total 2818 (delta 1200), reused 770 (delta 287)
Receiving objects: 100% (2818/2818), 2.19 MiB | 1.19 MiB/s, done.
Resolving deltas: 100% (1200/1200), done.
Checking connectivity... done.
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM