Skip to content

Commit 65e5d22

Browse files
committed
git-wrapper: interpret --cd=<directory> when configured via resources
This change accompanies the `--no-cd` option when configured via resources. It is required to support `Git Bash Here`: when right-clicking an icon in the Explorer to start a Bash, the working directory is actually the directory that is displayed in the Explorer. That means if the clicked icon actually refers to a directory, the working directory would be its *parent* directory. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 7f7353e commit 65e5d22

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

compat/win32/git-wrapper.c

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ static LPWSTR fixup_commandline(LPWSTR exepath, LPWSTR *exep, int *wait,
167167

168168
static int configure_via_resource(LPWSTR basename, LPWSTR exepath, LPWSTR exep,
169169
LPWSTR *prefix_args, int *prefix_args_len,
170-
int *is_git_command, int *start_in_home, int *full_path,
170+
int *is_git_command, LPWSTR *working_directory, int *full_path,
171171
int *skip_arguments)
172172
{
173173
int id, minimal_search_path, wargc;
@@ -264,12 +264,17 @@ static int configure_via_resource(LPWSTR basename, LPWSTR exepath, LPWSTR exep,
264264
*prefix_args_len = wcslen(buf);
265265

266266
*is_git_command = 0;
267+
*working_directory = (LPWSTR) 1;
267268
wargv = CommandLineToArgvW(GetCommandLine(), &wargc);
268-
if (wargc < 2 || wcscmp(L"--no-cd", wargv[1]))
269-
*start_in_home = 1;
270-
else {
271-
*start_in_home = 0;
272-
*skip_arguments = 1;
269+
if (wargc > 1) {
270+
if (!wcscmp(L"--no-cd", wargv[1])) {
271+
*working_directory = NULL;
272+
*skip_arguments = 1;
273+
}
274+
else if (!wcsncmp(L"--cd=", wargv[1], 5)) {
275+
*working_directory = wcsdup(wargv[1] + 5);
276+
*skip_arguments = 1;
277+
}
273278
}
274279
if (minimal_search_path)
275280
*full_path = 0;
@@ -281,10 +286,10 @@ static int configure_via_resource(LPWSTR basename, LPWSTR exepath, LPWSTR exep,
281286
int main(void)
282287
{
283288
int r = 1, wait = 1, prefix_args_len = -1, needs_env_setup = 1,
284-
is_git_command = 1, start_in_home = 0, full_path = 1,
285-
skip_arguments = 0;
289+
is_git_command = 1, full_path = 1, skip_arguments = 0;
286290
WCHAR exepath[MAX_PATH], exe[MAX_PATH];
287-
LPWSTR cmd = NULL, dir = NULL, exep = exe, prefix_args = NULL, basename;
291+
LPWSTR cmd = NULL, exep = exe, prefix_args = NULL, basename;
292+
LPWSTR working_directory = NULL;
288293
UINT codepage = 0;
289294

290295
/* Determine MSys2-based Git path. */
@@ -300,7 +305,7 @@ int main(void)
300305
basename = exepath + wcslen(exepath) + 1;
301306
if (configure_via_resource(basename, exepath, exep,
302307
&prefix_args, &prefix_args_len,
303-
&is_git_command, &start_in_home,
308+
&is_git_command, &working_directory,
304309
&full_path, &skip_arguments)) {
305310
/* do nothing */
306311
}
@@ -389,12 +394,12 @@ int main(void)
389394
cmd = fixup_commandline(exepath, &exep, &wait,
390395
prefix_args, prefix_args_len, is_git_command, skip_arguments);
391396

392-
if (start_in_home) {
397+
if (working_directory == (LPWSTR)1) {
393398
int len = GetEnvironmentVariable(L"HOME", NULL, 0);
394399

395400
if (len) {
396-
dir = malloc(sizeof(WCHAR) * len);
397-
GetEnvironmentVariable(L"HOME", dir, len);
401+
working_directory = malloc(sizeof(WCHAR) * len);
402+
GetEnvironmentVariable(L"HOME", working_directory, len);
398403
}
399404
}
400405

@@ -433,7 +438,7 @@ int main(void)
433438
TRUE, /* handles inheritable? */
434439
creation_flags,
435440
NULL, /* environment: use parent */
436-
dir, /* starting directory: use parent */
441+
working_directory, /* use parent's */
437442
&si, &pi);
438443
if (br) {
439444
if (wait)

0 commit comments

Comments
 (0)