Skip to content

Commit

Permalink
git-wrapper: allow overriding the command to spawn via command-line args
Browse files Browse the repository at this point in the history
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 <elyscape@gmail.com>
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
  • Loading branch information
dscho committed May 27, 2015
1 parent 722bd15 commit ac6b03c
Showing 1 changed file with 34 additions and 8 deletions.
42 changes: 34 additions & 8 deletions compat/win32/git-wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ static int configure_via_resource(LPWSTR basename, LPWSTR exepath, LPWSTR exep,
int *is_git_command, LPWSTR *working_directory, int *full_path,
int *skip_arguments, int *allocate_console, int *show_console)
{
int id, minimal_search_path, needs_a_console, no_hide, wargc;
int i, id, minimal_search_path, needs_a_console, no_hide, wargc;
LPWSTR *wargv;

#define BUFSIZE 65536
Expand Down Expand Up @@ -329,15 +329,41 @@ static int configure_via_resource(LPWSTR basename, LPWSTR exepath, LPWSTR exep,
*is_git_command = 0;
*working_directory = (LPWSTR) 1;
wargv = CommandLineToArgvW(GetCommandLine(), &wargc);
if (wargc > 1) {
if (!wcscmp(L"--no-cd", wargv[1])) {
for (i = 1; i < wargc; i++) {
if (!wcscmp(L"--no-cd", wargv[i]))
*working_directory = NULL;
*skip_arguments = 1;
}
else if (!wcsncmp(L"--cd=", wargv[1], 5)) {
*working_directory = wcsdup(wargv[1] + 5);
*skip_arguments = 1;
else if (!wcsncmp(L"--cd=", wargv[i], 5))
*working_directory = wcsdup(wargv[i] + 5);
else if (!wcscmp(L"--minimal-search-path", wargv[i]))
minimal_search_path = 1;
else if (!wcscmp(L"--no-minimal-search-path", wargv[i]))
minimal_search_path = 0;
else if (!wcscmp(L"--needs-console", wargv[i]))
needs_a_console = 1;
else if (!wcscmp(L"--no-needs-console", wargv[i]))
needs_a_console = 0;
else if (!wcscmp(L"--hide", wargv[i]))
no_hide = 0;
else if (!wcscmp(L"--no-hide", wargv[i]))
no_hide = 1;
else if (!wcsncmp(L"--command=", wargv[i], 10)) {
LPWSTR expanded;

wargv[i] += 10;
expanded = expand_variables(wargv[i], wcslen(wargv[i]));
if (expanded == wargv[i])
expanded = wcsdup(expanded);

extract_first_arg(expanded, exepath, exep);

*prefix_args = expanded;
*prefix_args_len = wcslen(*prefix_args);
*skip_arguments = i;
break;
}
else
break;
*skip_arguments = i;
}
if (minimal_search_path)
*full_path = 0;
Expand Down

0 comments on commit ac6b03c

Please sign in to comment.