Skip to content

Commit bed42cc

Browse files
committed
mingw: clean up the Git wrapper a bit
We should not conflate the 'exepath' with the 'top-level directory'. The former should be the directory in which the executable lives while the latter should be the top-level directory ("POSIX root directory") as far as Git is concerned. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 112b062 commit bed42cc

File tree

1 file changed

+27
-22
lines changed

1 file changed

+27
-22
lines changed

compat/win32/git-wrapper.c

Lines changed: 27 additions & 22 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
}
@@ -386,7 +386,7 @@ int main(void)
386386
int r = 1, wait = 1, prefix_args_len = -1, needs_env_setup = 1,
387387
is_git_command = 1, full_path = 1, skip_arguments = 0,
388388
allocate_console = 0, show_console = 0;
389-
WCHAR exepath[MAX_PATH], exe[MAX_PATH];
389+
WCHAR exepath[MAX_PATH], exe[MAX_PATH], top_level_path[MAX_PATH];
390390
LPWSTR cmd = NULL, exep = exe, prefix_args = NULL, basename;
391391
LPWSTR working_directory = NULL;
392392

@@ -400,6 +400,7 @@ int main(void)
400400
fwprintf(stderr, L"Invalid executable path: %s\n", exepath);
401401
ExitProcess(1);
402402
}
403+
wcscpy(top_level_path, exepath);
403404
basename = exepath + wcslen(exepath) + 1;
404405
if (configure_via_resource(basename, exepath, exep,
405406
&prefix_args, &prefix_args_len,
@@ -412,25 +413,27 @@ int main(void)
412413
static WCHAR buffer[BUFSIZE];
413414
wait = 0;
414415
allocate_console = 1;
415-
if (!PathRemoveFileSpec(exepath)) {
416+
if (!PathRemoveFileSpec(top_level_path)) {
416417
fwprintf(stderr,
417-
L"Invalid executable path: %s\n", exepath);
418+
L"Invalid executable path: %s\n",
419+
top_level_path);
418420
ExitProcess(1);
419421
}
420422

421423
/* set the default exe module */
422-
wcscpy(exe, exepath);
424+
wcscpy(exe, top_level_path);
423425
PathAppend(exe, msystem_bin);
424426
PathAppend(exe, L"wish.exe");
425427
if (_waccess(exe, 0) != -1)
426428
swprintf(buffer, BUFSIZE,
427429
L"\"%s\\%.*s\\libexec\\git-core\"",
428430
exepath, wcslen(msystem_bin) - 4, msystem_bin);
429431
else {
430-
wcscpy(exe, exepath);
432+
wcscpy(exe, top_level_path);
431433
PathAppend(exe, L"mingw\\bin\\wish.exe");
432434
swprintf(buffer, BUFSIZE,
433-
L"\"%s\\mingw\\libexec\\git-core\"", exepath);
435+
L"\"%s\\mingw\\libexec\\git-core\"",
436+
top_level_path);
434437
}
435438
PathAppend(buffer, L"git-gui");
436439
prefix_args = buffer;
@@ -446,43 +449,45 @@ int main(void)
446449
prefix_args_len -= 4;
447450

448451
/* set the default exe module */
449-
wcscpy(exe, exepath);
452+
wcscpy(exe, top_level_path);
450453
PathAppend(exe, L"git.exe");
451454
}
452455
else if (!wcsicmp(basename, L"git.exe")) {
453-
if (!PathRemoveFileSpec(exepath)) {
456+
if (!PathRemoveFileSpec(top_level_path)) {
454457
fwprintf(stderr,
455-
L"Invalid executable path: %s\n", exepath);
458+
L"Invalid executable path: %s\n",
459+
top_level_path);
456460
ExitProcess(1);
457461
}
458462

459463
/* set the default exe module */
460-
wcscpy(exe, exepath);
464+
wcscpy(exe, top_level_path);
461465
PathAppend(exe, msystem_bin);
462466
PathAppend(exe, L"git.exe");
463467
if (_waccess(exe, 0) == -1) {
464-
wcscpy(exe, exepath);
468+
wcscpy(exe, top_level_path);
465469
PathAppend(exe, L"bin\\git.exe");
466470
}
467471
}
468472
else if (!wcsicmp(basename, L"gitk.exe")) {
469473
static WCHAR buffer[BUFSIZE];
470474
allocate_console = 1;
471-
if (!PathRemoveFileSpec(exepath)) {
475+
if (!PathRemoveFileSpec(top_level_path)) {
472476
fwprintf(stderr,
473-
L"Invalid executable path: %s\n", exepath);
477+
L"Invalid executable path: %s\n",
478+
top_level_path);
474479
ExitProcess(1);
475480
}
476481

477482
/* set the default exe module */
478-
wcscpy(exe, exepath);
479-
swprintf(buffer, BUFSIZE, L"\"%s\"", exepath);
483+
wcscpy(exe, top_level_path);
484+
swprintf(buffer, BUFSIZE, L"\"%s\"", top_level_path);
480485
PathAppend(exe, msystem_bin);
481486
PathAppend(exe, L"wish.exe");
482487
if (_waccess(exe, 0) != -1)
483488
PathAppend(buffer, msystem_bin);
484489
else {
485-
wcscpy(exe, exepath);
490+
wcscpy(exe, top_level_path);
486491
PathAppend(exe, L"mingw\\bin\\wish.exe");
487492
PathAppend(buffer, L"mingw\\bin");
488493
}
@@ -492,7 +497,7 @@ int main(void)
492497
}
493498

494499
if (needs_env_setup)
495-
setup_environment(exepath, full_path);
500+
setup_environment(top_level_path, full_path);
496501
cmd = fixup_commandline(exepath, &exep, &wait,
497502
prefix_args, prefix_args_len, is_git_command, skip_arguments);
498503

0 commit comments

Comments
 (0)