git - 如何捕获 `git clone` 的完整输出?

标签 git shell unix pipe

当我像往常一样运行 git clone 时,我看到了这个:

$ 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 变量中)时,我只看到了这一点:

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 中尝试过,它做了同样的事情:

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 传递?

最佳答案

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

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

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

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

参见:man 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.


要以任何其他方式强制终端,您必须预加载一些库以强制 isatty() 始终返回 true(参见:man isatty)。 git 在 source code 中使用此函数.

关于git - 如何捕获 `git clone` 的完整输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37669115/

相关文章:

linux - *所有* Unix 系统的根分区警告

python - python可以生成可执行的shell脚本吗?

xcode - 无意中出现两个主分支 Git

linux - hexdump 可以用 ASCII 显示 8 列吗?

linux - 使用客户端(linux)中的资源在远程服务器中执行命令

c - 在 Unix 上的 C 中,进程如何在不打开文件的情况下知道它对文件具有哪些权限?

python - 如何使用 python 中的 Github api 从字符串计算 SHA?

git - 如何让 `git log --name-status` 与 merge 提交一起工作?

git - 致命的 : pathspec 'file.txt' did not match any files, GIT

linux - find -name "*.xyz"-o -name "*.abc"-exec 对所有找到的文件执行,而不仅仅是指定的最后一个后缀