Skip to content

Commit ac6b03c

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 722bd15 commit ac6b03c

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed

compat/win32/git-wrapper.c

+34-8
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ static int configure_via_resource(LPWSTR basename, LPWSTR exepath, LPWSTR exep,
267267
int *is_git_command, LPWSTR *working_directory, int *full_path,
268268
int *skip_arguments, int *allocate_console, int *show_console)
269269
{
270-
int id, minimal_search_path, needs_a_console, no_hide, wargc;
270+
int i, id, minimal_search_path, needs_a_console, no_hide, wargc;
271271
LPWSTR *wargv;
272272

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

0 commit comments

Comments
 (0)