Git 克隆:将 stderr 重定向到 stdout,但将错误写入 stderr [英] Git clone: Redirect stderr to stdout but keep errors being written to stderr

查看:41
本文介绍了Git 克隆:将 stderr 重定向到 stdout,但将错误写入 stderr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

git clone 将其输出写入 stderr,如此处.我可以使用以下命令重定向:

git clone https://myrepo c:
epo 2>&1

但这会将所有输出(包括错误)从 stderr 重定向到 stdout.有没有办法将进度消息重定向到 stdout 但错误消息仍然写入 stderr.

解决方案

MingW 更新提供了一种使用 Git 2.15.x/2.16(2018 年第一季度)处理重定向的新方法

参见 commit b2f5571commit 1a172e4, 约翰内斯·辛德林 (dscho).
(由 Junio C Hamano 合并 -- gitster --提交 421f21c,2017 年 11 月 9 日)

<块引用>

mingw:添加实验性功能以重定向标准句柄

特别是从应用程序调用 Git 时,例如 Visual Studio 的团队资源管理器,正确关闭 stdin/stdout/stderr 非常重要.
然而,当在 Windows 上生成进程时,如果我们想使用它们,必须将这些句柄标记为可继承的,但该标志是一个全局标志,很可能被其他不知道关闭这些句柄的生成进程使用.

让我们介绍一组指定文件路径的环境变量(GIT_REDIRECT_STDIN 和朋友),或者甚至更好的命名管道(类似于 Unix 套接字)并由衍生的 Git 使用过程.
这有助于解决上述问题:这些命名管道将在启动时以不可继承的方式打开,并且不会传递任何句柄(因此任何生成的子项都不需要关闭继承的句柄).

自 v2.11.0(2) 以来,此功能随 Git for Windows(标记为实验性)一起提供,因此在此期间进行了一些认真的测试.

Git 文档 现在包括:

GIT_REDIRECT_STDIN:GIT_REDIRECT_STDOUT:GIT_REDIRECT_STDERR:

<块引用>

Windows-only:允许将标准输入/输出/错误句柄重定向到由环境变量指定的路径.这在多线程应用程序中特别有用,其中通过 CreateProcess() 传递标准句柄的规范方式不是一种选择,因为它需要将句柄标记为可继承(因此每个 产生的进程会继承它们,可能会阻止常规的 Git 操作).

主要的预期用例是使用命名管道进行通信(例如 \.pipemy-git-stdin-123).

它补充说:

<块引用>

mingw:可选地通过相同的句柄重定向 stderr/stdout

Powershell 和 Unix shell 中的2>&1"表示法意味着 stderr被重定向到 stdout 已经写入的同一个句柄.

让我们使用这个特殊值来实现与 GIT_REDIRECT_STDERRGIT_REDIRECT_STDOUT 相同的技巧:如果前者的值是2>&1,然后 stderr 将被简单地写入与 stdout 相同的句柄.

该功能是 Jeff Hostetler 建议的.

用法示例:$env:GIT_REDIRECT_STDERR = '2>&1'

git clone writes its output to stderr as documented here. I can redirect this with the following command:

git clone https://myrepo c:
epo 2>&1

But this will redirect all output, including errors, from stderrto stdout. Is there a way to redirect progress messages to stdout but have error messages still written to stderr.

解决方案

A MingW update provide a new way to handle redirection with Git 2.15.x/2.16 (Q1 2018)

See commit b2f5571, commit 1a172e4, commit 3f94442 (01 Nov 2017) by Johannes Schindelin (dscho).
(Merged by Junio C Hamano -- gitster -- in commit 421f21c, 09 Nov 2017)

mingw: add experimental feature to redirect standard handles

Particularly when calling Git from applications, such as Visual Studio's Team Explorer, it is important that stdin/stdout/stderr are closed properly.
However, when spawning processes on Windows, those handles must be marked as inheritable if we want to use them, but that flag is a global flag and may very well be used by other spawned processes which then do not know to close those handles.

Let's introduce a set of environment variables (GIT_REDIRECT_STDIN and friends) that specify paths to files, or even better, named pipes (which are similar to Unix sockets) and that are used by the spawned Git process.
This helps work around above-mentioned issue: those named pipes will be opened in a non-inheritable way upon startup, and no handles are passed around (and therefore no inherited handles need to be closed by any spawned child).

This feature shipped with Git for Windows (marked as experimental) since v2.11.0(2), so it has seen some serious testing in the meantime.

The Git documentation now includes:

GIT_REDIRECT_STDIN:
GIT_REDIRECT_STDOUT:
GIT_REDIRECT_STDERR:

Windows-only: allow redirecting the standard input/output/error handles to paths specified by the environment variables. This is particularly useful in multi-threaded applications where the canonical way to pass standard handles via CreateProcess() is not an option because it would require the handles to be marked inheritable (and consequently every spawned process would inherit them, possibly blocking regular Git operations).

The primary intended use case is to use named pipes for communication (e.g. \.pipemy-git-stdin-123).

And it adds:

mingw: optionally redirect stderr/stdout via the same handle

The "2>&1" notation in Powershell and in Unix shells implies that stderr is redirected to the same handle into which stdout is already written.

Let's use this special value to allow the same trick with GIT_REDIRECT_STDERR and GIT_REDIRECT_STDOUT: if the former's value is 2>&1, then stderr will simply be written to the same handle as stdout.

The functionality was suggested by Jeff Hostetler.

Example of usage:$env:GIT_REDIRECT_STDERR = '2>&1'

这篇关于Git 克隆:将 stderr 重定向到 stdout,但将错误写入 stderr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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