gpt4 book ai didi

Git 克隆 : Redirect stderr to stdout but keep errors being written to stderr

转载 作者:IT王子 更新时间:2023-10-29 01:30:30 30 4
gpt4 key购买 nike

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

git clone https://myrepo c:\repo 2>&1

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

最佳答案

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

参见 commit b2f5571 , commit 1a172e4 , commit 3f94442 (2017 年 11 月 1 日)作者:Johannes Schindelin (dscho) .
(由 Junio C Hamano -- gitster -- merge 于 commit 421f21c ,2017 年 11 月 9 日)

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.

Git documentation现在包括:

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. \\.\pipe\my-git-stdin-123).

它补充说:

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.

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

关于Git 克隆 : Redirect stderr to stdout but keep errors being written to stderr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34820975/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com