git命令不尊重io重定向 [英] git commands not respecting io redirection

查看:181
本文介绍了git命令不尊重io重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一些git命令是关于io重定向的注释,例如 git bundle 。我怀疑这与使用的底层管道命令有关。关于 IO重定向有一个很好的教程,还有一个类似的问题回答了与 git命令。但是,该解决方案不适用于所有 git命令,例如。 git bundle



例如

  $ / usr / bin / git bundle create /tmp/backup.bundle --all> /tmp/backup.txt 2>& 1 
$ ls -al / tmp
-rw-rw-r-- 1 myuser mygroup 4329 1月25日10:46 backup.bundle
-rw-rw-r-- 1 myuser mygroup 0 1月25日10:46 backup.txt

但是,从命令行我可以看到有一些有趣的输出:

  $ / usr / bin / git bundle创建/tmp/backup.bundle --all 
计数对象:6,完成。
压缩对象:100%(6/6),完成。
写作对象:100%(6/6),4.11 KiB | 0字节/秒,完成。
总计6(delta 0),重用0(delta 0)

我用IO重定向捕获这个问题吗?输出到哪里去了?

解决方案

它无处不在 - 或者更确切地说,Git并不是首先打印它。

各种Git命令都有一个 -v - verbose 选项,并且 -q - 安静选项。 Verbose使他们能够打印诸如计数,压缩和书写对象等东西。安静使他们不打印这样的事情。然而,默认是检查文件描述符上的 isatty()结果,并根据结果设置或清除详细选项。这意味着当你重定向输出(或者使用来自Python subprocess pipes的命令)时,它们默认为安静,或者至少是非冗长的。 (安静可能会抑制更多的细节打开。)



有时您可以通过添加明确的 -v ,例如 git fetch -v ,但是在 git fetch 中至少有一个小错误,将选项传递给它运行的一些子命令,所以这对于所有情况都不适用(几周前我浪费了几个小时)。



git bundle 命令首先缺少一个 -v 选项,所以你不能设置它。通过使用pty运行命令可以获得详细的输出,因此 isatty()表示是的,这个I / O设备是一个tty。 sh模块可以做到这一点,尽管我自己并没有使用它。


Some git commands are note respecting io redirection, for example git bundle. I suspect this is related to the underlying "plumbing" commands used. There is a nice tutorial on IO redirection, and a similar question answered related to git commands. However that solution does not work for all git commands, eg. git bundle.

eg.

$ /usr/bin/git bundle create /tmp/backup.bundle --all >/tmp/backup.txt 2>&1
$ ls -al /tmp
-rw-rw-r--  1 myuser mygroup  4329 Jan 25 10:46 backup.bundle
-rw-rw-r--  1 myuser mygroup     0 Jan 25 10:46 backup.txt

However, from the command line I can see there is some interesting output:

$ /usr/bin/git bundle create /tmp/backup.bundle --all
Counting objects: 6, done.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (6/6), 4.11 KiB | 0 bytes/s, done.
Total 6 (delta 0), reused 0 (delta 0)

So why can't I capture this with IO redirection? Where is the output going?

解决方案

It's going nowhere—or more precisely, Git isn't printing it in the first place.

Various Git commands have a -v or --verbose option, and a -q or --quiet option. Verbose makes them print things like counting, compressing and writing objects. Quiet makes them not print such things. The default, however, is to examine isatty() results on file descriptors, and set or clear the verbose option based on the result.

This means that when you redirect the output (or use the commands from Python subprocess pipes), they default to quiet, or at least non-verbose. (Quiet may suppress more than verbose turns on.)

You can sometimes capture the verbose output by adding an explicit -v, e.g., git fetch -v, but there's at least a minor bug in git fetch where it fails to pass the option on to some of the subcommands it runs, so this doesn't quite work for all cases (I wasted several hours on this a few weeks ago).

The git bundle command lacks a -v option in the first place, so you cannot set it. It's possible to get the verbose output by running the command via a pty, so that isatty() says "yes, this I/O device is a tty". The sh module can do this, although I have not used it myself.

这篇关于git命令不尊重io重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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