Skip to content

Commit 4759cd9

Browse files
committed
win32: add a helper to run git.exe without a foreground window
On Windows, there are two kinds of executables, console ones and non-console ones. Git's executables are all console ones. When launching the former e.g. in a scheduled task, a CMD window pops up. This is not what we want for the tasks installed via the `git maintenance` command. To work around this, let's introduce `headless-git.exe`, which is a non-console program that does _not_ pop up any window. All it does is to re-launch `git.exe`, suppressing that console window, passing through all command-line arguments as-are. Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Derrick Stolee <[email protected]>
1 parent 2da6ff3 commit 4759cd9

File tree

6 files changed

+132
-2
lines changed

6 files changed

+132
-2
lines changed

Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2574,6 +2574,13 @@ compat/nedmalloc/nedmalloc.sp compat/nedmalloc/nedmalloc.o: EXTRA_CPPFLAGS = \
25742574
compat/nedmalloc/nedmalloc.sp: SP_EXTRA_FLAGS += -Wno-non-pointer-null
25752575
endif
25762576

2577+
headless-git.o: compat/win32/headless.c GIT-CFLAGS
2578+
$(QUIET_CC)$(CC) $(ALL_CFLAGS) $(COMPAT_CFLAGS) \
2579+
-fno-stack-protector -o $@ -c -Wall -Wwrite-strings $<
2580+
2581+
headless-git$X: headless-git.o git.res GIT-LDFLAGS
2582+
$(QUIET_LINK)$(CC) $(ALL_CFLAGS) $(ALL_LDFLAGS) -mwindows -o $@ $< git.res
2583+
25772584
git-%$X: %.o GIT-LDFLAGS $(GITLIBS)
25782585
$(QUIET_LINK)$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) $(LIBS)
25792586

@@ -3223,6 +3230,7 @@ cocciclean:
32233230
clean: profile-clean coverage-clean cocciclean
32243231
$(RM) *.res
32253232
$(RM) $(OBJECTS)
3233+
$(RM) headless-git.o
32263234
$(RM) $(LIB_FILE) $(XDIFF_LIB)
32273235
$(RM) $(ALL_PROGRAMS) $(SCRIPT_LIB) $(BUILT_INS) git$X
32283236
$(RM) $(TEST_PROGRAMS)
@@ -3251,6 +3259,7 @@ endif
32513259
$(RM) GIT-SCRIPT-DEFINES GIT-PERL-DEFINES GIT-PERL-HEADER GIT-PYTHON-VARS
32523260
ifdef MSVC
32533261
$(RM) $(patsubst %.o,%.o.pdb,$(OBJECTS))
3262+
$(RM) headless-git.o.pdb
32543263
$(RM) $(patsubst %.exe,%.pdb,$(OTHER_PROGRAMS))
32553264
$(RM) $(patsubst %.exe,%.ilk,$(OTHER_PROGRAMS))
32563265
$(RM) $(patsubst %.exe,%.iobj,$(OTHER_PROGRAMS))

compat/win32/headless.c

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* headless Git - run Git without opening a console window on Windows
3+
*/
4+
5+
#define STRICT
6+
#define WIN32_LEAN_AND_MEAN
7+
#define UNICODE
8+
#define _UNICODE
9+
#include <windows.h>
10+
#include <stdio.h>
11+
#include <stdlib.h>
12+
#include <wchar.h>
13+
14+
/*
15+
* If `dir` contains the path to a Git exec directory, extend `PATH` to
16+
* include the corresponding `bin/` directory (which is where all those
17+
* `.dll` files needed by `git.exe` are, on Windows).
18+
*/
19+
static int extend_path(wchar_t *dir, size_t dir_len)
20+
{
21+
const wchar_t *suffix = L"\\libexec\\git-core";
22+
size_t suffix_len = wcslen(suffix);
23+
wchar_t *env;
24+
DWORD len;
25+
26+
if (dir_len < suffix_len)
27+
return 0;
28+
29+
dir_len -= suffix_len;
30+
if (memcmp(dir + dir_len, suffix, suffix_len * sizeof(wchar_t)))
31+
return 0;
32+
33+
len = GetEnvironmentVariableW(L"PATH", NULL, 0);
34+
if (!len)
35+
return 0;
36+
37+
env = _alloca((dir_len + 5 + len) * sizeof(wchar_t));
38+
wcsncpy(env, dir, dir_len);
39+
wcscpy(env + dir_len, L"\\bin;");
40+
if (!GetEnvironmentVariableW(L"PATH", env + dir_len + 5, len))
41+
return 0;
42+
43+
SetEnvironmentVariableW(L"PATH", env);
44+
return 1;
45+
}
46+
47+
int WINAPI wWinMain(HINSTANCE instance, HINSTANCE previous_instance,
48+
wchar_t *command_line, int show)
49+
{
50+
wchar_t git_command_line[32768];
51+
size_t size = sizeof(git_command_line) / sizeof(wchar_t);
52+
const wchar_t *needs_quotes = L"";
53+
int slash = 0, i;
54+
55+
STARTUPINFO startup_info = {
56+
.dwFlags = STARTF_USESHOWWINDOW,
57+
.wShowWindow = SW_HIDE,
58+
};
59+
PROCESS_INFORMATION process_info = { 0 };
60+
DWORD creation_flags = CREATE_UNICODE_ENVIRONMENT |
61+
CREATE_NEW_CONSOLE | CREATE_NO_WINDOW;
62+
DWORD exit_code;
63+
64+
/* First, determine the full path of argv[0] */
65+
for (i = 0; _wpgmptr[i]; i++)
66+
if (_wpgmptr[i] == L' ')
67+
needs_quotes = L"\"";
68+
else if (_wpgmptr[i] == L'\\')
69+
slash = i;
70+
71+
if (slash + 11 >= sizeof(git_command_line) / sizeof(wchar_t))
72+
return 127; /* Too long path */
73+
74+
/* If it is in Git's exec path, add the bin/ directory to the PATH */
75+
extend_path(_wpgmptr, slash);
76+
77+
/* Then, add the full path of `git.exe` as argv[0] */
78+
i = swprintf_s(git_command_line, size, L"%ls%.*ls\\git.exe%ls",
79+
needs_quotes, slash, _wpgmptr, needs_quotes);
80+
if (i < 0)
81+
return 127; /* Too long path */
82+
83+
if (*command_line) {
84+
/* Now, append the command-line arguments */
85+
i = swprintf_s(git_command_line + i, size - i,
86+
L" %ls", command_line);
87+
if (i < 0)
88+
return 127;
89+
}
90+
91+
startup_info.cb = sizeof(STARTUPINFO);
92+
93+
startup_info.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
94+
startup_info.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
95+
startup_info.hStdError = GetStdHandle(STD_ERROR_HANDLE);
96+
97+
if (!CreateProcess(NULL, /* infer argv[0] from the command line */
98+
git_command_line, /* modified command line */
99+
NULL, /* inherit process handles? */
100+
NULL, /* inherit thread handles? */
101+
FALSE, /* handles inheritable? */
102+
creation_flags,
103+
NULL, /* use this process' environment */
104+
NULL, /* use this process' working directory */
105+
&startup_info, &process_info))
106+
return 129; /* could not start */
107+
WaitForSingleObject(process_info.hProcess, INFINITE);
108+
if (!GetExitCodeProcess(process_info.hProcess, &exit_code)) {
109+
CloseHandle(process_info.hProcess);
110+
return 130; /* Could not determine exit code? */
111+
}
112+
113+
return (int)exit_code;
114+
}

