Skip to content

Commit c777afe

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 93f210f commit c777afe

File tree

1 file changed

+28
-22
lines changed

1 file changed

+28
-22
lines changed

compat/win32/git-wrapper.c

Lines changed: 28 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,28 @@ 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\"",
428-
exepath, wcslen(msystem_bin) - 4, msystem_bin);
430+
top_level_path,
431+
wcslen(msystem_bin) - 4, msystem_bin);
429432
else {
430-
wcscpy(exe, exepath);
433+
wcscpy(exe, top_level_path);
431434
PathAppend(exe, L"mingw\\bin\\wish.exe");
432435
swprintf(buffer, BUFSIZE,
433-
L"\"%s\\mingw\\libexec\\git-core\"", exepath);
436+
L"\"%s\\mingw\\libexec\\git-core\"",
437+
top_level_path);
434438
}
435439
PathAppend(buffer, L"git-gui");
436440
prefix_args = buffer;
@@ -450,39 +454,41 @@ int main(void)
450454
PathAppend(exe, L"git.exe");
451455
}
452456
else if (!wcsicmp(basename, L"git.exe")) {
453-
if (!PathRemoveFileSpec(exepath)) {
457+
if (!PathRemoveFileSpec(top_level_path)) {
454458
fwprintf(stderr,
455-
L"Invalid executable path: %s\n", exepath);
459+
L"Invalid executable path: %s\n",
460+
top_level_path);
456461
ExitProcess(1);
457462
}
458463

459464
/* set the default exe module */
460-
wcscpy(exe, exepath);
465+
wcscpy(exe, top_level_path);
461466
PathAppend(exe, msystem_bin);
462467
PathAppend(exe, L"git.exe");
463468
if (_waccess(exe, 0) == -1) {
464-
wcscpy(exe, exepath);
469+
wcscpy(exe, top_level_path);
465470
PathAppend(exe, L"bin\\git.exe");
466471
}
467472
}
468473
else if (!wcsicmp(basename, L"gitk.exe")) {
469474
static WCHAR buffer[BUFSIZE];
470475
allocate_console = 1;
471-
if (!PathRemoveFileSpec(exepath)) {
476+
if (!PathRemoveFileSpec(top_level_path)) {
472477
fwprintf(stderr,
473-
L"Invalid executable path: %s\n", exepath);
478+
L"Invalid executable path: %s\n",
479+
top_level_path);
474480
ExitProcess(1);
475481
}
476482

477483
/* set the default exe module */
478-
wcscpy(exe, exepath);
479-
swprintf(buffer, BUFSIZE, L"\"%s\"", exepath);
484+
wcscpy(exe, top_level_path);
485+
swprintf(buffer, BUFSIZE, L"\"%s\"", top_level_path);
480486
PathAppend(exe, msystem_bin);
481487
PathAppend(exe, L"wish.exe");
482488
if (_waccess(exe, 0) != -1)
483489
PathAppend(buffer, msystem_bin);
484490
else {
485-
wcscpy(exe, exepath);
491+
wcscpy(exe, top_level_path);
486492
PathAppend(exe, L"mingw\\bin\\wish.exe");
487493
PathAppend(buffer, L"mingw\\bin");
488494
}
@@ -492,7 +498,7 @@ int main(void)
492498
}
493499

494500
if (needs_env_setup)
495-
setup_environment(exepath, full_path);
501+
setup_environment(top_level_path, full_path);
496502
cmd = fixup_commandline(exepath, &exep, &wait,
497503
prefix_args, prefix_args_len, is_git_command, skip_arguments);
498504

0 commit comments

Comments
 (0)