Skip to content

Commit f96fb70

Browse files
committed
git-wrapper: allow overriding the command to spawn via command-line args
By embedding string resources into the Git wrapper executable, it can be configured to execute custom commands (after setting up the environment in the way required for Git for Windows to work properly). This feature is used e.g. for `git-bash.exe` which launches a Bash in the configured terminal window. Here, we introduce command-line options to override those string resources. That way, a user can call `git-bash.exe` (which is a copy of the Git wrapper with `usr\bin\bash.exe --login -i` embedded as string resource) with command-line options that will override what command is run. ConEmu, for example, might want to call ...\git-bash.exe --needs-console --no-hide --minimal-search-path ^ --command=usr\\bin\\bash.exe --login -i In particular, the following options are supported now: --command=<command-line>:: Executes `<command-line>` instead of the embedded string resource --[no-]minimal-search-path:: Ensures that only `/cmd/` is added to the `PATH` instead of `/mingw??/bin` and `/usr/bin/`, or not --[no-]needs-console:: Ensures that there is a Win32 console associated with the spawned process, or not --[no-]hide:: Hides the console window, or not Helped-by: Eli Young <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 71690ca commit f96fb70

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed

compat/win32/git-wrapper.c

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ static int configure_via_resource(LPWSTR basename, LPWSTR exepath, LPWSTR exep,
271271
int *is_git_command, LPWSTR *working_directory, int *full_path,
272272
int *skip_arguments, int *allocate_console, int *show_console)
273273
{
274-
int id, minimal_search_path, needs_a_console, no_hide, wargc;
274+
int i, id, minimal_search_path, needs_a_console, no_hide, wargc;
275275
LPWSTR *wargv;
276276

277277
#define BUFSIZE 65536
@@ -333,15 +333,41 @@ static int configure_via_resource(LPWSTR basename, LPWSTR exepath, LPWSTR exep,
333333
*is_git_command = 0;
334334
*working_directory = (LPWSTR) 1;
335335
wargv = CommandLineToArgvW(GetCommandLine(), &wargc);
336-
if (wargc > 1) {
337-
if (!wcscmp(L"--no-cd", wargv[1])) {
336+
for (i = 1; i < wargc; i++) {
337+
if (!wcscmp(L"--no-cd", wargv[i]))
338338
*working_directory = NULL;
339-
*skip_arguments = 1;
340-
}
341-
else if (!wcsncmp(L"--cd=", wargv[1], 5)) {
342-
*working_directory = wcsdup(wargv[1] + 5);
343-
*skip_arguments = 1;
339+
else if (!wcsncmp(L"--cd=", wargv[i], 5))
340+
*working_directory = wcsdup(wargv[i] + 5);
341+
else if (!wcscmp(L"--minimal-search-path", wargv[i]))
342+
minimal_search_path = 1;
343+
else if (!wcscmp(L"--no-minimal-search-path", wargv[i]))
344+
minimal_search_path = 0;
345+
else if (!wcscmp(L"--needs-console", wargv[i]))
346+
needs_a_console = 1;
347+
else if (!wcscmp(L"--no-needs-console", wargv[i]))
348+
needs_a_console = 0;
349+
else if (!wcscmp(L"--hide", wargv[i]))
350+
no_hide = 0;
351+
else if (!wcscmp(L"--no-hide", wargv[i]))
352+
no_hide = 1;
353+
else if (!wcsncmp(L"--command=", wargv[i], 10)) {
354+
LPWSTR expanded;
355+
356+
wargv[i] += 10;
357+
expanded = expand_variables(wargv[i], wcslen(wargv[i]));
358+
if (expanded == wargv[i])
359+
expanded = wcsdup(expanded);
360+
361+
extract_first_arg(expanded, exepath, exep);
362+
363+
*prefix_args = expanded;
364+
*prefix_args_len = wcslen(*prefix_args);
365+
*skip_arguments = i;
366+
break;
344367
}
368+
else
369+
break;
370+
*skip_arguments = i;
345371
}
346372
if (minimal_search_path)
347373
*full_path = 0;

0 commit comments

Comments
 (0)