All of lore.kernel.org
 help / color / mirror / Atom feed
From: Johannes Schindelin <johannes.schindelin@gmx.de>
To: git@vger.kernel.org
Cc: Junio C Hamano <gitster@pobox.com>,
	Eric Sunshine <sunshine@sunshineco.com>,
	Jonathan Nieder <jrnieder@gmail.com>
Subject: [PATCH v2 2/3] mingw: optionally redirect stderr/stdout via the same handle
Date: Wed, 1 Nov 2017 18:10:30 +0100 (CET)	[thread overview]
Message-ID: <d98f6a9a6eeb01f68f4bc853952ca675e97940bd.1509556153.git.johannes.schindelin@gmx.de> (raw)
In-Reply-To: <cover.1509556153.git.johannes.schindelin@gmx.de>

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.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 compat/mingw.c  | 15 +++++++++++++++
 t/t0001-init.sh |  8 +++++++-
 2 files changed, 22 insertions(+), 1 deletion(-)

diff --git a/compat/mingw.c b/compat/mingw.c
index 6c6c7795a70..2d44d21aca8 100644
--- a/compat/mingw.c
+++ b/compat/mingw.c
@@ -2160,6 +2160,21 @@ static void maybe_redirect_std_handle(const wchar_t *key, DWORD std_id, int fd,
 			CloseHandle(handle);
 		return;
 	}
+	if (std_id == STD_ERROR_HANDLE && !wcscmp(buf, L"2>&1")) {
+		handle = GetStdHandle(STD_OUTPUT_HANDLE);
+		if (handle == INVALID_HANDLE_VALUE) {
+			close(fd);
+			handle = GetStdHandle(std_id);
+			if (handle != INVALID_HANDLE_VALUE)
+				CloseHandle(handle);
+		} else {
+			int new_fd = _open_osfhandle((intptr_t)handle, O_BINARY);
+			SetStdHandle(std_id, handle);
+			dup2(new_fd, fd);
+			/* do *not* close the new_fd: that would close stdout */
+		}
+		return;
+	}
 	handle = CreateFileW(buf, desired_access, 0, NULL, create_flag,
 			     flags, NULL);
 	if (handle != INVALID_HANDLE_VALUE) {
diff --git a/t/t0001-init.sh b/t/t0001-init.sh
index 0fd2fc45385..c413bff9cf1 100755
--- a/t/t0001-init.sh
+++ b/t/t0001-init.sh
@@ -456,7 +456,13 @@ test_expect_success 're-init from a linked worktree' '
 test_expect_success MINGW 'redirect std handles' '
 	GIT_REDIRECT_STDOUT=output.txt git rev-parse --git-dir &&
 	test .git = "$(cat output.txt)" &&
-	test -z "$(GIT_REDIRECT_STDOUT=off git rev-parse --git-dir)"
+	test -z "$(GIT_REDIRECT_STDOUT=off git rev-parse --git-dir)" &&
+	test_must_fail env \
+		GIT_REDIRECT_STDOUT=output.txt \
+		GIT_REDIRECT_STDERR="2>&1" \
+		git rev-parse --git-dir --verify refs/invalid &&
+	printf ".git\nfatal: Needed a single revision\n" >expect &&
+	test_cmp expect output.txt
 '
 
 test_done
-- 
2.15.0.windows.1



  parent reply	other threads:[~2017-11-01 17:10 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-10-30 17:10 [PATCH 0/3] mingw: introduce a way to avoid std handle inheritance Johannes Schindelin
2017-10-30 17:10 ` [PATCH 1/3] mingw: add experimental feature to redirect standard handles Johannes Schindelin
2017-10-30 17:10 ` [PATCH 2/3] mingw: special-case GIT_REDIRECT_STDERR=2>&1 Johannes Schindelin
2017-10-30 17:10 ` [PATCH 3/3] mingw: document the experimental standard handle redirection Johannes Schindelin
2017-10-30 18:58   ` Eric Sunshine
2017-10-31 17:08     ` Johannes Schindelin
2017-11-01  4:58       ` Junio C Hamano
2017-11-01 16:37         ` Johannes Schindelin
2017-11-02  1:31           ` Junio C Hamano
2017-11-02 17:20             ` Johannes Schindelin
2017-11-03  1:50               ` Junio C Hamano
2017-11-03 11:51                 ` Johannes Schindelin
2017-10-30 20:55 ` [PATCH 0/3] mingw: introduce a way to avoid std handle inheritance Jonathan Nieder
2017-10-31  5:48   ` Junio C Hamano
2017-10-31 17:12   ` Johannes Schindelin
2017-10-31 18:09     ` Jonathan Nieder
2017-11-01 17:07       ` Johannes Schindelin
2017-11-01 17:17         ` Stefan Beller
2017-11-01 17:10 ` [PATCH v2 " Johannes Schindelin
2017-11-01 17:10   ` [PATCH v2 1/3] mingw: add experimental feature to redirect standard handles Johannes Schindelin
2017-11-01 17:10   ` Johannes Schindelin [this message]
2017-11-01 17:10   ` [PATCH v2 3/3] mingw: document the standard handle redirection Johannes Schindelin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=d98f6a9a6eeb01f68f4bc853952ca675e97940bd.1509556153.git.johannes.schindelin@gmx.de \
    --to=johannes.schindelin@gmx.de \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=jrnieder@gmail.com \
    --cc=sunshine@sunshineco.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.