Skip to content

Commit 321eb2e

Browse files
dschoGit for Windows Build Agent
authored andcommitted
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 90d1330 commit 321eb2e

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
@@ -2562,6 +2562,13 @@ compat/nedmalloc/nedmalloc.sp compat/nedmalloc/nedmalloc.o: EXTRA_CPPFLAGS = \
25622562
compat/nedmalloc/nedmalloc.sp: SP_EXTRA_FLAGS += -Wno-non-pointer-null
25632563
endif
25642564

2565+
headless-git.o: compat/win32/headless.c GIT-CFLAGS
2566+
$(QUIET_CC)$(CC) $(ALL_CFLAGS) $(COMPAT_CFLAGS) \
2567+
-fno-stack-protector -o $@ -c -Wall -Wwrite-strings $<
2568+
2569+
headless-git$X: headless-git.o git.res GIT-LDFLAGS
2570+
$(QUIET_LINK)$(CC) $(ALL_CFLAGS) $(ALL_LDFLAGS) -mwindows -o $@ $< git.res
2571+
25652572
git-%$X: %.o GIT-LDFLAGS $(GITLIBS)
25662573
$(QUIET_LINK)$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) $(LIBS)
25672574

@@ -3208,6 +3215,7 @@ cocciclean:
32083215
clean: profile-clean coverage-clean cocciclean
32093216
$(RM) *.res
32103217
$(RM) $(OBJECTS)
3218+
$(RM) headless-git.o
32113219
$(RM) $(LIB_FILE) $(XDIFF_LIB)
32123220
$(RM) $(ALL_PROGRAMS) $(SCRIPT_LIB) $(BUILT_INS) git$X
32133221
$(RM) $(TEST_PROGRAMS)
@@ -3236,6 +3244,7 @@ endif
32363244
$(RM) GIT-SCRIPT-DEFINES GIT-PERL-DEFINES GIT-PERL-HEADER GIT-PYTHON-VARS
32373245
ifdef MSVC
32383246
$(RM) $(patsubst %.o,%.o.pdb,$(OBJECTS))
3247+
$(RM) headless-git.o.pdb
32393248
$(RM) $(patsubst %.exe,%.pdb,$(OTHER_PROGRAMS))
32403249
$(RM) $(patsubst %.exe,%.ilk,$(OTHER_PROGRAMS))
32413250
$(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
@@ -487,6 +487,8 @@ else
487487
endif
488488
X = .exe
489489

490+
EXTRA_PROGRAMS += headless-git$X
491+
490492
compat/msvc.o: compat/msvc.c compat/mingw.c GIT-CFLAGS
491493
endif
492494
ifeq ($(uname_S),Interix)
@@ -629,6 +631,7 @@ ifneq (,$(findstring MINGW,$(uname_S)))
629631
RC = windres -O coff
630632
NATIVE_CRLF = YesPlease
631633
X = .exe
634+
EXTRA_PROGRAMS += headless-git$X
632635
ifneq (,$(wildcard ../THIS_IS_MSYSGIT))
633636
htmldir = doc/git/html/
634637
prefix =

contrib/buildsystems/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,9 @@ if(WIN32)
659659
else()
660660
message(FATAL_ERROR "Unhandled compiler: ${CMAKE_C_COMPILER_ID}")
661661
endif()
662+
663+
add_executable(headless-git ${CMAKE_SOURCE_DIR}/compat/win32/headless.c)
664+
target_link_options(headless-git PUBLIC /NOLOGO /ENTRY:wWinMainCRTStartup /SUBSYSTEM:WINDOWS)
662665
elseif(UNIX)
663666
target_link_libraries(common-main pthread rt)
664667
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)