Skip to content

Commit 66f9bd3

Browse files
dschoGit for Windows Build Agent
authored andcommitted
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 cebb23f commit 66f9bd3

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
}
@@ -427,7 +427,7 @@ int main(void)
427427
int r = 1, wait = 1, prefix_args_len = -1, needs_env_setup = 1,
428428
is_git_command = 1, full_path = 1, skip_arguments = 0,
429429
allocate_console = 0, show_console = 0;
430-
WCHAR exepath[MAX_PATH], exe[MAX_PATH];
430+
WCHAR exepath[MAX_PATH], exe[MAX_PATH], top_level_path[MAX_PATH];
431431
LPWSTR cmd = NULL, exep = exe, prefix_args = NULL, basename;
432432
LPWSTR working_directory = NULL;
433433

@@ -441,6 +441,7 @@ int main(void)
441441
fwprintf(stderr, L"Invalid executable path: %s\n", exepath);
442442
ExitProcess(1);
443443
}
444+
wcscpy(top_level_path, exepath);
444445
basename = exepath + wcslen(exepath) + 1;
445446
if (configure_via_resource(basename, exepath, exep,
446447
&prefix_args, &prefix_args_len,
@@ -453,25 +454,28 @@ int main(void)
453454
static WCHAR buffer[BUFSIZE];
454455
wait = 0;
455456
allocate_console = 1;
456-
if (!PathRemoveFileSpec(exepath)) {
457+
if (!PathRemoveFileSpec(top_level_path)) {
457458
fwprintf(stderr,
458-
L"Invalid executable path: %s\n", exepath);
459+
L"Invalid executable path: %s\n",
460+
top_level_path);
459461
ExitProcess(1);
460462
}
461463

462464
/* set the default exe module */
463-
wcscpy(exe, exepath);
465+
wcscpy(exe, top_level_path);
464466
PathAppend(exe, msystem_bin);
465467
PathAppend(exe, L"wish.exe");
466468
if (_waccess(exe, 0) != -1)
467469
swprintf(buffer, BUFSIZE,
468470
L"\"%s\\%.*s\\libexec\\git-core\"",
469-
exepath, wcslen(msystem_bin) - 4, msystem_bin);
471+
top_level_path,
472+
wcslen(msystem_bin) - 4, msystem_bin);
470473
else {
471-
wcscpy(exe, exepath);
474+
wcscpy(exe, top_level_path);
472475
PathAppend(exe, L"mingw\\bin\\wish.exe");
473476
swprintf(buffer, BUFSIZE,
474-
L"\"%s\\mingw\\libexec\\git-core\"", exepath);
477+
L"\"%s\\mingw\\libexec\\git-core\"",
478+
top_level_path);
475479
}
476480
PathAppend(buffer, L"git-gui");
477481
prefix_args = buffer;
@@ -491,39 +495,41 @@ int main(void)
491495
PathAppend(exe, L"git.exe");
492496
}
493497
else if (!wcsicmp(basename, L"git.exe")) {
494-
if (!PathRemoveFileSpec(exepath)) {
498+
if (!PathRemoveFileSpec(top_level_path)) {
495499
fwprintf(stderr,
496-
L"Invalid executable path: %s\n", exepath);
500+
L"Invalid executable path: %s\n",
501+
top_level_path);
497502
ExitProcess(1);
498503
}
499504

500505
/* set the default exe module */
501-
wcscpy(exe, exepath);
506+
wcscpy(exe, top_level_path);
502507
PathAppend(exe, msystem_bin);
503508
PathAppend(exe, L"git.exe");
504509
if (_waccess(exe, 0) == -1) {
505-
wcscpy(exe, exepath);
510+
wcscpy(exe, top_level_path);
506511
PathAppend(exe, L"bin\\git.exe");
507512
}
508513
}
509514
else if (!wcsicmp(basename, L"gitk.exe")) {
510515
static WCHAR buffer[BUFSIZE];
511516
allocate_console = 1;
512-
if (!PathRemoveFileSpec(exepath)) {
517+
if (!PathRemoveFileSpec(top_level_path)) {
513518
fwprintf(stderr,
514-
L"Invalid executable path: %s\n", exepath);
519+
L"Invalid executable path: %s\n",
520+
top_level_path);
515521
ExitProcess(1);
516522
}
517523

518524
/* set the default exe module */
519-
wcscpy(exe, exepath);
520-
swprintf(buffer, BUFSIZE, L"\"%s\"", exepath);
525+
wcscpy(exe, top_level_path);
526+
swprintf(buffer, BUFSIZE, L"\"%s\"", top_level_path);
521527
PathAppend(exe, msystem_bin);
522528
PathAppend(exe, L"wish.exe");
523529
if (_waccess(exe, 0) != -1)
524530
PathAppend(buffer, msystem_bin);
525531
else {
526-
wcscpy(exe, exepath);
532+
wcscpy(exe, top_level_path);
527533
PathAppend(exe, L"mingw\\bin\\wish.exe");
528534
PathAppend(buffer, L"mingw\\bin");
529535
}
@@ -533,7 +539,7 @@ int main(void)
533539
}
534540

535541
if (needs_env_setup)
536-
setup_environment(exepath, full_path);
542+
setup_environment(top_level_path, full_path);
537543
cmd = fixup_commandline(exepath, &exep, &wait,
538544
prefix_args, prefix_args_len, is_git_command, skip_arguments);
539545

0 commit comments

Comments
 (0)