git的stderr输出不能通过管道 [英] git stderr output can't pipe

查看:303
本文介绍了git的stderr输出不能通过管道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个图形化的URI处理程序的git://使用bash和的zenity联系,我使用的zenity文本信息对话框,显示Git的克隆输出,而它的运行,使用FIFO管道。剧本是大约90线长,所以我不会打扰在这里张贴,但这里是最重要的行:

I'm writing a graphical URI handler for git:// links with bash and zenity, and I'm using a zenity 'text-info' dialog to show git's clone output while it's running, using FIFO piping. The script is about 90 lines long, so I won't bother posting it here, but here's the most important lines:

git clone "$1" "$target" 2>&1 | cat >> /tmp/githandler-fifo &
cat /tmp/githandler-fifo | zenity --text-info --text='Cloning git repository' &

我使用的,而不是直接管FIFO,使他们能够异步运行,并允许如果关闭窗口的zenity杀饭桶。

I'm using FIFO instead of a direct pipe to allow them to run asynchronously and allow for killing git if the zenity window is closed.

问题是,从Git的输出出现的唯一的线是第一条:

Problem is, the only line that appears from git's output is the first:

Initialized empty Git repository in /home/delan/a/.git/

的其他行以计数对象等不显示或显示在终端上。

The other lines with counting objects, etc. don't show or are shown on the terminal.

当前理由

,为什么这不工作目前的共识似乎是,是无阻塞的第一行之后退出,只轻描淡写地说,以的zenity而不是休息。我的目的是迫使阻塞读,并有的zenity的文本信息对话框显示所有的输出逐步

The current consensus as to why this isn't working seems to be that cat is non-blocking and quits after the first line, only passing that to zenity and not the rest. My aim is to force blocking for the read, and have zenity's text info dialog show all output progressively.

混帐输出进展STDERR信息(什么比初始化的消息等),但现在我试着管标准错误到一个文件或标准输出合并,消息消失。

git outputs progress messages (anything other than the "Initialized" message) on stderr, but the moment I try to pipe stderr to a file or to merge with stdout, the messages disappear.

修复尝试1

我试着写在C,面包和BWRITE的猫的函数的两个版本堵,像这样的:

I've tried to write two blocking versions of cat's functions in C, bread and bwrite, like this:

#include <stdio.h>
main(int argc, char **argv) {
    int c;
    for (;;) {
        freopen(argv[1], "r", stdin);
        while ((c = getchar()) != EOF)
            putchar(c);
    }
}

#include <stdio.h>
main(int argc, char **argv) {
    int c;
    for (;;) {
        freopen(argv[1], "w", stdout);
        while ((c = getchar()) != EOF)
            putchar(c), fputs("writing", stderr);
    }
}

他们的工作很好,因为他们阻止,不上EOF退出,但还没有完全解决了这个问题。此刻,使用一个,另一个,或两者兼而有之,工作在理论,但在实践中,的zenity表示什么都没有现在

They work nicely because they block and don't quit on EOF, but it hasn't quite solved the problem yet. At the moment, using one, the other, or both, works in theory, but in practice, zenity shows nothing at all now.

修复尝试2

@mvds建议使用普通文件,结合尾-f ,而不是,可以这样做这个。在这样一个简单的解决方案感到惊讶(感谢!)我尝试过,但遗憾的是,只有第一行中的zenity,没有别的出现了。

@mvds suggested that using a regular file, in combination with tail -f rather than cat, may do this. Surprised at such a simple solution (thanks!) I tried it but unfortunately, only the first line showed up in zenity and nothing else.

修复尝试3

做一些strace'ing和检查Git的源$ C ​​$ C之后,我才知道的git输出所有的进度信息标准错误(任何过去的初始化消息),而事实上,这是第一行和我的假设这是因为猫早早就退出EOF是一个巧合/误导的假设(Git并不EOF直到程序结束)的

After doing some strace'ing and inspecting git's source code, I realise that git outputs all its progress information (anything past the "Initialized" message) on stderr, and the fact that this is the first line and my assumption that it's because of cat quitting early on EOF was a coincidence/misguided assumption (git doesn't EOF until the program ends).

这种情况似乎变得简单了很多,因为我不应该改变从原来的code任何东西(在问题的开始),它应该工作。神不知鬼不觉,然而,stderr输出'消失'重定向时 - 而这仅是事情发生在混帐

The situation seemed to become a lot simpler, as I shouldn't have to change anything from the original code (at the start of the question) and it should work. Mysteriously, however, the stderr output 'vanishes' when redirected - and this is only something that happens in git.

测试案例?试试这个,看看你是否在文件中看到什么(你不会):

Test case? Try this, and see if you see anything in the file (you won't):

git clone git://anongit.freedesktop.org/xorg/proto/dri2proto 2> hurr

这违背了我所知道的标准错误和重定向一切;我甚至写了一个小C程序在stderr和stdout输出来证明自己,重定向只是不为git的工作。

This goes against everything I know about stderr and redirection; I've even written a little C program that outputs on stderr and stdout to prove to myself that redirection just doesn't work for git.

修复尝试4

在用的JakubNarębski的回答线,以及回复我的电子邮件发送到git的邮件列表, - 进步是我所需要的选项。请注意,此选项仅在命令后的作品,而不是之前克隆

In line with Jakub Narębski's answer, as well as replies to emails I sent to the git mailing list, --progress is the option I need. Note that this option only works after the command, and not before clone.

成功!

非常感谢你的一切帮助。这是固定电话:

Thank you very much for all your help. This is the fixed line:

git clone "$1" "$target" --progress > /tmp/githandler-fifo 2>&1 &

推荐答案

我认为,至少有一些进展报告被压制时,输出的不是终端的(TTY)。我不知道它是否适用于你的情况,但试图通过 - 进步 选项为混帐克隆(即使用 git的克隆--progress&LT;库方式&gt;

I think that at least some of progress reports gets silenced when output is not a terminal (tty). I'm not sure if it applies to your case, but try to pass --progress option to 'git clone' (i.e. use git clone --progress <repository>).

虽然我不知道这是否是你想拥有什么。

Though I don't know if it is what you wanted to have.

这篇关于git的stderr输出不能通过管道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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