forked from git-for-windows/git
-
Notifications
You must be signed in to change notification settings - Fork 101
Maintenance: create headless-git.exe to avoid foreground windows #304
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
derrickstolee
wants to merge
4
commits into
microsoft:tentative/vfs-2.30.0
from
derrickstolee:headless-git
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
0c602c9
win32: add a helper to run `git.exe` without a foreground window
dscho a7b10df
git maintenance: avoid console window in scheduled tasks on Windows
dscho e31ee2f
fixup! maintenance: use Windows scheduled tasks
derrickstolee 6d020f0
gvfs-helper-client: remove odb check
derrickstolee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
* headless Git - run Git without opening a console window on Windows | ||
*/ | ||
|
||
#define STRICT | ||
#define WIN32_LEAN_AND_MEAN | ||
#define UNICODE | ||
#define _UNICODE | ||
#include <windows.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <wchar.h> | ||
|
||
/* | ||
* If `dir` contains the path to a Git exec directory, extend `PATH` to | ||
* include the corresponding `bin/` directory (which is where all those | ||
* `.dll` files needed by `git.exe` are, on Windows). | ||
*/ | ||
static int extend_path(wchar_t *dir, size_t dir_len) | ||
{ | ||
const wchar_t *suffix = L"\\libexec\\git-core"; | ||
size_t suffix_len = wcslen(suffix); | ||
wchar_t *env; | ||
DWORD len; | ||
|
||
if (dir_len < suffix_len) | ||
return 0; | ||
|
||
dir_len -= suffix_len; | ||
if (memcmp(dir + dir_len, suffix, suffix_len * sizeof(wchar_t))) | ||
return 0; | ||
|
||
len = GetEnvironmentVariableW(L"PATH", NULL, 0); | ||
if (!len) | ||
return 0; | ||
|
||
env = _alloca((dir_len + 5 + len) * sizeof(wchar_t)); | ||
wcsncpy(env, dir, dir_len); | ||
wcscpy(env + dir_len, L"\\bin;"); | ||
if (!GetEnvironmentVariableW(L"PATH", env + dir_len + 5, len)) | ||
return 0; | ||
|
||
SetEnvironmentVariableW(L"PATH", env); | ||
return 1; | ||
} | ||
|
||
int WINAPI wWinMain(HINSTANCE instance, HINSTANCE previous_instance, | ||
wchar_t *command_line, int show) | ||
{ | ||
wchar_t git_command_line[32768]; | ||
size_t size = sizeof(git_command_line) / sizeof(wchar_t); | ||
const wchar_t *needs_quotes = L""; | ||
int slash = 0, i; | ||
|
||
STARTUPINFO startup_info = { | ||
.dwFlags = STARTF_USESHOWWINDOW, | ||
.wShowWindow = SW_HIDE, | ||
}; | ||
PROCESS_INFORMATION process_info = { 0 }; | ||
DWORD creation_flags = CREATE_UNICODE_ENVIRONMENT | | ||
CREATE_NEW_CONSOLE | CREATE_NO_WINDOW; | ||
DWORD exit_code; | ||
|
||
/* First, determine the full path of argv[0] */ | ||
for (i = 0; _wpgmptr[i]; i++) | ||
if (_wpgmptr[i] == L' ') | ||
needs_quotes = L"\""; | ||
else if (_wpgmptr[i] == L'\\') | ||
slash = i; | ||
|
||
if (slash + 11 >= sizeof(git_command_line) / sizeof(wchar_t)) | ||
return 127; /* Too long path */ | ||
|
||
/* If it is in Git's exec path, add the bin/ directory to the PATH */ | ||
extend_path(_wpgmptr, slash); | ||
|
||
/* Then, add the full path of `git.exe` as argv[0] */ | ||
i = swprintf_s(git_command_line, size, L"%ls%.*ls\\git.exe%ls", | ||
needs_quotes, slash, _wpgmptr, needs_quotes); | ||
if (i < 0) | ||
return 127; /* Too long path */ | ||
|
||
if (*command_line) { | ||
/* Now, append the command-line arguments */ | ||
i = swprintf_s(git_command_line + i, size - i, | ||
L" %ls", command_line); | ||
if (i < 0) | ||
return 127; | ||
} | ||
|
||
startup_info.cb = sizeof(STARTUPINFO); | ||
|
||
startup_info.hStdInput = GetStdHandle(STD_INPUT_HANDLE); | ||
startup_info.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE); | ||
startup_info.hStdError = GetStdHandle(STD_ERROR_HANDLE); | ||
|
||
if (!CreateProcess(NULL, /* infer argv[0] from the command line */ | ||
git_command_line, /* modified command line */ | ||
NULL, /* inherit process handles? */ | ||
NULL, /* inherit thread handles? */ | ||
FALSE, /* handles inheritable? */ | ||
creation_flags, | ||
NULL, /* use this process' environment */ | ||
NULL, /* use this process' working directory */ | ||
&startup_info, &process_info)) | ||
return 129; /* could not start */ | ||
WaitForSingleObject(process_info.hProcess, INFINITE); | ||
if (!GetExitCodeProcess(process_info.hProcess, &exit_code)) { | ||
CloseHandle(process_info.hProcess); | ||
return 130; /* Could not determine exit code? */ | ||
} | ||
|
||
return (int)exit_code; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jeffhostetler @dscho I hit this BUG during one of the functional tests near the end of the Scalar release build process so I'm going to cherry-pick this on top of
vfs-2.30.0
.