Skip to content

Commit 6cab30f

Browse files
authored
Merge pull request #1837 from tyrielv/hooks-stdout
Fix std handle forwarding
2 parents f231900 + bf4be0f commit 6cab30f

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

GVFS/GitHooksLoader/GitHooksLoader.cpp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,25 @@ int ExecuteHook(const std::wstring &applicationName, wchar_t *hookName, int argc
112112
PROCESS_INFORMATION pi;
113113
ZeroMemory(&si, sizeof(si));
114114
si.cb = sizeof(si);
115-
si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
116-
si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
117-
si.dwFlags = STARTF_USESTDHANDLES;
115+
DWORD creationFlags = 0;
116+
HANDLE consoleHandle;
117+
118+
/* If we have a console, use it (ie allow default behavior)*/
119+
if ((consoleHandle = CreateFile(L"CONOUT$", GENERIC_WRITE,
120+
FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
121+
FILE_ATTRIBUTE_NORMAL, NULL)) !=
122+
INVALID_HANDLE_VALUE)
123+
CloseHandle(consoleHandle);
124+
else {
125+
/* Otherwise, forward stdout/err in case they were redirected,
126+
* but do not allow creating a window.*/
127+
si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
128+
si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
129+
/* Git disallows stdin from hooks */
130+
si.dwFlags = STARTF_USESTDHANDLES;
131+
132+
creationFlags |= CREATE_NO_WINDOW;
133+
}
118134

119135
ZeroMemory(&pi, sizeof(pi));
120136

@@ -135,7 +151,7 @@ int ExecuteHook(const std::wstring &applicationName, wchar_t *hookName, int argc
135151
NULL, // Process handle not inheritable
136152
NULL, // Thread handle not inheritable
137153
TRUE, // Set handle inheritance to TRUE
138-
CREATE_NO_WINDOW, // Process creation flags
154+
creationFlags , // Process creation flags
139155
NULL, // Use parent's environment block
140156
NULL, // Use parent's starting directory
141157
&si, // Pointer to STARTUPINFO structure

0 commit comments

Comments
 (0)