config.mak.uname

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,8 @@ else
492492
endif
493493
X = .exe
494494

495+
EXTRA_PROGRAMS += headless-git$X
496+
495497
compat/msvc.o: compat/msvc.c compat/mingw.c GIT-CFLAGS
496498
endif
497499
ifeq ($(uname_S),Interix)
@@ -634,6 +636,7 @@ ifneq (,$(findstring MINGW,$(uname_S)))
634636
RC = windres -O coff
635637
NATIVE_CRLF = YesPlease
636638
X = .exe
639+
EXTRA_PROGRAMS += headless-git$X
637640
ifneq (,$(wildcard ../THIS_IS_MSYSGIT))
638641
htmldir = doc/git/html/
639642
prefix =

contrib/buildsystems/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,9 @@ if(WIN32)
681681
else()
682682
message(FATAL_ERROR "Unhandled compiler: ${CMAKE_C_COMPILER_ID}")
683683
endif()
684+
685+
add_executable(headless-git ${CMAKE_SOURCE_DIR}/compat/win32/headless.c)
686+
target_link_options(headless-git PUBLIC /NOLOGO /ENTRY:wWinMainCRTStartup /SUBSYSTEM:WINDOWS)
684687
elseif(UNIX)
685688
target_link_libraries(common-main pthread rt)
686689
endif()

contrib/buildsystems/Generators/Vcxproj.pm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ sub createProject {
7676

7777
my $libs_release = "\n ";
7878
my $libs_debug = "\n ";
79-
if (!$static_library) {
79+
if (!$static_library && $name ne 'headless-git') {
8080
$libs_release = join(";", sort(grep /^(?!libgit\.lib|xdiff\/lib\.lib|vcs-svn\/lib\.lib)/, @{$$build_structure{"$prefix${name}_LIBS"}}));
8181
$libs_debug = $libs_release;
8282
$libs_debug =~ s/zlib\.lib/zlibd\.lib/g;
@@ -254,7 +254,7 @@ EOM
254254
print F << "EOM";
255255
</ItemGroup>
256256
EOM
257-
if (!$static_library || $target =~ 'vcs-svn' || $target =~ 'xdiff') {
257+
if ((!$static_library || $target =~ 'vcs-svn' || $target =~ 'xdiff') && !($name =~ /headless-git/)) {
258258
my $uuid_libgit = $$build_structure{"LIBS_libgit_GUID"};
259259
my $uuid_xdiff_lib = $$build_structure{"LIBS_xdiff/lib_GUID"};
260260

contrib/buildsystems/engine.pl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ sub handleLinkLine
370370
# exit(1);
371371
foreach (@objfiles) {
372372
my $sourcefile = $_;
373+
$sourcefile =~ s/^headless-git\.o$/compat\/win32\/headless.c/;
373374
$sourcefile =~ s/\.o$/.c/;
374375
$sourcefile =~ s/\.res$/.rc/;
375376
push(@sources, $sourcefile);

0 commit comments

Comments
 (0)