Skip to content

Commit 0160e8e

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 0defb66 commit 0160e8e

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 = 0, minimal_search_path, wargc;
@@ -267,12 +267,17 @@ static int configure_via_resource(LPWSTR basename, LPWSTR exepath, LPWSTR exep,
267267
*prefix_args_len = wcslen(buf);
268268

269269
*is_git_command = 0;
270+
*working_directory = (LPWSTR) 1;
270271
wargv = CommandLineToArgvW(GetCommandLine(), &wargc);
271-
if (wargc < 2 || wcscmp(L"--no-cd", wargv[1]))
272-
*start_in_home = 1;
273-
else {
274-
*start_in_home = 0;
275-
*skip_arguments = 1;
272+
if (wargc > 1) {
273+
if (!wcscmp(L"--no-cd", wargv[1])) {
274+
*working_directory = NULL;
275+
*skip_arguments = 1;
276+
}
277+
else if (!wcsncmp(L"--cd=", wargv[1], 5)) {
278+
*working_directory = wcsdup(wargv[1] + 5);
279+
*skip_arguments = 1;
280+
}
276281
}
277282
if (minimal_search_path)
278283
*full_path = 0;
@@ -284,10 +289,10 @@ static int configure_via_resource(LPWSTR basename, LPWSTR exepath, LPWSTR exep,
284289
int main(void)
285290
{
286291
int r = 1, wait = 1, prefix_args_len = -1, needs_env_setup = 1,
287-
is_git_command = 1, start_in_home = 0, full_path = 1,
288-
skip_arguments = 0;
292+
is_git_command = 1, full_path = 1, skip_arguments = 0;
289293
WCHAR exepath[MAX_PATH], exe[MAX_PATH];
290-
LPWSTR cmd = NULL, dir = NULL, exep = exe, prefix_args = NULL, basename;
294+
LPWSTR cmd = NULL, exep = exe, prefix_args = NULL, basename;
295+
LPWSTR working_directory = NULL;
291296
UINT codepage = 0;
292297

293298
/* Determine MSys2-based Git path. */
@@ -303,7 +308,7 @@ int main(void)
303308
basename = exepath + wcslen(exepath) + 1;
304309
if (configure_via_resource(basename, exepath, exep,
305310
&prefix_args, &prefix_args_len,
306-
&is_git_command, &start_in_home,
311+
&is_git_command, &working_directory,
307312
&full_path, &skip_arguments)) {
308313
/* do nothing */
309314
}
@@ -392,12 +397,12 @@ int main(void)
392397
cmd = fixup_commandline(exepath, &exep, &wait,
393398
prefix_args, prefix_args_len, is_git_command, skip_arguments);
394399

395-
if (start_in_home) {
400+
if (working_directory == (LPWSTR)1) {
396401
int len = GetEnvironmentVariable(L"HOME", NULL, 0);
397402

398403
if (len) {
399-
dir = malloc(sizeof(WCHAR) * len);
400-
GetEnvironmentVariable(L"HOME", dir, len);
404+
working_directory = malloc(sizeof(WCHAR) * len);
405+
GetEnvironmentVariable(L"HOME", working_directory, len);
401406
}
402407
}
403408

@@ -436,7 +441,7 @@ int main(void)
436441
TRUE, /* handles inheritable? */
437442
creation_flags,
438443
NULL, /* environment: use parent */
439-
dir, /* starting directory: use parent */
444+
working_directory, /* use parent's */
440445
&si, &pi);
441446
if (br) {
442447
if (wait)

0 commit comments

Comments
 (0)