Skip to content

Commit 6291daa

Browse files
committed
Merge branch 'bash-redirector'
2 parents 7b94b82 + 0fd46b1 commit 6291daa

File tree

1 file changed

+68
-32
lines changed

1 file changed

+68
-32
lines changed

compat/win32/git-wrapper.c

Lines changed: 68 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ static void print_error(LPCWSTR prefix, DWORD error_number)
3737
LocalFree((HLOCAL)buffer);
3838
}
3939

40-
static void setup_environment(LPWSTR exepath, int full_path)
40+
static void setup_environment(LPWSTR top_level_path, int full_path)
4141
{
4242
WCHAR msystem[64];
4343
LPWSTR path2 = NULL;
@@ -90,22 +90,22 @@ static void setup_environment(LPWSTR exepath, int full_path)
9090
len = GetEnvironmentVariable(L"PATH", NULL, 0);
9191
len = sizeof(WCHAR) * (len + 2 * MAX_PATH);
9292
path2 = (LPWSTR)malloc(len);
93-
wcscpy(path2, exepath);
93+
wcscpy(path2, top_level_path);
9494
if (!full_path)
9595
PathAppend(path2, L"cmd;");
9696
else {
9797
PathAppend(path2, msystem_bin);
9898
if (_waccess(path2, 0) != -1) {
9999
/* We are in an MSys2-based setup */
100100
wcscat(path2, L";");
101-
wcscat(path2, exepath);
101+
wcscat(path2, top_level_path);
102102
PathAppend(path2, L"usr\\bin;");
103103
}
104104
else {
105105
/* Fall back to MSys1 paths */
106-
wcscpy(path2, exepath);
106+
wcscpy(path2, top_level_path);
107107
PathAppend(path2, L"bin;");
108-
wcscat(path2, exepath);
108+
wcscat(path2, top_level_path);
109109
PathAppend(path2, L"mingw\\bin;");
110110
}
111111
}
@@ -422,18 +422,59 @@ static int configure_via_resource(LPWSTR basename, LPWSTR exepath, LPWSTR exep,
422422
return 1;
423423
}
424424

425+
static void initialize_top_level_path(LPWSTR top_level_path, LPWSTR exepath,
426+
LPWSTR msystem_bin, int strip_count)
427+
{
428+
wcscpy(top_level_path, exepath);
429+
430+
while (strip_count) {
431+
if (strip_count < 0) {
432+
int len = wcslen(top_level_path);
433+
PathAppend(top_level_path, msystem_bin);
434+
if (_waccess(top_level_path, 0) != -1) {
435+
/* We are in an MSys2-based setup */
436+
top_level_path[len] = L'\0';
437+
return;
438+
}
439+
top_level_path[len] = L'\0';
440+
PathAppend(top_level_path, L"mingw\\bin");
441+
if (_waccess(top_level_path, 0) != -1) {
442+
/* We are in an MSys-based setup */
443+
top_level_path[len] = L'\0';
444+
return;
445+
}
446+
top_level_path[len] = L'\0';
447+
if (!(++strip_count)) {
448+
fwprintf(stderr, L"Top-level not found: %s\n",
449+
exepath);
450+
exit(1);
451+
}
452+
}
453+
454+
if (!PathRemoveFileSpec(top_level_path)) {
455+
fwprintf(stderr, L"Invalid executable path: %s\n",
456+
exepath);
457+
ExitProcess(1);
458+
}
459+
460+
if (strip_count > 0)
461+
--strip_count;
462+
}
463+
}
464+
425465
int main(void)
426466
{
427467
int r = 1, wait = 1, prefix_args_len = -1, needs_env_setup = 1,
428468
is_git_command = 1, full_path = 1, skip_arguments = 0,
429469
allocate_console = 0, show_console = 0;
430-
WCHAR exepath[MAX_PATH], exe[MAX_PATH];
470+
WCHAR exepath[MAX_PATH], exe[MAX_PATH], top_level_path[MAX_PATH];
431471
LPWSTR cmd = NULL, exep = exe, prefix_args = NULL, basename;
432472
LPWSTR working_directory = NULL;
433473

434474
/* Determine MSys2-based Git path. */
435475
swprintf(msystem_bin, sizeof(msystem_bin),
436476
L"mingw%d\\bin", (int) sizeof(void *) * 8);
477+
*top_level_path = L'\0';
437478

438479
/* get the installation location */
439480
GetModuleFileName(NULL, exepath, MAX_PATH);
@@ -453,25 +494,23 @@ int main(void)
453494
static WCHAR buffer[BUFSIZE];
454495
wait = 0;
455496
allocate_console = 1;
456-
if (!PathRemoveFileSpec(exepath)) {
457-
fwprintf(stderr,
458-
L"Invalid executable path: %s\n", exepath);
459-
ExitProcess(1);
460-
}
497+
initialize_top_level_path(top_level_path, exepath, NULL, 1);
461498

462499
/* set the default exe module */
463-
wcscpy(exe, exepath);
500+
wcscpy(exe, top_level_path);
464501
PathAppend(exe, msystem_bin);
465502
PathAppend(exe, L"wish.exe");
466503
if (_waccess(exe, 0) != -1)
467504
swprintf(buffer, BUFSIZE,
468505
L"\"%s\\%.*s\\libexec\\git-core\"",
469-
exepath, wcslen(msystem_bin) - 4, msystem_bin);
506+
top_level_path,
507+
wcslen(msystem_bin) - 4, msystem_bin);
470508
else {
471-
wcscpy(exe, exepath);
509+
wcscpy(exe, top_level_path);
472510
PathAppend(exe, L"mingw\\bin\\wish.exe");
473511
swprintf(buffer, BUFSIZE,
474-
L"\"%s\\mingw\\libexec\\git-core\"", exepath);
512+
L"\"%s\\mingw\\libexec\\git-core\"",
513+
top_level_path);
475514
}
476515
PathAppend(buffer, L"git-gui");
477516
prefix_args = buffer;
@@ -491,39 +530,31 @@ int main(void)
491530
PathAppend(exe, L"git.exe");
492531
}
493532
else if (!wcsicmp(basename, L"git.exe")) {
494-
if (!PathRemoveFileSpec(exepath)) {
495-
fwprintf(stderr,
496-
L"Invalid executable path: %s\n", exepath);
497-
ExitProcess(1);
498-
}
533+
initialize_top_level_path(top_level_path, exepath, NULL, 1);
499534

500535
/* set the default exe module */
501-
wcscpy(exe, exepath);
536+
wcscpy(exe, top_level_path);
502537
PathAppend(exe, msystem_bin);
503538
PathAppend(exe, L"git.exe");
504539
if (_waccess(exe, 0) == -1) {
505-
wcscpy(exe, exepath);
540+
wcscpy(exe, top_level_path);
506541
PathAppend(exe, L"bin\\git.exe");
507542
}
508543
}
509544
else if (!wcsicmp(basename, L"gitk.exe")) {
510545
static WCHAR buffer[BUFSIZE];
511546
allocate_console = 1;
512-
if (!PathRemoveFileSpec(exepath)) {
513-
fwprintf(stderr,
514-
L"Invalid executable path: %s\n", exepath);
515-
ExitProcess(1);
516-
}
547+
initialize_top_level_path(top_level_path, exepath, NULL, 1);
517548

518549
/* set the default exe module */
519-
wcscpy(exe, exepath);
520-
swprintf(buffer, BUFSIZE, L"\"%s\"", exepath);
550+
wcscpy(exe, top_level_path);
551+
swprintf(buffer, BUFSIZE, L"\"%s\"", top_level_path);
521552
PathAppend(exe, msystem_bin);
522553
PathAppend(exe, L"wish.exe");
523554
if (_waccess(exe, 0) != -1)
524555
PathAppend(buffer, msystem_bin);
525556
else {
526-
wcscpy(exe, exepath);
557+
wcscpy(exe, top_level_path);
527558
PathAppend(exe, L"mingw\\bin\\wish.exe");
528559
PathAppend(buffer, L"mingw\\bin");
529560
}
@@ -532,8 +563,13 @@ int main(void)
532563
prefix_args_len = wcslen(buffer);
533564
}
534565

535-
if (needs_env_setup)
536-
setup_environment(exepath, full_path);
566+
if (needs_env_setup) {
567+
if (!top_level_path[0])
568+
initialize_top_level_path(top_level_path, exepath,
569+
msystem_bin, -4);
570+
571+
setup_environment(top_level_path, full_path);
572+
}
537573
cmd = fixup_commandline(exepath, &exep, &wait,
538574
prefix_args, prefix_args_len, is_git_command, skip_arguments);
539575

0 commit comments

Comments
 (0)