如何捕获git clone的完整输出? [英] How to capture full output of `git clone`?

查看:128
本文介绍了如何捕获git clone的完整输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我像往常一样运行git clone时,会看到以下内容:

When I run git clone like usual, I see this:

$ git clone https://github.com/bensmithett/webpack-css-example
Cloning into 'webpack-css-example'...
remote: Counting objects: 179, done.
remote: Total 179 (delta 0), reused 0 (delta 0), pack-reused 179
Receiving objects: 100% (179/179), 24.07 KiB | 0 bytes/s, done.
Resolving deltas: 100% (79/79), done.
Checking connectivity... done

但是,当我尝试将其重定向到文件(或将其存储在shell变量中)时,我只会看到以下内容:

However, when I try to redirect that to a file (or store it in a shell variable) I only see this:

Cloning into 'webpack-css-example'...

这是我尝试过的:

$ git clone https://github.com/bensmithett/webpack-css-example 2>&1 | tee out.log
$ cat out.log
Cloning into 'sample-data'...

我也在Node.js中尝试过,它做同样的事情:

I tried it in Node.js as well, and it does the same thing:

const fs = require('fs');
const child = spawn('git clone https://github.com/bensmithett/webpack-css-example');
child.stdout.on('data', function(data){
  console.log(String(data));
});
child.stderr.on('data', function(data){
  console.log(String(data));
});
// Cloning into 'webpack-css-example'...

为什么所有 remote:等东西都无法通过管道传递到stdin/stderr上?有什么方法可以捕获输出?如果不是,那是什么原因使得输出在终端中显示,但没有通过stdout或stderr传递?

Why is all the remote: etc. stuff not getting piped to either stdin/stderr? Is there any way to capture the output? If not, what is happening that makes it so the output is displayed in the terminal, yet it is not being passed through either stdout or stderr?

推荐答案

默认情况下,只有在将标准错误流定向到终端时,Git才会显示克隆进度.由于您将其重定向到管道,因此输出流不再连接到终端.因此,为了捕获输出,您需要添加-progress 参数以强制显示进度状态,例如

By default Git will display the cloning progress only when the standard error stream is directed to a terminal. Since you're redirecting it to the pipe, the output stream is no longer attached to the terminal. So in order to capture the output, you need to add --progress parameter to force the progress status, e.g.

git clone --progress https://github.com/foo/bar 2> out.log

或者,为了将输出存储在shell变量中

or, in order to store the output in a shell variable

out=$(git clone --progress https://github.com/foo/bar 2>&1)

请参阅: man git-clone

-进度

默认情况下,当连接到终端时,将在标准错误流上报告进度状态,除非指定了 -q .即使标准错误流未定向到终端,该标志也会强制显示进度状态.

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.


要以任何其他方式强制终端,您必须预加载一些库以强制 isatty()返回始终为true(请参阅: man isatty ).git在源代码.


To force the terminal in any other way, you'll have to preload some library to force isatty() to return always true (see: man isatty). This function is used by git across the source code.

这篇关于如何捕获git clone的完整输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