Skip to content

Commit 03afb9d

Browse files
committed
Merge branch 'bash-redirector'
2 parents e73caa1 + 6e8dc35 commit 03afb9d

File tree

1 file changed

+67
-32
lines changed

1 file changed

+67
-32
lines changed

compat/win32/git-wrapper.c

Lines changed: 67 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,22 @@ 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\"",
469506
exepath, wcslen(msystem_bin) - 4, msystem_bin);
470507
else {
471-
wcscpy(exe, exepath);
508+
wcscpy(exe, top_level_path);
472509
PathAppend(exe, L"mingw\\bin\\wish.exe");
473510
swprintf(buffer, BUFSIZE,
474-
L"\"%s\\mingw\\libexec\\git-core\"", exepath);
511+
L"\"%s\\mingw\\libexec\\git-core\"",
512+
top_level_path);
475513
}
476514
PathAppend(buffer, L"git-gui");
477515
prefix_args = buffer;
@@ -487,43 +525,35 @@ int main(void)
487525
prefix_args_len -= 4;
488526

489527
/* set the default exe module */
490-
wcscpy(exe, exepath);
528+
wcscpy(exe, top_level_path);
491529
PathAppend(exe, L"git.exe");
492530
}
493531
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-
}
532+
initialize_top_level_path(top_level_path, exepath, NULL, 1);
499533

500534
/* set the default exe module */
501-
wcscpy(exe, exepath);
535+
wcscpy(exe, top_level_path);
502536
PathAppend(exe, msystem_bin);
503537
PathAppend(exe, L"git.exe");
504538
if (_waccess(exe, 0) == -1) {
505-
wcscpy(exe, exepath);
539+
wcscpy(exe, top_level_path);
506540
PathAppend(exe, L"bin\\git.exe");
507541
}
508542
}
509543
else if (!wcsicmp(basename, L"gitk.exe")) {
510544
static WCHAR buffer[BUFSIZE];
511545
allocate_console = 1;
512-
if (!PathRemoveFileSpec(exepath)) {
513-
fwprintf(stderr,
514-
L"Invalid executable path: %s\n", exepath);
515-
ExitProcess(1);
516-
}
546+
initialize_top_level_path(top_level_path, exepath, NULL, 1);
517547

518548
/* set the default exe module */
519-
wcscpy(exe, exepath);
520-
swprintf(buffer, BUFSIZE, L"\"%s\"", exepath);
549+
wcscpy(exe, top_level_path);
550+
swprintf(buffer, BUFSIZE, L"\"%s\"", top_level_path);
521551
PathAppend(exe, msystem_bin);
522552
PathAppend(exe, L"wish.exe");
523553
if (_waccess(exe, 0) != -1)
524554
PathAppend(buffer, msystem_bin);
525555
else {
526-
wcscpy(exe, exepath);
556+
wcscpy(exe, top_level_path);
527557
PathAppend(exe, L"mingw\\bin\\wish.exe");
528558
PathAppend(buffer, L"mingw\\bin");
529559
}
@@ -532,8 +562,13 @@ int main(void)
532562
prefix_args_len = wcslen(buffer);
533563
}
534564

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

0 commit comments

Comments
 (0)